<?php
ob_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');
header("Content-Type: text/html;charset=UTF-8");

$formname = 'Immigration Date Update';
$maindb = 'visamain';
$trandb = 'visatran';
$file = 'immig.php';
//$printfile = 'crmlist.php';
$keyfld = "myvouch";
$myyear = $_SESSION["tit_" . $dbname];

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$username = $_SESSION["username_" . $dbname];
$zroot = $_SESSION["zdir"];
//$modet = $_GET["mode"];
mysqli_select_db($conn, $zdb);
include("includes/vouchno_generate.php");
include("includes/prev&next.php");
if (isset($_POST["submit"])) {
    $a = 1;

    $vouchdt = $_POST["vouchdt"];
    $count = $_POST["items"];
    //    $myvouch = $_POST["myvouch"];
    //    browse($myvouch);

    if ($a == 1) {

        mysqli_query($conn, "LOCK TABLES `$trandb` WRITE");

        for ($i = 0; $i <= $count; $i++) {
            $cnt++;
            $srno = $_POST["srno"][$i];
            $paxname = $_POST["paxname"][$i];
            $passport = $_POST["passport"][$i];
            $releasedt = $_POST["releasedt"][$i];
            $manualno = $_POST["manualno"][$i];
            $jobid = $_POST["jobid"][$i];
            $enno = $_POST["enno"][$i];
            $myvouch = $_POST["myvouch"][$i];
            //            browse($myvouch);

            mysqli_query($conn, "UPDATE `$trandb` SET  `appdt`='$vouchdt', `jobid`='$jobid', `enno`='$enno' where `myvouch` = '$myvouch' and passport = '$passport'") or die(mysqli_error($conn));

            //            if (!empty($paxname)) {
            //                mysqli_query($conn, "INSERT INTO `$trandb`(`myvouch`, `yearno`, `visatype`, `vouchno`, `vouchdt`, `srno`, `paxname`, `passport`)
            //                                        VALUES ('$mvouch','$myyear','$visatype','$vouchno','$vouchdt','$cnt','$paxname','$passport')") or die(mysqli_error($conn));
            //            }
        }
    }
    mysqli_query($conn, "UNLOCK TABLES");
    browse("Immigration Date Updated");
}



if ($addflag != 1) {

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

    mysqli_query($conn, "SET NAMES 'utf8'");
    mysqli_query($conn, 'SET CHARACTER SET utf8');
    $result = mysqli_query($conn, "SELECT *, date_format(`vouchdt`,'%d-%m-%Y') as mydt FROM `$maindb` WHERE $keyfld='$getvouch'") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);
}

if (isset($_GET["delete"])) {

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

include("includes/linkshead.php");
?>
<script>
    $(document).ready(function() {
        $("body").on("blur", ".passport", getPass);

        function getPass() {
            var tr = $(this).closest("tr"),
                passport = tr.find(".passport").val(),
                manualno = tr.find(".manualno").val(),
                data = {
                    passport: passport,
                    manualno: manualno
                };
            $.ajax({
                type: 'GET',
                dataType: 'json',
                url: "get_ppdetail.php",
                data: data,
                success: function(response) {
                    if (response != '') {
                        $.each(response, function() {
                            var success = this.success;
                            if (success == false) {
                                alert("Passport No. Invalid ");
                                tr.find(".passport").val("");
                                tr.find(".passport").focus();
                            } else {
                                tr.find(".paxname").val(this.paxname);
                                tr.find(".releasedt").val(this.releasedt);
                                tr.find(".manualno").val(this.manualno);
                                tr.find(".visano").val(this.visano);
                                tr.find(".appdt").val(this.appdt);
                                tr.find(".jobid").val(this.jobid);
                                tr.find(".enno").val(this.enno);
                                tr.find(".ref").val(this.broker);
                                tr.find(".myvouch").val(this.myvouch);

                            }
                            if (response.releasedt != '0000-00-00' && response.releasedt != 'null') {
                                $(".releasedt").css("color", "red");
                            } else {
                                $(".releasedt").css("color", "black");
                            }
                        });
                    }
                }
            });
        }


        $("#hprint").hide();
        $("#search").hide();
        $("#edit").hide();
        $("#hprevious").hide();
        $("#hnext").hide();
        $("#hdelete").hide();
    });
</script>
<script src="MyScript/immig.js" type="text/javascript"></script>


</head>

<body>
    <form action='' method='POST' enctype="multipart/form-data" accept-charset="utf-8">
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-10">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title">
                            <?php echo $formname; ?>
                            <!--                            <span style="color: #5D96E4;font-size: 11px">
                                (Alt+T &nbsp;&nbsp;-&nbsp;&nbsp; New Visa Type)</span>-->
                        </h3>
                    </div>
                    <div class="panel-body">
                        <div class="row">

                            <div class="col-lg-2 col-md-3 col-sm-1 col-xs-1 text-right pad">
                                <label class="label-control marg">Date</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-2 pad">
                                <input type="date" class="form-control form vouchdt" name="vouchdt" value="<?php echo ($addflag != 1) ? $get["vouchdt"] : date("Y-m-d"); ?>" required>
                            </div>
                        </div>
                    </div>
                    <div class="pax_d">
                        <div class="table-responsive">
                            <table class="  table-striped table-condensed table-bordered table-fixed-top" width="100%">
                                <thead class="mySearchTr">
                                    <tr>
                                        <th style="width: 1%;"></th>
                                        <th style="width: 2%;">Sr.</th>
                                        <th style="text-align:center ; width: 10%">Passport</th>
                                        <th style="text-align:center ; width: 10%">File No</th>
                                        <th style="width: 15%;text-align:center">Pax Name</th>
                                        <th style="width: 10%;text-align:center">Visa No</th>
                                        <th style="width: 15%;text-align:center">Ref Agent</th>
                                        <th style="width: 10%;text-align:center">Job Id</th>
                                        <th style="width: 15%;text-align:center">E.N No</th>
                                        <th style="width: 15%;text-align:center">Approval Date</th>
                                    </tr>
                                </thead>
                                <tbody class="tbody">
                                    <tr class='colrrow'>
                                        <td><i class='fa fa-close remove form-control form'></i></td>
                                        <td><input type='text' name='srno[]' class='srno form-control form' value='1'></td>
                                        <td>
                                            <input type='text' name='passport[]' class='passport form-control  form' value=''>
                                        </td>
                                        <td>
                                            <input type='text' name='manualno[]' class='manualno form-control  form' readonly value=''>
                                            <input type='hidden' name='myvouch[]' class='myvouch form-control  form' readonly value=''>
                                        </td>
                                        <td>
                                            <input type='text' name='paxname[]' class='paxname form-control  form' readonly value=''>
                                        </td>
                                        <td>
                                            <input type='text' name='visano[]' class='visano form-control  form' readonly value=''>
                                        </td>
                                        <td>
                                            <input type='text' name='ref[]' class='ref form-control  form' readonly value=''>
                                        </td>
                                        <td>
                                            <input type='text' name='jobid[]' class='jobid form-control  form' value=''>
                                        </td>
                                        <td>
                                            <input type='text' name='enno[]' class='enno form-control  form' value=''>
                                        </td>
                                        <td>
                                            <input type='date' name='appdt[]' class='appdt form-control  form' readonly value=''>
                                        </td>
                                        <?php
                                        ?>
                                </tbody>
                            </table>
                        </div>
                        <div>
                            <button type='button' class='btn btn-success btn-square form' id='addnew'>
                                <i class='fa fa-plus' style='color:#ffffff' data-toggle='tooltip' title='Add Row'></i>
                            </button>
                            <input type='hidden' name='items' id="items" class='items form-control form' value="<?php echo ($addflag != 1) ? $ctr : 1; ?>">
                        </div>
                    </div>

                    <div class="panel-footer">
                        <? include("includes/buttons.php"); ?>
                    </div>
                </div>
            </div>
        </div>
    </form>
    <?php include("includes/linksfoot.php"); ?>
</body>

</html>