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

$formname = 'Godown';
$maindb = 'godown';
$file = 'godown.php';
$grid = '';
$keyfld = "code";
$trandbexist = 0;
$focusfld = "gdcode";
$printfile = 'godwnlst.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;
    $code   = $_POST["code"];
    $gdcode = $_POST["gdcode"];
    $name   = $_POST["name"];
    $add1   = $_POST["add1"];
    $add2   = $_POST["add2"];
    $add3   = $_POST["add3"];
    // browse($qvouch);
    $mvouch = $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["code"])) {
        $result1 = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `code`= '$code' AND code != '$qvouch'") or die(mysqli_error($conn));
        if (mysqli_num_rows($result1) > 0) {
            $a1 = 0;
            browse("Code Already Exist");
            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: ; $file?vouchno=$_GET[vouchno]");
        }
    }


    if ($a == 1) {
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`( code, `gdcode`, `name`, `add1`, `add2`, `add3`)
                    Values ('$qvouch', '$gdcode', '$name', '$add1', '$add2','$add3')") or die(mysqli_error($conn));
            insmess();
//            $mvouch = $qvouch;
//            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            $mvouch = $qvouch;
            mysqli_query($conn, "UPDATE `$maindb` SET  code='$qvouch', `gdcode`='$gdcode', `name`='$name', `add1`='$add1',`add2`='$add2' ,`add3`='$add3'  where code = '$mvouch' ") or die(mysqli_error($conn));
            updmess();
//            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        }
    }
}

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

<body>
    <form method="POST" class="form-horizontal" role="form" action='' name="country">
        <div class="row" style="">
            <div class="col-lg-6" style=''>
                <div class="box dark">
                    <header>
                        <div class="icons"><i class="fa fa-edit"></i></div>
                        <h5 style="font-family: cipher-regular,helvetica; "><?= $formname ?></h5>
                        <!-- .toolbar -->
                        <div class="toolbar">
                            <ul class="nav">
                                <li>
                                    <a class="accordion-toggle minimize-box" data-toggle="collapse" href="#div-1">
                                        <i class="fa fa-chevron-up"></i>
                                    </a>
                                </li>
                            </ul>
                        </div>
                        <!-- /.toolbar -->
                    </header>
                    <div id="div-1" class="accordion-body collapse in body" style="height: 240px;font-family: cipher-regular,helvetica;">
                        <div class="form-horizontal">
                            <br>
                            <input type="hidden" name="code"  value="<?php echo ($addflag != 1) ? $get['code'] : $qvouch;   ?>">

<!--                            <div class="form-group">
                                <label class="control-label col-lg-4" style="width: 150px;"> Code </label>
                                <div class="col-lg-8"> 
                                </div> 
                            </div>-->
                            <div class="form-group">
                                <label class="control-label col-lg-4" style="width: 150px;"> Gd.Code </label>
                                <div class="col-lg-8"> 
                                    <input type="text" name="gdcode" autofocus="" id="gdcode" placeholder="gdcode" value="<?php echo ($addflag != 1) ? $get['gdcode'] : '';   ?>" class="form-control form" style="width:130px; height: 30px;" required=>
                                </div> 
                            </div> 
                            <div class="form-group">
                                <label class="control-label col-lg-4" style="width: 150px;font-family: cipher-regular,helvetica;"> Name </label>
                                <div class="col-lg-8"> 
                                    <input type="text" name="name" placeholder="Name" value="<?php echo ($addflag != 1) ? $get['name'] : ''; ?>" style="height: 30px;" class="form-control form"  required=>
                                </div> 
                            </div> 
                            <div class="form-group">
                                <label class="control-label col-lg-4" style="width: 150px;font-family: cipher-regular,helvetica;"> Address </label>
                                <div class="col-lg-8"> 
                                    <input type="text" name="add1" placeholder="address" value="<?php echo ($addflag != 1) ? $get['add1'] : ''; ?>" style="height: 30px;" class="form-control form">
                                    <input type="text" name="add2" placeholder="address" value="<?php echo ($addflag != 1) ? $get['add2'] : ''; ?>" style="height: 30px;" class="form-control form">
                                    <input type="text" name="add3" placeholder="address" value="<?php echo ($addflag != 1) ? $get['add3'] : ''; ?>" style="height: 30px;" class="form-control form">
                                </div> 
                            </div>
                        </div>   
                    </div> 
                </div> 
            </div> 
        </div>
        <div id="formfoot" style="width:50%;">                        
            <h1><?php include("includes/buttons.php"); ?></h1>
        </div>

        <!--SEARCH PART-->

        <div class="modal fade in slacker-modal" id="searchModal" 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 class="modal-body" style="height: 100%;white-space: nowrap;">
                        <div class="row">

                            <div class="col-md-12">

                                <div class="col-lg-12">

                                    <div class="box">
                                        <div id="collapse4" class="body">
                                            <table id="example1" class="table table-bordered table-condensed table-hover table-striped">
                                                <thead>
                                                    <tr>
                                                        <th align='left'>Name</th>                                                                                                                
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php
                                                    $srchresult = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE $keyfld!=''") or die(mysqli_error($conn));
                                                    while ($row = mysqli_fetch_array($srchresult)) {
                                                        echo "<tr>";
                                                        echo "<td align='left'><a href=\"$file?mode=edit&$keyfld=$row[$keyfld]\">$row[name]</td>";                                                                                                               
                                                        echo "</tr>";
                                                    }
                                                    ?>
                                                </tbody>
                                            </table>
                                            <script>
                                                $(function () {
                                                    metisTable();
                                                    metisSortable();
                                                });
                                            </script>

                                        </div>

                                    </div>

                                </div>

                            </div><!-- /.row -->

                        </div>
                    </div>  
                    <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>
    </form>

    <script>
        $(function () {
            formGeneral();
        });
    </script>
</body>
</html>