<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

if (!isset($_SESSION['username'])) {
    header("location: /login");
}

$user = $_SESSION['user'];

$CI = &get_instance();
$CI->load->model('User_model');

if (isset($_POST['login'])) {
    $login = $_POST['login'];
    $password = $_POST['password'];
    $full_name = $_POST['full_name'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];
    $type = $_POST['type'];

    $msg_response = $CI->User_model->insert_app_user_admin($login, $password, $full_name, $phone, $email, $type);
    $msg = $msg_response['message'];
    if ($msg_response['data']) {
        $error = 'confirmation';
    } else {
        $error = 'error';
    }
    header("location: add_user?msg=" . $msg . "!&type=" . $error."");
    exit();
}

?>
<script type="text/javascript">
    function verifyPassword() {
        var pw = document.getElementById("password").value;
        var cpw = document.getElementById("confirm_password").value;
        //minimum password length validation  
        if (pw.length < 6) {
            document.getElementById("message").innerHTML = "**Password length must be atleast 8 characters";
            return false;
        } else {
            document.getElementById("message").innerHTML = "";
        }

        if (pw != cpw) {
            document.getElementById("message").innerHTML = "**Password mismatch!";
            return false;
        } else {
            document.getElementById("message").innerHTML = "";
        }

    }


    // function changeHandlerImg() {
    //     var fullPath = document.getElementById('img_url').value;
    //     if (fullPath) {
    //         var startIndex = (fullPath.indexOf('\\') >= 0 ? fullPath.lastIndexOf('\\') : fullPath.lastIndexOf('/'));
    //         var filename = fullPath.substring(startIndex);
    //         if (filename.indexOf('\\') === 0 || filename.indexOf('/') === 0) {
    //             filename = filename.substring(1);
    //         }
    //         document.getElementById('image_url').setAttribute("value", filename);
    //     }
    // }

    // function changeHandlerMedia() {
    //     var fullPath = document.getElementById('media_file_url').value;
    //     if (fullPath) {
    //         var startIndex = (fullPath.indexOf('\\') >= 0 ? fullPath.lastIndexOf('\\') : fullPath.lastIndexOf('/'));
    //         var filename = fullPath.substring(startIndex);
    //         if (filename.indexOf('\\') === 0 || filename.indexOf('/') === 0) {
    //             filename = filename.substring(1);
    //         }
    //         document.getElementById('media_url').setAttribute("value", filename);
    //     }
    // }
</script>

<!-- Begin Page Content -->
<div class="container-fluid">

    <div class="d-sm-flex align-items-center justify-content-between mb-4">
        <h1 class="h3 mb-0 text-gray-800">Users</h1>
        <a href="#" class="d-none d-sm-inline-block btn btn-sm btn-primary shadow-sm"><i class="fas fa-download fa-sm text-white-50"></i> Generate Report</a>
    </div>

    <!-- Content Row -->
    <div class="row">

        <!-- Earnings (Monthly) Card Example -->
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card border-left-primary shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-primary text-uppercase mb-1">
                                Total Users</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo $_SESSION['countUsers'] ?></div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-user fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Earnings (Monthly) Card Example -->
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card border-left-success shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                Sermons</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo $_SESSION['countSermon'] ?></div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-book fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Earnings (Monthly) Card Example -->
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card border-left-info shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                Categories</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo $_SESSION['countServices'] ?></div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-clipboard-list fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Pending Requests Card Example -->
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card border-left-warning shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-warning text-uppercase mb-1">
                                Last Login</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo $_SESSION['user_login'] ?></div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-clock fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-xl-5 col-lg-6 col-md-4">
            <div class="card o-hidden border-0 shadow-lg my-5">
                <div class="card-body p-0">
                    <!-- Nested Row within Card Body -->
                    <div class="row">
                        <div class="col-lg-12">
                            <div class="p-5">
                                <div class="text-center">

                                    <h1 class="h4 text-gray-900 mb-4">New User</h1>
                                </div>
                                <form enctype="multipart/form-data" class="user" method="post" action="">
                                    <p> <?php
                                        if (isset($_REQUEST['msg']) && isset($_REQUEST['type'])) {

                                            if ($_REQUEST['type'] == "error")
                                                $msg = "<div class='col-lg-6 mb-4'><div class='card bg-danger text-white shadow'><div class='card-body'>" . $_REQUEST['msg'] . "<div class='text-white-50 small'></div></div></div></div>";
                                            else if ($_REQUEST['type'] == "warning")
                                                $msg = "<div class='col-lg-6 mb-4'><div class='card bg-warning text-white shadow'><div class='card-body'>" . $_REQUEST['msg'] . "<div class='text-white-50 small'></div></div></div></div>";
                                            else if ($_REQUEST['type'] == "confirmation")
                                                $msg = "<div class='col-lg-6 mb-4'><div class='card bg-success text-white shadow'><div class='card-body'>" . $_REQUEST['msg'] . "<div class='text-white-50 small'></div></div></div></div>";
                                            else if ($_REQUEST['type'] == "information")
                                                $msg = "<div class='col-lg-6 mb-4'><div class='card bg-info text-white shadow'><div class='card-body'>" . $_REQUEST['msg'] . "<div class='text-white-50 small'></div></div></div></div>";

                                            echo $msg;
                                        }
                                        ?>
                                    </p>
                                    <div class="form-group">
                                        <input type="text" class="form-control form-control-user" name="login" id="login" placeholder="Login ID" required>
                                    </div>
                                    <div class="form-group">
                                        <input type="password" class="form-control form-control-user" id="password" name="password" placeholder="Password" onchange="verifyPassword()" required>
                                    </div>
                                    <div class="form-group">
                                        <input type="password" class="form-control form-control-user" id="confirm_password" name="confirm_password" placeholder="Confirm Password" onchange="verifyPassword()" required>
                                    </div>
                                    <span id="message" style="color:red"> </span> <br>
                                    <hr>
                                    <div class="form-group">
                                        <input type="text" class="form-control form-control-user" name="full_name" id="full_name" placeholder="Full Name" />
                                    </div>
                                    <div class="form-group">
                                        <input type="text" class="form-control form-control-user" name="phone" id="phone" placeholder="Phone" />
                                    </div>
                                    <div class="form-group">
                                        <input type="email" class="form-control form-control-user" name="email" id="email" placeholder="Email" />
                                    </div>
                                    <div class="form-group"> Select User Type
                                        <select class="form-control form-control-select" name="type" id="type" required>
                                            <option value="">Select User Type...</option>
                                            <option value="ADMIN">ADMINISTRATOR</option>
                                            <option value="USER">BASIC USER</option>
                                        </select>
                                    </div>
                                    <span id="type_message" style="color:red"> </span> <br>
                                    <hr>
                                    <button class="btn btn-primary btn-user btn-block" type="Submit">
                                        Submit
                                    </button>
                                    <button class="btn btn-primary btn-user btn-block" type="Reset">
                                        Reset
                                    </button>
                                </form>
                                <hr />
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

</div>
<!-- /.container-fluid -->