<?php
include("../db.php");
$requestData = $_REQUEST;
$columns = array(
    0 => 'name_inp',
);
$start = $_POST['dataTableSend'][0] ?? "";
$end = $_POST['dataTableSend'][1] ?? "";
$idInp = $_POST['dataTableSend'][2] ?? 0;
if ($idInp != 0) {
    $addInp = " and `out_inp`.`id_inp` = '$idInp' ";
} else {
    $addInp = " ";
}

$sql = "SELECT * FROM  `out_inp` left join `emp` on `emp`.`id_emp` = `out_inp`.`id_emp` left join `inp` on  `inp`.`id_inp` = `out_inp`.`id_inp`  where `out_inp`.`active_inp` = '0' and `out_inp`.`id_user` = '$idPublicUser' and `out_inp`.`date_add` BETWEEN '$start' and '$end' " . $addInp;
//echo $sql;
$query = mysqli_query($db, $sql);
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;
$sql = "SELECT * FROM  `out_inp` left join `emp` on `emp`.`id_emp` = `out_inp`.`id_emp` left join `inp` on  `inp`.`id_inp` = `out_inp`.`id_inp`  where `out_inp`.`active_inp` = '0' and `out_inp`.`id_user` = '$idPublicUser' and `out_inp`.`date_add` BETWEEN '$start' and '$end' " . $addInp;
if (!empty($requestData['search']['value'])) {
    $sql .= " AND ( name_inp 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_inp"];
    $nestedData[] = $row["inp"];
    $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['inp'];
}
$json_data = array(
    "draw" => intval($requestData['draw']),
    "recordsTotal" => intval($totalData),
    "recordsFiltered" => intval($totalFiltered),
    "data" => $data,
    "totalPrice" => $totalPrice
);

echo json_encode($json_data);

?>