<?php
include("../db.php");
$requestData = $_REQUEST;
$columns = array(
    0 => 'name_student',
    1 => 'phone_student',
);
$start = $_POST['dataTableSend'][0] ?? "";
$end = $_POST['dataTableSend'][1] ?? "";
$year = $_POST['dataTableSend'][2] ?? 0;
$subscription = $_POST['dataTableSend'][3] ?? 0;
if ($year != 0) {
    $addYear = " and `year`.`id_year` = '$year' ";
} else {
    $addYear = " ";
}
if ($subscription != 0) {
    $addSubscription = " and `subscription`.`id_subscription` = '$subscription' ";
} else {
    $addSubscription = " ";
}

$sql = "SELECT * FROM  `discount` 
left join `student` on  `discount`.`id_student` = `student`.`id_student` 
left join `subscription_student` on `subscription_student`.`id_subscription_student` = `discount`.`id_subscription_student`  
left join `subscription` on `subscription`.`id_subscription` = `subscription_student`.`id_subscription` 
left join `emp` on `emp`.`id_emp` = `discount`.`id_emp` 
left join `year` on `year`.`id_year` = `subscription`.`id_year`
where  `discount`.`id_user` = '$idPublicUser' and `discount`.`date_add` BETWEEN '$start' and '$end' " . $addYear . $addSubscription;

$query = mysqli_query($db, $sql);
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;
$sql = "SELECT * FROM  `discount` 
left join `student` on  `discount`.`id_student` = `student`.`id_student` 
left join `subscription_student` on `subscription_student`.`id_subscription_student` = `discount`.`id_subscription_student`  
left join `subscription` on `subscription`.`id_subscription` = `subscription_student`.`id_subscription` 
left join `emp` on `emp`.`id_emp` = `discount`.`id_emp` 
left join `year` on `year`.`id_year` = `subscription`.`id_year`
where  `discount`.`id_user` = '$idPublicUser' and `discount`.`date_add` BETWEEN '$start' and '$end' " . $addYear . $addSubscription;
if (!empty($requestData['search']['value'])) {
    $sql .= " AND ( name_student LIKE '" . $requestData['search']['value'] . "%'  ";
    $sql .= " OR  code_student LIKE '" . $requestData['search']['value'] . "%'  ";
    $sql .= " OR  name_year LIKE '" . $requestData['search']['value'] . "%'  ";
    $sql .= " OR  name_emp LIKE '" . $requestData['search']['value'] . "%'  ";
    $sql .= " OR  phone_student 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();

    $nots = $row["notes_discount"];
    $nestedData[] = $row["name_student"];
    $nestedData[] = $row["code_student"];
    $nestedData[] = $row["phone_student"];
    $nestedData[] = $row["name_year"];
    $nestedData[] = $row["name_subscription"];
    $nestedData[] = $row["money_discount"];
    $nestedData[] = $row["money_stay"];
    $nestedData[] = $row["name_emp"];
    $nestedData[] = $row["date_add"];
    $nestedData[] = "<button class='btn btn-info' onclick='alert(`$nots`)'>الملاحظات</button>";

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


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

echo json_encode($json_data);

?>