<?php
include("../db.php");
$requestData = $_REQUEST;
$columns = array(
    0 => 'name_exp',
);
$start = $_POST['dataTableSend'][0] ?? "";
$end = $_POST['dataTableSend'][1] ?? "";
$Emp = $_POST['dataTableSend'][2] ?? 0;
if ($Emp != 0) {
    $addExp = " and `emp`.`id_emp` = '$Emp' ";
} else {
    $addExp = " ";
}

$sql = "SELECT * FROM  `out_exp` left join `emp` on `emp`.`id_emp` = `out_exp`.`id_emp` left join `exp` on  `exp`.`id_exp` = `out_exp`.`id_exp`  where `out_exp`.`active_exp` = '0' and `out_exp`.`id_user` = '$idPublicUser' and `out_exp`.`date_add` BETWEEN '$start' and '$end' " . $addExp;
//echo $sql;
$query = mysqli_query($db, $sql);
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;
$sql = "SELECT * FROM  `out_exp` left join `emp` on `emp`.`id_emp` = `out_exp`.`id_emp` left join `exp` on  `exp`.`id_exp` = `out_exp`.`id_exp`  where `out_exp`.`active_exp` = '0' and `out_exp`.`id_user` = '$idPublicUser' and `out_exp`.`date_add` BETWEEN '$start' and '$end' " . $addExp;
if (!empty($requestData['search']['value'])) {
    $sql .= " AND ( name_exp LIKE '" . $requestData['search']['value'] . "%'  ";
    $sql .= " OR  notes LIKE '" . $requestData['search']['value'] . "%'  ";
    $sql .= " OR  name_emp 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();


    $nestedData[] = $row["name_exp"];
    $nestedData[] = $row["exp"];
    $nestedData[] = $row["date_add"];
    $nestedData[] = $row["name_emp"];
    $nestedData[] = $row["notes"];

    $data[] = $nestedData;
}


$totalPrice = 0;
$queryTotal = mysqli_query($db, $sql);
while ($r = mysqli_fetch_assoc($queryTotal)) {
    $totalPrice += $r['exp'];
}
$json_data = array(
    "draw" => intval($requestData['draw']),
    "recordsTotal" => intval($totalData),
    "recordsFiltered" => intval($totalFiltered),
    "data" => $data,
    "totalPrice" => $totalPrice
);

echo json_encode($json_data);

?>