<?php include('../config.php');
include('inc.session-create.php');
$Pagefullname = "Manage Users";
$FileName = 'userlogin.php';
$validate = new Validation();
if ($_SESSION['SUPER_ADMIN']['usertype'] == '1') {
    redirect('panel_login.php');
    exit;
}
if ($_SESSION['success'] != "") {
    $stat['success'] = $_SESSION['success'];
    unset($_SESSION['success']);
}
if (isset($_POST['addnewrecord'])) {
    $validate->addRule($_POST['emailid'], 'email', 'emailid', true);
    $validate->addRule($_POST['password'], '', 'Password', true, 6, 14);
    if ($validate->validate() && count($stat) == 0) {
        if (isset($_FILES["emailid"]["name"]) && !empty($_FILES["emailid"]["name"])) {
            $filename = basename($_FILES['emailid']['name']);
            $ext1 = strtolower(substr($filename, strrpos($filename, '.') + 1));
            if (in_array($ext1, array('jpg', 'JPG', 'JPEG', 'jpeg', 'png', 'PNG', 'gif', 'svg', 'webp'))) {
                $newfile = date("d-m-Y-h-i-s") . rand(1000, 9999) . "." . $ext1;
                move_uploaded_file($_FILES['emailid']['tmp_name'], "../uploads/" . $newfile);
            }
        }
        if (isset($_FILES["emailid_one"]["name"]) && !empty($_FILES["emailid_one"]["name"])) {
            $filename = basename($_FILES['emailid_one']['name']);
            $ext1 = strtolower(substr($filename, strrpos($filename, '.') + 1));
            if (in_array($ext1, array('jpg', 'JPG', 'JPEG', 'jpeg', 'png', 'PNG', 'gif', 'svg', 'webp'))) {
                $newfile1 = date("d-m-Y-h-i-s") . rand(1000, 9999) . "." . $ext1;
                move_uploaded_file($_FILES['emailid_one']['tmp_name'], "../uploads/" . $newfile1);
            }
        }
        $aryData = array(
            'token'                     =>     token('admin_login'),
            'fullname'                     =>     $_POST['fullname'],
            'emailid'                  =>     $_POST['emailid'],
            'password'                  => md5($_POST['password']),
            'distict_name'                  =>     $_POST['distict_name'],
            'usertype'                  =>     $_POST['usertype'],
            'status'                    =>     $_POST['status'],
        );
        $flgIn1 = $db->insertAry("admin_login", $aryData);
        //echo $db->getLastQuery();
        exit;
        $_SESSION['success'] = "Submited Successfully";
        redirect($FileName);
        unset($_POST);
    } else {
        $stat['error'] = $validate->errors();
    }
} elseif (isset($_POST['updaterecord'])) {
    $validate->addRule($_POST['fullname'], '', 'fullname', true);
    if ($validate->validate() && count($stat) == 0) {
        if (isset($_FILES["image"]["name"]) && !empty($_FILES["image"]["name"])) {
            $filename = basename($_FILES['image']['name']);
            $ext1 = strtolower(substr($filename, strrpos($filename, '.') + 1));
            if (in_array($ext1, array('jpg', 'JPG', 'JPEG', 'jpeg', 'png', 'PNG', 'gif', 'svg', 'webp'))) {
                $newfile = date("d-m-Y-h-i-s") . rand(1000, 9999) . "." . $ext1;
                move_uploaded_file($_FILES['image']['tmp_name'], "../uploads/" . $newfile);
            }
        } else {
            $newfile = $_POST['image_old'];
        }

        $aryData = array(
            'fullname'                     =>     $_POST['fullname'],
            'emailid'                  =>     $_POST['emailid'],
            'password'                  => md5($_POST['password']),
            'distict_name'                  =>     $_POST['distict_name'],
            'usertype'                  =>     $_POST['usertype'],
            'status'                    =>     $_POST['status'],
        );
        $flgIn = $db->updateAry("admin_login", $aryData, "where id='" . $_GET['id'] . "' ");
        //echo $db->getLastQuery();	exit;
        $_SESSION['success'] = "Update Successfully";
        unset($_POST);
        redirect($FileName);
    } else {
        $stat['error'] = $validate->errors();
    }
} elseif (($_REQUEST['action'] == 'delete')) {
    $aryData = array(
        'status'                     => 2,
    );
    $flgIn = $db->updateAry("admin_login", $aryData, "where id='" . $_GET['id'] . "' ");
    $_SESSION['success'] = 'Deleted Successfully';
    redirect($FileName);
}
?>
<!DOCTYPE html>
<html>

<head>
    <?php include('inc.meta.php'); ?>
</head>

<body class="fixed-left">
    <div id="wrapper">
        <?php include('inc.header.php'); ?>
        <?php include('inc.sideleft.php'); ?>
        <div class="content-page">
            <div class="content">
                <div class="container">
                    <div class="row">
                        <div class="col-sm-12">
                            <h4 class="page-fullname"><?php echo $Pagefullname; ?></h4>
                            <ol class="breadcrumb">
                                <li> <a href="<?php echo $iClassName; ?>">Home</a> </li>
                                <li class="active"> <?php echo $Pagefullname; ?> </li>
                            </ol>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12">
                            <div class="card-box">
                                <div class="row">
                                    <div class="col-md-9"> <?php echo msg($stat); ?> </div>
                                    <div class="col-md-3"> <a href="<?php echo $FileName; ?>?action=add" class="btn btn-default" style="float:right">Add New Record</a> </div>
                                </div>
                            </div>
                            <?php if ($_GET['action'] == 'add') { ?>
                                <div class="card-box">
                                    <form role="form" action="" method="post" enctype="multipart/form-data">
                                        <div class="row">
                                            <div class="col-md-6">
                                                <div class="form-group clearfix">
                                                    <label class="col-md-12 control-label">fullname</label>
                                                    <div class="col-md-12">
                                                        <input class="required form-control" type="text" name="fullname" value="<?php echo $_POST['fullname']; ?>">
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="col-md-6">
                                                <div class="form-group clearfix">
                                                    <label class="col-md-12 control-label">Email ID</label>
                                                    <div class="col-md-12">
                                                        <input class="required form-control" type="text" name="emailid" value="<?php echo $_POST['emailid']; ?>">
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="col-md-6">
                                                <div class="form-group clearfix">
                                                    <label class="col-md-12 control-label">Password</label>
                                                    <div class="col-md-12">
                                                        <input class="required form-control" type="text" name="password" value="<?php echo $_POST['password']; ?>">
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="col-md-6">
                                                <div class="form-group clearfix">
                                                    <label class="col-md-12 control-label">District Name</label>
                                                    <div class="col-md-12">
                                                        <select name="distict_name" id="distict_name" class="selects">
                                                            <option value="0">Select District</option>
                                                            <?php $aryList = $db->getRows("select * from district  order by id asc");
                                                            foreach ($aryList as $iList) { ?>
                                                                <option value="<?php echo $iList['distict_name']; ?>" name="distict_name" required><?php echo $iList['distict_name']; ?></option>
                                                            <?php } ?>
                                                        </select>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="col-md-6">
                                            <div class="form-group clearfix">
                                                <label class="col-md-12 control-label">User Type</label>
                                                <div class="col-md-12">
                                                    <select class="required form-control" name="usertype">
                                                        <option value="1" <?php if ($_POST['usertype'] == '0') {
                                                                                echo "selected";
                                                                            } ?>>Main Admin</option>
                                                        <option value="0" <?php if ($_POST['usertype'] == '1') {
                                                                                echo "selected";
                                                                            } ?>>User</option>
                                                    </select>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="col-md-6">
                                            <div class="form-group clearfix">
                                                <label class="col-md-12 control-label">Status</label>
                                                <div class="col-md-12">
                                                    <select class="required form-control" name="status">
                                                        <option value="1" <?php if ($_POST['status'] == '1') {
                                                                                echo "selected";
                                                                            } ?>>Active</option>
                                                        <option value="0" <?php if ($_POST['status'] == '0') {
                                                                                echo "selected";
                                                                            } ?>>Inactive</option>
                                                    </select>
                                                </div>
                                            </div>
                                        </div>

                                        <div class="row footsect">
                                            <div class="col-md-12">
                                                <button type="submit" name="addnewrecord" class="btn btn-default">Submit</button>
                                                <a href="<?php echo $FileName; ?>" class="btn btn-default">Back</a>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                        </div>
                    <?php } elseif ($_GET['action'] == 'edit') {
                                $iSingleRec = $db->getRow("select * from  admin_login where id='" . $_GET['id'] . "'");
                    ?>
                        <div class="card-box">
                            <form role="form" action="" method="post" enctype="multipart/form-data">
                                <div class="row">

                                    <div class="col-md-6">
                                        <div class="form-group clearfix">
                                            <label class="col-md-12 control-label">fullname</label>
                                            <div class="col-md-12">
                                                <input class="required form-control" type="text" name="fullname" value="<?php echo $iSingleRec['fullname']; ?>">
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group clearfix">
                                            <label class="col-md-12 control-label">Email ID</label>
                                            <div class="col-md-12">
                                                <input class="required form-control" type="text" name="emailid" value="<?php echo $iSingleRec['emailid']; ?>">
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group clearfix">
                                            <label class="col-md-12 control-label">Password</label>
                                            <div class="col-md-12">
                                                <input class="required form-control" type="text" name="password" value="<?php echo $iSingleRec['password']; ?>">
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-lg-12 col-md-12 col-sm-12">
                                        <div class="col-lg-4 col-md-12 col-sm-12">
                                            <div class="dropdown" style="background-color:#fff;margin-top:10px;padding:15px;border-radius:10px">
                                                <lable style="font-weight:700">जिला/ District</lable><br>
                                                <select name="distict_name" id="distict_name" class="selects">
                                                    <option value="0">Select District</option>
                                                    <?php $aryList = $db->getRows("select * from district  order by id asc");
                                                    foreach ($aryList as $iSingleRec) { ?>
                                                        <option value="<?php echo $iSingleRec['distict_name']; ?>" name="distict_name" required><?php echo $iSingleRec['distict_name']; ?></option>
                                                    <?php } ?>
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group clearfix">
                                            <label class="col-md-12 control-label">User Type</label>
                                            <div class="col-md-12">
                                                <select class="required form-control" name="usertype">
                                                    <option value="0" <?php if ($iSingleRec['usertype'] == '0') {
                                                                            echo "selected";
                                                                        } ?>>Main Admin</option>
                                                    <option value="1" <?php if ($iSingleRec['usertype'] == '1') {
                                                                            echo "selected";
                                                                        } ?>>User</option>
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group clearfix">
                                            <label class="col-md-12 control-label">Status</label>
                                            <div class="col-md-12">
                                                <select class="required form-control" name="status">
                                                    <option value="0" <?php if ($iSingleRec['status'] == '0') {
                                                                            echo "selected";
                                                                        } ?>>Pending</option>
                                                    <option value="1" <?php if ($iSingleRec['status'] == '1') {
                                                                            echo "selected";
                                                                        } ?>>Active</option>
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <div class="row footsect">
                                    <div class="col-md-12">
                                        <button type="submit" name="updaterecord" class="btn btn-default">Submit</button>
                                        <a href="<?php echo $FileName; ?>" class="btn btn-default">Back</a>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>

                <?php  } else { ?>
                    <div class="card-box">
                        <table id="datatable" class="table table-striped table-bordered">
                            <thead>
                                <tr>
                                    <th>#</th>
                                    <th>fullname</th>
                                    <th>Email ID</th>
                                    <th>Password</th>
                                    <th>District Name</th>
                                    <th>User Type</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $i = 0;
                                $aryList = $db->getRows("select * from admin_login order by id desc");
                                foreach ($aryList as $iList) {
                                    $districtname = $db->getRow("select * from district where id='" . $iList['distict_name'] . "'");
                                    $i = $i + 1;  ?>
                                    <tr>
                                        <td><?php echo $i ?></td>
                                        <td><?php echo $iList['fullname']; ?></td>
                                        <td><?php echo $iList['emailid']; ?></td>
                                        <td><?php echo $iList['password']; ?></td>
                                        <td><?php echo $iList['distict_name']; ?></td>
                                        <td><?php echo $iList['usertype']; ?></td>
                                        <td><a href="<?php echo $FileName; ?>?action=edit&id=<?php echo $iList['id']; ?>" class="btn-view"><i class="ti-pencil-alt"></i> Edit</a> <a href="javascript:del('<?php echo $FileName; ?>?action=delete&id=<?php echo $iList['id']; ?>')" class="btn-view"><i class="ti-trash"></i> Delete</a></td>
                                    </tr>
                                <?php } ?>
                            </tbody>
                        </table>
                    </div>
                <?php } ?>
                </div>
            </div>
        </div>
    </div>
    <?php include('inc.footer.php'); ?>
    </div>
    </div>
    <?php include('inc.js.php'); ?>
</body>

</html>