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

$formname = 'New Visa Records';
$maindb = 'visamain';
$trandb = 'visatran';
$printfile = "";
$file = 'visanew.php';
$fileadd = 'visaedit.php';
$trandbexist = 0;
$grid = '';
$keyfld = "vouchno";
$keycond = " ";
$focusfield = "name";
//$mgrpcode = '009';

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];


$usertype = $_SESSION["usertype"];
$user = $_SESSION['username_' . $dbname];
//browse($user);

mysqli_select_db($conn,$zdb);
include("includes/vouchno_generate.php");
include("includes/prev&next.php");

if (isset($_POST["update"])) {

    $count = $_POST["count"];
    $subvouch = $_REQUEST["subvouch"];
    $cancresn = $_REQUEST["cancresn"];
//    browse($cancresn);
    if (!empty($cancresn)) {
        mysqli_query($conn,"UPDATE `visamain` SET `cancresn`='$cancresn' WHERE `vouchno`='$subvouch'")or die(mysqli_error($conn));
    }

    for ($i = 0; $i < $count; $i++) {
        $msrno = $_POST["srno_$i"];
        $vouchno = $_POST["vouchno_$i"];
        $status = $_POST["status_$i"];


//        browse($vouchno);
//        browse($cancresn);
        if (!empty($msrno)) {
//      
            mysqli_query($conn,"UPDATE `visamain` SET `status`='$status' WHERE `vouchno`='$vouchno'")or die(mysqli_error($conn));
        }
    }
    updmess();
//    header("refresh: 1; $file");
}

if ($addflag != 1) {

    $getvouch = '';
    if ($addflag == 0) {
        $getvouch = $_GET[$keyfld];
    } else {
        $getvouch = $qvouch;
    }

    $result = mysqli_query($conn,"SELECT * FROM `$maindb` WHERE $keyfld='$getvouch' ") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);
}

$senable = "";
$cenable = "";


if (isset($_GET["delete"])) {
    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    delete($zdb, "$trandb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
}

include("includes/linkshead.php");
?>
<!--<script src="MyScript/leadenq.js" type="text/javascript"></script>-->
<script type="text/javascript">
    $(document).ready(function () {
        $("body").on('change', '.quotvisa', getPaxModal);
//        $("body").on('click', '.CMail', sendMail);
        $("body").on('change', '.status', GetReason);
        //    $("body").on('click', '.exceltest', test);

        function GetReason() {
            var $row = $(this).closest("tr");
            var vouchno = $row.find(".vouchno").val();
            var status = $row.find(".status").val();
            if (status == "5") {
                $.post('get_reason.php', {vouchno: vouchno}, function (data) {
                    if (data != '') {
                        $(".cancresn").html(data);
                        $(".cancresn").focus();

                    }
                });
                $('#subvouch').val(vouchno);
                $("#myModal").modal("show");

            }
        }

        function getPaxModal() {
            var $row = $(this).closest("tr");
            var option_value1 = $row.find('.quotvisa').val();
            var vouchno = $row.find('.vouchno').val();
//            alert(option_value1);
            if (option_value1 == "Y") {
                $("#newvouch").val(vouchno);
                $("#emailModal").modal("show");

            }
        }

        $(".search").click(function () {
            var sortby = $(".sortby").val(),
                    sorderby = $(".sorderby").val(),
                    field = $(".field").val(),
                    fromdate = $(".fromdate").val(),
                    todate = $(".todate").val(),
                    search = $(".tsearch").val();


            $.post('get_srchvisa.php', {field: field, search: search, sortby: sortby, sorderby: sorderby, fromdate: fromdate,todate: todate}, function (data) {
//                alert(data);
                $(".mytable").html(data);
            });
        });

        $(".btsort").click(function () {
            var sortby = $(".sortby").val(),
                    sorderby = $(".sorderby").val(),
                    field = $(".field").val(),
                    fromdate = $(".fromdate").val(),
                    todate = $(".todate").val(),
                    search = $(".tsearch").val();

            $.post('get_sortvisa.php', {field: field, search: search, sortby: sortby, sorderby: sorderby, fromdate: fromdate,todate: todate}, function (data) {
//                alert(data);
                $(".mytable").html(data);
            });
        });
    });

//        function sendMail() {
//            var ename = $(".ename").val(), elastname = $(".elastname").val(), edest = $(".edest").val(),
//                    etravdate = $(".etravdate").val(), eremarks = $(".eremarks").val();
//            $.post('customer_SendMail.php', {ename: ename, elastname: elastname, edest: edest, etravdate: etravdate, eremarks: eremarks}, function (data) {
//                if (data = "succcess") {
//                    alert("Details Are Send To The Visa Department");
//                    $("#emailModal").modal("hide");
//                }
//            });
//        }

//        $('table tbody tr  td').on('change', function () {
//            $("#myModal").modal("show");
//            $("#status").val($(this).closest('tr'));
//    });
</script>
<script type="text/javascript">
    $(document).keydown(function (event) {
        if (!(String.fromCharCode(event.which).toLowerCase() == 'a' && event.altKey) && !(event.which == 19))
            return true;
        $("#add").click();
        event.preventDefault();
        return false;
    });

    $(document).keydown(function (event) {
        //19 for Mac Command+S
        if (!(String.fromCharCode(event.which).toLowerCase() == 's' && event.altKey) && !(event.which == 19))
            return true;
        //alert("Ctrl-s pressed");
        $("#update").click();
        event.preventDefault();
        return false;
    });

    $(document).keydown(function (event) {
        //19 for Mac Command+S
        if (!(String.fromCharCode(event.which).toLowerCase() == 'f' && event.altKey) && !(event.which == 19))
            return true;
        //alert("Ctrl-s pressed");
        $("#btsearch").click();
        event.preventDefault();
        return false;
    });
</script>

</head>
<body>
    <!--    <div id="preloader" class="col-lg-12 text-center">
            <h3 class="text-center">Loading</h3>
            <div id="status">&nbsp;</div>
        </div>-->
    <form action='' method='POST'>
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-12">
                <div class="panel panel-default">
                    <!--                    <div class="panel-heading">
                                            <h3 class="panel-title"><?php Echo $formname; ?></h3>
                                            <div class="actions pull-right">
                    
                                                <i class="fa fa-chevron-down"></i>
                                                <i class="fa fa-times"></i>
                                            </div> 
                                            <input type='button' style="float: right;" name='exit' value='Exit' class="btn btn-danger" onClick="window.location.href = '<? Echo $file; ?>'">
                    
                                        </div> -->
                    <div style="padding-left: 0px;">
                        <input id='add' value="Add Visa Entry" type="button"  class="btn btn-pad btn-square button" style="background-color:#64CCFC;" onclick="window.location.href = '<?= $fileadd . "?mode=true" ?>'" class="btn btn-labeled btn-success">
                        <i class="fa fa-plus" style="color:#ffffff"></i>    

                        <!--</button>-->

                        <input style="float: right;padding-right: 15px;"  type='submit' id="update" name='update' value='Save' class="btn btn-primary" tabindex="4">

                        <input type='button' class="btn btn-warning search" id='btsearch'  style="float: right;" value="Search" tabindex="3">
                        <input type='text' style="width:150px;float: right;" name='search' class="form-control tsearch"  tabindex="2">
                        <select class="form-control field" style="float:right;width:150px;" autofocus="autofocus" tabindex="1">
                            <?php
                            if ($usertype == 1) {
                                ?>
                                <option value="9">Staff</option>
                            <?php } ?>
                            <option value="1">Name</option>
                            <option value="2">Last Name</option>
                            <option value="3">Book No.</option>
                            <option value="4">Mobile</option>
                            <option value="5">Date</option>
                            <option value="6">Destination</option>
                            <option value="7">Status</option>
                            <!--<option value="8">Remark</option>-->
                        </select>
                        <label style="float:right;padding-top: 9px;padding-right: 7px;">Search By</label>
                        <label style="float:right;padding-right: 30px;"></label>

                        <input type='button' class="btn btn-default btsort" id='btsort'  style="float: right;" value="Sort">
                        <select class="form-control sorderby" style="float:right;width:120px;">
                            <option value="1">ASCENDING</option>
                            <option value="2">DESCENDING</option>
                        </select>
                        <label style="float:right;padding-top: 9px;padding-right: 7px;color: #64CCFC;">Order By</label>
                        <select class="form-control sortby" style="float:right;width:100px;">
                            <?php
                            if ($usertype == 1) {
                                ?>
                                <option value="9">Staff</option>
                            <?php } ?>
                            <option value="1">Name</option>
                            <option value="2">Last Name</option>
                            <option value="3">Book No.</option>
                            <option value="4">Mobile</option>
                            <option value="5">Date</option>
                            <option value="6">Destination</option>
                            <option value="7">Status</option>
                            <!--<option value="8">Remark</option>-->
                        </select>
                        <label style="float:right;padding-top: 9px;padding-right: 7px;color: #64CCFC;">Sort By</label>
                        <!--<label style="float:right;">Search By</label>-->
                    </div>
                    <div class="row">
                        <div class="col-lg-5 text-right"><label class="label-control marg text-right">From Date</label></div>
                        <div class="col-lg-2">
                            <input type="date" name="fromdate" id="fromdate"class="fromdate form-control" value=""> 
                        </div>
                        <div class="col-lg-1 text-right"><label class="label-control marg text-right">To Date</label></div>
                        <div class="col-lg-2">
                            <input type="date" name="todate" id="todate" class="todate form-control"   value="">
                        </div>
                        
                    </div>
                    <div class="panel-body table-responsive" style="width:100%">
                        <table id="" class="table-bordered" width="100%">
                            <thead>
                                <tr style="background-color: #64CCFC">
                                    <th>Sr.</th>
                                    <th>Queued By</th>
                                    <th>Voucher</th>
                                    <th>Lead Vouch</th>
                                    <th>Date</th>
                                    <th>Name</th>
                                    <th>Last Name</th>
                                    <th>Mobile</th>
                                    <th>Destination</th>
                                    <th>Travel Date</th>
                                    <th>Client Status</th>
                                    <!--<th>Attach Image</th>-->
                                    <!--<th>Visa Status</th>-->
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody class="mytable table-bordered">
                                <?php
//                                if ($usertype == 1) {
                                $srchresult = mysqli_query($conn,"SELECT *, DATE_FORMAT(`deptdate`, '%d-%m-%Y')as deptdate FROM `$maindb` where `aknwgive`!='' ORDER BY VOUCHNO DESC") or die(mysqli_error($conn));
//                                } else {
//                                    $srchresult = mysqli_query($conn,"SELECT *, DATE_FORMAT(`deptdate`, '%d-%m-%Y')as deptdate FROM `$maindb`
//                                              WHERE `username` ='$user' ") or die(mysqli_error($conn));
//                                }

                                $ctr = 1;

                                while ($row = mysqli_fetch_array($srchresult)) {
                                    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='$row[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=$row[vouchno]\"><input type='text' value='$row[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='$row[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='$row[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='$row[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='$row[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='$row[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='$row[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='$row[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" style='width:150px;'>
                                    <option value='0'<?php
                                    if ($addflag != 1) {
                                        if ($row["status"] == "0") {
                                            echo "selected='selected'";
                                        }
                                    }
                                    ?> >Still to Speak</option>
                                    <option value='1'<?php
                                    if ($addflag != 1) {
                                        if ($row["status"] == "1") {
                                            echo "selected='selected'";
                                        }
                                    }
                                    ?>>Requirement to be Given</option>
                                    <option value='2'<?php
                                    if ($addflag != 1) {
                                        if ($row["status"] == "2") {
                                            echo "selected='selected'";
                                        }
                                    }
                                    ?>>Requirement Given - Follow up</option>
                                    <option value='3'<?php
                                    if ($addflag != 1) {
                                        if ($row["status"] == "3") {
                                            echo "selected='selected'";
                                        }
                                    }
                                    ?>>Copy Collected</option>
                                    <option value='4'<?php
                                    if ($addflag != 1) {
                                        if ($row["status"] == "4") {
                                            echo "selected='selected'";
                                        }
                                    }
                                    ?>>Original Collected</option>
                                    <option value='5'<?php
                                    if ($addflag != 1) {
                                        if ($row["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='$row[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='$row[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=$row[vouchno]\"><img class='' src='images/edit.png'></a>";
                                ?>
                                <a id='delete' class="btn btn-pad btn-square delete-entry" href="?delete&<?= $keyfld . "=" . $row[vouchno] . "" ?>"  onClick="return confirm('Confrim Delete?');" >
                                    <i align="left" class="fa fa-trash" style="color:red;width:10px;"></i>
                                </a>
                                <?php
                                echo "</td>";
                                echo "</tr>";
//                                Echo "<td class='text-right'><input type='text' value='$row[remarks1]' name='remarks1_$ctr' style='width:160px;border:none;border-bottom:1px solid black; padding : 0px;text-align:left;' class='remarks1'></td>";
                                $ctr++;
                            }
                            ?>
                            <input type = "text" style="display: none;" name = "count" value = "<? Echo $ctr; ?>">
                            </tbody> 
                        </table>

                    </div>
                    <!--                    <div class="panel-footer">
                    <?php // include("includes/buttons.php");         ?>
                                        </div>-->
                </div>
            </div>
        </div>
        <?php
        mysqli_select_db($conn,$zdb);

//        $ressubvouch = mysqli_query($conn,"SELECT *,DATE_FORMAT(`flwup2dt`, '%d-%m-%Y')as flwup2dt, DATE_FORMAT(`flwup3dt`, '%d-%m-%Y')as flwup3dt FROM `$maindb` WHERE $keyfld='$subvouch' ") or die(mysqli_error($conn));
//        $getsub = mysqli_fetch_array($ressubvouch);
        ?>
        <div class="modal fade bs-example-modal-lg" id="emailModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
            <div class="modal-dialog modal-lg" style="width: 70%;height: 100%;   ">
                <div class="modal-content">
                    <div class="modal-header">
                        <button class="close" data-dismiss="modal"><span>&times;</span></button>
                        <h4 class="modal-title" id="myLargeModalLabel"></h4>
                    </div>
                    <!--<input type='button' style="float: right;" name='exit' value='Exit' class="btn btn-danger" onClick="window.location.href = '<? Echo $file; ?>'">-->
                    <!--<button style="float: right;" type='submit' name='update' value='Update' class="btn btn-success">-->
                    <!--<button id='submit'  type='submit' name='submit' value='Update' class="btn btn-success">Save</button>--> 


                    <div class="modal-body">
                        <div class="table-responsive">
                            <table id="data" align="center" border="1px" width="100%" class="table-bordered">
                                <thead>
                                    <tr>
                                        <th width="10%" style="font-family: arial-regular;color:black;" align="center"><label class="control-label">Sr.</label></th>
                                        <th width="50%" style="font-family: arial-regular;color:black;" align="center"><label class="control-label">Pax Name</label></th>
                                        <th width="20%" align="right" style="text-align:left;color:black;font-family: arial-regular;"><label class="control-label">Age</label></th>
                                        <th width="20%" align="right" style="text-align:right;color:black;font-family: arial-regular;"><label class="control-label">Image</label></th>
                                <input type="text" name="newvouch" id="newvouch" class="newvouch"> 
                                </tr>
                                </thead>

                                <?php
                                if ($addflag != 1) {
                                    $newvch = $_POST["newvouch"];

//                                    $qry = mysqli_query($conn,"SELECT * FROM  `$trandb` WHERE vouchno='$row[vouchno]' ORDER BY srno") or die(mysqli_error($conn));
//
                                    $ctr = 0;
                                    while ($row = mysqli_fetch_array($qry)) {
                                        echo "<tr>";
                                        echo "<td style='height:30px;' align='left' valign='center'><input type='text' size='6' maxlength='6' name='srno_" . $ctr . "' class='srno form-control form' style='height:29px;' value='$row[srno]'/></td>";
                                        echo "<td style='height:30px;' align='center' valign='center'><input type='text' size=''  name='paxname_$ctr' class='paxname form-control form' style='height:29px;text-align:left;' value='$row[particulars]'/></td>";
                                        echo "<td style='height:30px;' align='center' valign='center'><input type='text' size=''  name='age_$ctr' class='age form-control form' style='height:29px;text-align:right;' value='$row[amt]'/></td>";
                                        echo "<td style='height:30px;' align='center' valign='center'><input type='text' size=''  name='image_$ctr' class='image form-control form' style='height:29px;text-align:left;' value='$row[curr]'/></td>";
                                        echo "</tr>";
                                        $ctr++;
                                    }
                                    //browse($ctr);  
                                } else {
//                                    browse('1');
                                    $pctr = 0;
                                    $yctr = 0;
                                    for ($i = $pctr; $i < 3; $i++) {
                                        $yctr++;
                                        echo "<tr>";
                                        echo "<td style='height:30px;' align='left' valign='center'><input type='text' size='6' maxlength='6' name='srno_$pctr' class='sno form-control form' style='height:29px;' value='$yctr'/></td>";
                                        echo "<td align='center' style='height:30px;' valign='center'><input type='text' size=''  name='paxname_$pctr' class='paxname form-control form' style='height:29px;text-align:left;' value='' /></td>";
                                        echo "<td align='center' style='height:30px;' valign='center'><input type='number' size=''  name='age_$pctr' class='age form-control form' style='height:29px;text-align:right;' value='0'/></td>";
                                        echo "<td align='center' style='height:30px;' valign='center'><input type='text' size=''  name='image_$pctr' class='image form-control form' style='height:29px;text-align:left;' value=''></td>";
                                        echo "</tr>";
                                        $pctr++;
                                    }
                                }
                                ?>
                            </table>
                            <!--                        <label class="control-label col-lg-3"></label>
                                                    <div class="col-lg-8">
                                                        <button class="btn btn-lg btn-success form" data-toggle="modal" data-target=".slacker-modal" style="width: 100%;">Detail</button>
                                                    </div>-->
                        </div>
                    </div>
                    <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pad text-right">
                        <button type="button" name="mailbutn" id="mailbutn" class="btn btn-primary CMail" style="margin-right: 20px"><i class="fa fa-envelope"></i> Send Mail To Customer</button>
                    </div>
                    <div class="modal-footer"></div>
                </div>
            </div>
        </div>


        <!--       Reason Modal-->
        <div class="modal fade bs-example-modal-lg" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
            <div class="modal-dialog modal-lg" style="width: 70%;">
                <div class="modal-content">
                    <div class="modal-header">
                        <button class="close" data-dismiss="modal"><span>&times;</span></button>
                        <h4 class="modal-title" id="myLargeModalLabel"></h4>
                    </div>
                    <!--<input type='button' style="float: right;" name='exit' value='Exit' class="btn btn-danger" onClick="window.location.href = '<? Echo $file; ?>'">-->
                    <!--<button style="float: right;" type='submit' name='update' value='Update' class="btn btn-success">-->
                    <div class="container">
                        <button id='submit'  type='submit' name='update' value='Update' class="btn btn-success">Save</button> 
                    </div>

                    <div class="modal-body">
                        <div class="table-responsive">
                            <table>
                                <tr id="">
                                    <td><label class="label-control marg">Reason</label></td>
                                    <!--<td><input type="text" name="cancresn" id='cancresn' placeholder="" value="<?php // echo ($addflag != 1) ? $row['cancresn'] : "";               ?>" style="width:300px;" class="form-control reason"></td>-->
                                    <!--<td><input type="text" name="subvouch" id='subvouch' placeholder="" value="<?php echo ($addflag != 1) ? $row['vouchno'] : ""; ?>" style="width:100px;" class="subvouch form-control "></td>-->
                                    <td>
                                        <select class="form-control select2 cancresn"  autofocus="" name="cancresn" style="width:300px;">
                                            <!--<option value="<? // Echo ($addflag != 1 ) ? $row[cancresn] : ""    ?>"><? // Echo ($addflag != 1 ) ? $row[cancresn] : ""    ?></option>-->
                                            <?
//                                            Echo "<option value=''></option>";
                                            $rowsource = mysqli_query($conn,"SELECT * FROM `reason` order by `code` desc")or die(mysqli_error($conn));
                                            while ($msource = mysqli_fetch_array($rowsource)) {
                                                Echo "<option value='$msource[name]'>$msource[name]</option>";
                                            }
                                            ?>
                                        </select>

                                    </td>
                                </tr>
                            </table>
                        </div>
                    </div>
                    <div class="modal-footer"></div>
                </div>
            </div>
        </div>
    </form>

    <?php include("includes/linksfoot.php"); ?>

</body>
</html>
