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

$resultData = $commonFunctions->getData('sys_erp_adm_user_roles',' WHERE isActive = 0 ');
$role_id = "";
if( !empty($_GET['role_id']) and $_GET['flag'] == 1){
    $editData = $commonFunctions->getRowData('sys_erp_adm_user_roles'," WHERE id = '".$_GET['role_id']."' ");
    $role_id = $_GET['role_id'];
}
if( !empty($_GET['role_id']) and $_GET['flag'] == 2){
    $commonFunctions->deleteRowData('sys_erp_adm_user_roles'," WHERE id = '".$_GET['role_id']."' ", " isActive='1' ",'role.php');
    exit();
}
if(!empty($_POST)){
    if(!empty($_POST['role'])){    
        if(!empty($_POST['role_id'])){
            $commonFunctions->updateData('sys_erp_adm_user_roles'," WHERE id = '".$_POST['role_id']."' ", " role='".$_POST['role']."' ",'role.php');
        }else{
            $commonFunctions->addData('sys_erp_adm_user_roles'," (role) VALUES ('".$_POST['role']."') ",'role.php');
        }
    }else{
        echo "<script>location.href='role.php';</script>";
        Session::set('msg', '<div class="alert alert-danger alert-dismissible mt-3" id="flash-msg">
        <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
        <strong>Error !</strong> Please enter all required data !</div>');
    }
}
?>

<!-- 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">Admin Settings</h4>
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="javascript:void(0);">Admin Settings</a></li>
                        <li class="breadcrumb-item"><a href="javascript:void(0);">Modules & Settings</a></li>
                        <li class="breadcrumb-item"><a href="javascript:void(0);">Role</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">Role</h4>
                <?php echo !empty($msg)?$msg:''; ?>        
            </div>

    
            <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-5">
                        <div class="main-card mb-3 card">
                            <div class="card-body">
                                <h5 class="card-title">Role Form</h5>
                                <form action="" method="post">
                                    <input type="hidden" name="role_id" value="<?php echo $role_id;?>">
                                    <div class="position-relative form-group">
                                        <label for="role" class="">Role</label>
                                        <input name="role" id="role" placeholder="Please enter role" type="text" class="form-control" value="<?php echo @$editData['role'];?>">
                                    </div>
                                    <button type="submit" class="mt-1 btn btn-primary">Submit</button>
                                </form>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-7">
                        <div class="main-card mb-3 card">
                            <div class="card-body">
                                <h5 class="card-title">Role List</h5>
                                <table style="width: 100%;" id="example" class="table table-hover table-striped table-bordered">
                                    <thead>
                                        <tr>
                                            <th>SR NO.</th>
                                            <th>Role Name</th>
                                            <th>Added Date</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php 
                                            $i=1;
                                            foreach ($resultData as $row) {
                                                echo "<tr>";
                                                echo "<td>".$i++."</td>";
                                                echo "<td>".$row['role']."</td>";
                                                echo "<td>".$row['created_date']."</td>";
                                                echo "<td><a class='btn btn-primary' href='role.php?flag=1&role_id=".$row['id']."'><i class='fa fa-pen'></i></a> <a class='btn btn-danger' href='role.php?flag=2&role_id=".$row['id']."'><i class='fa fa-trash '></i></a></td>";
                                                echo "</tr>";
                                            }
                                        ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>

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

<?php require "./inc/footer.php"; ?>