<?php
include("db.php");
$date = date('Y-m-d');
 $empPower = $_POST['empPower'];
//$empPower = [1,1,1];
if($empPower[0] == 1 && $empPower[1] == 1 && $empPower[2] == 1  ){
    $data["success"] = 1;
     $palette = [
    "#22c55e", "#3b82f6", "#f97316", "#ef4444",
    "#a855f7", "#14b8a6", "#eab308", "#0ea5e9",
    "#f43f5e", "#64748b"
];
    $txtTotalCus = "SELECT COUNT(DISTINCT(id_cus))  as `total` from `sales_header` where `id_user` = '$idPublicUser' and `date_add` BETWEEN '$date' and '$date' ";
    $qTotalCus = mysqli_query($db,$txtTotalCus);
    $rowTotalCus = mysqli_fetch_assoc($qTotalCus);
    $data['allUser'] = $rowTotalCus['total'];
    if($data['allUser'] == null){
        $data['allUser'] = 0;
    }
    $txtTotalExp = "SELECT SUM(exp)  as `total` from `out_exp` where `id_user` = '$idPublicUser' and `date_add` BETWEEN '$date' and '$date' and `active_out_exp` ='0'";
    $qTotalExp = mysqli_query($db,$txtTotalExp);
    $rowTotalExp = mysqli_fetch_assoc($qTotalExp);
    $data['allExp'] = $rowTotalExp['total'];
    if($data['allExp'] == null){
        $data['allExp'] = 0;
    }

    $txtTotalSales = "SELECT SUM(total)  as `total` from `sales_header` where `id_user` = '$idPublicUser' and `date_add` BETWEEN '$date' and '$date' and `active_sales_header` ='0'";
    $qTotalSales = mysqli_query($db,$txtTotalSales);
    $rowTotalSales = mysqli_fetch_assoc($qTotalSales);
    $data['allSales'] = $rowTotalSales['total'];

    $data['total'] = $data['allSales'] - $data['allExp'] ;
    if($data['total'] == null){
        $data['total'] = 0;
    }

    $txtPaymentChart = "SELECT 
        tp.name_type_paids AS pay_name,
        SUM(sh.total) AS total_sum
    FROM sales_header sh
    INNER JOIN type_paids tp 
        ON tp.id_type_paids = sh.type_paid
    WHERE sh.id_user = '$idPublicUser'
      AND sh.date_add BETWEEN '$date' AND '$date'
    GROUP BY sh.type_paid
    ORDER BY total_sum DESC";
    
    $qPaymentChart = mysqli_query($db, $txtPaymentChart);

    $paymentLabels = [];
    $paymentData   = [];
    $paymentColors = [];

    $i = 0;
    while($row = mysqli_fetch_assoc($qPaymentChart)) {
        $paymentLabels[] = $row['pay_name'];
        $paymentData[]   = (float)$row['total_sum'];
        $paymentColors[] = $palette[$i % count($palette)];
        $i++;
    }

    $data["paymentChart"] = [
        "labels" => $paymentLabels,
        "data"   => $paymentData,
        "colors" => $paymentColors
    ];

$txtEmpChart = "
    SELECT 
        e.name_emp AS emp_name,
        SUM(sh.total) AS total_sum
    FROM sales_header sh
    INNER JOIN emp e 
        ON e.id_emp = sh.id_emp
    WHERE sh.id_user = '$idPublicUser'
      AND sh.date_add BETWEEN '$date' AND '$date'
    GROUP BY sh.id_emp
    ORDER BY total_sum DESC
";

$qEmpChart = mysqli_query($db, $txtEmpChart);

$empLabels = [];
$empData   = [];
$empColors = [];

$i = 0;
while($row = mysqli_fetch_assoc($qEmpChart)) {
    $empLabels[] = $row['emp_name'];
    $empData[]   = (float)$row['total_sum'];
    $empColors[] = $palette[$i % count($palette)];
    $i++;
}


    $data["empChart"] = [
        "labels" => $empLabels,
        "data"   => $empData,
        "colors" => $empColors
    ];

}else{
    $data["success"] = 0;
}

echo json_encode($data);

?>