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

$formname = 'Bank';
$maindb = 'accmast';
$file = 'bank.php';
$grid = '';
$keyfld = "accode";
$trandbexist = 0;
$focusfld = "name";
$printfile = 'banklst.php';
$grpcode = "001";
$keycond = " AND `grpcode` = '001' ";

$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"];
    $init = $_POST["init"];
    $openbal = $_POST["openbal"];
    $dc = $_POST["dc"];

    // 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["accode"])) {
        $result1 = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `accode`= '$qvouch' 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 accode != '$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`( accode, `name`, `init`, `openbal`, `dc`,`grpcode`)
                    Values ('$qvouch', '$name', '$init', '$openbal','$dc','$grpcode')") 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  accode='$qvouch',  `name`='$name', `init`='$init',`openbal`='$openbal' ,`dc`='$dc', `grpcode`='$grpcode'  where accode = '$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' $keycond ") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);
//    $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE $keyfld='$getvouch' and `grpcode`='$grpcode'") 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 class="form-horizontal" action=''  name="airline" method="POST">
        <div class="col-md-6">
            <div class="box">
                <div class="box-header with-border" style="border: 1px solid black">
                    <h4 class="box-title"><i class="fa fa-files-o"></i> <?php Echo $formname ?></h4>
<!--                    <div class="box-tools pull-right">
                        <h3 class="box-title"><?php // echo $usernme['user']; ?></h3>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    </div>
                    <div class="box-tools pull-right">
                        <button class="btn btn-box-tool" data-widget="collapse"  title="Collapse"><i class="fa fa-minus"></i></button>
                    </div>-->
                </div>

                <div class="box-body" style="border: 1px solid black">

                    <div class="box-body">
                        <div class="form-group">
                            <label class="control-label col-lg-4" style="width: 150px;">Name</label>
                            <div class="col-lg-3"> 
                                <input type="text" name="name" autofocus="" placeholder="Name" value="<?php echo ($addflag != 1) ? $get['name'] : ''; ?>" class="form-control form" style="width:420px; height: 30px;" required="">
                            </div>
                        </div>
                        
                         
                        <div class="form-group">
                            <label class="control-label col-lg-4"  style="width: 150px;"> Initial. </label>
                            <div class="col-lg-3"> 
                                <input type="text" name="init"  autofocus=""placeholder="Initial" value="<?php echo ($addflag != 1) ? $get['init'] : ''; ?>" style=" width:60px; height: 30px;" class="form-control form" required="">
                            </div> 
                        </div>
                        <div class="form-group">
                            <label class="control-label col-lg-4"  style="width: 150px;"> Opening Balance </label>
                            <div class="col-lg-3"> 
                                <input type="text" name="openbal" autofocus="" placeholder="Openbal" value="<?php echo ($addflag != 1) ? $get['openbal'] : ''; ?>" style=" width:180px; height: 30px; margin:0px; text-align: right;" class="form-control form">
                                <input type="text" name="dc" value ="<?php echo ($addflag == 1) ? $get['dc'] : "D"; ?>" style=" width:50px; height: 30px; float: right;" class="form-control form"  required="">                                
                            </div> 
                        </div>
                         
                    </div>
                </div>
                <div class="box-footer" style="border: 1px solid black">
                    <div class="row">
                    <?php include("includes/buttons.php"); ?>
                    </div>
                </div>
                <!-- /box-end -->
            </div>

        </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="dataTable" 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>
<?php include("includes/footlinks.php"); ?>