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

$formname = 'User';
$maindb = 'passmain';
$printfile = "";
$file = 'user2.php';
$trandbexist = 0;
$keyfld = "code";
$focusfield = "name";

$admin = $_SESSION["admin_" . $dbname];
$spectype = $_SESSION["spltype_" . $dbname];
//browse($spectype);
//$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;
    $user = $_POST["username"];
    $password = $_POST["pass"];

    if (isset($_POST["name"])) {
        $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `name`= '$name' AND code != '$qvouch' and `spltype`='$spectype'") or die(mysqli_error($conn));
        if (mysqli_num_rows($result) > 0) {
            $a = 0;
            browse("Name Already Exist");
            header("refresh: 1; $file?code=$qvouch&mode=last");
        }
    }

    if ($a == 1) {
        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX($keyfld) AS maxcode FROM `$maindb` LIMIT 0,1 ") or die(mysqli_error($conn));

            if (mysqli_num_rows($gen1) > 0) {
                $vvouch = mysqli_fetch_object($gen1);
                $qvouch = $vvouch["maxcode"] + 1;
            } else {
                $qvouch = '1';
            }
            $mvouch = $qvouch;
        }

        ############################## MAIN  ######################################################

        if ($addflag == 1) {

            mysqli_query($conn, "INSERT INTO `$maindb`(code, `user`, `password`)
                            VALUES($qvouch, '$user', '$password')") 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 `user`='$user', `password`='$password' WHERE code = '$mvouch' and `spltype`='$spectype' ") 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;
    }
    $myuser = $_SESSION["username_" . $dbname];
    $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE $keyfld='$getvouch' and `user`='$myuser' and `spltype`='$spectype'") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);

//    $getbrach = mysqli_query($conn, "SELECT * FROM `series` WHERE `code`='$get[branch]'") or die(mysqli_error($conn));
//    $br = mysqli_fetch_array($getbrach);
}

$senable = "";
$cenable = "";

/* DELETE */
if (isset($_GET["delete"])) {
    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
}
?>
<?php include("includes/linkshead.php"); ?>
<script>
    $(document).ready(function () {
        $(".confpass").keyup(function () {
            var pass = $(".password").val();
            var cpass = $(".confpass").val();
            if (pass !== cpass) {
                $("#alert").html("<b style='color:red;'>Password Doesn't Match</b>");
            } else {
                $("#alert").html("<b style='color:green;'>Password Matched</b>");
            }
        });
    });
</script>
</head>
<body>
    <form action='' method='POST'>
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-6">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title"><?php Echo $formname; ?></h3>
                        <div class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                            <i class="fa fa-times"></i>
                        </div>
                    </div>
                    <div class="panel-body">
                        <div class="row">
                            <label class="col-sm-3 control-label">User Name</label>
                            <div class="col-sm-6">
                                <input type="text" class="form-control form" name="username" placeholder="User Name" autocomplete="off" value='<? echo ($addflag != 1) ? $get['user'] : ''; ?>'>
                            </div>
                        </div>
                        <div class="row">
                            <label class="col-sm-3 control-label">Password</label>
                            <div class="col-sm-6">
                                <input type="password" class="form-control form password" name="pass" autocomplete="off" placeholder="Password" value='<? echo ($addflag != 1) ? $get['password'] : ''; ?>' required>
                            </div>
                        </div>
                        <div class="row">
                            <label class="col-sm-3 control-label">Confirm Pass</label>
                            <div class="col-sm-6">
                                <input type="password" class="form-control form confpass"  placeholder="Cofirm Password" value='<? echo ($addflag != 1) ? $get['password'] : ''; ?>'>
                            </div>
                        </div>
                        <div class="row">
                            <label class="col-sm-3 control-label"></label>
                            <div class="col-sm-6">
                                <p id="alert"></p>
                            </div>
                        </div>
                    </div>
                    <div class="panel-footer">
                        <table class="table-condensed" style="margin: 0 auto;">
                            <tr>
                                <td class=" buttonform tooltip-bottom" id="hedit" style="display: <? Echo $editdisabled ?>" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="Edit Record">
                                    <button id='edit' type="button" style="background-color:#F0AD4E; " class="btn btn-pad btn-square">
                                        <i class="fa fa-pencil" style="color:#ffffff"></i>
                                    </button>
                                </td>
                                <td class=" buttonform tooltip-bottom" id="ssubmit" style="display: <? Echo $savedisabled ?>" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="Save Record">
                                    <button id='submit' type='submit' name='submit' value='<?php echo ($addflag == 0) ? "Update" : "Save"; ?>' class="btn btn-pad btn-square " style="background-color:#008D4C; ">
                                        <i class="fa fa-check" style="color:#ffffff"></i>
                                    </button>
                                </td>
                                <td class=" buttonform tooltip-bottom" id="scancel" style="display: <? Echo $canceldisabled ?>" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="Cancel Record">
                                    <button id='cancel' type="button" class="btn btn-pad btn-square " style="background-color:#39CCCC; "  onClick="window.location.href = '<?= $file . "?mode=last&series=$zseries" ?>'">
                                        <i class="fa fa-times" style="color:#ffffff"></i>
                                    </button>
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </form>
    <div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" data-dismiss="modal"><span>&times;</span></button>
                    <h4 class="modal-title" id="myLargeModalLabel">Search <? Echo $formname; ?></h4>
                </div>
                <div class="modal-body">
                    <div class=" table-responsive">
                        <table id="example1" class="table table-striped">
                            <thead>
                                <tr style="background-color: #1ABC9C;">
                                    <th>Name</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                if ($addflag != 1) {
                                    $srchresult = mysqli_query($conn, "SELECT * FROM `$maindb`")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]\"><i class='fa fa-pencil' style='font-size:1.3em;color:#00C0EF;cursor:pointer;'></i></a></td>";
                                        echo "</tr>";
                                    }
                                }
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
