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

$formname = 'State';
$maindb = 'state';
$file = 'state.php';
$grid = '';
$keyfld = "accode";
$trandbexist = 0;
$focusfld = "name";
$printfile = 'statelst.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;
    $cname = $_POST["name"];
    $count = $_POST["cmbcount"];
    // browse($qvouch);
    $mvouch = $qvouch;

    if ($cname == " ") {
        $a = 0;
        browse("Name Cannot Be Empty");
        if ($addflag == 1) {
            header("location:javascript://history.go(-1)");
        } else {
            header("refresh: ; $file?vouchno=$_GET[vouchno]");
        }
    }
    if ($count == " ") {
        $a = 0;
        browse("Country 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`= '$cname' AND $keyfld != '$qvouch'") or die(mysqli_error($conn));
        if (mysqli_num_rows($result) > 0) {
            $a = 0;
            browse("Name Already Exist");
            header("refresh: 1; $file?$keyfld=$qvouch");
        }
    }


    if ($a == 1) {
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(accode, code, `name`, country)
                                Values ('$qvouch', '$qvouch', '$cname', '$count')") 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', code='$qvouch', `name`='$cname', `cmbcount`='$count'  where $keyfld = '$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);

    $coun = mysqli_query($conn, "SELECT * FROM `country` WHERE code='$get[country]'") or die(mysqli_error($conn));
    $getcount = mysqli_fetch_object($coun);
}

/* DELETE */
if (isset($_GET["delete"])) {
    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
}
include("includes/files.php");
?>
<!-- Select2 Combo -->

<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/select2/3.5.0/select2.min.css" />
<!-- CSS to make Select2 fit in with Bootstrap 3.x -->
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/select2/3.5.0/select2-bootstrap.min.css" />
<!-- Include Select2 JS -->
<script src="//cdnjs.cloudflare.com/ajax/libs/select2/3.5.0/select2.min.js"></script>
<script>
    $(function () {
        // turn the element to select2 select style
        $('#transporter').select2();
    });
    
</script>
<body>
    <form method="POST" 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" >
                        <div class="form-horizontal">
                            <div class="form-group">
                                <label class="control-label col-lg-4" style="width: 150px;font-family:  cipher-regular,helvetica;"> State </label>
                                <div class="col-lg-8"> 
                                    <input type="text" name="name" autofocus="" placeholder="State Name" value="<?php echo ($addflag != 1) ? $get['name'] : ''; ?>" class="form-control form" style="height: 33px;width: 390px;" required="">
                                </div> 
                            </div> 
                            <div class="form-group">
                                <label class="control-label col-lg-4" style="width: 150px;font-family:  cipher-regular,helvetica;"> Country </label>
                                <div class="col-lg-8"> 
                                    <select name="cmbcount" id="count" class="form-control form" style="font-size:14px; height: 33px; width: 390px;">
                                        <option value="<?php echo ($addflag != 1) ? $getcount->code : ''; ?>"><?php echo ($addflag != 1) ? $getcount->name : ''; ?></option>
                                        <?php
                                        $result = mysqli_query($conn, "SELECT code, `name` FROM country ORDER BY `name`") or die(mysqli_error($conn));
                                        while ($coun = mysqli_fetch_array($result)) {
                                            echo "<option value='$coun[code]'>$coun[name]</option>";
                                        }
                                        ?>
                                    </select>
                                </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="dataTable" class="table table-bordered table-condensed table-hover table-striped">
                                                <thead>
                                                    <tr>
                                                        <th align='left'>Name</th>
                                                        <th align='left'>Action</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'>$row[name]</td>";
                                                        echo "<td align='left'><a href=\"$file?mode=edit&$keyfld=$row[$keyfld]\"><img src='images/edit.png'></a></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>