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

$formname = 'User Creation';
$maindb = 'passmain';

$printfile = "";
$file = 'user.php';
$trandbexist = 0;
$grid = '';
$keyfld = "code";
$admin = $_SESSION["admin_" . $dbname];
$username = $_SESSION["username_" . $dbname];
// $zdb = $_SESSION["zdbyear_" . $dbname];
// mysqli_select_db($conn, $zdb) or die(mysqli_error($conn));
browse(1);
include("includes/vouchno_generate.php");
include("includes/prev&next.php");
browse(1);
if (isset($_POST["submit"])) {

    $a = 1;
    $user = $_POST["user"];
    $password = $_POST["password"];
    $usertype = $_POST["usertype"];
    $dob = $_POST["dob"];
    $series = $_POST["series"];
    $count = $_POST["items"];
####################  MASTERS  ################################
    $godown = $_POST["godown"];
    $unit = $_POST["unit"];
    $item = $_POST["item"];
####################  TRANSACTIONS  ###########################
    $inward = $_POST["inward"];
    $outward = $_POST["outward"];
    $damaged = $_POST["damaged"];
    $transfer = $_POST["transfer"];
####################  Reports  ################################
    $stock = $_POST["stock"];
    $itemquery = $_POST["itemquery"];
    $prodhist = $_POST["prodhist"];
####################  REGISTERS  ################################
    $inwreg = $_POST["inwreg"];
    $outreg = $_POST["outreg"];
    $damreg = $_POST["damreg"];
    $intrnfreg = $_POST["intrnfreg"];
    $estreg = $_POST["estreg"];

############### IP Address #############################
    $ipadd = $_POST["ipadd"];
#########################################################

    if (isset($_POST["user"])) {
        $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `user`= '$user' AND code != '$qvouch'") 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_assoc($gen1);
                $qvouch = $vvouch["maxcode"] + 1;
            } else {
                $qvouch = '1';
            }
            $mvouch = $qvouch;
        }


        if ($addflag == 1) {

            $usecode = mysqli_query($conn, "SELECT code FROM `passmain` WHERE user='$username'") or die(mysqli_error($conn));
            $getuser = mysqli_fetch_object($usecode);
            $code = $getuser->code;

            mysqli_query($conn, "INSERT INTO `$maindb`(`code`, `user`, `password`, `usertype`, `godown`, `unit`, `item`,
                                                       `inward`, `outward`, `damaged`, `transfer`,
                                                       `stock`, `itemquery`, `prodhist`,
                                                       `inwreg`, `outreg`, `damreg`, `intrnfreg`, `estreg`)
                                                VALUES($qvouch, '$user', '$password', '$usertype', '$godown', '$unit', '$item',
                                                       '$inward', '$outward', '$damaged', '$transfer',
                                                       '$stock',  '$itemquery', '$prodhist',
                                                       '$inwreg',  '$outreg', '$damreg', '$intrnfreg', '$estreg')") or die(mysqli_error($conn));


            insmess();
            $mvouch = $qvouch;
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            $mvouch = $qvouch;

            $usecode = mysqli_query($conn, "SELECT code FROM `passmain` WHERE user='$username'") or die(mysqli_error($conn));
            $getuser = mysqli_fetch_object($usecode);
            $code = $getuser->code;

            mysqli_query($conn, "UPDATE `$maindb` SET `user`='$user', `password`='$password', `usertype`='$usertype',
                                           `godown`='$godown', `unit`='$unit', `item`='$item',
                                            `inward`='$inward', `outward`='$outward', `damaged`='$damaged', `transfer`='$transfer',
                                           `stock`='$stock', `itemquery`='$itemquery', `prodhist`='$prodhist',
                                           `inwreg`='$inwreg', `outreg`='$outreg', `damreg`='$damreg', `intrnfreg`='$intrnfreg', `estreg`='$estreg' WHERE `code` = '$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);
}

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

/* DELETE */
if (isset($_GET["delete"])) {
//     mysqli_query($conn, "DELETE FROM `$maindb` WHERE `code`='$qvouch'") or die(mysqli_error($conn));
//     header("refresh: 1; $file?mode=last");
    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last", $conn);
//    delete($zdb, "$trndb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
}
?>
<?php include("includes/linkshead.php"); ?>
<style>
    input[type=checkbox] {
        width: 20px;
        height: 20px;
    }
</style>
<script src="MyScript/user.js" type="text/javascript"></script>

<body>
    <!-- Default box -->
    <form action='' method='POST'>
        <!-- Default box -->
        <div class="">
            <div class="col-lg-6">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title"><?php echo $formname; ?></h3>
                    </div>
                    <div class="panel-body">
                        <div class="row">
                            <label class="col-sm-3 control-label marg">User Name</label>
                            <div class="col-sm-6">
                                <input type="text" class="form-control form" name="user" placeholder="User Name" autocomplete="off" value='<? echo ($addflag != 1) ? $get[user] : ''; ?>' required="" autofocus>
                            </div>
                        </div>
                        <div class="row">
                            <label class="col-sm-3 control-label marg">Password</label>
                            <div class="col-sm-6">
                                <input type="password" class="form-control form" name="password" id="password" autocomplete="off" placeholder="Password" value='<? echo ($addflag != 1) ? $get[password] : ''; ?>' required="">
                            </div>
                        </div>
                        <div class="row">
                            <label class="col-sm-3 control-label marg">Confirm Pass</label>
                            <div class="col-sm-6">
                                <input type="password" name="confpass" id="confpass" class="form-control form" placeholder="Confirm Password" required="" value='<? echo ($addflag != 1) ? $get[password] : ''; ?>'>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-sm-6">
                                <p id="alert"></p>
                            </div>
                        </div>

                        <div class="row pad">
                            <label class="col-sm-3 control-label marg">User Type</label>
                            <div class="col-sm-6">

                                <select class='form-control select2 form' name="usertype" id="usertype" required>
                                    <?php
                                    if ($addflag != 1) {
                                        $usertype = "";
                                        if ($get[usertype] == 1) {
                                            $ucode = "1";
                                            $usertype = "Supervisor";
                                        }
                                        if ($get[usertype] == 2) {
                                            $ucode = "2";
                                            $usertype = "Admin";
                                        }
                                        if ($get[usertype] == 3) {
                                            $ucode = "3";
                                            $usertype = "Staff";
                                        }
                                        echo "<option value='$ucode'>$usertype</option>";
                                    }
                                    ?>
                                    <option value="1">Supervisor</option>
                                    <option value="2">Admin</option>
                                    <option value="3">Staff</option>
                                </select>
                            </div>
                        </div>


                        <!--                        <div class="row pad">
                                                    <label class="col-sm-3 control-label marg">IP Address</label>
                                                    <div class="col-sm-4" style=""> 
                                                        <input type="text" class="form-control form  ipadd" id="ipadd" name="ipadd" value="<?php echo ($addflag != 1) ? $get["ipadd"] : ""; ?>">
                                                    </div>
                                                </div>-->
                        <hr>           

                        <br>
                        <div class="nav-tabs-custom">
                            <ul class="nav nav-tabs">
                                <li class="active"><a href="#tab_1" data-toggle="tab" aria-expanded="true">Masters</a></li>
                                <li class=""><a href="#tab_2" data-toggle="tab" aria-expanded="false">Transactions</a></li>
                                <li class=""><a href="#tab_3" data-toggle="tab" aria-expanded="false">Reports</a></li>
                                <li class=""><a href="#tab_4" data-toggle="tab" aria-expanded="false">Registers</a></li>

                            </ul>
                            <div class="tab-content">
                                <div class="tab-pane active" id="tab_1">
                                    <div class="row">
                                        <label class="col-sm-4 control-label marg selectall">Select All/UnSelect All</label>
                                        <div class="col-sm-4" style="margin-top:6px">
                                            <input type="checkbox" class="form " id="Smasters" value="1">
                                        </div>
                                    </div>
                                    <div class="row">
                                        <label class="col-sm-4 control-label marg">Godown</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  masters" id="godown" name="godown" value="1" <?
                                            if ($get[godown] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                        <label class="col-sm-4 control-label marg">Unit</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  masters" id="unit" name="unit" value="1" <?
                                            if ($get[unit] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <label class="col-sm-4 control-label marg">Item</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  masters" id="item" name="item" value="1" <?
                                            if ($get[item] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                    </div>
                                </div><!-- /.tab-pane -->
                                <div class="tab-pane" id="tab_2">
                                    <div class="row">
                                        <label class="col-sm-4 control-label marg selectall">Select All/UnSelect All</label>
                                        <div class="col-sm-4" style="margin-top:6px">
                                            <input type="checkbox" class="form " id="Stransactions" value="1">
                                        </div>
                                    </div>
                                    <div class="row">
                                        <label class="col-sm-4 control-label marg">Inward</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  transactions" id="inward" name="inward" value="1" <?
                                            if ($get[inward] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                        <label class="col-sm-4 control-label marg">Outward</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  transactions" id="outward" name="outward" value="1" <?
                                            if ($get[outward] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <label class="col-sm-4 control-label marg">Damaged</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  transactions" id="damaged" name="damaged" value="1" <?
                                            if ($get[damaged] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                        <label class="col-sm-4 control-label marg">Internal Transfer</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  transactions" id="transfer" name="transfer" value="1" <?
                                            if ($get[transfer] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <label class="col-sm-4 control-label marg">Estimate Register</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  transactions" id="estreg" name="estreg" value="1" <?
                                            if ($get[estreg] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                    </div>

                                </div><!-- /.tab-pane -->

                                <div class="tab-pane" id="tab_3">
                                    <div class="row">
                                        <label class="col-sm-4 control-label marg selectall">Select All/UnSelect All</label>
                                        <div class="col-sm-4" style="margin-top:6px">
                                            <input type="checkbox" class="form " id="Sreports" value="1">
                                        </div>
                                    </div>
                                    <div class="row">
                                        <label class="col-sm-4 control-label marg">Stock</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  reports" id="stock" name="stock" value="1" <?
                                            if ($get[stock] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                        <label class="col-sm-4 control-label marg">Item Query</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  reports" id="itemquery" name="itemquery" value="1" <?
                                            if ($get[itemquery] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <label class="col-sm-4 control-label marg">Product History</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  reports" id="prodhist" name="prodhist" value="1" <?
                                            if ($get[prodhist] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                    </div>
                                </div>

                                <div class="tab-pane" id="tab_4">
                                    <div class="row">
                                        <label class="col-sm-4 control-label marg selectall">Select All/UnSelect All</label>
                                        <div class="col-sm-4" style="margin-top:6px">
                                            <input type="checkbox" class="form " id="Sregisters" value="1">
                                        </div>
                                    </div>
                                    <div class="row">
                                        <label class="col-sm-4 control-label marg">Inward Register</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  registers" id="inwreg" name="inwreg" value="1" <?
                                            if ($get[inwreg] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                        <label class="col-sm-4 control-label marg">Outward Register</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  registers" id="outreg" name="outreg" value="1" <?
                                            if ($get[outreg] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <label class="col-sm-4 control-label marg">Internal Transfer Register</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  registers" id="intrnfreg" name="intrnfreg" value="1" <?
                                            if ($get[intrnfreg] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                    </div>
                                </div><!-- /.tab-pane-->
                            </div><!-- /.tab-content -->
                        </div>
                    </div>
                </div>
                <div class="panel-footer">
                    <?php include("includes/buttons.php"); ?>
                </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 class="mySearchTr">
                                    <th>code</th>
                                    <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[code]</td>";
                                        echo "<td align='left'>$row[user]</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>
</body>
</html>


<?php include("includes/linksfoot.php"); ?>