<?php
include("../db.php");
$requestData = $_REQUEST;
$columns = array(
    0 => 'name_emp',
    1 => 'name_service',
);
$start = $_POST['dataTableSend'][0] ?? "";
$end = $_POST['dataTableSend'][1] ?? "";
$emp = $_POST['dataTableSend'][2] ?? 0;

if ($emp != 0) {
    $addEmp = " and `emp`.`id_emp` = '$emp' ";
} else {
    $addEmp = " ";
}

$sql = "SELECT * FROM  `sales_header` 
left join `emp` on `emp`.`id_emp` = `sales_header`.`id_emp` 
left join `cus` on `cus`.`id_cus` = `sales_header`.`id_cus`
left join `type_paids` on `type_paids`.`id_type_paids` = `sales_header`.`type_paid`

where `sales_header`.`active_sales_header` = '0' and  `sales_header`.`id_user` = '$idPublicUser' and `sales_header`.`date_add` BETWEEN '$start' and '$end' "  . $addEmp;

$query = mysqli_query($db, $sql);
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;
$sql = "SELECT * FROM  `sales_header` 
left join `emp` on `emp`.`id_emp` = `sales_header`.`id_emp` 
left join `cus` on `cus`.`id_cus` = `sales_header`.`id_cus`
left join `type_paids` on `type_paids`.`id_type_paids` = `sales_header`.`type_paid`

where  `sales_header`.`active_sales_header` = '0' and  `sales_header`.`id_user` = '$idPublicUser' and `sales_header`.`date_add` BETWEEN '$start' and '$end' "  . $addEmp;
if (!empty($requestData['search']['value'])) {
    $sql .= " AND ( name_emp LIKE '" . $requestData['search']['value'] . "%'  ";
    $sql .= " OR  id_sales_header LIKE '" . $requestData['search']['value'] . "%'  ";
    $sql .= " OR  name_cus LIKE '" . $requestData['search']['value'] . "%' ) ";
}
$query = mysqli_query($db, $sql);
$totalFiltered = mysqli_num_rows($query);
$sql .= " ORDER BY " . $columns[$requestData['order'][0]['column']] . "   " . $requestData['order'][0]['dir'] . "  LIMIT " . $requestData['start'] . " ," . $requestData['length'] . "   ";
$query = mysqli_query($db, $sql);
$data = array();
while ($row = mysqli_fetch_array($query)) {
    $nestedData = array();
        if($row['type_paid'] == 0){
            $typePaid = "Cash";
        }else{
            $typePaid = $row['name_type_paids'];
        }
    $id = $row["id_sales_header"];
    $nestedData[] = $row["id_sales_header"];
    $nestedData[] = $row["name_cus"];
    $nestedData[] = $row["phone_cus"];
    $nestedData[] = $typePaid;
    $nestedData[] = $row["info"];
    $nestedData[] = $row["counts"];
    $nestedData[] = $row["total_befor_discount"];
    $nestedData[] = $row["discount"];
    $nestedData[] = $row["total"];
    $nestedData[] = $row["name_emp"];
    $nestedData[] = $row["time_add"];
    $nestedData[] = "
    <button class='btn btn-danger' onclick='sales.deleteInv($id)'>حذف</button>
    <button  class='btn btn-success' onclick='sales.pushInv($id)'>طباعة</button>
    ";

    $data[] = $nestedData;
}
$totalPrice = 0;
$queryTotal = mysqli_query($db,  $sql);
while ($r = mysqli_fetch_assoc($queryTotal)) {
    $totalPrice += $r['total'];
}


$json_data = array(
    "draw" => intval($requestData['draw']),
    "recordsTotal" => intval($totalData),
    "recordsFiltered" => intval($totalFiltered),
    "data" => $data,
    "totalPrice" => $totalPrice
);

echo json_encode($json_data);

?>