<?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"];
    $toddate = date("Y-m-d");

    $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%'";
    } else if ($_REQUEST["field"] == "10") {
        $scond = "AND `source` 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";
        }
    } else if ($_REQUEST["sortby"] == "10") {
        if ($sortTerm == "1") {
            $cond = "order by `source` ASC";
        } else {
            $cond = "order by `source` DESC";
        }
    }

//    $scond = " AND `name` = '$_REQUEST[search]'";
//    brecse($scond);
//    $getrec = mysqli_query($conn,"SELECT *, DATE_FORMAT(`recddate`, '%d-%m-%Y') as recddt FROM `salemain` WHERE `vouchno`!='' $dtcond $vcond $pcond $scond ORDER BY `vouchno`")or die(mysqli_error($conn));
//    if ($usertype == 1) {
    $getrec = mysqli_query($conn,"SELECT *, DATE_FORMAT(`deptdate`, '%d-%m-%Y') as deptdate 
                           FROM `leadenq` WHERE `vouchno`!='' and `vouchdt`='$toddate' $scond $cond ")or die(mysqli_error($conn));
//    } else {
//        $getrec = mysqli_query($conn,"SELECT *, DATE_FORMAT(`deptdate`, '%d-%m-%Y') as deptdate 
//                           FROM `leadenq` WHERE `vouchno`!='' and `username`='$user' AND `status` != 'Destination Change'  
//                           and `status` != 'Cancelled' and `status` != 'Confirmed'  $cond ORDER BY `vouchno` desc")or die(mysqli_error($conn));
//    }
    $get1 = mysqli_num_rows($getrec);
//    browse($get1);
    $ctr = 1;
    while ($row = mysqli_fetch_array($getrec)) {

        $name = strtoupper($row[name]);
        $lastname = strtoupper($row[lastname]);

        echo "<tr>";
        Echo "<td class=''><input type='text' value='$ctr' name='srno_$ctr' style='border : none;width : 30px;text-align:center;' readonly></td>";
        if ($usertype == 1) {
            Echo "<td class=''><input type='text' value='$row[username]' name='username_$ctr' style='border : none;width : 80px;text-align:center;' readonly></td>";
        }

        Echo "<td class=''><a href=\"leadenq.php?vouchno=$row[vouchno]&mode=edit\"><input type='text' value='$row[vouchno]' name='vouchno_$ctr' style='border : none;width : 70px; text-align:center;' readonly class='vouchno'></a></td>";
        Echo "<td class=''><input type='date' value='$row[vouchdt]' name='vouchdt_$ctr' style='border : none;width : 120px;text-align:center;' readonly class='vouchdt'></td>";
        Echo "<td class=''><a href=\"leadenq.php?vouchno=$row[vouchno]&mode=edit\"><input type='text'value='$name' name='name_$ctr' style='border : none;text-align:center;width:110px;' readonly class='name'></a></td>";
        Echo "<td class=''><a href=\"leadenq.php?vouchno=$row[vouchno]&mode=edit\"><input type='text'value='$lastname' name='lastname_$ctr' style='border : none;text-align:center;width:110px;' readonly class='lastname'></a></td>";
        Echo "<td class=''><input type='text'value='$row[mobile]' name='mobile_$ctr' style='border : none;text-align:center;width:110px;' readonly class='mobile'></td>";
        Echo "<td class=''><a href=\"leadenq.php?vouchno=$row[vouchno]&mode=edit\"><input type='text'value='$row[destination]' name='destination_$ctr' style='border : none;text-align:center;width:100px;' readonly class='destination'></a></td>";
        Echo "<td class=''><input type='text' value='$row[source]' name='source_$ctr' style='border : none;text-align:center;' readonly class='source'></td>";
        Echo "<td class=''><input type='text' value='$row[deptdate]' name='deptdate_$ctr' style='border : none;text-align:center;width:120px;' readonly class='deptdate'></td>";
        ?>
        <?php
//                                           echo "</td>";
        echo "<td align='left'>"
        ?> 
        <select  name='status_<?= $ctr ?>' id="status"  class="status form-control" style='height:33px;border: 0px;width:150px;'>
            <option value='Not Quoted'<?php
        if ($addflag != 1) {
            if ($row["status"] == "Not Quoted") {
                echo "selected='selected'";
            }
        }
        ?> >Not Quoted</option>
            <option value='Quoted Not Called'<?php
        if ($addflag != 1) {
            if ($row["status"] == "Quoted Not Called") {
                echo "selected='selected'";
            }
        }
        ?>>Quoted Not Called</option>
            <option value='Quoted & Called'<?php
        if ($addflag != 1) {
            if ($row["status"] == "Quoted & Called") {
                echo "selected='selected'";
            }
        }
        ?>>Quoted & Called</option>
            <option value='Follow up 2'<?php
        if ($addflag != 1) {
            if ($row["status"] == "Follow up 2") {
                echo "selected='selected'";
            }
        }
        ?>>Follow up 2</option>
            <option value='Follow up 3'<?php
        if ($addflag != 1) {
            if ($row["status"] == "Follow up 3") {
                echo "selected='selected'";
            }
        }
        ?>>Follow up 3</option>
            <option value='Destination Change'<?php
        if ($addflag != 1) {
            if ($row["status"] == "Destination Change") {
                echo "selected='selected'";
            }
        }
        ?>>Destination Change</option>
            <option value='Cancelled'<?php
        if ($addflag != 1) {
            if ($row["status"] == "Cancelled") {
                echo "selected='selected'";
            }
        }
        ?>>Cancelled</option>
            <option value='Postponed'<?php
        if ($addflag != 1) {
            if ($row["status"] == "Postponed") {
                echo "selected='selected'";
            }
        }
        ?>>Postponed</option>
            <option value='Confirmed'<?php
        if ($addflag != 1) {
            if ($row["status"] == "Confirmed") {
                echo "selected='selected'";
            }
        }
        ?>>Confirmed</option>
            <option value='Amendment Requoting'<?php
        if ($addflag != 1) {
            if ($row["status"] == "Amendment Requoting") {
                echo "selected='selected'";
            }
        }
        ?>>Amendment Requoting</option>
        </select>

        <?php
        echo "</td>";
        echo "<td align='left'>"
        ?>
        <select name='quotvisa_<?= $ctr ?>' id="quotvisa"  class="quotvisa form-control" style='text-align: center;height:33px;width:100px;border: 0px;'>
            <option value='N'<?php
        if ($addflag != 1) {
            if ($row["quotvisa"] == "N") {
                echo "selected='selected'";
            }
        }
        ?> >No</option>
            <option value='Y'<?php
        if ($addflag != 1) {
            if ($row["quotvisa"] == "Y") {
                echo "selected='selected'";
            }
        }
        ?>>Yes</option>
        </select>
        <?php
        echo "</td>";
        Echo "<td class='text-right'><input type='text' value='$row[remarks]' name='remarks_$ctr' style='width:150px;border:none;padding : 0px;text-align:left;' class='remarks'></td>";
        Echo "<td class='text-right'><input type='text' value='$row[remarks1]' name='remarks1_$ctr' style='width:150px;border:none;border-bottom:1px solid black; padding : 0px;text-align:left;' class='remarks1'></td>";
        ?>
        <!--                                <td class=" buttonform tooltip-bottom text-right" id="hdelete" style="display: <? Echo $deletedisabled ?>" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="Delete Record">
            <a id='delete' class="btn btn-pad btn-square delete-entry" href="?delete&<?= $keyfld . "=" . $row[vouchno] . "" ?>"  onClick="return confirm('Confrim Delete?');" style="background-color:#D9534F;">
                <i class="fa fa-trash" style="color:#ffffff;height:10px;width:10px;"></i>
            </a>
        </td>-->
        <!--//                                echo "<td><a href=\"?vouchno=$row->vouchno\" onClick=\"return confirm('Confrim Delete?');\"><i class='fa fa-trash-o' style='font-size:1.3em;color:red;cursor:pointer;'></i></a></td>";-->
        <?php
        echo "</tr>";
        $ctr++;
    }
}