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

$formname = 'School';
$maindb = 'accmast';
$printfile = "school_print.php";
$file = 'school.php';
$keyfld = "accode";
$focusfield = "code";
$keycond = " and `grpcode` = '010'";
$keycond1 = "";
$mgrpcode = '010';
$envelope = 'schlenvlp_prin.php';


$myyear = $_SESSION[$myyear];
$zroot = $_SESSION["zdir"];

$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;
    $accode = $_POST["accode"];
    $name = mysqli_real_escape_string($conn, $_POST["name"]);
    $custcode = $_POST["custcode"];
    $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"];
    $email = $_POST["email"];
    $web = $_POST["website"];
    $fax = $_POST["fax"];
    $stcode = $_POST["stdepot"];
    $centcode = $_POST["center"];
    $courcode = $_POST["courier"];
    $medmcode = $_POST["medium"];
    $openbal = $_POST["openbal"];
    $opendc = $_POST["opendc"];
    $localflg = $_POST["localflg"];

    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 (isset($_POST["name"])) {
        $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `name`= '$name' and grpcode = '010' AND accode != '$qvouch' ") or die(mysqli_error($conn));
        if (mysqli_num_rows($result) > 0) {
            $a = 0;
            browse("Name Already Exist");
            header("refresh: 1; $file?mode=last");
        }
    }
    if (isset($_POST["custcode"])) {
        $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `custcode`= '$custcode' and grpcode = '010' AND accode != '$qvouch' ") or die(mysqli_error($conn));
        if (mysqli_num_rows($result) > 0) {
            $a = 0;
            browse("Code Already Exist");
            header("refresh: 1; $file?mode=last");
        }
    }
//    if (isset($_POST["username"])) {
//        $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `username`= '$username' AND `grpcode`='$mgrpcode' AND accode != '$qvouch'") or die(mysqli_error($conn));
//        if (mysqli_num_rows($result) > 0) {
//            $a = 0;
//            browse("Name Already Exist");
//            header("refresh: 1; $file?mode=last");
//        }
//    }


    if ($a == 1) {
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`( `accode`, `name`, `custcode`, `grpcode`,`add1`, `add2`, `add3`, `city`, `pin`, `station`, `country`, `mobile` ,`tel`,
                                                 `email`,`website`,`stcode`,`centcode`,`courcode`,`medmcode`,`openbal`,`dc`,`fax`, `localflg`)
                                        VALUES($qvouch,'$name', '$custcode', '$mgrpcode', '$add1', '$add2', '$add3', '$city', '$pin', '$station', '$coun', '$mob' , '$tel',
                                                '$email', '$web','$stcode','$centcode','$courcode','$medmcode','$openbal','$opendc','$fax', '$localflg')") 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 `name`='$name',`custcode`='$custcode', `add1`='$add1', `add2`='$add2', `add3`='$add3', `city`='$city', `pin`='$pin',
                          `station`='$station', `country`='$coun', mobile= '$mob',tel='$tel', mobile='$mob', `email`='$email',
                          `website`='$web', `stcode`='$stcode', `centcode`='$centcode', `courcode`='$courcode', `medmcode`='$medmcode',`openbal`='$openbal',
                           `grpcode`='$mgrpcode',`dc`='$opendc',`fax`='$fax', `localflg`='$localflg' where accode = '$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' $keycond ") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);

    $fcenter = mysqli_query($conn, "SELECT * FROM `center` WHERE centcode = '$get[centcode]' ") or die(mysqli_error($conn));
    $center = mysqli_fetch_array($fcenter);

    $fstdepot = mysqli_query($conn, "SELECT * FROM `stdepot` WHERE code = '$get[stcode]' ") or die(mysqli_error($conn));
    $stdepot = mysqli_fetch_array($fstdepot);

    $fcour = mysqli_query($conn, "SELECT * FROM `courier` WHERE code = '$get[courcode]' ") or die(mysqli_error($conn));
    $courier = mysqli_fetch_array($fcour);

    $fmed = mysqli_query($conn, "SELECT * FROM `medium` WHERE code = '$get[medmcode]' ") or die(mysqli_error($conn));
    $medium = mysqli_fetch_array($fmed);
}

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


if (isset($_GET["delete"])) {

    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
}

include("includes/linkshead.php");
?>

<script src="MyScript/school_search.js"  type="text/javascript"></script>   

</head>
<body>
    <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>
                        </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">Code</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" class="form-control form focusfield" name="custcode" value="<?php Echo ($addflag != 1) ? $get["custcode"] : ""; ?>" required>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" class="form-control form" name="accode" value="<?php Echo ($addflag != 1) ? $get["accode"] : "$qvouch"; ?>" readonly>
                            </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" style="color: #6082BF ">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"] : ""; ?>" required>
                            </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">Address</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="add1" value="<?php Echo ($addflag != 1) ? $get["add1"] : ""; ?>" >
                                <input type="text" class="form-control form" name="add2" value="<?php Echo ($addflag != 1) ? $get["add2"] : ""; ?>" >
                                <input type="text" class="form-control form" name="add3" value="<?php Echo ($addflag != 1) ? $get["add3"] : ""; ?>" >
                            </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-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" class="form-control form" name="city" value="<?php Echo ($addflag != 1) ? $get["city"] : ""; ?>" >
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Pin</label>
                            </div>
                            <div class="col-lg-3 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" class="form-control form " name="pin" value="<?php Echo ($addflag != 1) ? $get["pin"] : ""; ?>" >
                            </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">Station</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form" name="station" value="<?php Echo ($addflag != 1) ? $get["station"] : ""; ?>" >
                            </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">Country</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form" name="country" value="<?php Echo ($addflag != 1) ? $get["country"] : ""; ?>" >
                            </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.</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form " name="tel" value="<?php Echo ($addflag != 1) ? $get["tel"] : ""; ?>" >
                            </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 </label>
                            </div>
                            <div class="col-lg-4 col-md-4 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>
                        <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">Fax</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4s col-xs-8 pad">
                                <input type="text" class="form-control fax form" name="fax" value="<?php Echo ($addflag != 1) ? $get["fax"] : ""; ?>" >
                            </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</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">Website</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="website" value="<?php Echo ($addflag != 1) ? $get["website"] : ""; ?>" >
                            </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">Center Name</label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                                <select name="center" class="form-control form">
                                    <option value="<?php echo ($addflag != 1) ? $center['centcode'] : ''; ?>"><?php echo ($addflag != 1) ? $center['name'] : ''; ?></option>
                                    <option></option>
                                    <?php
                                    $fcent = mysqli_query($conn, "SELECT * FROM `center`") or die(mysqli_error($conn));

                                    while ($cent = mysqli_fetch_array($fcent)) {
                                        echo "<option value='$cent[centcode]'>$cent[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">Medium</label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                                <select name="medium" class="form-control form">
                                    <option value="<?php echo ($addflag != 1) ? $medium['code'] : ''; ?>"><?php echo ($addflag != 1) ? $medium['name'] : ''; ?></option>
                                    <option></option>
                                    <?php
                                    $fmedm = mysqli_query($conn, "SELECT * FROM `medium` ") or die(mysqli_error($conn));
                                    while ($med = mysqli_fetch_array($fmedm)) {
                                        echo "<option value='$med[code]'>$med[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">S.T Depot</label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                                <select name="stdepot" class="form-control form">
                                    <option value="<?php echo ($addflag != 1) ? $stdepot['code'] : ''; ?>"><?php echo ($addflag != 1) ? $stdepot['name'] : ''; ?></option>
                                    <option></option>
                                    <?php
                                    $fst = mysqli_query($conn, "SELECT * FROM `stdepot` order by name") or die(mysqli_error($conn));
                                    while ($st = mysqli_fetch_array($fst)) {
                                        echo "<option value='$st[code]'>$st[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">Courier</label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                                <select name="courier" class="form-control form">
                                    <option value="<?php echo ($addflag != 1) ? $courier['code'] : ''; ?>"><?php echo ($addflag != 1) ? $courier['name'] : ''; ?></option>
                                    <option></option>
                                    <?php
                                    $fst = mysqli_query($conn, "SELECT * FROM `courier` order by name ") or die(mysqli_error($conn));
                                    while ($st = mysqli_fetch_array($fst)) {
                                        echo "<option value='$st[code]'>$st[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">Opening Balance</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="number" size="40" name="openbal" step="any" class="form-control form text-right openbal " value="<?php echo ($addflag != 1) ? $get['openbal'] : "0"; ?>" >
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <select name="opendc" class="form-control form pad select2">
                                    <option
                                    <?php
                                    if ($get['dc'] == 'D') {
                                        echo "selected=selected";
                                    }
                                    ?> value='D'>DR</option>
                                    <option
                                    <?php
                                    if ($get['dc'] == 'C') {
                                        echo "selected=selected";
                                    }
                                    ?> value='C'>CR</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">Local</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <input type="checkbox"  class="form-control form localflg" <?php
                                if ($get['localflg'] == "1") {
                                    Echo "checked";
                                }
                                ?> value="1" name="localflg" >                         
                            </div>
                        </div>
                    </div>
                    <div class="panel-footer">
                        <?php include("includes/buttons.php"); ?>
                        <div style="text-align: right">
                            <a target="_blank" id='cancel' type="button"  class="btn btn-pad btn-square" style="background-color:#DAB565; height:60px; width:70px;"  href="<?= $envelope . "?code=$qvouch" ?>">
                                <i class="fa fa-envelope" style="color:black; font-size:35px"></i>
                            </a>
                        </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">Code</option>
                                <option value="2">Name</option>
                                <option value="3">City</option>
                                <option value="4">Pin</option>
                                <option value="5">Station</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">Code</option>
                                <option value="2">Name</option>
                                <option value="3">City</option>
                                <option value="4">Pin</option>
                                <option value="5">Station</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 style="background-color:#6082BF;">
                                    <th width="10%">Code</th>
                                    <th width="20%">Name</th>
                                    <th width="20%">City</th>
                                    <th width="10%">Pin</th>
                                    <th width="20%">Station</th>
                                    <th width="20%">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>
