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

$formname = 'Courier In';
$maindb   = 'courin';
$file     = 'courin.php';
$grid     = '';

$keyfld      = "vouchno";
$trandbexist = 0;
$focusfld    = "name";
$printfile   = 'courreg.php?para=I';

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

$keycond1 = "";

$zyear      = right($zdb, 4);
$zStartDate = getzdates($zyear, 'S');
$zEndDate   = getzdates($zyear, 'E');
mysqli_select_db($conn, $zdb);

include("includes/vouchno_generate.php");
include("includes/prev&next.php");

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

    $a = 1;

    $vouchdt  = $_POST["vouchdt"];
    $paxname  = $_POST["paxname"];
    $passport = $_POST["passport"];
    $medical  = $_POST["medical"];
    $license  = $_POST["license"];
    $photo    = $_POST["photo"];
    $remark1  = $_POST["remark1"];
    $remark2  = $_POST["remark2"];
    $recdfrom = $_POST["recdfrom"];
    $recdby   = $_POST["recdby"];
    $courdt   = $_POST["courdt"];
    $trackid  = $_POST["trackid"];

    $mvouch = $qvouch;

    if ($paxname == "") {
        $a = 0;
        browse("Paxname Cannot Be Empty");
        header("refresh: ; $file?vouchno=$_GET[vouchno]");
    }

    if ($a == 1) {
        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `accmast` WRITE ") or die(mysqli_error($conn));

        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX(vouchno) AS maxcode FROM `$maindb` LIMIT 0,1 ") or die(mysqli_error($conn));
            if (mysqli_num_rows($gen1) > 0) {
                $vvouch = mysqli_fetch_assoc($gen1);
                $qvouch = $vvouch["maxcode"] + 1;
            } else {
                $qvouch = '1';
            }
        }


        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`( vouchno ,`vouchdt`, `paxname`, `passport`, `medical`, `license`, `photo`, `recdfrom`,`recdby`, `remark1`, `remark2`,`courdt`, `trackid`)
                                       Values ('$qvouch', '$vouchdt', '$paxname', '$passport', '$medical','$license', '$photo', '$recdfrom','$recdby', '$remark1','$remark2','$courdt', '$trackid')") or die(mysqli_error($conn));
        } else {
            $mvouch = $qvouch;
            mysqli_query($conn, "UPDATE `$maindb` SET  vouchdt='$vouchdt', `paxname`='$paxname', `passport`='$passport', `license` ='$license' ,`photo`='$photo',
                     `recdfrom`='$recdfrom',`recdby`='$recdby',`remark1`='$remark1', `remark2`='$remark2', `courdt`='$courdt', `medical`='$medical', `trackid`='$trackid'   where vouchno = '$mvouch' ") or die(mysqli_error($conn));
        }

        mysqli_query($conn, "UNLOCK TABLES");

        if ($addflag == 1) {
            insmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=last");
        } else {
            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        }
    }
}

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

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

}

/* DELETE */
if (isset($_GET["delete"])) {
    mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `accmast` WRITE ") or die(mysqli_error($conn));

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

    $delsett = " source = '$source' AND bnkvouch = '$_GET[vouchno]'";
    $delcond = " source = '$source' and vouchno = '$_GET[vouchno]' ";

    mysqli_query($conn, "UNLOCK TABLES");
}

include("includes/linkshead.php");
?>
<script src="MyScript/courin.js" type="text/javascript"></script>
<script src="MyScript/courin_search.js" type="text/javascript"></script>

</head>

<body>

    <form action='' method='POST'>
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 pad">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title"><?php echo $formname; ?></h3>
                    </div>
                    <input type="date" value="<?= $zStartDate ?>" style="width: 1px;border: none" class="zstdate">
                    <input type="date" value="<?= $zEndDate ?>" style="width: 1px;border: none" class="zenddate">
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4  pad text-right">
                                <label class="label-control marg pad">From Date</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-8 col-xs-8 pad">
                                <input type="text" name="vouchno" id="frmdt" value="<?php echo ($addflag != 1) ? $get['vouchno'] : $qvouch; ?>" class="form-control form" readonly>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-4 col-xs-4 pad text-right">
                                <label class="label-control marg pad">Date</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-8 col-xs-8 pad">
                                <input type="date" name="vouchdt" value="<?php echo ($addflag != 1) ? $get['vouchdt'] : date('Y-m-d'); ?>" class="form-control form vouchdt" style="height: 30px;" required autofocus="autofocus">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4 pad text-right">
                                <label class="label-control marg pad">Pax Name</label>
                            </div>
                            <div class="col-lg-6 col-md-8 col-sm-8 col-xs-8 pad">
                                <select id="paxname" name="paxname" class="form-control paxname form select2">
                                    <option value="<?php echo ($addflag != 1) ? $get['paxname'] : ""; ?>"><?php echo ($addflag != 1) ? $get['paxname'] : ""; ?></option>
                                    <?php
                                    $mown1 = mysqli_query($conn, "SELECT distinct(paxname) FROM `visatran` where passport!='' order by `paxname` ") or die(mysqli_error($conn));
                                    while ($trow1 = mysqli_fetch_array($mown1)) {
                                        echo "<option value='$trow1[paxname]'>$trow1[paxname]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4 pad  text-right">
                                <label class="label-control marg pad">Passport No.</label>
                            </div>
                            <div class="col-lg-2 col-md-6 col-sm-8 col-xs-8 pad">
                                <input type="text" name="passport" value="<?php echo ($addflag != 1) ? $get['passport'] : ''; ?>" class="form-control form " >
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4 pad  text-right">
                                <label class="label-control marg pad">Medical</label>
                            </div>
                            <div class="col-lg-1 col-md-6 col-sm-8 col-xs-8 pad" style="background-color: red;border-radius:8px;margin-bottom:8px">
                                <input type='checkbox' name='medical'  class='medical form-control form' value="1" <?
                                        if ($get['medical'] == 1) {
                                            Echo "checked";
                                        }
                                        ?>>
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-4 col-xs-4 pad  text-right" >
                                <label class="label-control marg pad">License</label>
                            </div>
                            <div class="col-lg-1 col-md-6 col-sm-8 col-xs-8 pad" style="background-color: green;border-radius:8px;margin-bottom:8px">
                                    <input type='checkbox' name='license' class='license form-control form' value="1" <?
                                        if ($get['license'] == 1) {
                                            Echo "checked";
                                        }
                                        ?>>
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-4 col-xs-4 pad  text-right" >
                                <label class="label-control marg pad">Photo</label>
                            </div>
                            <div class="col-lg-1 col-md-6 col-sm-8 col-xs-8 pad" style="background-color: blue;border-radius:8px;margin-bottom:8px">
                                    <input type='checkbox' name='photo' class='photo form-control form' value="1" <?
                                          if ($get['photo'] == 1) {
                                            Echo "checked";
                                        }
                                        ?>>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4 pad  text-right">
                                <label class="label-control marg pad">From</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                                <input type="text" name="recdfrom" value="<?php echo ($addflag != 1) ? $get['recdfrom'] : ''; ?>" class="form-control form recdfrom" required>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4 pad  text-right">
                                <label class="label-control marg pad">Received By</label>
                            </div>
                            <div class="col-lg-2 col-md-6 col-sm-8 col-xs-8 pad">
                                <input type="text" name="recdby" value="<?php echo ($addflag != 1) ? $get['recdby'] : ''; ?>" class="form-control form recdby" required>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4 pad  text-right">
                                <label class="label-control marg pad">Courier Date</label>
                            </div>
                            <div class="col-lg-2 col-md-6 col-sm-8 col-xs-8 pad">
                                <input type="date" name="courdt" value="<?php echo ($addflag != 1) ? $get['courdt'] : ''; ?>" class="form-control form courdt" required>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4 pad  text-right">
                                <label class="label-control marg pad">Tracking No.</label>
                            </div>
                            <div class="col-lg-2 col-md-6 col-sm-8 col-xs-8 pad">
                                <input type="text" name="trackid" value="<?php echo ($addflag != 1) ? $get['trackid'] : ''; ?>" class="form-control form trackid">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4 pad  text-right">
                                <label class="label-control marg pad">Remarks</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                                <input type="text" name="remark1" value="<?php echo ($addflag != 1) ? $get['remark1'] : ''; ?>" class="form-control form remark1">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4 pad  text-right">
                                <label class="label-control marg pad"></label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                                <input type="text" name="remark2" value="<?php echo ($addflag != 1) ? $get['remark2'] : ''; ?>" class="form-control form remark2">
                            </div>
                        </div>

                    </div>
                    <div class="panel-footer">
                        <?php include("includes/buttons.php"); ?>
                    </div>
                </div>
            </div>
            <div class="modal fade " tabindex="" id="settmodal" role="dialog" aria-labelledby="myLargeModalLabel">
                <div class="modal-dialog modal-lg">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button class="close" data-dismiss="modal"><span>&times;</span></button>
                            <h4 class="modal-title" id="myLargeModalLabel">Bill Settlement</h4>
                        </div>
                        <div class="modal-body">
                            <div class=" table-responsive">
                                <div class="toolbar">
                                    <div class="row">
                                        <div class="col-lg-2"><label class="label-control marg" style="color:red">Total Remain</label></div>
                                        <div class="col-lg-2"><input type="text" id="remain" name="remain" class="form-control" readonly style=""></div>
                                        <div class="col-lg-2"><label class="label-control marg" style="color:blue">Total Setoff</label></div>
                                        <div class="col-lg-2"><input type="text" id="totsetoff" name="totsetoff" class="form-control" readonly style=""></div>
                                        <!--<div class="col-lg-2"><input id="submit" type='submit' name='submit' value='<?php // echo ($addflag == 0) ? "Update" : "Save";                      
                                                                                                                        ?>' class="btn btn-primary"></div>-->
                                        <?php
                                        //                                    if (!isset($_GET["method"])) {
                                        ?>
                                        <!--<div class="col-lg-2"><input type='button' name='exit' value='Exit' class="btn btn-danger" onClick="window.location.href = '<?php echo $grid; ?>'"></div>-->
                                        <?php // }    
                                        ?>
                                    </div>
                                </div>
                                </header>
                                <div id="div-2" class="accordion-body collapse in body">
                                    <div style="max-height:300px;overflow: auto; border: 0px solid;">
                                        <div id="data"></div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer"></div>
                    </div>
                </div>
            </div>
        </div>

    </form>

    <div class="modal fade bs-example-modal-lg searchModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" data-dismiss="modal"><span>&times;</span></button>
                    <h4 class="modal-title" id="myLargeModalLabel">Search <? echo $formname; ?></h4>
                </div>
                <div class="modal-body">
                    <input type="hidden" value="<?php echo $file ?>" class="form-control file">
                    <div class="row">
                        <div class="col-lg-2">
                            <select class="form-control sfield">
                            <option value="1">Voucher No.</option>
                                <option value="2">Voucher Date</option>
                                <option value="3">Paxname</option>
                                <option value="4">Passport</option>
                                <option value="5">Medical</option>
                                <option value="6">License</option>
                                <option value="7">Photo</option>
                                <option value="8">Recd From</option>
                                <option value="9">Recd By</option>
                                <option value="10">Courier Date</option>
                                <option value="11">Track ID</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control soperator">
                                <option value="">Type of Search</option>
                                <option value="%">Like</option>
                                <option value="=">Exactly</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control sortBy">
                                <option value="">Sort By</option>
                                <option value="1">Voucher No.</option>
                                <option value="2">Voucher Date</option>
                                <option value="3">Paxname</option>
                                <option value="4">Passport</option>
                                <option value="5">Medical</option>
                                <option value="6">License</option>
                                <option value="7">Photo</option>
                                <option value="8">Recd From</option>
                                <option value="9">Recd By</option>
                                <option value="10">Courier Date</option>
                                <option value="11">Track ID</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control orderAs">
                                <option value="desc">Desc</option>
                                <option value="asc">Asc</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <input type="text" value="" class="form-control ssearch" placeholder="Search Here">
                        </div>
                        <div class="col-lg-2">
                            <input type="button" class="btn btn-primary sload" value="Search">
                        </div>
                    </div>

                    <div class="col-lg-12 sloader text-center">
                        <!--<img src="images/search.gif" alt="loading">-->
                    </div>
                    <div class="col-lg-12 snoRecord text-center">
                        <h3>NO RECORDS FOUND</h3>
                    </div>
                    <div class="table-responsive" style="max-height: 400px;overflow: auto;">
                        <table id="" class="table table-striped table-condensed table-bordered" width="100%">
                            <thead>
                                <tr class="mySearchTr">
                                <th width="5%">#</th>
                                    <th width="8%">Date</th>
                                    <th width="20%">Paxname</th>
                                    <th width="10%">Passport</th>
                                    <th width="5%">Medical</th>
                                    <th width="5%">License</th>
                                    <th width="5%">Photo</th>
                                    <th width="20%">Recd From</th>
                                    <th width="20%">Recd By</th>
                                    <th width="8%">Courier Date</th>
                                    <th width="10%">Track ID</th>
                                    <th width="10%">Remarks</th>
                                    <th width="7%">Action</th>
                                </tr>
                            </thead>
                            <tbody class="sTbody">
                            </tbody>
                        </table>
                    </div>
                    <div class="row">
                        <div class="col-xs-12 text-right search-action-bar">
                            <input type="button" class="btn btn-primary sprev" value="Previous">
                            <input type="button" class="btn btn-primary snext" value="Next">
                        </div>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <?php include("includes/linksfoot.php"); ?>
</body>

</html>