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

if (isset($_GET["mode"]) == "true") {
    $addflag = 1;
    $gen1 = mysqli_query($conn, "SELECT MAX(code) AS maxcode FROM passmain LIMIT 0,1 ") or die(mysqli_error($conn));

    if (mysqli_num_rows($gen1) > 0) {

        $vvouch = mysqli_fetch_assoc($gen1);
        $qvouch = $vvouch["maxcode"] + 1;
    } else {
        $qvouch = '1';
    }
} else {
    $addflag = 0;
    $qvouch = $_GET["vouchno"];
}

if (isset($_POST["submit"])) {

    $nuser = $_POST["user"];
    $npwd = $_POST["pwd1"];
    $nemail = $_POST["email"];
    $nactive = isset($_POST["active"]) ? $_POST["active"] : 0;
    $nadmin = isset($_POST["admin"]) ? $_POST["admin"] : 0;


    $user_check = $nuser;
    $r_1 = mysqli_query($conn, "SELECT * FROM `passmain` WHERE `user` = '$user_check' AND  code != '$qvouch'");
    $rows_1 = mysqli_num_rows($r_1);
    if ($rows_1 >= 1) {
        browse("User Name Already Exist");
        if ($addflag == 1) {
            header("refresh: 1; usernew.php?mode=true");
        } else {
            header("refresh: 1; usernew.php?vouchno=$qvouch");
        }
    } else { //assign email variable
        $e = $_POST['user'];
    }


    if (isset($e)) {
        if ($addflag == 1) {
            if ($_POST["pwd1"] == $_POST["pwd2"]) {
                mysqli_query($conn, "INSERT INTO `passmain`(code, `user`, `password`, active, `email`, admin)
            Values
                                                   ('$qvouch', '$nuser', '$npwd', '$nactive', '$nemail', '$nadmin')") or die(mysqli_error($conn));

                insmess();
                header('refresh: 1; user.php?mode=true');
            }
        } else {
            $mvouch = $_GET["vouchno"];
            mysqli_query($conn, "UPDATE `passmain` SET `user`='$nuser', `password`='$npwd', active='$nactive',
                         email='$nemail', admin='$nadmin' WHERE code='$mvouch'")or die(mysqli_error($conn));
            updmess();
            header("refresh: 1;usermain.php");
        }
    }
}

if ($addflag == 0) {
    $getvouch = $_GET["vouchno"];
    $result = mysqli_query($conn, "SELECT * FROM `passmain` WHERE code='$getvouch'")or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);
}
?>

<html lang="en-US">

    <head>
        <meta charset="UTF-8">
        <title>Change Password</title>
        <meta name="msapplication-TileColor" content="#5bc0de"/>
        <meta name="msapplication-TileImage" content="assets/img/metis-tile.png"/>

        <link rel="stylesheet" href="assets/lib/bootstrap/css/bootstrap.css">
        <link rel="stylesheet" href="assets/css/main.css"/>
        <link rel="stylesheet" href="assets/lib/Font-Awesome/css/font-awesome.css"/>

        <link rel="stylesheet" href="assets/css/theme.css">


        <link rel="stylesheet" href="assets/lib/uniform/themes/default/css/uniform.default.css">
        <link rel="stylesheet" href="assets/lib/inputlimiter/jquery.inputlimiter.1.0.css">
        <link rel="stylesheet" href="assets/lib/chosen/chosen.min.css">
        <link rel="stylesheet" href="assets/lib/colorpicker/css/colorpicker.css">
        <link rel="stylesheet" href="assets/lib/tagsinput/jquery.tagsinput.css">
        <link rel="stylesheet" href="assets/lib/daterangepicker/daterangepicker-bs3.css">
        <link rel="stylesheet" href="assets/lib/datepicker/css/datepicker.css">
        <link rel="stylesheet" href="assets/lib/timepicker/css/bootstrap-timepicker.min.css">
        <link rel="stylesheet" href="assets/lib/switch/static/stylesheets/bootstrap-switch.css">
        <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/flick/jquery-ui.css">


        <script>
            (function (i, s, o, g, r, a, m) {
                i['GoogleAnalyticsObject'] = r;
                i[r] = i[r] || function () {
                    (i[r].q = i[r].q || []).push(arguments)
                }, i[r].l = 1 * new Date();
                a = s.createElement(o),
                        m = s.getElementsByTagName(o)[0];
                a.async = 1;
                a.src = g;
                m.parentNode.insertBefore(a, m)
            })(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');

            ga('create', 'UA-1669764-16', 'onokumus.com');
            ga('send', 'pageview');

        </script>

        <script src="assets/lib/modernizr-build.min.js"></script>
        <script type="text/javascript" src="js/jquery.js"></script>
        <!--[if lt IE 9]>
                <script src="http://ie7-js.googlecode.com/svn/version/2.1(beta4)/IE9.js"></script>
                <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
        <script type="text/javascript">
            function checkPasswordMatch() {
                var password = $("#pwd1").val();
                var confirmPassword = $("#pwd2").val();

                if (password != confirmPassword)
                    $("#status").html('<font color="red"><strong>Passwords Does Not Match'),
                            $("#save").attr("disabled", "disabled");

                else
                    $("#status").html('<font color="green"><strong>Passwords Match'),
                            $("#save").removeAttr("disabled");

            }
        </script>
        <style>
            .check-with-label:checked + .label-for-check {
                color: #228B22;
            }
            .check-with-label2:checked + .label-for-check2 {
                color: #228B22;
            }


        </style>

    </head>
    <body onLoad="document.boq.vouchdt.focus();">
        <form action=''  name="customer" method="POST">
            <div class="row">
                <div class="col-lg-6" style='width:50%;float:center;'>
                    <div class="box dark">
                        <header>
                            <div class="icons"><i class="fa fa-edit"></i></div>
                            <h5>Create User</h5>
                            <!-- .toolbar -->
                            <div class="toolbar">
                                <ul class="nav">
                                    <li><input type='submit' name='submit' id='save' value='Save' class="btn btn-primary"></li>
                                    <li><input class="btn btn-danger" type="button" onClick="window.location.href = 'usermain.php'" value="Exit" /></li>
                                    <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">
                                <input type="hidden" size="6" maxlength="6" id="mvouch" maxlength="6" name="mvouch" value="<?php echo ($addflag == 0) ? $get['code'] : $qvouch; ?>" />      
                                <div class="form-group">
                                    <label class="control-label col-lg-4">User Name </label>
                                    <div class="col-lg-8"> 
                                        <input type="text" size="40" name="user" value="<?php echo ($addflag == 0) ? $get['user'] : ""; ?>" class="form-control" required /> 
                                    </div> 
                                </div> 

                                <div class="form-group">
                                    <label class="control-label col-lg-4">Password </label>
                                    <div class="col-lg-8"> 
                                        <input id="pwd1" type="password" size="40" name="pwd1" class="form-control" required />
                                    </div> 
                                </div> 

                                <div class="form-group">
                                    <label class="control-label col-lg-4">Confirm Password </label>
                                    <div class="col-lg-8"> 
                                        <input id="pwd2" type="password" size="40" name="pwd2" class="form-control" onkeyup="checkPasswordMatch();" required/><div id="status"></div>
                                    </div> 
                                </div> 

                                <div class="form-group">
                                    <label class="control-label col-lg-4">Email </label>
                                    <div class="col-lg-8"> 
                                        <input type="email" size="40" name="email" class="form-control" value="<?php echo ($addflag == 0) ? $get['email'] : ""; ?>" required/>
                                    </div> 
                                </div> 

                                <div class="form-group">
                                    <label class="control-label col-lg-4"></label>
                                    <div class="col-lg-8"> 
                                         <input type="checkbox" id="check" class="check-with-label" name='admin' value="1" <?php
                                        if ($addflag == 0) {
                                            if ($get["admin"] == 1)
                                                echo 'checked="checked""';
                                        }
                                        ?>/>
                                         <label for="check" class="label-for-check">Admin</label> &nbsp;&nbsp;
                                        <input type="checkbox" id="check2" class="check-with-label2" name='active' value="1" <?php
                                        if ($addflag == 0) {
                                            if ($get["active"] == 1)
                                                echo 'checked="checked""';
                                        }
                                        ?>/>
                                        <label for="check" class="label-for-check2">Active</label>

                                    </div> 
                                </div> 
                            </div> 
                        </div> 
                    </div> 


        </form>

   </body>
</html>
