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

$formname = 'Zimmedar Details';
$maindb = 'zimdtl';
$file = 'zimmedar.php';
$grid = '';
$keyfld = "accode";
$trandbexist = 0;
$focusfld = "name";
$printfile = 'zimmtlst.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;
    $name = $_POST["name"];
    $qab = $_POST["qabiliyat"];
    $mob = $_POST["mob"];
    $email = $_POST["email"];
    // browse($qvouch);
    $mvouch = $qvouch;

    if ($name == " ") {
        $a = 0;
        browse("Zimmedar Name Cannot Be Empty");
        if ($addflag == 1) {
            header("location:javascript://history.go(-1)");
        } else {
            header("refresh: ; $file?vouchno=$_GET[vouchno]");
        }
    }
    
    if ($name == " ") {
        $a = 0;
        browse("Qabiliyat Cannot Be Empty");
        if ($addflag == 1) {
            header("location:javascript://history.go(-1)");
        } else {
            header("refresh: ; $file?vouchno=$_GET[vouchno]");
        }
    }

    if (isset($_POST["name"])) {
        $result1 = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `code`= '$code' AND $keyfld != '$qvouch'") or die(mysqli_error($conn));
        if (mysqli_num_rows($result1) > 0) {
            $a1 = 0;
            browse("Zimmedar Name Already Exist");
            header("refresh: 1; $file?$keyfld=$qvouch");
        }
    }
    

    if ($a == 1) {
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(accode, code, `name`, `qabiliyat`, mobile, `email`)
                    Values ('$qvouch', '$qvouch', '$name', '$qab', '$mob', '$email')") 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`='$name', `qabiliyat`='$qab', mobile='$mob', `email`='$email' 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);
}

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

<body>
    <form method="POST" action='' name="country">
        <div class="row" style="margin-left: 300px; margin-right: 300px; margin-top: 100px;">
            <div class="col-lg-6" style='width:100%; '>
                <div class="box dark">
                    <header>
                        <div class="icons"><i class="fa fa-edit"></i></div>
                        <h5><?= $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: 150px;">
                        <div class="form-horizontal">
                            <div class="form-group">
                                <label class="control-label col-lg-4"> Name </label>
                                <div class="col-lg-8"> 
                                    <input type="text" name="name" autofocus="" placeholder="Zimmedar Name" value="<?php echo ($addflag != 1) ? $get['name'] : ''; ?>" class="form-control form" style="height: 30px;" required="">
                                </div> 
                            </div> 
                            <div class="form-group">
                                <label class="control-label col-lg-4"> Qabiliyat </label>
                                <div class="col-lg-8"> 
                                    <input type="text" name="qabiliyat" placeholder="Qabiliyat" value="<?php echo ($addflag != 1) ? $get['qabiliyat'] : ''; ?>" style="height: 30px;" class="form-control form"  required="">
                                </div> 
                            </div> 
                            <div class="form-group">
                                <label class="control-label col-lg-4"> E-mail </label>
                                <div class="col-lg-8"> 
                                    <input type="text" name="email" placeholder="E-mail" value="<?php echo ($addflag != 1) ? $get['email'] : ''; ?>" style="height: 30px;" class="form-control form"  required="">
                                </div> 
                            </div>
                            <div class="form-group">
                                <label class="control-label col-lg-4"> Mobile </label>
                                <div class="col-lg-8"> 
                                    <input type="number" name="mob" placeholder="Mobile " value="<?php echo ($addflag != 1) ? $get['mobile'] : ''; ?>" style="height: 30px; width: 150px;" class="form-control form"  required="">
                                </div> 
                            </div> 
                             
                        </div>   
                    </div> 
                </div> 
            </div> 
        </div>
        <div id="formfoot" style="width:100%;">                        
            <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'>Qabiliyat</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'>$row[qabiliyat]</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>