<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE);
?>


<?php
include "config/init.php";
include "template/header.php";
//Main Header
include "template/main-header.php";
//Left side column. contains the logo and sidebar -->
include "template/main-sidebar.php";

if($_SESSION['role']=="SYSTEMADMIN"){

 
if(isset($_POST['add-user'])){
    $username = html_filter($dbc,$_POST['username']);
    $password = $_POST['password'];
    $c_password = $_POST['c_password'];
    $userPIN = html_filter($dbc,$_POST['pin']);
    $fullName = html_filter($dbc,$_POST['fullName']);
    $phoneNum = html_filter($dbc,$_POST['phoneNum']);
    $emailadd = html_filter($dbc,$_POST['emailadd']);
    $role = html_filter($dbc,$_POST['role']);
    $status = html_filter($dbc,$_POST['status']);
    $country = "gh";

    if(check_if_username_exist($dbc,$username)){
        if($password == $c_password){
            $password = $mySalt.$password.$newSalt;
            $password = md5($password);
            if(addNewUser($dbc,$fullName,$username,$password,$role,$userPIN,$_SESSION['uid'],$country,$status,$phoneNum,$emailadd)){
                
                //if(activity_log($dbc,"A new user ({$fullName}) was added.","USER ACTIVITY")){
                    $result = "<p class='alert alert-success'>A new user was added</p>";
                //}
            } else{
                $result = "<p class='alert alert-danger' style='margin:15px'>Sorry, adding new user was unsuccessful. Contact Administrator</p>";
            }
        } else{
            $result = "<p class='alert alert-danger' style='margin:15px'>Passwords do not much</p>";
        }
    } else{
        $result = "<p class='alert alert-danger' style='margin:15px'>Username Already Exist</p>";
    }
}



if(isset($_POST['reset_password'])){
    
    $phoneNum = html_filter($dbc,$_POST['phoneNum']);
    $emailadd = html_filter($dbc,$_POST['emailadd']);
  
    if(check_email_and_phoneNum($dbc,$emailadd,$phoneNum)){
			$newPass = genSMSCode();
            $password = $mySalt.$newPass.$newSalt;
            $newPassword = md5($password);
            if(change_user_password($dbc,$newPassword,$emailadd,$phoneNum)){
                if(activity_log($dbc,"Password change executed for {$emailadd}","USER ACTIVITY")){
					sendSMS($phoneNum, "Please use {$newPass} as default password. Login and change accordingly");
                    $result = "<p class='alert alert-success'>Password change completed successfully.</p>";
                }
            } else{
                $result = "<p class='alert alert-danger'>Sorry, password change was unsuccessful. Contact Administrator</p>";
            }
        
    } else{
        $result = "<p class='alert alert-danger'>Wrong e-mail address or phone number</p>";
    }
}

if(isset($_GET['user'])){
 //$cus = get_user_by_id($dbc,$_GET['user']);
 change_user_status($dbc,$_GET['user']);
}


?>

<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
	<? echo "{$cus['phoneNum']} {$cus['emailadd']}";?>
        <h1>
            Back Office User Management
        </h1>
      
        <?php if(isset($result)){echo $result;}?>
    </section>

    <!-- Main content -->
    <section class="content">
        <div class="row">
            <div class="col-md-4">
                <div class="box box-primary">
                    <div class="box-header with-border">Add New User</div>
                    <div class="box-body">
                        <form role="form" method="post">
                            <div class="form-group">
                                <label for="username">Username</label>
                                <input type="text" class="form-control" name="username" required>
                            </div>
                            <div class="form-group">
                                <label for="password">Password</label>
                                <input type="password" class="form-control" name="password" required>
                            </div>
                            <div class="form-group">
                                <label for="c_password">Confirm Password</label>
                                <input type="password" class="form-control" name="c_password" required>
                            </div>
                            <div class="form-group">
                                <label for="pin">Reset PIN</label>
                                <input type="pin" class="form-control" name="pin" required>
                            </div>
                            <div class="form-group">
                                <label for="fullName"> Full Name</label>
                                <input type="text" class="form-control" name="fullName" required>
                            </div>
							
							<div class="form-group">
                                <label for="phoneNum"> Phone Number</label>
                                <input type="text" class="form-control" name="phoneNum" required>
                            </div>
							
							<div class="form-group">
                                <label for="emailadd"> E-mail Address</label>
                                <input type="text" class="form-control" name="emailadd" required>
                            </div>
							
                            <div class="form-group">
                                <label for="role">Role</label>
                                <select class="form-control staff_block" required name="role" id="role">
                                    <option value="">Select Role...</option>
                                    <option value="ADMINISTRATOR">ADMINISTRATOR</option>
                                    <option value="MANAGER">MANAGER</option>
                                    <option value="SALES">SALES</option>
                                    <option value="INVENTORY">INVENTORY</option>
                                    <option value="CASHIER">CASHIER</option>
                                   
                                </select>
                            </div>
                            <div class="form-group">
                                <label for="status">Status</label>
                                <select class="form-control" required name="status" id="status">
                                    <option value="">Select Status...</option>
                                    <option value="active">ACTIVE</option>
                                    <option value="inactive">INACTIVE</option>
                                </select>
                            </div>
							
                            <!--<div class="form-group">
                                <label for="country">Country</label>
                                <select class="form-control" required name="country" id="country">
                                    <option value="">Select country...</option>
                                    <?php
									/**
                                        $countries = get_system_country($dbc);
                                        foreach ($countries as $value ){
                                            echo"<option value='{$value['countryCode']}'>{$value['countryName']}</option>";
                                        }
										*/
                                    ?>
                                </select>
                            </div>-->
							
                            <div class="form-group">
                                <button type="submit" name="add-user" class="btn btn-primary">Submit</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
			
			<!-- Reset Password -->
				<div class="col-md-8">
                <div class="box box-primary">
                    <div class="box-header">Reset Password</div>
                    <div class="box-body">
                        <form method="post" role="form">
                            <div class="form-group">
                                <label for="phoneNum">Phone Number</label>
                                <input type="text" name="phoneNum" class="form-control" placeholder='+233243056828' id="phoneNum" required>
                            </div>
                            <div class="form-group">
                                <label for="emailadd">E-mail Adrress</label>
                                <input type="text" name="emailadd" class="form-control" placeholder='ceo@pscinternational.com'  id="emailadd" required>
                            </div>

                            <input type="submit" name="reset_password" value="Reset" class="btn btn-primary">
                        </form>
                    </div>
                </div>
                </div>
			
			
            <div class="col-md-8">
                <div class="panel panel-default">
                    <!-- List of All Payout -->
                    <div class="panel-heading">List of Back Office Users</div>
                    <div class="panel-body">
                        <table class="table table-striped" id="rcc_users">
                            <thead>
                            <tr>
                                <th>No.</th>
                                <!--<th>Country</th>-->
                                <th>Name</th>
                                <th>Username</th>
                                <th>Account Type</th>
                                <th>Added By</th>
                                <th>Status</th>
                                <th>Action</th>
                            </tr>
                            </thead>
                        </table>
                        <hr>
                    </div>
                </div>
            </div>
        </div>

    </section>
    <!-- /.content -->
</div>
<!-- /.content-wrapper -->

<?php 

} 

include "template/footer.php";
?>


