<?php
include("db.php");
$requestData = $_REQUEST;
$columns = array(
    0 => 'name_teacher',
);

$sql = "SELECT * ";
$sql .= " FROM  `subscription`
left join `year` on `year`.`id_year` = `subscription`.`id_year`
left join `subject` on `subscription`.`id_subject` = `subject`.`id_subject`
left join `teacher` on `subscription`.`id_teacher` = `teacher`.`id_teacher`
left join `date_addation_subscription` on `subscription`.`id_date_addation_subscription` = `date_addation_subscription`.`id_date_addation_subscription`
where `subscription`.`active_subscription` = '0' and `subscription`.`id_user` = '$idPublicUser'";
$query = mysqli_query($db, $sql);
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;
$sql = "SELECT * ";
$sql .= " FROM  `subscription`
left join `year` on `year`.`id_year` = `subscription`.`id_year`
left join `subject` on `subscription`.`id_subject` = `subject`.`id_subject`
left join `teacher` on `subscription`.`id_teacher` = `teacher`.`id_teacher`
left join `date_addation_subscription` on `subscription`.`id_date_addation_subscription` = `date_addation_subscription`.`id_date_addation_subscription`
where `subscription`.`active_subscription` = '0' and `subscription`.`id_user` = '$idPublicUser'";
if (!empty($requestData['search']['value'])) {
    $sql .= " AND ( subscription.name_subscription LIKE '" . $requestData['search']['value'] . "%'  ";
    $sql .= " OR  subject.name_subject LIKE '" . $requestData['search']['value'] . "%'  ";
    $sql .= " OR  teacher.name_teacher 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_subscription"];
    $nestedData[] = $row["name_year"];
    $nestedData[] = $row["name_subject"];
    $nestedData[] = $row["name_teacher"];
    $nestedData[] = $row["tax_teacher"];
    $nestedData[] = $row["count_sec"];
    $nestedData[] = $row["price_subscription"];
    $nestedData[] = "<button type='button' class='btn btn-danger' onclick=main.deleteData('subscription','" . $row['id_subscription'] . "')>حذف</button>
	<button type='button' class='btn btn-success' onclick='pages.getSubscription(" . $row['id_subscription'] . ")'>تعديل</button>
	<button type='button' class='btn btn-success' onclick='pages.addPrivteSubscription(" . $row['id_subscription'] . ")'>بيرافيت</button>
";

    $data[] = $nestedData;
}



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

echo json_encode($json_data);

?>