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

$formname = 'Lead Enquiry';
$maindb = 'leadenq';
$printfile = "customer_print.php";
$file = 'leadenq.php';
$trandbexist = 0;
$grid = '';
$keyfld = "vouchno";
//$keycond = " AND `grpcode`='010'";
$focusfield = "vouchdt";
$mgrpcode = '010';

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

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

    $a = 1;
    $vouchdt = $_POST["vouchdt"];
    $name = mysqli_real_escape_string($conn,$_POST["name"]);
    $lastname = mysqli_real_escape_string($conn,$_POST["lastname"]);
    $city = $_POST["city"];
    $destination = $_POST["destination"];
    $station = $_POST["station"];
    $mob = $_POST["mobile"];
    $mob2 = $_POST["mobile2"];
    $tel = $_POST["tel"];
    $tel2 = $_POST["tel2"];
    $email = $_POST["email"];
    $destcity1 = $_POST["destcity1"];
    $destcity2 = $_POST["destcity2"];
    $destcity3 = $_POST["destcity3"];
    $destcity4 = $_POST["destcity4"];
    $destcity5 = $_POST["destcity5"];
    $travdate = $_POST["travdate"];
    $adult = $_POST["adult"];
    $child = $_POST["child"];
    $infant = $_POST["infant"];
    $totpax = $_POST["totpax"];
    $username = $_POST["username"];

    if ($name == " ") {
        $a = 0;
        browse("Name Cannot Be Empty");
        if ($addflag == 1) {
            header("location:javascript://history.go(-1)");
        } else {
            header("refresh: ; $file?mode=last");
        }
    }

    if ($a == 1) {
        if ($addflag == 1) {
            mysqli_query($conn,"INSERT INTO `$maindb`(vouchno, `vouchdt`, `name`, `lastname`, `city`, `destination`, mobile , mobile2 ,tel, tel2,
                                                 `email`,`destcity1`, `destcity2`, `destcity3`,`destcity4`,`destcity5`, `deptdate`, `username`, 
                                                 adult, child, infant, totpax   )
                                        VALUES($qvouch, '$vouchdt', '$name', '$lastname', '$city', '$destination', '$mobile' , '$mobile2' , '$tel', '$tel2',
                                                 '$email', '$destcity1', '$destcity2', '$destcity3', '$destcity4', '$destcity5', '$travdate', '$username', 
                                                 '$adult', '$child', '$infant', '$totpax'   )") 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', `city`='$city', `destination`='$destination', mobile= '$mob', mobile2= '$mob2',tel='$tel', tel2='$tel2',
                         `email`='$email', `destcity1`='$destcity1',  `destcity2`='$destcity2', `destcity3`='$destcity3', `destcity4`='$destcity4', `destcity5`='$destcity5',
                        `deptdate`='$travdate', `adult`='$adult', `child`='$child', infant='$infant', `totpax`='$totpax', `username`='$username' 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>
    $(document).ready(function () {
        $("body").on('blur keyup', '.cpass', checkPass);
        $("body").on('blur', '.cpass', checkReturn);
        $("body").on('click', '.CMail', sendMail);
        $("body").on('blur', '.email', getUser);
        $("body").on('keyup, change', '.adult, .child, .infant, .totpax', Calculat);

        function Calculat() {
            var madult = parseFloat($(".adult").val());
            var mchild = parseFloat($(".child").val());
            var minfant = parseFloat($(".infant").val());

            var mtotpax = parseFloat($(".totpax").val());

            mtotpax = madult + mchild + minfant;

            $(".totpax").val(mtotpax);

        }
        function checkPass() {
            var pass = $(".password").val(),
                    cpass = $(".cpass").val();
            var check = "";
            if (pass != cpass) {
                var check = "<label class='label-control' style='color:red'>Password Not Matched</label>";
            } else {
                var check = "<label class='label-control' style='color:green'>Password Matched</label>";
            }

            $(".checkdiv").html(check);
        }
        function getUser() {
            var email = $(".email").val();

            $(".username").val(email);
        }

        function checkReturn() {
            var pass = $(".password").val(),
                    cpass = $(".cpass").val();
            if (pass != cpass) {
                alert("Password Did Not Match");
                $(".cpass").val("");
                $(".pass").val("");
            }
        }

        function sendMail() {
            var user = $(".username").val(), pass = $(".password").val(), email = $(".email").val();
            $.post('customer_SendMail.php', {user: user, pass: pass, email: email}, function (data) {
                if (data = "succcess") {
                    alert("Username & Password Are Send To The Customer");
                }
            });
        }
    });
</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-6 pad">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title"><?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-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Voucher</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" class="form-control form" name="vouchno" value="<?php Echo ($addflag != 1) ? $get["vouchno"] : "$qvouch"; ?>" >
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Enquiry Date</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="date" class="form-control form" name="vouchdt" value="<?php Echo ($addflag != 1) ? $get["vouchdt"] : date('Y-m-d'); ?>" >
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Name</label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form" name="name" value="<?php Echo ($addflag != 1) ? $get["name"] : ""; ?>" >
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Last Name</label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form" name="lastname" value="<?php Echo ($addflag != 1) ? $get["lastname"] : ""; ?>" >
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Mobile 1</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" class="form-control form  " name="mobile" value="<?php Echo ($addflag != 1) ? $get["mobile"] : ""; ?>" >
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Mobile 2</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" class="form-control form " name="mobile2" value="<?php Echo ($addflag != 1) ? $get["mobile2"] : ""; ?>" >
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Tel. 1</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" class="form-control form " name="tel" value="<?php Echo ($addflag != 1) ? $get["tel"] : ""; ?>" >
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Tel. 2</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" class="form-control form " name="tel2" value="<?php Echo ($addflag != 1) ? $get["tel2"] : ""; ?>" >
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Email Id</label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form" name="email" value="<?php Echo ($addflag != 1) ? $get["email"] : ""; ?>" >
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">City</label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form" name="city" value="<?php Echo ($addflag != 1) ? $get["city"] : ""; ?>" >
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Destination</label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                                <select class="form-control form select2 destination" name="destination">
                                    <option value="<? Echo ($addflag != 1 ) ? $get[destination] : "" ?>"><? Echo ($addflag != 1 ) ? $get[destination] : "" ?></option>
                                    <?
                                    $getdest = mysqli_query($conn,"SELECT * FROM `destination`")or die(mysqli_error($conn));
                                    while ($mdest = mysqli_fetch_array($getdest)) {
                                        Echo "<option value='$mdest[name]'>$mdest[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">City 1</label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                                <select class="form-control form select2 destcity1" name="destcity1">
                                    <option value="<? Echo ($addflag != 1 ) ? $get[destcity1] : "" ?>"><? Echo ($addflag != 1 ) ? $get[destcity1] : "" ?></option>
                                    <?
                                    $getdestc1 = mysqli_query($conn,"SELECT * FROM `city`")or die(mysqli_error($conn));

                                    while ($mdestc1 = mysqli_fetch_array($getdestc1)) {
                                        Echo "<option value='$mdestc1[name]'>$mdestc1[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">City 2</label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                                <select class="form-control form select2 destcity2" name="destcity2">
                                    <option value="<? Echo ($addflag != 1 ) ? $get[destcity2] : "" ?>"><? Echo ($addflag != 1 ) ? $get[destcity2] : "" ?></option>
                                    <?
                                    $getdestc2 = mysqli_query($conn,"SELECT * FROM `city`")or die(mysqli_error($conn));

                                    while ($mdestc2 = mysqli_fetch_array($getdestc2)) {
                                        Echo "<option value='$mdestc2[name]'>$mdestc2[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">City 3</label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                                <select class="form-control form select2 destcity3" name="destcity3">
                                    <option value="<? Echo ($addflag != 1 ) ? $get[destcity3] : "" ?>"><? Echo ($addflag != 1 ) ? $get[destcity3] : "" ?></option>
                                    <?
                                    $getdestc3 = mysqli_query($conn,"SELECT * FROM `city`")or die(mysqli_error($conn));

                                    while ($mdestc3 = mysqli_fetch_array($getdestc3)) {
                                        Echo "<option value='$mdestc3[name]'>$mdestc3[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">City 4</label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                                <select class="form-control form select2 destcity4" name="destcity4">
                                    <option value="<? Echo ($addflag != 1 ) ? $get[destcity4] : "" ?>"><? Echo ($addflag != 1 ) ? $get[destcity4] : "" ?></option>
                                    <?
                                    $getdestc4 = mysqli_query($conn,"SELECT * FROM `city`")or die(mysqli_error($conn));

                                    while ($mdestc4 = mysqli_fetch_array($getdestc4)) {
                                        Echo "<option value='$mdestc4[name]'>$mdestc4[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">City 5</label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                                <select class="form-control form select2 destcity5" name="destcity5">
                                    <option value="<? Echo ($addflag != 1 ) ? $get[destcity5] : "" ?>"><? Echo ($addflag != 1 ) ? $get[destcity5] : "" ?></option>
                                    <?
                                    $getdestc5 = mysqli_query($conn,"SELECT * FROM `city`")or die(mysqli_error($conn));

                                    while ($mdestc5 = mysqli_fetch_array($getdestc5)) {
                                        Echo "<option value='$mdestc5[name]'>$mdestc5[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 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 pad">
                                <input type="date" class="form-control form" name="travdate" value="<?php Echo ($addflag != 1) ? $get["deptdate"] : ""; ?>" >
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Adult</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <input type="text" class="form-control form adult" name="adult" value="<?php Echo ($addflag != 1) ? $get["adult"] : ""; ?>" >
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad">
                                <label class="label-control marg">Child</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <input type="text" class="form-control form child" name="child" value="<?php Echo ($addflag != 1) ? $get["child"] : ""; ?>" >
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad">
                                <label class="label-control marg">Infant</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <input type="text" class="form-control form infant" name="infant" value="<?php Echo ($addflag != 1) ? $get["infant"] : ""; ?>" >
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad">
                                <label style="color: red;" class="label-control marg">Total</label>
                            </div>
                            <div style="color: red;" class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <input type="text"  style="color: red;background-color: #E7E7E7;" class="form-control totpax form" name="totpax" value="<?php Echo ($addflag != 1) ? $get["totpax"] : ""; ?>" >
                            </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: #1ABC9C">
                                    <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`  WHERE `grpcode`='$mgrpcode'") 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>
