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

$zdb = $_SESSION["zdbyear_$dbname"];
$ztit = $_SESSION["tit"];
$usertype = $_SESSION["usertype"];
$user = $_SESSION['username_' . $dbname];
$keyfld = "vouchno";

mysqli_select_db($conn,$zdb);

if (isset($_REQUEST["search"])) {
    $type = $_REQUEST["type"];
    $tpcond = "AND `status` = '$type'";
    $frmdate = $_REQUEST["fromdate"];
    $todate = $_REQUEST["todate"];
    
    $searchTerm = $_REQUEST["search"];
    $sortTerm = $_REQUEST["sorderby"];
    $sortorder = $_REQUEST["sortby"];
    
    if ($_REQUEST["field"] == "1") {
        $scond = "AND `name` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["field"] == "2") {
        $scond = "AND `lastname` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["field"] == "3") {
        $scond = "AND `vouchno` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["field"] == "4") {
        $scond = "AND `mobile` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["field"] == "5") {
        $vchdate =  $_REQUEST["search"]; 
        $timedt = strtotime($vchdate);
        $vchdt =  date('Y-m-d',$timedt);
        $scond = "AND `vouchdt` LIKE '%$vchdt%'";
    } else if ($_REQUEST["field"] == "6") {
        $scond = "AND `destination` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["field"] == "7") {
        $scond = "AND `status` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["field"] == "8") {
        $scond = "AND `remarks` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["field"] == "9") {
        $scond = "AND `username` LIKE '%$searchTerm%'";
    }
    
    if ($_REQUEST["sortby"] == "1") {
        if($sortTerm == "1"){
            $cond = "order by `name` ASC";
        }else{
            $cond = "order by `name` DESC";
        }
        
    } else if ($_REQUEST["sortby"] == "2") {
        if($$sortTerm == "1"){
            $cond = "order by `lastname` ASC";
        }else{
            $cond = "order by `lastname` DESC";
        }
    } else if ($_REQUEST["sortby"] == "3") {
        if($sortTerm == "1"){
            $cond = "order by `vouchno` ASC";
        }else{
            $cond = "order by `vouchno` DESC";
        }
    } else if ($_REQUEST["sortby"] == "4") {
        if($sortTerm == "1"){
            $cond = "order by `mobile` ASC";
        }else{
            $cond = "order by `mobile` DESC";
        }
    } else if ($_REQUEST["sortby"] == "5") {
        if($sortTerm == "1"){
            $cond = "order by `vouchdt` ASC";
        }else{
            $cond = "order by `vouchdt` DESC";
        }
    } else if ($_REQUEST["sortby"] == "6") {
        if($sortTerm == "1"){
            $cond = "order by `destination` ASC";
        }else{
            $cond = "order by `destination` DESC";
        }
    } else if ($_REQUEST["sortby"] == "7") {
        if($sortTerm == "1"){
            $cond = "order by `status` ASC";
        }else{
            $cond = "order by `status` DESC";
        }
    } else if ($_REQUEST["sortby"] == "8") {
        if($sortTerm == "1"){
            $cond = "order by `remarks` ASC";
        }else{
            $cond = "order by `remarks` DESC";
        }
    } else if ($_REQUEST["sortby"] == "9") {
        if($sortTerm == "1"){
            $cond = "order by `username` ASC";
        }else{
            $cond = "order by `username` DESC";
        }
    }
    if (!empty($frmdate) && !empty($todate)){
        $dtcond = " and `vouchdt` >= '$frmdate' and `vouchdt` <= '$todate'";
    }


        $getrec = mysqli_query($conn,"SELECT *, DATE_FORMAT(`deptdate`, '%d-%m-%Y') as deptdate 
                           FROM `visamain` WHERE `vouchno`!='' $dtcond $scond  $cond ")or die(mysqli_error($conn));

    $get1 = mysqli_num_rows($getrec);
//    browse($get1);
    $ctr = 1;
    while ($rec = mysqli_fetch_array($getrec)) {
        $name = strtoupper($rec[name]);
        $lastname = strtoupper($rec[lastname]);
        Echo "<tr>";
        Echo "<td class='text-right'><input type='text' value='$ctr' name='srno_$ctr' style='border : none;width : 30px;text-align:center;' readonly></td>";
        Echo "<td class=''><input type='text' value='$rec[username]' name='username_$ctr' style='border : none;width : 100px; padding : 0px;text-align:center;' readonly class='username'></td>";
        Echo "<td class=''><a href=\"visaedit.php?mode=edit&vouchno=$rec[vouchno]\"><input type='text' value='$rec[vouchno]' name='vouchno_$ctr' style='border : none;width : 30px; padding : 0px;text-align:center;' readonly class='vouchno'></a></td>";
        Echo "<td class=''><input type='text' value='$rec[leadvouch]' name='leadvouch_$ctr' style='border : none;width : 30px; padding : 0px;text-align:center;' readonly class='leadvouch'></td>";
        Echo "<td class=''><input type='date' value='$rec[vouchdt]' name='vouchdt_$ctr' style='border : none;width : 120px; padding : 0px;text-align:center;' readonly class='vouchdt'></td>";
        Echo "<td class=''><input type='text'value='$rec[name]' name='name_$ctr' style='border : none; padding : 0px;text-align:left;width:110px;' readonly class='name'></td>";
        Echo "<td class=''><input type='text'value='$rec[lastname]' name='lastname_$ctr' style='border : none; padding : 0px;text-align:left;width:110px;' readonly class='lastname'></td>";
        Echo "<td class=''><input type='text'value='$rec[mobile]' name='mobile_$ctr' style='border : none; padding : 0px;text-align:left;width:80px;' readonly class='mobile'></td>";
        Echo "<td class=''><input type='text'value='$rec[destination]' name='destination_$ctr' style='border : none; padding : 0px;text-align:left;width:90px;' readonly class='destination'></td>";
        Echo "<td class=''><input type='text' value='$rec[deptdate]' name='deptdate_$ctr' style='border : none; padding : 0px;text-align:left;width:100px;' readonly class='deptdate'></td>";
        echo "<td>";
        ?>
        <select  name='status_<?= $ctr ?>' id="status"  class="status form-control select2" style='height:25px;width:150px;'>
            <option value='0'<?php
        if ($addflag != 1) {
            if ($rec["status"] == "0") {
                echo "selected='selected'";
            }
        }
        ?> >Still to Speak</option>
            <option value='1'<?php
        if ($addflag != 1) {
            if ($rec["status"] == "1") {
                echo "selected='selected'";
            }
        }
        ?>>Requirement to be Given</option>
            <option value='2'<?php
        if ($addflag != 1) {
            if ($rec["status"] == "2") {
                echo "selected='selected'";
            }
        }
        ?>>Requirement Given - Follow up</option>
            <option value='3'<?php
        if ($addflag != 1) {
            if ($rec["status"] == "3") {
                echo "selected='selected'";
            }
        }
        ?>>Copy Collected</option>
            <option value='4'<?php
        if ($addflag != 1) {
            if ($rec["status"] == "4") {
                echo "selected='selected'";
            }
        }
        ?>>Original Collected</option>
            <option value='5'<?php
        if ($addflag != 1) {
            if ($rec["status"] == "5") {
                echo "selected='selected'";
            }
        }
        ?>>Cancelled</option>
        </select>
        <?php
        echo "</td>";
//                                    Echo "<td class='text-right'><input type='text' value='$stget' name='status_$ctr' style='border : none; padding : 0px;text-align:left;width:100px;' readonly class='status'></td>";
//                                Echo "<td class='text-right'><input type='text' value='$rec[imagetkn]' name='atchimg_$ctr' style='border : none; padding : 0px;text-align:left;width:60px;' readonly class='atchimg'></td>";
//                                echo "</td>";
//                                Echo "<td class='text-right'><input type='text' value='$rec[visadone]' name='quotvisa_$ctr' style='width:60px;border:none;padding : 0px;text-align:left;' class='quotvisa'></td>";
        echo "<td align=center><a href=\"visaedit.php?mode=edit&vouchno=$rec[vouchno]\"><img class='' src='images/edit.png'></a>";
        ?>
        <a id='delete' class="btn btn-pad btn-square delete-entry" href="?delete&<?= $keyfld . "=" . $rec[vouchno] . "" ?>"  onClick="return confirm('Confrim Delete?');" style="">
            <i align="left" class="fa fa-trash" style="color:red;height:10px;width:10px;"></i>
        </a>
        <?php
        echo "</td>";
        Echo "</tr>";
        $ctr++;
    }
}