<?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'];

$edit_user = $_SESSION['edit_user'];

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

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

    $updated_user = ['id'=>$_REQUEST['user_id'],'login' => $login, 'password' => $password, 'full_name' => $full_name, 'phone' => $phone, 'email' => $email, 'user_class' => $type];

    $msg_response = $CI->User_model->update_app_user($updated_user);
    $msg = $msg_response['message'];
    if ($msg_response['data']) {
        $error = 'confirmation';
    } else {
        $error = 'error';
    }
    if($msg_response['code']=='00'){
    header("location: users?msg=" . $msg . "!&type=" . $error . "");
    }else{
        header("location: edit_user?msg=" . $msg . "!&type=" . $error . "");
    }
    exit();
}

?>
<script type="text/javascript">
    function validatePassword() {
        var pw = document.getElementById("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 = "";
        }

    }

    function verifyPassword() {
        var pw = document.getElementById("password").value;
        var cpw = document.getElementById("confirm_password").value;
        //minimum password length validation  
        if (pw != cpw) {
            document.getElementById("message").innerHTML = "**Password mismatch!";
            return false;
        } else {
            document.getElementById("message").innerHTML = "";
            document.getElementById("updated_password").setAttribute("value", pw);
        }

    }

    function selectType() {
        var pw = document.getElementById("type").value;
        document.getElementById("updated_type").setAttribute("value", pw);
    }
</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">Edit User - <?php echo $edit_user->full_name; ?></h1>
                                </div>
                                <form enctype="multipart/form-data" class="user" method="post" action="edit_user?user_id=<?php echo $_REQUEST['user_id']?>">
                                    <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">Login
                                        <input type="text" class="form-control form-control-user" name="login" id="login" placeholder="Login ID" value="<?php echo $edit_user->login; ?>" required>
                                    </div>
                                    <div class="form-group">Password
                                        <input type="password" class="form-control form-control-user" id="password" name="password" placeholder="Password" onchange="verifyPassword()">
                                    </div>
                                    <div class="form-group">Confirm Password
                                        <input type="password" class="form-control form-control-user" id="confirm_password" name="confirm_password" placeholder="Confirm Password" onchange="verifyPassword()">
                                    </div>
                                    <span id="message" style="color:red"> </span> <br>
                                    <hr>
                                    <div class="form-group">Full Name
                                        <input type="text" class="form-control form-control-user" name="full_name" id="full_name" placeholder="Full Name" value="<?php echo $edit_user->full_name; ?>" />
                                    </div>
                                    <div class="form-group">Phone
                                        <input type="text" class="form-control form-control-user" name="phone" id="phone" placeholder="Phone" value="<?php echo $edit_user->phone; ?>" />
                                    </div>
                                    <div class="form-group">Email
                                        <input type="email" class="form-control form-control-user" name="email" id="email" placeholder="Email" value="<?php echo $edit_user->email; ?>" />
                                    </div>
                                    <div class="form-group"> Current User Type
                                    <input type="text" class="form-control form-control-user" value="<?php echo $edit_user->user_class; ?>" disabled/>
                                    Select User Type
                                        <select class="form-control form-control-select" name="type" id="type" onchange="selectType()">
                                            <option value="">Select User Type...</option>
                                            <option value="ADMIN">ADMINISTRATOR</option>
                                            <option value="USER">BASIC USER</option>
                                        </select>
                                    </div>

                                    <input type="password" class="form-control form-control-user" name="updated_password" id="updated_password" value="<?php echo $edit_user->password; ?>" hidden />
                                    <input type="password" class="form-control form-control-user" name="updated_type" id="updated_type" value="<?php echo $edit_user->user_class; ?>" hidden />
                                    <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 -->