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

$formname = 'Department';
$maindb = 'dept';
$printfile = "department_print.php";
$file = 'department.php';
$trandbexist = 0;
$grid = '';
$keyfld = "id";
$focusfield = "name";

$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;
    $name = $_POST["name"];
    $custcode = $_POST["code"];
    $add1 = mysqli_real_escape_string($conn, $_POST["add1"]);
    $add2 = mysqli_real_escape_string($conn, $_POST["add2"]);
    $add3 = mysqli_real_escape_string($conn, $_POST["add3"]);
    $cpersno = $_POST["contno"];
    $perinc = $_POST["incharge"];
    $bankname = $_POST["bankname"];
    $branchname = $_POST["branchname"];
    $acno = $_POST["acno"];
    $ifsccode = $_POST["ifsccode"];
    $accode = $_POST["accode"];
    $agstaccode = $_POST["agstaccode"];
    $workhrs = $_POST["workhrs"];
    $mvouch = $qvouch;

    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 $keyfld != '$mvouch'") or die(mysqli_error($conn));
        if (mysqli_num_rows($result) > 0) {
            $a = 0;
            browse("Department Already Exist");
            header("refresh: 1; $file?mode=last");
        }
    }


    if ($a == 1) {
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(id, `accode`, `agstaccode`,code,`name`, `add1`, `add2`, `add3`, `contno`, `incharge`,
                         bankname, branchname, acno, ifsccode,workhrs)
                         VALUES($mvouch, '$accode', '$agstaccode', '$custcode', '$name', '$add1', '$add2', '$add3', '$cpersno', '$perinc',
                                '$bankname','$branchname','$acno','$ifsccode','$workhrs')") or die(mysqli_error($conn));


//            mysqli_query($conn, "INSERT INTO $zroot.`deptsal`(`deptcode`)
//                         VALUES('$custcode')") or die(mysqli_error($conn));
            $mvouch = $qvouch;
            insmess();
            header("refresh: 1; $file?mode=last");
        } else {
            $mvouch = $qvouch;

            mysqli_query($conn, "UPDATE `$maindb` SET `accode`='$accode', `agstaccode`='$agstaccode',code='$custcode', `name`='$name', `add1`='$add1', `add2`='$add2', `add3`='$add3',
                        `contno`='$cpersno', `incharge`='$perinc', `bankname`='$bankname',`branchname`='$branchname',`acno`='$acno',workhrs='$workhrs',
                        `ifsccode`='$ifsccode' where $keyfld = '$mvouch' ") or die(mysqli_error($conn));

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

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);
    $dpcode = $get['code'];
}

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


if (isset($_GET["delete"])) {
    $get1 = mysqli_query($conn, "Select deptcode from accmast where deptcode = '$dpcode'") or die(mysqli_error($conn));
    if (mysqli_num_rows($get1) > 0) {
        browse('Department Exist in Employee Entry');
        header("refresh: 1; $file?mode=last");
    } else {
        delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    }
}

include("includes/linkshead.php");
?>
<style>
    #ccode{
        margin-left: -15px;
    }
</style>
<script src="MyScript/department.js?a=<?= mt_rand(); ?>" 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>
                    <?php
                    $read = '';
                    if ($addflag != 1) {
                        $read = "readonly";
                    }
                    ?>
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4 text-right pad">
                                <label class="control-label marg">Department Code</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="number" class="form-control form focusfield" name="code" value="<?php Echo ($addflag != 1) ? $get["code"] : ""; ?>" required <?= $read ?>> 
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4 text-right pad">
                                <label class="control-label marg">Name</label>
                            </div>
                            <div class="col-lg-8 col-md-7 col-sm-7 col-xs-7 pad">
                                <input type="text" class="form-control form " name="name" value="<?php Echo ($addflag != 1) ? $get["name"] : ""; ?>" required >
                                <input type="hidden" class="form-control form " name="accode" value="<?php Echo ($addflag != 1) ? $get["accode"] : "29"; ?>" >
                                <input type="hidden" class="form-control form " name="agstaccode" value="<?php Echo ($addflag != 1) ? $get["agstaccode"] : "39"; ?>" >
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4 text-right pad">
                                <label class="label-control marg">Address</label>
                            </div>
                            <div class="col-lg-8 col-md-7 col-sm-7 col-xs-7 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-4 col-xs-4 text-right pad">
                                <label class="control-label marg">Contact No</label>
                            </div>
                            <div class="col-lg-8 col-md-7 col-sm-7 col-xs-7 pad">
                                <input type="text" class="form-control form" name="contno" value="<?php Echo ($addflag != 1) ? $get["contno"] : ""; ?>" >
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4 text-right pad">
                                <label class="control-label marg">Person Incharge</label>
                            </div>
                            <div class="col-lg-8 col-md-7 col-sm-7 col-xs-7 pad">
                                <input type="text" class="form-control form" name="incharge" value="<?php Echo ($addflag != 1) ? $get["incharge"] : ""; ?>" >
                            </div>
                        </div>
                        <!-- <div class="row" style="background-color:yellow;margin-right:-5px">
                            <div class="col-lg-3 text-right pad">
                                <label class="control-label marg">Bank Name</label>
                            </div>
                            <div class="col-lg-8 pad">
                                <input type="text" class="form-control form " name="bankname" value="<?php Echo ($addflag != 1) ? $get["bankname"] : ""; ?>" >
                            </div>
                        </div>
                        <div class="row" style="background-color:yellow;margin-right:-5px">
                            <div class="col-lg-3 text-right pad">
                                <label class="control-label marg">Branch Name</label>
                            </div>
                            <div class="col-lg-8 pad">
                                <input type="text" class="form-control form " name="branchname" value="<?php Echo ($addflag != 1) ? $get["branchname"] : ""; ?>"  >
                            </div>
                        </div>
                        <div class="row" style="background-color:yellow;margin-right:-5px">
                            <div class="col-lg-3 text-right pad">
                                <label class="control-label marg">A/c. No.</label>
                            </div>
                            <div class="col-lg-8 pad">
                                <input type="text" class="form-control form " name="acno" value="<?php Echo ($addflag != 1) ? $get["acno"] : ""; ?>" >
                            </div>
                        </div>
                        <div class="row" style="background-color:yellow;margin-right:-5px">
                            <div class="col-lg-3 text-right pad">
                                <label class="control-label marg">IFSC Code</label>
                            </div>
                            <div class="col-lg-8 pad">
                                <input type="text" class="form-control form " name="ifsccode" value="<?php Echo ($addflag != 1) ? $get["ifsccode"] : ""; ?>" >
                            </div>
                        </div>
                        <div class="row" style="background-color:yellow;margin-right:-5px">
                            <div class="col-lg-3 text-right pad">
                                <label class="control-label marg">Work Hrs.</label>
                            </div>
                            <div class="col-lg-1 pad">
                                <input type="text" class="form-control form " name="workhrs" value="<?php Echo ($addflag != 1) ? $get["workhrs"] : ""; ?>" >
                            </div>
                        </div> -->
                    </div>
                    <div class="panel-footer">
                        <?php include("includes/buttons.php"); ?>
                    </div>
                </div>
            </div>

        </div>
    </form> 

    <!--SEARCH PART-->
    <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">
                    <input type="hidden" value="<?php // Echo $fseries           ?>" class="form-control series">
                    <input type="hidden" value="<?php // Echo $get[vouchno];           ?>" class="form-control vouchno">
                    <input type="hidden" value="<?php // Echo $file           ?>" class="form-control file">
                    <input type="hidden" value="<?php // Echo $para           ?>" class="form-control para">
                    <div class="row">
                        <div class="col-lg-2">
                            <select class="form-control dfield">
                                <option value="1">Department Code</option>
                                <option value="2">Name</option>
                                <option value="3">Address</option>
                                <option value="4">Contact No.</option>
                                <option value="5">Person Incharge</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control doperator">
                                <option value=""></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">Department Code</option>
                                <option value="2">Name</option>
                                <option value="3">Address</option>
                                <option value="4">Contact No.</option>
                                <option value="5">Person Incharge</option>
                                <option value="6">Action</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 dsearch" 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 dloader text-center">
                        <!--<img src="images/search.gif" alt="loading">-->       
                    </div>
                    <div class="col-lg-12 dnoRecord 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 class="mySearchTr">
                                    <th width="10%">Department Code</th>
                                    <th width="20%">Name</th>
                                    <th width="20%">Address</th>
                                    <th width="10%">Contact No.</th>
                                    <th width="15%">Person Incharge</th>
                                    <th width="5%">Action</th>
                                </tr>
                            </thead>
                            <tbody class="dTbody">
                            </tbody>
                        </table>
                    </div>
                    <div class="row">
                        <div class="col-xs-12 text-right search-action-bar">
                            <input type="button"  class="btn btn-primary dprev" value="Previous">
                            <input type="button"  class="btn btn-primary dnext" value="Next">
                        </div>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>