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

$formname = 'Create Admin User';
$maindb = 'usermain';
$file = 'admin.php';
$grid = '';
$keyfld = "userid";
$trandbexist = 0;
$focusfld = "user";
$printfile = 'admin.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;
    $muser = $_POST["user"];
    $mpwd1 = $_POST["pwd1"];
    $memail = $_POST["email"];
    $active = $_POST["active"];
    // browse($qvouch);
    $mvouch = $qvouch;

    if (isset($_POST["user"])) {
        $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `username`= '$muser' AND $keyfld != '$qvouch'") or die(mysqli_error($conn));
        if (mysqli_num_rows($result) > 0) {
            $a = 0;
            browse("User Name Already Exist");
            header("refresh: 1; $file?mode=true");
        }
    }


    if ($a == 1) {
        if ($addflag == 1) {
            if ($_POST["pwd1"] == $_POST["pwd2"]) {
                mysqli_query($conn, "INSERT INTO `$maindb`(`userid`, `username`, `password`, admin, active, `email`)
                    Values ('$qvouch', '$muser', '$mpwd1', '1', '$active', '$memail')") 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 userid='$qvouch', username='$muser',  `password`='$mpwd1', email='$memail', admin='1', `active`='$active' 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="admin">
        <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><?= $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="">
                        <div class="form-horizontal">
                            <div class="form-group">
                                <label class="control-label col-lg-4"> User Name </label>
                                <div class="col-lg-8"> 
                                    <input type="text" name="user" autofocus="" placeholder="User Name" value="<?php echo ($addflag != 1) ? $get['username'] : ''; ?>" class="form-control form" style="height: 33px;" required="">
                                </div> 
                            </div> 
                            <div class="form-group">
                                <label class="control-label col-lg-4"> Password </label>
                                <div class="col-lg-8"> 
                                    <input type="password" name="pwd1" placeholder="Password" value="" class="form-control form" style="height: 33px;" required="">
                                </div> 
                            </div> 
                            <div class="form-group">
                                <label class="control-label col-lg-4">Confirm Password </label>
                                <div class="col-lg-8"> 
                                    <input type="password" name="pwd2" placeholder="Confirm Your Password" value="" class="form-control form" style="height: 33px;" 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'] : ''; ?>" class="form-control form" style="height: 33px;" required="">
                                </div> 
                            </div> 
                            <div class="form-group">
                                <label class="control-label col-lg-4"> Active </label>
                                <div class="col-lg-8"> 
                                    <input type="checkbox" name="active" class="form-control form" style="width:10%;" value="<?php echo ($addflag != 1) ? $get['active'] : "1"; ?>" <?php
                                    if ($addflag != 1) {
                                        if ($get["active"] == 1) {
                                            echo "checked";
                                        }
                                    }
                                    ?> checked/>
                                </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>