<?php
include("db.php");
$requestData = $_REQUEST;
$idYear = $_POST['dataTableSend'][0] ?? 0;
$idGender = $_POST['dataTableSend'][1] ?? 0;
$columns = array(
    0 => 'name_student',
    1 => 'phone_student',
    2 => 'code_student',
    3 => 'name_year',
);

$sql = "SELECT * FROM  `student` 
left join `year` on `year`.`id_year` = `student`.`id_year`
where  `student`.`active_student` = '0' and  `student`.`id_user` = '$idPublicUser' ";
if ($idYear != 0) {
    $sql .= " and  `student`.`id_year` = $idYear";
}
if ($idGender != 0) {
    if ($idGender == 1) {
        $gender = 0;
    }
    if ($idGender == 2) {
        $gender = 1;
    }
    $sql .= " and `student`.`gender_student` = $gender";
}

$query = mysqli_query($db, $sql);
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;

$sql = "SELECT * FROM  `student` 
left join `year` on `year`.`id_year` = `student`.`id_year`
where  `student`.`active_student` = '0' and  `student`.`id_user` = '$idPublicUser'";
if ($idYear != 0) {
    $sql .= " and `student`.`id_year` = $idYear";
}
if ($idGender != 0) {
    if ($idGender == 1) {
        $gender = 0;
    }
    if ($idGender == 2) {
        $gender = 1;
    }
    $sql .= " and `student`.`gender_student` = $gender";
}

if (!empty($requestData['search']['value'])) {
    $sql .= " AND ( code_student LIKE '" . $requestData['search']['value'] . "%'  ";
    $sql .= " OR  phone_student LIKE '" . $requestData['search']['value'] . "%'  ";
    $sql .= " OR  name_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();
    if ($row['image_student'] == '') {
        if ($row['gender_student'] == 0) {
            $img = "img/man.png";
        }
        if ($row['gender_student'] == 1) {
            $img = "img/girl.png";
        }
    } else {
        $img = $row['image_student'];
    }

    $nestedData[] = "<center><img style='width:50px' src='$img'/></center>";
    $nestedData[] = $row["name_student"];
    $nestedData[] = $row["phone_student"];
    $nestedData[] = $row["code_student"];
    $nestedData[] = $row["name_year"];
    $nestedData[] = "
	<button type='button' class='btn btn-success' onclick='student.getSubscription(" . $row['id_student'] . ")'>الأشتراكات</button>
	<button type='button' class='btn btn-success' onclick='student.getImage(" . $row['id_student'] . ")'>الصوره</button>
	<button type='button' class='btn btn-success' onclick='student.getStudent(" . $row['id_student'] . ")'>تعديل</button>
    <button type='button' class='btn btn-danger' onclick=main.deleteData('student','" . $row['id_student'] . "')>حذف</button>
";

    $data[] = $nestedData;
}



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

echo json_encode($json_data);

?>