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

$formname = 'Lead Records';
$maindb = 'leadenq';
$printfile = "";
$file = 'leadfeed.php';
$fileadd = 'leadenq.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["submit"])) {
//
////    if (isset($_POST["submit"])) {
//    $count = $_POST["count"];
// 
//    $subvouch = $_REQUEST["subvouch"];
//    $flwup2dt = $_REQUEST["flwup2dt"];
//    $flwup3dt = $_REQUEST["flwup3dt"];
//    $postdate = $_REQUEST["postdate"];
//    $cancresn = $_REQUEST["cancresn"];
//    $newvouch = $_REQUEST["newvouch"];
//
//    if (!empty($flwup2dt)) {
//        mysqli_query($conn,"UPDATE `leadenq` SET `flwup2dt`='$flwup2dt'  WHERE `vouchno`='$subvouch'")or die(mysqli_error($conn));
//    }
//    if (!empty($flwup3dt)) {
//        mysqli_query($conn,"UPDATE `leadenq` SET `flwup3dt`='$flwup3dt' WHERE `vouchno`='$subvouch'")or die(mysqli_error($conn));
//    }
//    if (!empty($postdate)) {
//        mysqli_query($conn,"UPDATE `leadenq` SET `postdate`='$postdate' WHERE `vouchno`='$subvouch'")or die(mysqli_error($conn));
//    }
//    if (!empty($newvouch)) {
//        mysqli_query($conn,"UPDATE `leadenq` SET `newvouch`='$newvouch' WHERE `vouchno`='$subvouch'")or die(mysqli_error($conn));
//    }
//    if (!empty($cancresn)) {
//        mysqli_query($conn,"UPDATE `leadenq` 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"];
//        $quotvisa = $_POST["quotvisa_$i"];
//        $remarks = trim($_POST["remarks_$i"]);
//
//
//
//        if (!empty($msrno)) {
//            mysqli_query($conn,"UPDATE `leadenq` SET `status`='$status', `quotvisa`='$quotvisa', `remarks`='$remarks' WHERE `vouchno`='$vouchno'")or die(mysqli_error($conn));
//        }
//    }
//    updmess();
//}
//
//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"]);
//    $mobile = trim($_POST["mobile"]);
//    
//    $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`='$mobile', `lastname`='$elastname', `deptdate`='$etravdate', 
//                        `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`, mobile , `deptdate`, 
//                                               `username`)
//                                        VALUES($qlvouch, '$date', '$mailvouch', '$mobile', '$ename', '$elastname', '$edest', '$mob' , '$etravdate', 
//                                               '$username')") or die(mysqli_error($conn));    
//        }
//
//    $count = $_POST["count"];
//
//    for ($i = 0; $i < $count; $i++) {
//        $msrno = $_POST["srno_$i"];
//        $vouchno = $_POST["vouchno_$i"];
//        $status = $_POST["status_$i"];
//        $quotvisa = $_POST["quotvisa_$i"];
//        $remarks = trim($_POST["remarks_$i"]);
//        $remarks1 = trim($_POST["remarks_$i"]);
//        
//
//        if (!empty($msrno)) {
//            mysqli_query($conn,"UPDATE `leadenq` SET `status`='$status', `quotvisa`='$quotvisa', `remarks`='$remarks',`remarks1`='$remarks1'  WHERE `vouchno`='$vouchno'")or die(mysqli_error($conn));
//        }
//    }
//    updmess();
//}
//
//if (isset($_POST["update"])) {
//
////    if (isset($_POST["submit"])) {
//    $count = $_POST["count"];
//    BROWSE($count);
//
//    for ($i = 0; $i < $count; $i++) {
//        $msrno = $_POST["srno_$i"];
//        $vouchno = $_POST["vouchno_$i"];
//        $status = $_POST["status_$i"];
//        $quotvisa = $_POST["quotvisa_$i"];
//        $remarks = trim($_POST["remarks_$i"]);
//        $remarks1 = trim($_POST["remarks1_$i"]);
//
//        if (!empty($msrno)) {
//            mysqli_query($conn,"UPDATE `leadenq` SET `status`='$status', `quotvisa`='$quotvisa', `remarks`='$remarks', `remarks1`='$remarks1' 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");
}

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

        function getModal() {
            var $row = $(this).closest("tr");
            var option_value = $row.find('.status').val();
            var vouchno = $row.find('.vouchno').val();
            var data = {vouchno: vouchno};
            $.ajax({
                type: 'GET',
                dataType: 'json',
                url: "show_lead_dtl.php",
                data: data,
                success: function (response) {
                    //            var option_value = document.getElementById("status").value;
                    if (option_value == "Follow up 2") {
                        $('#subvouch').val(vouchno);
//                                    $($row.find('.rate')).val(response.rate);
                        $('.modal-body .flwup2dt').val(response.flwup2dt);
                        $("#myModal").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();

                    } else if (option_value == "Follow up 3") {
                        $('#subvouch').val(vouchno);
//                                    $($row.find('.rate')).val(response.rate);
                        $('.modal-body .flwup3dt').val(response.flwup3dt);
                        $("#myModal").modal("show");
                        $(".modal-body #tr1").hide();
                        $(".modal-body #tr2").hide();
                        $(".modal-body #tr3").show();
                        $(".modal-body #tr4").hide();
                        $(".modal-body #tr5").hide();
                        $(".modal-body #tr6").hide();

                    } else if (option_value == "Destination Change") {
                        $('#subvouch').val(vouchno);
//                                    $($row.find('.rate')).val(response.rate);
                        $('.modal-body .newvouch').val(response.newvouch);
                        $("#myModal").modal("show");
                        $(".modal-body #tr1").hide();
                        $(".modal-body #tr2").hide();
                        $(".modal-body #tr3").hide();
                        $(".modal-body #tr4").hide();
                        $(".modal-body #tr5").hide();
                        $(".modal-body #tr6").show();
                    } else if (option_value == "Postponed") {
                        $('#subvouch').val(vouchno);
//                                    $($row.find('.rate')).val(response.rate);
                        $('.modal-body .postdate').val(response.postdate);
                        $("#myModal").modal("show");
                        $(".modal-body #tr1").hide();
                        $(".modal-body #tr2").hide();
                        $(".modal-body #tr3").hide();
                        $(".modal-body #tr4").show();
                        $(".modal-body #tr5").hide();
                        $(".modal-body #tr6").hide();
                    } else if (option_value == "Cancelled") {
                        $('#subvouch').val(vouchno);
//                                    $($row.find('.rate')).val(response.rate);
                        $('.modal-body .cancresn').val(response.cancresn);
                        $("#myModal").modal("show");
                        $(".modal-body #tr1").hide();
                        $(".modal-body #tr2").hide();
                        $(".modal-body #tr3").hide();
                        $(".modal-body #tr4").hide();
                        $(".modal-body #tr5").show();
                        $(".modal-body #tr6").hide();
                    }
                },
                error: function (request, textStatus, errorThrown) {
                    alert(errorThrown);
                }
                //dataType:'json'
            });
//                    $.get('get_followup_date.php', {vouchno: vouchno}, function (data) {
//
//
//
//                    });
        }

        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 .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();
            $.post('customer_SendMail2.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>


<style>
    .button {
        padding: 15px 25px;
        font-size: 15px;
        text-align: center;
        cursor: pointer;
        outline: none;
        color: #fff;
        background-color: #4CAF50;
        border: none;
        border-radius: 15px;
        box-shadow: 0 9px #999;
    }

    .button:hover {background-color: #3e8e41}

    .button:active {
        background-color: #3e8e41;
        box-shadow: 0 5px #666;
        transform: translateY(4px);
    }
</style>
</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='GET'>
        <!-- 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;">
                        <button id='add' 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" value="Add Leads">
                            <i class="fa fa-plus" style="color:#ffffff"></i>    
                            Add Lead Entry
                        </button>
<!--                        <input style="float: right;padding-right: 15px;"  type='submit' name='update' value='Save' class="btn btn-primary">-->
                    </div>
                    <br>
                    <div class="panel-body table-responsive" style="width:100%">
                        <table id="" class="" width="100%">
                            <thead>
                                <tr style="background-color: #64CCFC">
                                    <th>Sr.</th>
                                    <th>Book No.</th>
                                    <th>Enq. Date</th>
                                    <th>Name</th>
                                    <th>Last Name</th>
                                    <th>Mobile</th>
                                    <th>Que to Visa</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                if($usertype == 1){
                                    $srchresult = mysqli_query($conn,"SELECT *, DATE_FORMAT(`deptdate`, '%d-%m-%Y')as deptdate FROM `$maindb`
                                              WHERE `status` != 'Destination Change'   and `status` != 'Cancelled' and `status` != 'Confirmed' ") or die(mysqli_error($conn));
                                }else{
                                    $srchresult = mysqli_query($conn,"SELECT *, DATE_FORMAT(`deptdate`, '%d-%m-%Y')as deptdate FROM `$maindb`
                                              WHERE `username` ='$user' and `status` != 'Destination Change'   and `status` != 'Cancelled' and `status` != 'Confirmed' ") or die(mysqli_error($conn));
                                }
                                
                                $ctr = 1;

                                while ($row = mysqli_fetch_array($srchresult)) {
//                                    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>";
                                    Echo "<td class='text-right'  style='display: $adddisabled '><a href=\"leadenqed.php?vouchno=$row[vouchno]&mode=edit\"><input type='text' value='$row[vouchno]' name='vouchno_$ctr' style='border : none;width : 30px; padding : 0px;text-align:left;' readonly class='vouchno'></a></td>";
                                    Echo "<td class='text-right'><input type='date' value='$row[vouchdt]' name='vouchdt_$ctr' style='border : none;width : 110px; padding : 0px;text-align:center;' readonly class='vouchdt'></td>";
                                    Echo "<td class='text-right'><a href=\"leadenqed.php?vouchno=$row[vouchno]&mode=edit\"><input type='text'value='$row[name]' name='name_$ctr' style='border : none; padding : 0px;text-align:left;width:120px;' readonly class='name'></a></td>";
                                    Echo "<td class='text-right'><a href=\"leadenqed.php?vouchno=$row[vouchno]&mode=edit\"><input type='text'value='$row[lastname]' name='lastname_$ctr' style='border : none; padding : 0px;text-align:left;width:110px;' readonly class='lastname'></a></td>";
                                    Echo "<td class='text-right'><a href=\"leadenqed.php?vouchno=$row[vouchno]&mode=edit\"><input type='text'value='$row[mobile]' name='lastname_$ctr' style='border : none; padding : 0px;text-align:left;width:110px;' readonly class='lastname'></a></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>";
                                $ctr++;
                            }
                            ?>
                            <input type = "hidden" name = "count" value = "<? Echo $ctr; ?>">
                            </tbody> 
                        </table>

                    </div>
                </div>
            </div>
        </div>
        <?php
        mysqli_select_db($conn,$zdb);
        ?>
        <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>
                    <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>
                                </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>
                            </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>
