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

$formname = 'Courier Out';
$maindb   = 'courout';
$file     = 'courout.php';
$grid     = '';

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

$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"];
    $courier   = $_POST["courier"];
    $oldpass   = $_POST["oldpass"];

    $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`, `courier`, `oldpass`)
                                       Values ('$qvouch', '$vouchdt', '$paxname', '$passport', '$medical','$license', '$photo', '$recdfrom','$recdby', '$remark1','$remark2','$courdt', '$trackid', '$courier', '$oldpass')") 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',
                      `courier`='$courier',`oldpass`='$oldpass' 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/courout.js" type="text/javascript"></script>
<script src="MyScript/courout_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">Voucher</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-8 col-xs-8 pad">
                                <input type="text" name="vouchno" id="vouchno" 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" style="color: blue;">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 passport" >
                            </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 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">Medical</label>
                            </div>
                            <div class="col-lg-1 col-md-6 col-sm-8 col-xs-8 pad" >
                            <input type='checkbox' name='medical' style="accent-color: red;" 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" >
                            <input type='checkbox' name='license' style="accent-color: green;" 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" >
                            <input type='checkbox' name='photo' class='photo form-control form' value="1" <?
                                        if ($get['photo'] == 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">Old Passport</label>
                            </div>
                            <div class="col-lg-1 col-md-6 col-sm-8 col-xs-8 pad" >
                            <input type='checkbox' name='oldpass' style="accent-color: yellow;" class='oldpass form-control form' value="1" <?
                                        if ($get['oldpass'] == 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">To</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">Send 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 Name</label>
                            </div>
                            <div class="col-lg-4 col-md-6 col-sm-8 col-xs-8 pad">
                                <input type="text" name="courier" value="<?php echo ($addflag != 1) ? $get['courier'] : ''; ?>" class="form-control form courier" 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>

    </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>
                                <option value="12">Courier</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="10%">Courier</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>