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

$formname = 'Weekly Confirmed Records';
$maindb = 'leadenq';
$printfile = "";
$file = 'weekconf.php';
$trandbexist = 0;
$grid = '';
$keyfld = "vouchno";
$keycond = " ";
$focusfield = "name";
$mgrpcode = '009';
$usertype = $_SESSION["usertype"];
$user = $_SESSION['username_' . $dbname];

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn,$zdb);
include("includes/vouchno_generate.php");
include("includes/prev&next.php");

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


//    if (isset($_POST["submit"])) {
    $mailvouch = trim($_POST["mailvouch"]);
    $ename = trim($_POST["ename"]);
    $elastname = trim($_POST["elastname"]);
    $edest = trim($_POST["edest"]);
    $etravdate = $_POST["etravdate"];
    $eremarks = trim($_POST["eremarks"]);
    $emobile = trim($_POST["emobile"]);
    $epackage = trim($_POST["epackage"]);

//    browse($emobile);
    $getnewvouch = mysqli_query($conn,"SELECT * FROM `visamain` WHERE leadvouch='$mailvouch' ") or die(mysqli_error($conn));
    $ledvouch = mysqli_num_rows($getnewvouch);
    $ledget = mysqli_fetch_array($getnewvouch);
    if ($ledvouch > 0) {
        $oldledvouch = $ledget[vouchno];
    } else {
        $gen1 = mysqli_query($conn,"SELECT MAX($keyfld) AS maxcode FROM `visamain` LIMIT 0,1 ") or die(mysqli_error($conn));

        if (mysqli_num_rows($gen1) > 0) {
            $vvouch = mysqli_fetch_assoc($gen1);
            $qlvouch = $vvouch["maxcode"] + 1;
        } else {
            $qlvouch = '1';
        }
    }
    $date = date("Y-m-d");
    if ($ledvouch > 0) {
        mysqli_query($conn,"UPDATE `visamain` SET `name`='$ename', `mobile`='$emobile', `lastname`='$elastname', `deptdate`='$etravdate', `package`='$epackage',
                        `destination`='$edest',leadvouch='$mailvouch',vouchdt='$date' WHERE `vouchno`='$oldledvouch'")or die(mysqli_error($conn));
    } else {
        mysqli_query($conn,"INSERT INTO `visamain`(vouchno, `vouchdt`, `leadvouch`, `mobile`, `name`, `lastname`, `destination`, `deptdate`, `package`,
                                               `username`)
                                        VALUES($qlvouch, '$date', '$mailvouch', '$emobile', '$ename', '$elastname', '$edest',  '$etravdate', '$epackage',
                                               '$username')") or die(mysqli_error($conn));
    }

    $count = $_POST["count"];

    for ($i = 0; $i < $count; $i++) {
        $msrno = $_POST["srno_$i"];
        $vouchno = $_POST["vouchno_$i"];
        $quotvisa = $_POST["quotvisa_$i"];
        $mup = $_POST["mup_$i"];
        $conftot = $_POST["conftot_$i"];


        if (!empty($msrno)) {
            mysqli_query($conn,"UPDATE `leadenq` SET `quotvisa`='$quotvisa',`mup`='$mup', `conftot`='$conftot'  WHERE `vouchno`='$vouchno'")or die(mysqli_error($conn));
        }
    }
    updmess();
}
if (isset($_POST["submit"])) {

    $count1 = $_POST["count"];

    for ($i = 0; $i < $count1; $i++) {
        $msrno = $_POST["srno_$i"];
        $vouchno = $_POST["vouchno_$i"];
        $quotvisa = $_POST["quotvisa_$i"];
        $mup = $_POST["mup_$i"];
        $conftot = $_POST["conftot_$i"];


        if (!empty($msrno)) {
            mysqli_query($conn,"UPDATE `leadenq` SET `quotvisa`='$quotvisa',`mup`='$mup', `conftot`='$conftot'  WHERE `vouchno`='$vouchno'")or die(mysqli_error($conn));
        }
    }
    updmess();
}

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


include("includes/linkshead.php");
?>
</head>
<script type="text/javascript">
    $(document).ready(function () {
        $("body").on('change', '.quotvisa', getEmailModal);
        $("body").on('click', '.CMail', sendMail);

        function getEmailModal() {
            var $row = $(this).closest("tr");
            var option_value1 = $row.find('.quotvisa').val();
            var vouchno1 = $row.find('.vouchno').val();
            var data = {vouchno: vouchno1};
            $.ajax({
                type: 'GET',
                dataType: 'json',
                url: "show_cust_dtl.php",
                data: data,
                success: function (response) {
                    //            var option_value = document.getElementById("status").value;
                    if (option_value1 == "Y") {
                        $('#mailvouch').val(vouchno1);
//                                    $($row.find('.rate')).val(response.rate);
                        $('.modal-body .emobile').val(response.mobile);
                        $('.modal-body .ename').val(response.name);
                        $('.modal-body .elastname').val(response.lastname);
                        $('.modal-body .edest').val(response.destination);
                        $('.modal-body .etravdate').val(response.deptdate);
                        $("#emailModal").modal("show");
//                        $(".modal-body #tr2").show();
//                        $(".modal-body #tr1").hide();
//                        $(".modal-body #tr3").hide();
//                        $(".modal-body #tr4").hide();
//                        $(".modal-body #tr5").hide();
//                        $(".modal-body #tr6").hide();

                    }
                },
                error: function (request, textStatus, errorThrown) {
                    alert(errorThrown);
                }
                //dataType:'json'
            });
//                    $.get('get_followup_date.php', {vouchno: vouchno}, function (data) {
//
//
//
//                    });
        }


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

        $(".search").click(function () {
            var sortby = $(".sortby").val(),
                    sorderby = $(".sorderby").val(),
                    field = $(".field").val(),
                    search = $(".tsearch").val(),
                    type = "Confirmed",
                    section = "2";

            $.post('get_sortconf.php', {field: field, search: search, sortby: sortby, sorderby: sorderby, type: type, section: section}, function (data) {
//                alert(data);
                $(".mytable").html(data);
            });
        });

        $(".btsort").click(function () {
            var sortby = $(".sortby").val(),
                    type = "Confirmed",
                    sorderby = $(".sorderby").val(),
                    field = $(".field").val(),
                    search = $(".tsearch").val(),
                    section = "2";

            $.post('get_sortconf.php', {field: field, search: search, sortby: sortby, sorderby: sorderby, type: type, section: section}, function (data) {
//                alert(data);
                $(".mytable").html(data);
            });
        });
    });
</script>
<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="row" style="padding-left: 0px;">
                        <input style="float: right;padding-right: 15px;"  type='submit' id="update" name='submit' 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">Conf. 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>
                        <!--                    </div>
                                            <div style="padding-left: 0px;">-->
                        <!--                        <input id='addnew' value="Add Lead 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-default btsort" id='btsort'  style="float: right;" value="Sort">
                        <!--<input type='text' style="width:200px;float: right;" name='sortbtn' class="form-control sortbtn">-->
                        <select class="form-control sorderby" style="float:right;width:120px;">
                            <option value="2">DESCENDING</option>
                            <option value="1">ASCENDING</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;">
                            <option value="3">Book No.</option>
                            <?php
                            if ($usertype == 1) {
                                ?>
                                <option value="9">Staff</option>
                            <?php } ?>
                            <option value="1">Name</option>
                            <option value="2">Last Name</option>

                            <option value="4">Mobile</option>
                            <option value="5">Conf. 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="panel-body table-responsive" style="">
                        <table id="" class="table-bordered" width="">
                            <thead>
                                <tr style="background-color: #64CCFC;padding-right: 5px;">
                                    <th style="width:5%;">Sr.</th>
                                    <?php
                                    if ($usertype == 1) {
                                        ?>
                                        <th style="width:10%;">Staff</th>
                                    <?php } ?>
                                    <th style="width:10%;">Book No.</th>
                                    <th style="width:10%;">Conf. Date</th>
                                    <th style="width:10%;">Name</th>
                                    <th style="width:10%;">Last Name</th>
                                    <th style="width:10%;">Destination</th>
                                    <th style="width:10%;">Travel Date</th>
                                    <th style="width:10%;">Mobile</th>
                                    <th style="width:10%;">Email Id</th>
                                    <th style="width:10%;" class="text-right">MUP</th>
                                    <th style="width:10%;" class="text-right">Tot. Pax</th>
                                    <th style="width:10%;">Que to Visa</th>
                                    <th style="width:10%;">Source</th>
                                    <th style="width:10%;">Bill No.</th>
                                    <th style="width:10%;">Bill Date</th>
                                    <th style="width:10%;">Feedback</th>
                                    <th style="width:5%;">Action</th>
                                </tr>
                            </thead>  
                            <tbody class="mytable">
                                <?php
                                $query_date = date("Y-m-d");

// First day of the month.
                                $frstdt = date('Y-m-01', strtotime($query_date));

// Last day of the month.
                                $lastdt = date('Y-m-t', strtotime($query_date));
//                    
//                    browse($frstdt);
//                    browse($lastdt);
                                $getmup = mysqli_query($conn,"Select * from `leadenq` where `confdate` >= '$frstdt' and   `confdate` <= '$lastdt' order by confdate desc") or die(mysqli_error($conn));

//                                $row = mysqli_fetch_array($getmup);


                                $ctr = 1;

                                while ($row = mysqli_fetch_array($getmup)) {
//                                    browse($row[deptdate]);
                                    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>";
                                    if ($usertype == 1) {
                                        Echo "<td class=''><input type='text 'value='$row[username]' name='username_$ctr' style='border : none;width : 100px; padding : 0px;text-align:left;' readonly class='username'></td>";
                                    }
                                    Echo "<td class=''><input type='text' value='$row[vouchno]' name='vouchno_$ctr' style='border : none;width : 30px; padding : 0px;text-align:left;' readonly class='vouchno'></td>";
                                    Echo "<td class=''><input type='date' value='$row[confdate]' name='vouchdt_$ctr' style='border : none;width : 120px; padding : 0px;text-align:left;' readonly class='vouchdt'></td>";
                                    Echo "<td class=''><input type='text' value='$row[name]' name='name_$ctr' style='border : none; padding : 0px;text-align:left;' 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 : 120px;' readonly class='lastname'></td>";
                                    Echo "<td class=''><input type='text' value='$row[destination]' name='destination_$ctr' style='width : 100px;border : none; padding : 0px;text-align:left;' readonly class='destination'></td>";
                                    Echo "<td class=''><input type='date' value='$row[deptdate]' name='deptdate_$ctr' style='width : 120px;border : none; padding : 0px;text-align:left;' readonly class='deptdate'></td>";
                                    Echo "<td class=''><input type='text' value='$row[mobile]' name='mobile_$ctr' style='width : 100px;border : none; padding : 0px;text-align:left;' readonly class='mobile'></td>";
                                    Echo "<td class=''><input type='text' value='$row[email]' name='email_$ctr' style='width : 145px;border : none; padding : 0px;text-align:left;' readonly class='email'></td>";
                                    Echo "<td class=''><input type='text' value='$row[mup]' name='mup_$ctr' style='width : 100px;border : none; padding : 0px;text-align:right;' class='mup'></td>";
                                    Echo "<td class=''><input type='text' value='$row[conftot]' name='conftot_$ctr' style='width : 100px;border : none; padding : 0px;text-align:right;' class='conftot'></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=''><input type='text' value='$row[source]' name='source_$ctr' style='width : 100px;border : none; padding : 0px;text-align:left;' readonly class='source'></td>";
                                Echo "<td class=''><input type='text' value='$row[billno]' name='billno_$ctr' style='border : none; padding : 0px;text-align:left;' readonly class='billno'></td>";
                                Echo "<td class=''><input type='text' value='$row[billdate]' name='billdatedate_$ctr' style='width : 100px;border : none; padding : 0px;text-align:left;' readonly class='billdate'></td>";
                                Echo "<td class=''><input type='text' value='$row[feedback]' name='feedback_$ctr' style='width : 50px;border : none; padding : 0px;text-align:left;' readonly class='feedback'></td>";

                                $leadvchres = mysqli_query($conn,"SELECT * FROM `confmain` WHERE `leadvouch` = '$row[vouchno]' ") or die(mysqli_error($conn));
                                $getlead = mysqli_num_rows($leadvchres);
                                $ldmvch = mysqli_fetch_array($leadvchres);
//                                    browse($getlead);
                                if ($getlead > 0) {
                                    echo "<td align=left><a href=\"confrecd.php?mode=edit&vouchno=$ldmvch[vouchno]\"><img class='leadvouch' src='images/edit.png'></a></td>";
                                } else {
//                                        echo "<td align=center><a href=\"confrecd.php?vouchno=$row[vouchno]\"><img src='images/edit.png'></a></td>";
                                    echo "<td align=left><a href = 'confrecd.php?mode=true&vouchno=$row[vouchno]'><img class='leadvouch' src='images/edit.png'></a></td>";
                                }
                                echo "</tr>";
                                $ctr++;
                            }
                            ?>

                            </tbody> 
                        </table>
                        <input type = "hidden" name = "count" value = "<? Echo $ctr; ?>">
                    </div>
                    <!--                    <div class="panel-footer">
                    <?php // include("includes/buttons.php");     ?>
                                        </div>-->
                </div>
            </div>
        </div>
        <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%;">
                <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>
                                <tr id="">
                                    <td><label class="label-control marg">Booking No.</label></td>
                                    <td><input type="text" name="mailvouch" id="mailvouch" value="<?php echo ($addflag != 1) ? $row['mailvouch'] : ""; ?>"  style="width:175px;" class="mailvouch form-control" readonly="readonly"></td>
                                    <td><label class="label-control marg">Mobile</label></td>
                                    <td><input type="text" name="emobile" id='mobile1' placeholder="" value="<?php echo ($addflag != 1) ? $row['mobile'] : ""; ?>" style="width:175px;" class="form-control emobile" readonly="readonly"></td>
                                </tr>
                                <tr id="">
                                    <td><label class="label-control marg">Name</label></td>
                                    <td><input type="text" name="ename" id='ename' placeholder="" value="<?php echo ($addflag != 1) ? $row['name'] : ""; ?>" style="width:300px;" class="form-control ename" readonly="readonly"></td>
                                    <td><label class="label-control marg">Last Name</label></td>
                                    <td><input type="text" name="elastname" id='elastname' placeholder="" value="<?php echo ($addflag != 1) ? $row['lastname'] : ""; ?>" style="width:300px;" class="form-control elastname" readonly="readonly"></td>
                                </tr>
                                <tr id="">
                                    <td><label class="label-control marg">Destination</label></td>
                                    <td><input type="text" name="edest" id='edest' placeholder="" value="<?php echo ($addflag != 1) ? $row['destination'] : ""; ?>" style="width:175px;" class="form-control edest" readonly="readonly"></td>
                                </tr>
                                <tr id="">
                                    <td><label class="label-control marg">Travel Date</label></td>
                                    <td><input type="date" name="etravdate" id='etravdate' placeholder="" value="<?php echo ($addflag != 1) ? $row['deptdate'] : ""; ?>" style="width:175px;" class="form-control etravdate" readonly="readonly"></td>
                                </tr>
                                <tr id="">
                                    <td><label class="label-control marg">Remarks</label></td>
                                    <td><input type="text" name="eremarks" id='eremarks' placeholder="" value="<?php echo ($addflag != 1) ? $row['remarks'] : ""; ?>" style="width:400px;" class="form-control eremarks"></td>
                                </tr>
                                <tr id="">
                                    <td><label class="label-control marg">Type</label></td>
                                    <td><select name="epackage" id='epackage' class="form-control epackage" style="width:175px;" >
                                            <option></option>   
                                            <option value="P">Package</option>   
                                            <option value="N">Non Package</option>   
                                        </select>
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </div>
                    <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pad text-right">
                        <button type="submit" name="mailbutn" id="mailbutn" class="btn btn-primary CMail" style="margin-right: 20px"><i class="fa fa-envelope"></i>Send Mail To Visa Department</button>
                    </div>
                    <div class="modal-footer"></div>
                </div>
            </div>
        </div>
    </form>

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

</body>
</html>
