<?php

require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include ('includes/function.php');

$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);
$admin = $_SESSION["admin_" . $dbname];
$success = false;

$file = "courin.php";
$maindb = "courin";
 
$keyfld = "vouchno";
$rangeFrom = $_REQUEST["rangeFrom"];
$rangeTo = $_REQUEST["rangeTo"];
$searchTerm = $_REQUEST["ssearch"];
$operator = $_REQUEST["soperator"];

$cond = "";
if ($operator == "=") {
    if ($_REQUEST["sfield"] == "1") {
        $cond = "AND `vouchno` = '$searchTerm'";
    } elseif ($_REQUEST["sfield"] == "2") {
        $cond = "AND `vouchdt` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "3") {
        $cond = "AND `paxname` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "4") {
        $cond = "AND `passport` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "5") {
        $cond = "AND `medical` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "6") {
        $cond = "AND `license` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "7") {
        $cond = "AND `photo` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "8") {
        $cond = "AND `recdfrom` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "9") {
        $cond = "AND `recdby` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "10") {
        $cond = "AND `courdt` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "11") {
        $cond = "AND `trackid` = '$searchTerm'";
    }   
} else {
    if ($_REQUEST["sfield"] == "1") {
        $cond = "AND `vouchno` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "2") {
        $cond = "AND `vouchdt` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "3") {
        $cond = "AND `paxname` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "4") {
        $cond = "AND `passport` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "5") {
        $cond = "AND `medical` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "6") {
        $cond = "AND `license` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "7") {
        $cond = "AND `photo` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "8") {
        $cond = "AND `recdfrom` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "9") {
        $cond = "AND `recdby` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "10") {
        $cond = "AND `courdt` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "11") {
        $cond = "AND `trackid` LIKE '%$searchTerm%'";
    }  
}

$sortcond = "ORDER BY `vouchno`";
if ($_REQUEST["sortBy"] == "1") {
    $sortcond = " ORDER BY `vouchno`";
} else if ($_REQUEST["sortBy"] == "2") {
    $sortcond = " ORDER BY `vouchdt`";
} else if ($_REQUEST["sortBy"] == "3") {
    $sortcond = " ORDER BY `paxname`";
} else if ($_REQUEST["sortBy"] == "4") {
    $sortcond = " ORDER BY `passport`";
} else if ($_REQUEST["sortBy"] == "5") {
    $sortcond = " ORDER BY `medical`";
} else if ($_REQUEST["sortBy"] == "6") {
    $sortcond = " ORDER BY `license`";
} else if ($_REQUEST["sortBy"] == "7") {
    $sortcond = " ORDER BY `photo`";
} else if ($_REQUEST["sortBy"] == "8") {
    $sortcond = " ORDER BY `recdfrom`";
} else if ($_REQUEST["sortBy"] == "9") {
    $sortcond = " ORDER BY `recdby`";
} else if ($_REQUEST["sortBy"] == "10") {
    $sortcond = " ORDER BY `courdt`";
} else if ($_REQUEST["sortBy"] == "11") {
    $sortcond = " ORDER BY `trackid`";
}

$ordcond = "DESC";
if ($_REQUEST["orderAs"] == "desc") {
    $ordcond = "DESC";
}else{
    $ordcond = "ASC";
}


$query = "SELECT *, DATE_FORMAT(`vouchdt`,'%d-%m-%Y') as vouchdt,DATE_FORMAT(`courdt`,'%d-%m-%Y') as courdt FROM $maindb
                    WHERE vouchno != '' $cond $sortcond $ordcond LIMIT $rangeFrom, $rangeTo";
$result = mysqli_query($conn, $query)or die(mysqli_error($conn));

$rows = array();
$ctr = 0;
while ($r = mysqli_fetch_assoc($result)) {
   
    if ($r['medical'] > 0){
        $medical = "Yes";
    } else {
        $medical = "No";
    }
    if ($r['license'] > 0){
        $license = "Yes";
    } else {
        $license = "No";
    }
    if ($r['photo'] > 0){
        $photo = "Yes";
    } else {
        $photo = "No";
    }

    $rows[] = [ 'success' => true,
        'value' => $ctr,
        'keyfld' => $keyfld,
        'vouchno' => $r['vouchno'],
        'vouchdt' => $r['vouchdt'],
        'paxname' => $r['paxname'],
        'passport' => $r['passport'],
        'medical' => $medical,
        'license' => $license,
        'photo' => $photo,
        'recdfrom' => $r['recdfrom'],
        'recdby' => $r['recdby'],
        'courdt' => $r['courdt'],
        'trackid' => $r['trackid'],
        'remarks' => $r['remark1'] . $r['remark2'],
        'action' => "$file?mode=edit&vouchno=$r[vouchno]",
    ];
    $ctr++;
} 

header("Content-type: application\json");
echo json_encode($rows);
