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

$sql = "SELECT * ";
$sql .= " FROM  `log_login`
left join `emp` on `emp`.`id_emp` = `log_login`.`id_emp`
where  `log_login`.`id_user` = '$idPublicUser' order by `log_login`.`id_log_login` DESC limit 5000";
$query = mysqli_query($db, $sql);
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;
$sql = "SELECT * ";
$sql .= " FROM  `log_login`
left join `emp` on `emp`.`id_emp` = `log_login`.`id_emp`
where  `log_login`.`id_user` = '$idPublicUser' order by `log_login`.`id_log_login` DESC limit 5000";
if (!empty($requestData['search']['value'])) {
    $sql .= " AND ( name_emp LIKE '" . $requestData['search']['value'] . "%'  ";
    $sql .= " OR  ip_address 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['active_emp'] == 0){
        $btn = "<button class='btn btn-success'>نشط</button>";
    }
    if($row['active_emp'] == 1){
        $btn = "<button class='btn btn-danger'>محذوف</button>";
    }

    $nestedData[] = $row["name_emp"];
    $nestedData[] = "$btn";
    $nestedData[] = $row["ip_address"];
    $nestedData[] = $row["time_login"];


    $data[] = $nestedData;
}



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

echo json_encode($json_data);

?>