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

$formname = 'Visa New Entry';
$maindb = 'visanewmain';
$trandb = 'visanewtran';

// $printfile = "estimate_print.php";
$file = 'visanew.php';
$trandbexist = 1;
$keyfld = "vouchno";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
$compcd = $_SESSION["comp_" . $dbname];

$zroot = $_SESSION["zdir"];

mysqli_select_db($conn, $zdb);
include("includes/vouchno_generate.php");
include("includes/prev&next.php");

if (isset($_POST["submit"])) {
    $a = 1;
    $qvouch = $_POST["vouchno"];
    $vouchdt = $_POST["vouchdt"];
    $customer = $_POST["customer"];
    $visagrpcode = $_POST["visagrpcode"];
    $nopax = $_POST["nopax"];
    $rate = $_POST["rate"];
    $amount = $_POST["amount"];

    $count = $_POST["items"];

    $mvouch = $qvouch;

    $salecode = 11;

    if ($a == 1) {
        // mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `$trandb` WRITE ");

        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX(vouchno) AS maxcode FROM `$maindb` where vouchno != '' 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';
            }
            $mvouch = $qvouch;
        }

        #################################TRAN PART #################################

        mysqli_query($conn, "DELETE FROM `$trandb` WHERE `$keyfld`='$mvouch'") or die(mysqli_error($conn));
        mysqli_query($conn, "DELETE FROM `roomsacn` WHERE `$keyfld`='$mvouch' and source = '$source' ") or die(mysqli_error($conn));

        $srl = 0;
        $batch = date("Y-m-d") . $mvouch;
        $cnt = 0;
        for ($i = 0; $i <= $count; $i++) {

            $srno = $_POST["srno_$i"];
            $paxname = $_POST["paxname_$i"];
            $ppno = $_POST["ppno_$i"];
            $dob = $_POST["dob_$i"];
            $doi = $_POST["doi_$i"];
            $doe = $_POST["doe_$i"];
            $visano = $_POST["visano_$i"];
            $period = $_POST["period_$i"];
            $type = $_POST["type_$i"];

            $nigctr = $nonights;

            if (!empty($paxname)) {
                $srl++;
                mysqli_query($conn, "INSERT INTO `$trandb`(`vouchno`, `vouchdt`, `srno`, `paxname`, `ppno`, `dob`, `doi`, `doe`, 
                                                            `visano`, `period`, `type`)
                                                    VALUES('$mvouch', '$vouchdt', '$srl', '$paxname', '$ppno', '$dob', '$doi', '$doe',
                                                           '$visano', '$period', '$type' )") or die(mysqli_error($conn));

                $bookdate = $checkin;
                // for ($r = 0; $r < $nigctr; $r++) {

                //     mysqli_query($conn, "INSERT INTO `roomsacn`(`source`, `vouchno`, `bookdate`, `hotel`, `city`, 
                //                                                       `roomcatg`, `roomtype`, `qty`, `rate`, `hotelcatg`)
                //                                       VALUES('$source', '$mvouch', '$bookdate', '$hotelnm', '$city', 
                //                                                       '$roomcatg', '$roomtype', '$qty', '$rate', '$hotelcatg' )") or die(mysqli_error($conn));

                //     $bookdate = date('Y-m-d', strtotime($bookdate . ' + 1 days'));
                //     if ($bookdate > $checkout) {
                //         break;
                //     }
                // }
            }
        }

        #################################TRAN PART END #################################
        ############################## MAIN ######################################################
        if ($addflag == 1) {

            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `vouchdt`, `customer`, `visagrpcode`, `nopax`, `rate`, `totamt`)
                                               VALUES ($qvouch, '$vouchdt',  '$customer', '$visagrpcode','$nopax', '$rate', '$amount')") or die(mysqli_error($conn));
            insmess();
            $mvouch = $qvouch; //1
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            $mvouch = $qvouch; //1
            mysqli_query($conn, "UPDATE `$maindb` SET `vouchdt`='$vouchdt', `customer`='$customer', `visagrpcode`='$visagrpcode', `nopax`='$nopax',
                                         `rate`='$rate',`totamt`='$amount' WHERE `vouchno`= '$mvouch' ") or die(mysqli_error($conn));
            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        }

        ################################# MAIN PART END #################################
    }
}

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);

    $magent = mysqli_query($conn, "SELECT accode,name FROM accmast where accode='$get[customer]'") or die(mysqli_error($conn));
    $lagent = mysqli_fetch_array($magent);
}

$senable = "";
$cenable = "";

if (isset($_GET["delete"])) {
    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    delete($zdb, "$trandb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    $delcond = " source = '$source' and vouchno = '$_GET[vouchno]' ";
}

include("includes/linkshead.php");
?>
<script src="MyScript/shortkeys.js?a=2" type="text/javascript"></script>
<script src="MyScript/visanew.js?a=3" type="text/javascript"></script>
<script src="MyScript/visanew_search.js?a=2" type="text/javascript"></script>
</head>

<body>
    <form action='' method='POST'>
        <!-- Default box -->
        <div>
            <div class="col-lg-12 col-md-10 col-sm-12 col-xs-12  pad">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title" style="color:inherit"><?php echo $formname; ?></h3>
                        <!-- <div class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                            <i class="fa fa-times"></i>
                        </div> -->
                    </div>
                    <div class="panel-body">
                        <div class="row">
                            <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-1 col-md-1 col-sm-3 col-xs-3 pad">
                                <input type="text" class="form-control text-size form" name="vouchno" value="<?php echo ($addflag != 1) ? $get["vouchno"] : "$qvouch"; ?>" required readonly>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Date</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-4 col-xs-4 pad">
                                <input type="date" class="form-control form focusfield" name="vouchdt" value="<?php echo ($addflag != 1) ? $get["vouchdt"] : date('Y-m-d'); ?>" autofocus>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Agent / Customer</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4  pad">
                                <select name="customer" class="form-control form customer select2">
                                    <option value='<?php echo ($addflag != 1) ? $lagent['accode'] : '' ?>'><?php echo ($addflag != 1) ? $lagent['name'] : '' ?></option>
                                    <?php
                                    $mhotl = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `grpcode`='044' OR `grpcode`='010'") or die(mysqli_error($conn));
                                    while ($thotl = mysqli_fetch_array($mhotl)) {
                                        echo "<option value='$thotl[accode]'>$thotl[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Visa Group Code</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4  pad">
                                <input type="text" class="form-control text-size form" name="visagrpcode" value="<?php echo ($addflag != 1) ? $get["visagrpcode"] : ""; ?>">
                            </div>
                        </div>

                        <div class="table-responsive">
                            <table class="table table-striped table-condensed table-bordered table-padd" width="100%">
                                <thead class="mySearchTr">
                                    <tr>
                                        <th width="2%"></th>
                                        <th width="3%" class='text-center'>Sr.</th>
                                        <th width="25%">Pax Name</th>
                                        <th width="15%">Passport</th>
                                        <th width="8%">DOB</th>
                                        <th width="8%">DOI</th>
                                        <th width="8%">DOE</th>
                                        <th width="11%">Visa No.</th>
                                        <th width="10%">Stay Period</th>
                                        <th width="10%">Type</th>
                                    </tr>
                                </thead>

                                <tbody class="tbody">
                                    <?php
                                    if ($addflag != 1) {
                                        $ctr = 0;
                                        $mctr = 0;
                                        $tqty = 0;
                                        $tamt = 0;
                                        $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$get[vouchno]' ORDER BY `srno`") or die(mysqli_error($conn));
                                        while ($tran = mysqli_fetch_array($gettran)) {

                                            $mctr++;
                                            echo "<tr>";
                                            echo "<td><i class='fa fa-close remove' style='display:none'></i></td>";
                                            echo "<td><input type='text' class='form-control form text-right srno'    name='srno_$ctr'     value='$mctr'></td>";
                                            echo "<td><input type='text' class='form-control form paxname' name='paxname_$ctr' value='$tran[paxname]'></td>";
                                            echo "<td><input type='text' class='form-control form ppno' name='ppno_$ctr' value='$tran[ppno]'></td>";
                                            echo "<td><input type='date' class='form-control form dob' name='dob_$ctr' value='$tran[dob]'></td>";
                                            echo "<td><input type='date' class='form-control form doi' name='doi_$ctr'  value='$tran[doi]'></td>";
                                            echo "<td><input type='date' class='form-control form doe' name='doe_$ctr'  value='$tran[doe]'></td>";
                                            echo "<td><input type='text' class='form-control form visano'  name='visano_$ctr'  value='$tran[visano]'></td>";
                                            echo "<td><input type='text' class='form-control form period'  name='period_$ctr'  value='$tran[period]'></td>";
                                            echo "<td><input type='text' class='form-control form type'  name='type_$ctr'  value='$tran[type]' ></td>";
                                            echo "</tr>";
                                            $ctr++;
                                        }
                                    } else {
                                        $pctr = 0;
                                        echo "<tr>";
                                        echo "<td><i class='fa fa-close remove' style=''></i></td>";
                                        echo "<td><input type='text' class='form-control form text-right srno'    name='srno_0'     value='1'></td>";
                                        echo "<td><input type='text' class='form-control form paxname' name='paxname_0' value=''></td>";
                                        echo "<td><input type='text' class='form-control form ppno' name='ppno_0' value=''></td>";
                                        echo "<td><input type='date' class='form-control form dob' name='dob_0' value=''></td>";
                                        echo "<td><input type='date' class='form-control form doi' name='doi_0'  value=''></td>";
                                        echo "<td><input type='date' class='form-control form doe' name='doe_0'  value=''></td>";
                                        echo "<td><input type='text' class='form-control form visano'  name='visano_0'  value=''></td>";
                                        echo "<td><input type='text' class='form-control form period'  name='period_0'  value='90 Days'></td>";
                                        echo "<td><input type='text' class='form-control form type'  name='type_0'  value='Umrah' ></td>";

                                        echo "</tr>";
                                    }
                                    $pctr++;
                                    ?>
                                    <input type="hidden" id="items" name="items" value="<?php echo ($addflag != 1) ? $ctr : 1; ?>" />
                                </tbody>
                            </table>
                        </div>
                        <div class="col-lg-12 pad">
                            <button type="button" class="btn btn-primary btn-square form" id="addnew">
                                <i class="fa fa-plus" data-toggle='tooltip' title='Add Row'></i>
                            </button>
                        </div>

                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">No of Pax</label>
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-4 col-xs-4  pad">
                                <input type="text" class="form-control text-right text-size nopax form" name="nopax" value="<?php echo ($addflag != 1) ? $get["nopax"] : ""; ?>">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Rate</label>
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-4 col-xs-4  pad">
                                <input type="text" class="form-control text-right text-size rate form" name="rate" value="<?php echo ($addflag != 1) ? $get["rate"] : ""; ?>">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Amount</label>
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-4 col-xs-4  pad">
                                <input type="text" class="form-control text-right text-size totamt form" name="amount" value="<?php echo ($addflag != 1) ? $get["totamt"] : ""; ?>" readonly>
                            </div>
                        </div>
                        <div class="row">
                        <div class="col-lg-4 col-md-2 col-sm-2 col-xs-2 pad text-right invprn" >
                            <img src="assets/img/invoicesk.png" title="Invoice Print" class="invoice" style="width: 45px; height: 45px;cursor: pointer" onclick="window.open('visanew_invprint.php?vouchno=<?= $get['vouchno']; ?>', 'popUponclickWindow', 'height=600,width=970,left=20,top=20, resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes');">
                        </div>
                            <div class="col-lg-8 col-md-7 col-sm-8 col-xs-8 text-left pad">
                                <?php include("includes/buttons.php"); ?>
                            </div>
                        </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">
                    <div class="row">
                        <div class="col-lg-2">
                            <select class="form-control sfield">
                                <option value="1">Voucher</option>
                                <option value="2">Date</option>
                                <option value="3">Customer/Agent</option>
                                <option value="4">Group</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</option>
                                <option value="2">Date</option>
                                <option value="3">Customer/Agent</option>
                                <option value="4">Group</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">
                    </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-striped table-condensed table-bordered" width="100%">
                            <thead>
                                <tr class="mySearchTr">
                                    <th style="width:8%">Voucher</th>
                                    <th style="width:10%">Date</th>
                                    <th style="width:42%">Customer / Agent</th>
                                    <th style="width:35%">Group</th>
                                    <th style="width:5%">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>