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

$formname = 'Transporter';
$maindb = 'transporter';
$file = 'transport.php';
$grid = '';
$keyfld = "code";
$trandbexist = 0;
$focusfld = "name";
$printfile = 'translst.php';

$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;
    $name = $_POST["name"];
    $add1 = mysqli_real_escape_string($conn, $_POST["add1"]);
    $add2 = mysqli_real_escape_string($conn, $_POST["add2"]);
    $add3 = mysqli_real_escape_string($conn, $_POST["add3"]);
    $city = $_POST["city"];
    $pin = $_POST["pin"];
    $coun = $_POST["country"];
    $station = $_POST["station"];
    $mob = $_POST["mobile"];
    $tel = $_POST["tel"];
    $fax = $_POST["fax"];
    $email = $_POST["email"];
    $web = $_POST["web"];
    $cpers1 = $_POST["contpers"];
   
    // browse($qvouch);

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

    if (isset($_POST["name"])) {
        $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `name`= '$name' AND code != '$qvouch'") or die(mysqli_error($conn));
        if (mysqli_num_rows($result) > 0) {
            $a = 0;
            browse("Name Already Exist");
            header("refresh: 1; $file?vouchno=$qvouch");
        }
    }


    if ($a == 1) {
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(code,`name`, `add1`, `add2`, `add3`, `city`, `pin`, `station`, `country`, mobile ,tel,
                                                fax, `email`,`website`, `contpers`)
                                        VALUES($qvouch,'$name', '$add1', '$add2', '$add3', '$city', '$pin', '$station', '$coun', '$mob' , '$tel',
                                               '$fax', '$email', '$web', '$cpers1')") or die(mysqli_error($conn));
            insmess();
            $mvouch = $_GET["vouchno"];
            $mvouch = $qvouch;
            header("refresh: 1; $file?vouchno=$mvouch");
//            header("refresh: 1; $file?mode=true");
        } else {
            $mvouch = $_GET["vouchno"];
            mysqli_query($conn, "UPDATE `$maindb` SET `name`='$name', `add1`='$add1', `add2`='$add2', `add3`='$add3', `city`='$city', `pin`='$pin', `station`='$station', `country`='$coun', mobile= '$mob',tel='$tel', 
                         fax='$fax', mobile='$mob', `email`='$email', `website`='$web', `contpers`='$cpers1'  where code = '$mvouch' ") or die(mysqli_error($conn));

            updmess();
            header("refresh: 1; $file?vouchno=$mvouch");
//            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);
}

/* DELETE */
if (isset($_GET["delete"])) {
    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
}
include("includes/files.php");
?>
</head>
<form class="form-horizontal" action='' method='POST'>
    <div class="row">
        <div class="col-lg-12">
            <div class="box">
                <header style="width:80%;">
                    <div class="icons">
                        <i class="fa fa-edit"></i>
                    </div>
                    <h5><?= $formname ?></h5>
                    <div class="toolbar">
                        <ul class="nav pull-right">
                            <li>
                                <?php include("includes/buttons.php"); ?>
                            </li>
                            <li>
                                <a class="minimize-box" data-toggle="collapse" href="#div-4">
                                    <i class="fa fa-chevron-up"></i>
                                </a>
                            </li>
                        </ul>
                    </div><!-- /.toolbar -->
                </header> 
                <div id="div-4" class="accordion-body collapse in body" style="width:50%;">

                    <div class="form-group">
                        <label class="control-label col-lg-4">Name</label>
                        <div class="col-lg-8"> 
                            <input type="text" size="40" name="name" value="<?php echo ($addflag != 1) ? $get['name'] : ""; ?>" class="form-control form" required />
                        </div> 
                    </div> 
                    <div class="form-group">
                        <label class="control-label col-lg-4">Address</label>
                        <div class="col-lg-8"> 
                            <input type="text" size="80" name="add1" value="<?php echo ($addflag != 1) ? $get['add1'] : ""; ?>" class="form-control form"/>
                        </div> 
                    </div> 
                    <div class="form-group">
                        <label class="control-label col-lg-4"></label>
                        <div class="col-lg-8"> 
                            <input type="text" size="80" name="add2" value="<?php echo ($addflag != 1) ? $get['add2'] : ""; ?>" class="form-control form"/>
                            <input type="text" size="80" name="add3" value="<?php echo ($addflag != 1) ? $get['add3'] : ""; ?>" class="form-control form"/>
                        </div> 
                    </div> 
                    <div class="form-group">
                        <label class="control-label col-lg-4">City</label>
                        <div class="col-lg-8"> 
                            <div style='width:100%;'>
                                <div style='float:left;'>
                                    <input type="text" size="40" name="city" class="form-control form" value="<?php echo ($addflag != 1) ? $get['city'] : "MUMBAI"; ?>" style='width:200px;'/>
                                </div>
                                <label class="control-label col-lg-4" style="10px;">Pin</label>
                                <div style='float:right;width:80px;text-align:left;'>
                                    <input type="text" maxlenght="8" size="8" name="pin" class="form-control form" value="<?php echo ($addflag != 1) ? $get['pin'] : ""; ?>" style='float:left;width:80px;'/>
                                </div>
                            </div>
                        </div> 
                    </div> 
                    <div class="form-group">
                        <label class="control-label col-lg-4">Station</label>
                        <div class="col-lg-8"> 
                            <div style='float:left;width:50%;'>
                                <input type="text" size="40" name="station" class="form-control form" value="<?php echo ($addflag != 1) ? $get['station'] : ""; ?>" style='width:200px;'/>
                            </div>
                        </div> 
                    </div> 
                    <div class="form-group">
                        <label class="control-label col-lg-4">Country</label>
                        <div class="col-lg-8"> 
                            <input type="text" size="40" name="country" class="form-control form" value="<?php echo ($addflag != 1) ? $get['country'] : "INDIA"; ?>"/>
                        </div> 
                    </div> 
                    <div class="form-group">
                        <label class="control-label col-lg-4">Mobile</label>
                        <div class="col-lg-8"> 
                            <input type="text" size="40" name="mobile" class="form-control form" value="<?php echo ($addflag != 1) ? $get['mobile'] : ""; ?>" />
                        </div> 
                    </div> 
                    <div class="form-group">
                        <label class="control-label col-lg-4">Telephone </label>
                        <div class="col-lg-8"> 
                            <input type="text" size="40" name="tel" class="form-control form" value="<?php echo ($addflag != 1) ? $get['tel'] : ""; ?>" />
                        </div> 
                    </div> 
                    <div class="form-group">
                        <label class="control-label col-lg-4">Fax </label>
                        <div class="col-lg-8"> 
                            <input type="text" size="40" name="fax" class="form-control form" value="<?php echo ($addflag != 1) ? $get['fax'] : ""; ?>"/>
                        </div> 
                    </div> 
                    <div class="form-group">
                        <label class="control-label col-lg-4">Email </label>
                        <div class="col-lg-8"> 
                            <input type="email" size="40" name="email" class="form-control form" value="<?php echo ($addflag != 1) ? $get['email'] : ""; ?>"/>
                        </div> 
                    </div> 
                    <div class="form-group">
                        <label class="control-label col-lg-4">Website </label>
                        <div class="col-lg-8"> 
                            <input type="text" size="40" name="web" class="form-control form" value="<?php echo ($addflag != 1) ? $get['website'] : ""; ?>"/>
                        </div> 
                    </div> 
                    <div class="form-group">
                        <label class="control-label col-lg-4">Contact Person 1 </label>
                        <div class="col-lg-8"> 
                            <input type="text" size="40" name="contpers" class="form-control form" value="<?php echo ($addflag != 1) ? $get['contpers'] : ""; ?>"/>
                        </div> 
                    </div> 
                     
                </div>
            </div>
            <!--SEARCH PART-->

            <div class="modal fade in slacker-modal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="false">
                <div class="modal-dialog modal-slacker">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                            <h4 class="modal-title" id="myLargeModalLabel"></h4>
                        </div>
                        <div id="div-1" class="accordion-body collapse in body">
                            <div class="form-horizontal">
                                <input type="hidden" size="6" maxlength="6" id="mvouch" maxlength="6" name="mvouch" value="<?php echo ($addflag != 1) ? $get['vouchno'] : $qvouch; ?>" />      

                                <div class="modal-footer" style="bottom: 0px;position: absolute;padding:0px;">
                                    <!--"This is where i want to load my salemain.php"-->
                                </div>
                            </div>
                        </div>
                    </div>            
                </div>
            </div>
            </form>
<?php
include("includes/files_footer.php");
?>
            <script>
                $(function () {
                    formGeneral();
                });
            </script>