<?php
include('../extension/connect.php');
include('../extension/check-login.php');
include('../extension/rank_check.php');
$userid = $_SESSION['userid'];

$cred = mysqli_query($con, "select user_rank from users where user_id='$userid'");
$myinfo = mysqli_fetch_array($cred);
$mrank = $myinfo['user_rank'];

$thm = mysqli_query($con, "select theme, theme_text from admin");
$thminfo = mysqli_fetch_array($thm);
$thm_color = $thminfo['theme'];
$thm_text = $thminfo['theme_text'];

$requestData = $_REQUEST;
if (empty($requestData)) {
    header("location:dashboard");
    exit;
}

$columns = array(
    0 => 'server_name',
    1 => 'server_ip',
    2 => 'category',
    3 => 'status',
    4 => null,
    5 => null
);

$sql = "select * from servers where 1=1";
$query = mysqli_query($con, $sql);
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;

if (!empty($requestData['search']['value'])) {
    $sql .= " AND ( server_name LIKE '%" . $requestData['search']['value'] . "%' ";
    $sql .= " OR server_ip LIKE '%" . $requestData['search']['value'] . "%' ) ";
}

$query = mysqli_query($con, $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($con, $sql);

$data = array();
while ($row = mysqli_fetch_array($query)) {
    $nestedData = array();

    $id = $row['id'];
    $ip_address = $row['server_ip'];
    $name = $row['server_name'];
    $status = $row['status'];
    $proto_ = $row['protocol'];
    $category = $row['category'];
    $tcp_port = $row['tcp_port'];
    $udp_port = $row['udp_port'];
    $user_pass = $row['username'];
    $user_name = $row['password'];
    $flag = $row['country'];
    $aydi = "'$id'";
    $delete_ = "'delete'";
    $restart_ = "'restart'";

    if ($status == 1) {
        $stat = '<label class="badge badge-success">Online</label>';
    } else {
        $stat = '<label class="badge badge-danger">Online</label>';
    }


    $name2 = preg_replace("![^a-z0-9]+!i", "_", $name);
    if ($category == 'server') {
        $modalbutton = '<a href="/script/client.php?ip=' . $ip_address . '&name=' . $name2 . '" class="btn btn-sm" style="background-color: ' . $thm_color . '; color: ' . $thm_text . '">OVPN Downloads</a>';
        $restart = '<td>
                        <a href="#" class="btn btn-sm" onclick="submitForm(' . $restart_ . ',' . $aydi . ')" style="background-color: ' . $thm_color . '; color: ' . $thm_text . '">Restart</a>
                    </td>';
    } else {
        $modalbutton = '<small class="border border-info rounded p-1 bg-info text-black">Use Original OVPN</small>';
        $restart = '<td>
                        <button type="button" href="#" class="btn btn-sm btn-secondary" disabled="">Restart</button>
                    </td>';
    } 



    $nestedData[] = $name;
    $nestedData[] = $ip_address;
    $nestedData[] = strtoupper($category);
    $nestedData[] = $stat;
    $nestedData[] = $modalbutton;
    $nestedData[] = '<td>
                        <a href="#" class="btn btn-info btn-sm" onclick="edit('.$aydi.', \''.$name.'\', \''.$ip_address.'\', \''.$user_name.'\', \''.$user_pass.'\', \''.$tcp_port.'\', \''.$flag.'\')">Edit</a>
                        <a href="#" class="btn btn-danger btn-sm" onclick="submitForm(' . $delete_ . ',' . $aydi . ')">Delete</a>
                        '.$restart.'
                    </td>';

    $data[] = $nestedData;
}

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

echo json_encode($json_data);
?>