<?php 
define('DOCUMENT_ROOT_CLASS','');
define('DOCUMENT_ROOT_INC_FOLDER','');
require "./inc/header.php";
Session::CheckSession(); 
$msg = Session::get('msg');
Session::set("msg", NULL);

//$resultData = $commonFunctions->getData('tbl_users','');

if (isset($_GET['remove'])) {
  $remove = preg_replace('/[^a-zA-Z0-9-]/', '', (int)$_GET['remove']);
  $msg1 = $users->deleteUserById($remove);
}

/*if (isset($removeUser)) {
  echo $removeUser;
}*/
if (isset($_GET['deactive'])) {
  $deactive = preg_replace('/[^a-zA-Z0-9-]/', '', (int)$_GET['deactive']);
  $msg1 = $users->userDeactiveByAdmin($deactive);
}

/*if (isset($deactiveId)) {
  echo $deactiveId;
}*/
if (isset($_GET['active'])) {
  $active = preg_replace('/[^a-zA-Z0-9-]/', '', (int)$_GET['active']);
  $msg1 = $users->userActiveByAdmin($active);
}

/*if (isset($activeId)) {
  echo $activeId;
}*/

?>
    <!-- Page-Title -->
    <div class="row">
        <div class="col-sm-12">
            <div class="page-title-box">
                <div class="row">
                    <div class="col">
                        <h4 class="page-title">Manage Users</h4>
                        <ol class="breadcrumb">
                            <li class="breadcrumb-item"><a href="javascript:void(0);">Setting</a></li>
                            <li class="breadcrumb-item"><a href="javascript:void(0);">Manage Users</a></li>
                        </ol>
                    </div><!--end col-->
                    <div class="col-auto align-self-center">
                        <a href="#" class="btn btn-sm btn-outline-primary" id="Dash_Date">
                            <span class="day-name" id="Day_Name">Today:</span>&nbsp;
                            <span class="" id="Select_date">Jan 11</span>
                            <i data-feather="calendar" class="align-self-center icon-xs ml-1"></i>
                        </a>
                        <a href="#" class="btn btn-sm btn-outline-primary">
                            <i data-feather="download" class="align-self-center icon-xs"></i>
                        </a>
                    </div><!--end col-->  
                </div><!--end row-->                                                              
            </div><!--end page-title-box-->
        </div><!--end col-->
    </div><!--end row-->
    <!-- end page title end breadcrumb -->
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h4 class="card-title">Manage Users</h4>
                    <?php echo !empty($msg)?$msg:''; ?>        
                    <?php echo !empty($msg1)?$msg1:''; ?>
                </div><!--end card-header-->

                <div class="card-body">
                    <div class="row">
                        <div class="col-md-12">
                            <?php if(!empty($error_msg['status']) and $error_msg['status'] == "success"): ?>
                                <div class="alert alert-info alert-dismissible fade show" role="alert">
                                    <button type="button" class="close" aria-label="Close" data-dismiss="alert">
                                        <span aria-hidden="true">×</span>
                                    </button>
                                    <?php echo $error_msg['message']; ?>
                                    <script type="text/javascript">
                                        window.setTimeout(redirect, 900);
                                        function redirect(){
                                            window.location.href = 'role.php';
                                        }
                                    </script>
                                </div>
                            <?php endif; ?>

                            <?php if(!empty($error_msg['status']) and $error_msg['status'] == "error"): ?>
                                <div class="alert alert-danger alert-dismissible fade show" role="alert">
                                    <button type="button" class="close" aria-label="Close" data-dismiss="alert">
                                        <span aria-hidden="true">×</span>
                                    </button>
                                    <?php echo $error_msg['message']; ?>
                                </div>
                            <?php endif; ?>

                        </div>
                        <div class="col-md-12">
                            <!--div class="main-card mb-3 card">
                                <div class="card-body"-->
                                    <table style="width: 100%;" id="example" class="table table-hover table-striped table-bordered">
                                        <thead>
                                            <tr>
                                                <th  class="text-center">SL</th>
                                                <th  class="text-center">Name</th>
                                                <th  class="text-center">User Name</th>
                                                <th  class="text-center">eMail Address</th>
                                                <th  class="text-center">Mobile</th>
                                                <th  class="text-center">Status</th>
                                                <th  class="text-center">Last Login</th>
                                                <th  class="text-center">User Type</th>
                                                <!-- <th  width='25%' class="text-center">Action</th> -->
                                            </tr>
                                        </thead>
                                        <style type="text/css">
                                            tr {cursor: pointer;}
                                        </style>
                                        <tbody>
                                        <?php $allUser = $users->selectAllUserData();
                                            if ($allUser) {
                                                $i = 0;
                                                foreach ($allUser as  $value) {
                                                    $i++; ?>

                                                <tr class="text-center"
                                                <?php if (Session::get("id") == $value->id) {
                                                    echo "style='background:#d9edf7' ";
                                                } ?> onclick="redirect_('profile.php?id=<?php echo $value->id;?>')" >

                                                <td><?php echo $i; ?></td>
                                                <td><?php echo $value->name; ?></td>
                                                <td><?php echo $value->username; ?> <br>

                                                  <?php //echo "<span class='badge badge-lg badge-info text-white'>".$commonFunctions->getName($value->roleid,'sys_erp_adm_user_roles','role','id')."</span>"; ?>
                                                </td>
                                                <td><?php echo $value->email; ?></td>
                                                <td><span class="badge badge-lg badge-secondary text-white"><?php echo $value->mobile; ?></span></td>
                                                <td>
                                                    <?php if ($value->isActive == '0') { ?>
                                                        <span class="badge badge-lg badge-info text-white">Active</span>
                                                    <?php }else{ ?>
                                                        <span class="badge badge-lg badge-danger text-white">Deactive</span>
                                                    <?php } ?>
                                                </td>
                                                <td><span class="badge badge-lg badge-secondary text-white"><?php echo $users->formatDate($value->last_logged_date); ?></span></td>
                                                <td><?php echo "<span class='badge badge-lg badge-info text-white'>".$commonFunctions->getName($value->roleid,'sys_erp_adm_user_roles','role','id')."</span>"; ?>
                                                </td>
                                                <!-- <td>
                                                    <?php if ( Session::get("roleid") == '1') {?>
                                                        <a class="btn btn-success btn-sm
                                                    " href="profile.php?id=<?php echo $value->id;?>">View</a>
                                                        <a class="btn btn-info btn-sm " href="profile.php?id=<?php echo $value->id;?>">Edit</a>
                                                        <a onclick="return confirm('Are you sure To Delete ?')" class="btn btn-danger
                                                        <?php if (Session::get("id") == $value->id) {
                                                            echo "disabled";
                                                        } ?> btn-sm " href="?remove=<?php echo $value->id;?>">Remove</a>

                                                        <?php if ($value->isActive == '0') {  ?>
                                                            <a onclick="return confirm('Are you sure To Deactive ?')" class="btn btn-warning
                                                            <?php if (Session::get("id") == $value->id) {
                                                                echo "disabled";
                                                            } ?> btn-sm " href="?deactive=<?php echo $value->id;?>">Disable</a>
                                                        <?php } elseif($value->isActive == '1'){?>
                                                            <a onclick="return confirm('Are you sure To Active ?')" class="btn btn-secondary
                                                            <?php if (Session::get("id") == $value->id) {
                                                                echo "disabled";
                                                            } ?> btn-sm " href="?active=<?php echo $value->id;?>">Active</a>
                                                        <?php } ?>

                                                        <?php  }elseif(Session::get("id") == $value->id  && Session::get("roleid") == '2'){ ?>
                                                            <a class="btn btn-success btn-sm " href="profile.php?id=<?php echo $value->id;?>">View</a>
                                                            <a class="btn btn-info btn-sm " href="profile.php?id=<?php echo $value->id;?>">Edit</a>
                                                        <?php  }elseif( Session::get("roleid") == '2'){ ?>
                                                                <a class="btn btn-success btn-sm
                                                            <?php if ($value->roleid == '1') {
                                                                echo "disabled";
                                                            } ?> " href="profile.php?id=<?php echo $value->id;?>">View</a>
                                                            <a class="btn btn-info btn-sm
                                                            <?php if ($value->roleid == '1') {
                                                                echo "disabled";
                                                            } ?> " href="profile.php?id=<?php echo $value->id;?>">Edit</a>
                                                        <?php }elseif(Session::get("id") == $value->id  && Session::get("roleid") == '3'){ ?>
                                                            <a class="btn btn-success btn-sm " href="profile.php?id=<?php echo $value->id;?>">View</a>
                                                            <a class="btn btn-info btn-sm " href="profile.php?id=<?php echo $value->id;?>">Edit</a>
                                                        <?php }else{ ?>
                                                            <a class="btn btn-success btn-sm <?php if ($value->roleid == '1') {
                                                                echo "disabled";
                                                                } ?> " href="profile.php?id=<?php echo $value->id;?>">View</a>
                                                        <?php } ?>

                                                    </td>
                                                </tr> -->
                                            <?php }}else{ ?>
                                            <tr class="text-center">
                                                <td>No user availabe now !</td>
                                            </tr>
                                            <?php } ?>
                                        </tbody>
                                    </table>
                                <!--/div>
                            </div-->
                        </div>

                    </div>
                </div>
            </div>
        </div> <!-- end col -->
    </div> <!-- end row -->

<?php require "./inc/footer.php"; ?>
<script type="text/javascript">
    function redirect_(uri){
        window.location.href = uri;
    }
</script>