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

$formname = 'Visa new';
$maindb = 'visamain';
$printfile = "supplier_print.php";
//$file = 'supplier.php';
$file = 'visanew.php';
$trandbexist = 0;
$grid = '';
$keyfld = "vouchno";
$keycond = " ";
$focusfield = "name";

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



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

    $a = 1;
    $name = mysqli_real_escape_string($conn,$_POST["name"]);
    $lastname = mysqli_real_escape_string($conn,$_POST["lastname"]);
    $vouchdt = $_POST["vouchdt"];
    $reference = $_POST["reference"];
    $destination = $_POST["destination"];
    $deptdate = $_POST["deptdate"];
    $totpax = $_POST["totpax"];
    $issplace = $_POST["issplace"];
    $expdate = $_POST["expdate"];



    if ($a == 1) {
        if ($addflag == 1) {
            mysqli_query($conn,"INSERT INTO `$maindb`(vouchno, `vouchdt`, `name`, `lastname`, `reference`, `destination`, `deptdate`, `totpax`,
                                                `issplace`, `expdate`)
                                        VALUES($qvouch,'$vouchdt', '$name', '$lastname', '$reference', '$destination', '$deptdate', '$totpax',
                                                '$issplace', '$expdate')") or die(mysqli_error($conn));
            insmess();
            $mvouch = $_GET["vouchno"];
//            $mvouch = $qvouch;
//            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
//            header("refresh: 1; $file?mode=true");
        } else {
            $mvouch = $qvouch;
            mysqli_query($conn,"UPDATE `$maindb` SET `vouchdt`='$vouchdt',`name`='$name',`lastname`='$lastname', `reference`='$reference', `destination`='$destination', 
                         `deptdate`='$deptdate', `totpax`='$totpax', `issplace`='$issplace', `expdate`='$expdate'  where vouchno = '$mvouch' ") or die(mysqli_error($conn));

            updmess();
//            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
//            header("refresh: 1; $grid");
        }
    }
}

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 type="text/javascript">
    $(document).ready(function () {
        $("body").on('change', '.deptdate,.vouchdt', checkDate);
        function checkDate() {
            var travdate = $(".deptdate").val();
            var toddate = $(".vouchdt").val();
//        var toddate = date("Y-m-d");
//        var toddate = new Date();

            if (travdate < toddate) {
                alert("Travel Date Cannot be Less than Today's Date");
                $(".travdate").focus();
            }
        }
    });
</script>
<!--<script src="MyScript/challan.js" type="text/javascript"></script>-->
</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='POST'>
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-7">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h2 class="panel-title"><?php Echo $formname; ?></h2>
                    </div>
                    <div class="panel-body">
                        <div class="row">
                            <div class="form-group">
                                <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad ">
                                    <label class="label-control marg">Voucher</label>
                                </div>
                                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 ">
                                    <input type="text" name="voucher" class="form-control form" value="<?php Echo ($addflag != 1) ? $get["vouchno"] : $qvouch; ?>">
                                </div>
                                <div class="col-lg-1 col-md-1 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-3">
                                    <input type="date" name="vouchdt" class="form-control form vouchdt" value="<?php Echo ($addflag != 1) ? $get["vouchdt"] : date('Y-m-d'); ?>">
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="form-group">
                                <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                    <label class="label-control marg">Name</label>
                                </div>
                                <div class="col-lg-7 col-md-7 col-sm-7 col-xs-7 ">
                                    <input type="text"  name="name" class="form-control form" value="<?php Echo ($addflag != 1) ? $get["name"] : ""; ?>" autofocus="autofocus">
                                </div>       

                            </div>
                        </div>
                        <div class="row">
                            <div class="form-group">
                                <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                    <label class="label-control marg">Last</label>
                                </div>
                                <div class="col-lg-7 col-md-7 col-sm-7 col-xs-7">
                                    <input type="text" name="lastname" class="form-control form" value="<?php Echo ($addflag != 1) ? $get["lastname"] : ""; ?>">
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="form-group">
                                <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                    <label class="label-control">Reference</label>
                                </div>
                                <div class="col-lg-7 col-md-7 col-sm-7 col-xs-7 ">
                                    <select name="reference" class="form-control form select2">
                                        <option value="<? Echo ($addflag != 1 ) ? $get[reference] : "" ?>"><? Echo ($addflag != 1 ) ? $get[reference] : "" ?></option>
                                        <?php
                                        $getrefer = mysqli_query($conn,"select * from `source` order by `name`") or die(mysqli_error($conn));
                                        while ($mrefer = mysqli_fetch_array($getrefer)) {
                                            echo "<option value='$mrefer[name]'>$mrefer[name]</option>";
                                        }
                                        ?>

                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="form-group">
                                <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                    <label class="label-control marg">Destination</label>
                                </div>
                                <div class="col-lg-7 col-md-7 col-sm-7 col-xs-7 ">
                                    <select name="destination" class="form-control form select2">
                                        <option value="<? Echo ($addflag != 1 ) ? $get[destination] : "" ?>"><? Echo ($addflag != 1 ) ? $get[destination] : "" ?></option>
                                        <?php
                                        $getdest = mysqli_query($conn,"select * from `destination` order by `name`") or die(mysqli_error($conn));
                                        while ($mdest = mysqli_fetch_array($getdest)) {
                                            echo "<option value='$mdest[name]'>$mdest[name]</option>";
                                        }
                                        ?>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="form-group">
                                <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                    <label class="label-control marg">Travel Date</label>
                                </div>
                                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 ">
                                    <input type="date" class="form-control form deptdate" name="deptdate"  value="<?php Echo ($addflag != 1) ? $get["deptdate"] : ""; ?>">
                                </div>

                                <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad">
                                    <label class="label-control marg">No. Pax</label>
                                </div>
                                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 ">
                                    <input type="text" class="form-control form text-right" name="totpax"  value="<?php Echo ($addflag != 1) ? $get["totpax"] : ""; ?>">
                                </div> 

                            </div>
                        </div>   
                        <div class="row">
                            <div class="form-group">
                                <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad ">
                                    <label class="label-control">P Issue Place</label>
                                </div>
                                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 ">
                                    <input type="text" name="issplace" class="form-control form"  value="<?php Echo ($addflag != 1) ? $get["issplace"] : ""; ?>">
                                </div>

                                <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad">                                  
                                    <label class="label-control">Exp. Date</label>
                                </div>
                                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">
                                    <input type="date"  name="expdate"  class="form-control form"  value="<?php Echo ($addflag != 1) ? $get["expdate"] : ""; ?>">
                                </div>
                            </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" 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">
                    <div class=" table-responsive">
                        <table id="example1" class="table table-striped">
                            <thead>
                                <tr style="background-color: #64CCFC">
                                    <th>Name</th>
                                    <th>Address</th>
                                    <th>City</th>
                                    <th>Mobile</th>
                                    <th>Email</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $srchresult = mysqli_query($conn,"SELECT * FROM `$maindb`  order by `vouchno` desc") or die(mysqli_error($conn));
                                while ($row = mysqli_fetch_array($srchresult)) {
                                    echo "<tr class='clickable-row' data-href='$file?mode=edit&$keyfld=$row[$keyfld]'>";
                                    echo "<td align='left'>$row[name]</td>";
                                    echo "<td align='left'>$row[add1]</td>";
                                    echo "<td align='left'>$row[city]</td>";
                                    echo "<td align='left'>$row[mobile]</td>";
                                    echo "<td align='left'>$row[email]</td>";
                                    echo "<td align='left'><a href=\"$file?mode=edit&$keyfld=$row[$keyfld]\"><i class='fa fa-pencil' style='font-size:1.3em;color:#00C0EF;cursor:pointer;'></i></a></td>";
                                    echo "</tr>";
                                }
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
