<?php include 'controller/pageCode.php' ?>
<!DOCTYPE html>
<html lang="en">
 <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<head>
    <meta charset="utf-8">
    <title>Profile</title>
    <?php include 'inc/meta_inc.php'; ?>
    <!-- base css -->
    <?php include 'inc/styles_inc.php'; ?>
    <link rel="stylesheet" media="screen, print" href="assets/css/markdown.css">
    <link rel="stylesheet" media="screen, print" href="assets/css/datagrid/datatables/datatables.bundle.css">
    <style>
        .container {
            position: relative;
            width: 50%;
            max-width: 300px;
        }
        .image {
            display: block;
        }

        .overlay {
            position: absolute; 
            bottom: 0; 
            background: rgb(0, 0, 0);
            background: rgba(0, 0, 0, 0.5); /* Black see-through */
            color: #f1f1f1; 
            width: 80%;
            height:20px;
            transition: .5s ease;
            opacity:0.6;
            border-bottom-left-radius:10px;
            border-bottom-right-radius:10px;
            color: white;
            font-size: 8px;
            padding: 5px;
            text-align: center;
        }

        #picture_holder{
            cursor:pointer;
        }

        .container:hover .overlay {
            opacity: 1;
        }
    </style>
</head>
    <body class="mod-bg-1 ">
        <!-- DOC: script to save and load page settings -->
        <?php include 'inc/theme_script_inc.php'; ?>
        <!-- BEGIN Page Wrapper -->
        <div class="page-wrapper" id="app">
            <div class="page-inner">
                <!-- BEGIN Left Aside -->
        <?php include 'inc/aside_inc.php'; ?>
<!-- END Left Aside -->
                <div class="page-content-wrapper">
                    <!-- BEGIN Page Header -->
        <?php include 'inc/header_inc.php'; ?>
<!-- END Page Header -->
                    <!-- BEGIN Page Content -->
                    <!-- the #js-page-content id is needed for some plugins to initialize -->
                    <main id="js-page-content" role="main" class="page-content">
                        <ol class="breadcrumb page-breadcrumb">
                            <li class="position-absolute pos-top pos-right d-none d-sm-block"><span class="js-get-date"></span></li>
                        </ol>
                        <div class="row">
                            <div class="col-md-6">
                                <div id="panel-1" class="panel">
                                    <div class="panel-hdr">
                                        <h2>
                                            Extend/Limit User Permission <span class="fw-300"><i></i></span>
                                        </h2>
                                        <div class="panel-toolbar"></div>
                                    </div>
                                    <div class="panel-container show">
                                        <div class="panel-content">
                                <div class="card mb-g rounded-top">
                                    <div class="row no-gutters row-grid">
                                        <div class="col-12">
                                            <div class="p-4">
                                                <div class="form-group">
                                                    <label for="role">Select User</label>
                                                    <select class="select2 form-control" multiple name="employee[]" id="employee">
                                                    <?php if($list_of_employees):?>
                                                        <?php foreach($list_of_employees as $key):?>
                                                            <option value="<?php echo $key['employee_id'] ?>"><?php echo $key['employee_name'] ?></option>
                                                        <?php endforeach ?>
                                                    <?php endif ?>
                                                    </select>
                                                </div>
                                                <div class="form-group permission-group d-none">
                                                    <label for="system_permission">Select System permission</label>
                                                    <select class="select2 form-control" multiple name="permission[]" id="system_permission">
                                                    <option value="All">All</option>
                                                    <?php if($system_permissions):?>
                                                        <?php foreach($system_permissions as $key):?>
                                                            <option value="<?php echo $key['system_permission_id'] ?>"><?php echo $key['display_name'] ?></option>
                                                        <?php endforeach ?>
                                                    <?php endif ?>
                                                    </select>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="col-12">
                                            <div class="p-3 text-center buttons-group d-none">
                                            <button class="btn btn-xs btn-danger" id="remove_permission"><i class="fal fa-times"></i> Remove</button>
                                            <button class="btn btn-xs btn-warning" id="clear_permission"><i class="fal fa-times"> Limit </i></button>  
                                            <button class="btn btn-xs btn-info" id="extend_permission"><i class="fal fa-cogs"></i> Extend</button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-6">
                            <div id="panel-1" class="panel">
                                    <div class="panel-hdr">
                                        <h2>
                                            User Permissions <span class="fw-300"><i></i></span>
                                        </h2>
                                        <div class="panel-toolbar">
                                            <button class="btn btn-panel" data-action="panel-collapse" data-toggle="tooltip" data-offset="0,10" data-original-title="Collapse"></button>
                                            <button class="btn btn-panel" data-action="panel-fullscreen" data-toggle="tooltip" data-offset="0,10" data-original-title="Fullscreen"></button>
                                        </div>
                                    </div>
                                    <div class="panel-container show">
                                        <div class="panel-content">
                                <div class="card mb-g rounded-top">
                                    <div class="row no-gutters row-grid">
                                        <div class="p-4">
                                            <table id="dt-basic-example" class="table table-bordered table-hover table-striped w-100"></table>
                                            
                                        </div>
                                    </div>
                                </div>
                                        </div>
                                    </div>
                                </div>
                            
                            </div>
                        </div>
                    </main>
                    <!-- END Page Content -->
                    <!-- this overlay is activated only when mobile menu is triggered -->
<div class="page-content-overlay" data-action="toggle" data-class="mobile-nav-on"></div>
<!-- BEGIN Page Footer -->
<?php include 'inc/footer_inc.php' ?>
<!-- END Page Footer -->
<!-- BEGIN Shortcuts -->
<!-- modal shortcut -->
<?php //include 'inc/header_menu.php'; ?>
 <!-- END Shortcuts -->
                </div>
            </div>
        </div>
        <!-- END Page Wrapper -->
        <!-- BEGIN Quick Menu -->
<!-- to add more items, please make sure to change the variable '$menu-items: number;' in your _page-components-shortcut.scss -->
<nav class="shortcut-menu d-none d-sm-block">
    <input type="checkbox" class="menu-open" name="menu-open" id="menu_open" />
    <label for="menu_open" class="menu-open-button ">
        <span class="app-shortcut-icon d-block"></span>
    </label>
    <a href="#" class="menu-item btn" data-toggle="tooltip" data-placement="left" title="Scroll Top">
        <i class="fal fa-arrow-up"></i>
    </a>
    <a href="login/logout.php" class="menu-item btn" data-toggle="tooltip" data-placement="left" title="Logout">
        <i class="fal fa-sign-out"></i>
    </a>
    <a href="#" class="menu-item btn" data-action="app-fullscreen" data-toggle="tooltip" data-placement="left" title="Full Screen">
        <i class="fal fa-expand"></i>
    </a>
    <a href="#" class="menu-item btn" data-action="app-print" data-toggle="tooltip" data-placement="left" title="Print page">
        <i class="fal fa-print"></i>
    </a>
</nav>
<!-- END Quick Menu -->
<!-- BEGIN Page Settings -->
 <!-- END Page Settings -->
 <?php $employee_id = $_SESSION['employee_id'] ?>
<?php include 'inc/script_inc.php' ?>
        <script src="assets/js/datagrid/datatables/datatables.bundle.js"></script>
        <script>
            $(document).ready(function()
            {
                toastr.options = {
                    "closeButton": true,
                    "debug": false,
                    "newestOnTop": true,
                    "progressBar": true,
                    "positionClass": "toast-top-right",
                    "preventDuplicates": true,
                    "onclick": null,
                    "showDuration": 300,
                    "hideDuration": 5000,
                    "timeOut": 5000,
                    "extendedTimeOut": 1000,
                    "showEasing": "swing",
                    "hideEasing": "linear",
                    "showMethod": "fadeIn",
                    "hideMethod": "fadeOut"
                }
                let btn_template = `
                                    <div class="process">
                                        <span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
                                        <span class="sr-only"> ...</span>
                                    </div> `;
                $('#employee').on('change',function(){
                    let employee = $('#employee').val();
                    employee_ids = employee;
                    
                    if(employee_ids && employee_ids.length>0){
                        console.log(employee_ids.length)
                        $('.permission-group').removeClass('d-none');
                    }else{
                        $('.permission-group').addClass('d-none');
                        toastr.warning('Please Select a User     to Proceed!', 'Admin');
                        toastr.clear()
                    }
                });
                
                $('#system_permission').on('change',function(){
                    let system_permission = $('#system_permission').val();
                    permission_ids = system_permission;
                    if(permission_ids && permission_ids.length>0){
                        $('.buttons-group').removeClass('d-none');
                    }else{
                        $('.buttons-group').addClass('d-none');
                        toastr.warning('Please Select a Permission to Proceed!', 'Admin');
                        toastr.clear()
                    }
                });

                $('#extend_permission').click(function(){
                    var employees = $('#employee').val();
                    var system_permission = $('#system_permission').val();
                    var serverData = {
                        system_permission:system_permission,
                        employees:employees,
                        add_employee_permission:''
                    }

                    $.post('controller/userPermissionCode.php',serverData,function(serverResponse){
                        console.log(serverResponse)
                    if(serverResponse){
                        let response = JSON.parse(serverResponse);
                        if(response.data=="success"){
                            $('#dt-basic-example').DataTable().ajax.reload();
                            $('#extend_permission').text('Extend');
                            toastr.success('Permission Extended Successfully.', 'Admin');
                            toastr.clear()
                        }else if(response.data=="fail"){
                            $('#extend_permission').html('Extend');
                            toastr.warning('Action Failed!', 'Admin');
                            toastr.clear()
                        }else{
                            $('#extend_permission').html('Extend');
                            toastr.error('Internal Error!', 'Admin');
                            toastr.clear()
                        }
                    }
                    });
                });

                $('#clear_permission').click(function(){
                    var employees = $('#employee').val();
                    var system_permission = $('#system_permission').val();
                    var serverData = {
                        system_permission:system_permission,
                        employees:employees,
                        limit_employee_permission:''
                    }

                    $.post('controller/userPermissionCode.php',serverData,function(serverResponse){
                        console.log(serverResponse)
                    if(serverResponse){
                        let response = JSON.parse(serverResponse);
                        if(response.data=="success"){
                            $('#dt-basic-example').DataTable().ajax.reload();
                            $('#clear_permission').text('Limit');
                            toastr.success('Permission limited Successfully.', 'Admin');
                            toastr.clear()
                        }else if(response.data=="fail"){
                            $('#clear_permission').html('Limit');
                            toastr.warning('Action Failed!', 'Admin');
                            toastr.clear()
                        }else{
                            $('#clear_permission').html('Limit');
                            toastr.error('Internal Error!', 'Admin');
                            toastr.clear()
                        }
                    }
                    });
                });

                $('#remove_permission').click(function(){
                    var employees = $('#employee').val();
                    var system_permission = $('#system_permission').val();
                    var serverData = {
                        system_permission:system_permission,
                        employees:employees,
                        remove_employee_permission:''
                    }

                    $.post('controller/userPermissionCode.php',serverData,function(serverResponse){
                        console.log(serverResponse)
                    if(serverResponse){
                        let response = JSON.parse(serverResponse);
                        if(response.data=="success"){
                            $('#dt-basic-example').DataTable().ajax.reload();
                            $('#remove_permission').text('Remove');
                            toastr.success('Permission Removed Successfully.', 'Admin');
                            toastr.clear()
                        }else if(response.data=="fail"){
                            $('#remove_permission').html('Remove');
                            toastr.warning('Action Failed!', 'Admin');
                            toastr.clear()
                        }else{
                            $('#remove_permission').html('Remove');
                            toastr.error('Internal Error!', 'Admin');
                            toastr.clear()
                        }
                    }
                    });
                });

                
                
                // Event Lot
                var events = $("#app-eventlog");

                // Column Definitions
                var columnSet = [
                {
                    title: "RowId",
                    id: "user_permission_id",
                    data: "user_permission_id",
                    placeholderMsg: "Server Generated ID",
                    "visible": false,
                    "searchable": false,
                    type: "readonly"
                },
                {
                    title: "User",
                    id: "employee_name",
                    data: "employee_name",
                    type: "readonly"
                },
                {
                    title: "Extended/Limited Permission",
                    id: "display_name",
                    data: "display_name",
                    required: true, 
                    type: "readonly"
                },
                {
                    title: "State",
                    id: "state",
                    data: "state",
                    required: true, 
                    type: "readonly"
                }

                
            ]
                /* start data table */
                var myTable = $('#dt-basic-example').dataTable(
                {
                    /* check datatable buttons page for more info on how this DOM structure works */
                    dom: "<'row mb-3'<'col-sm-12 col-md-6 d-flex align-items-center justify-content-start'f><'col-sm-12 col-md-6 d-flex align-items-center justify-content-end'B>>" +
                        "<'row'<'col-sm-12'tr>>" +
                        "<'row'<'col-sm-12 col-md-5'i><'col-sm-12 col-md-7'p>>",
                    ajax: "controller/userPermissionCode.php?all_user_permissions",
                    columns: columnSet,
                    /* selecting multiple rows will not work */
                    select: 'single',
                    order:[[1,'asc']],
                    /* altEditor at work */
                    altEditor: true,
                    responsive: true,
                    buttons: [
                    {
                        text: '<i class="fal fa-sync mr-1"></i> Refresh',
                        name: 'refresh',
                        className: 'btn-xs btn-primary btn-sm'
                    }],
                    columnDefs: [
                        {
                            targets: 3,
                            render: function(data, type, full, meta)
                            {
                                if(data=="Extend"){
                                    data = `<span class="badge badge-info">${data}ed</span>`;
                                }else if(data=="Limit"){
                                    data = `<span class="badge badge-warning">${data}ed</span>`;
                                }else{
                                    data = `<span class="badge badge-dark">${data}</span>`;
                                }
                                return data;
                            },
                            
                        }
                    ],

                    /* default callback for insertion: mock webservice, always success */
                    onAddRow: function(dt, rowdata, success, error)
                    {
                        let process = `Please Wait
                            <div class="spinner-grow spinner-grow-sm text-light">
                            <span class="sr-only"> ...</span>
                        </div>
                        `;
                        $('#addRowBtn').html(process);
                        rowdata['add_employee_permission'] = '';
                        //success(rowdata);
                        $.post('controller/rolePermissionCode.php',rowdata,function(serverResponse){
                            console.log(serverResponse)
                            if(serverResponse){
                                let response = JSON.parse(serverResponse);
                                if(response.data=="success"){
                                    $('#addRowBtn').html("Add");
                                    $('#dt-basic-example').DataTable().ajax.reload();
                                    $('.modal').modal('hide');
                                    Swal.fire(
                                    {
                                        type: "success",
                                        title: "Group Created",
                                        showConfirmButton: false,
                                        timer: 2500
                                    });
                                }else if(response.data=="fail"){
                                    Swal.fire(
                                    {
                                        type: "error",
                                        title: "Action Failed",
                                        showConfirmButton: false,
                                        timer: 2500
                                    });
                                }else{
                                    Swal.fire(
                                    {
                                        type: "error",
                                        title: "Internal Error!",
                                        showConfirmButton: false,
                                        timer: 2500
                                    });
                                }
                            }
                        });
                        // demo only below:
                        events.prepend('<p class="text-success fw-500">' + JSON.stringify(rowdata, null, 4) + '</p>');
                    },
                    onEditRow: function(dt, rowdata, success, error)
                    {
                        
                        success(rowdata);
                        rowdata['delete_employee_permission'] = '';
                        //success(rowdata);
                        $.post('controller/rolePermissionCode.php',rowdata,function(serverResponse){
                            console.log(serverResponse)
                            if(serverResponse){
                                let response = JSON.parse(serverResponse);
                                if(response.data=="success"){
                                    Swal.fire(
                                    {
                                        type: "success",
                                        title: "Group Updated",
                                        showConfirmButton: false,
                                        timer: 2500
                                    });
                                }else if(response.data=="fail"){
                                    Swal.fire(
                                    {
                                        type: "error",
                                        title: "Action Failed",
                                        showConfirmButton: false,
                                        timer: 2500
                                    });
                                }else{
                                    Swal.fire(
                                    {
                                        type: "error",
                                        title: "Internal Error!",
                                        showConfirmButton: false,
                                        timer: 2500
                                    });
                                }
                            }
                        });
                        // demo only below:
                        events.prepend('<p class="text-info fw-500">' + JSON.stringify(rowdata, null, 4) + '</p>');
                    },
                    onDeleteRow: function(dt, rowdata, success, error)
                    {
                        
                        success(rowdata);
                        rowdata['remove_system_group'] = '';
                        //success(rowdata);
                        $.post('controller/groupCode.php',rowdata,function(serverResponse){
                            console.log(serverResponse)
                            if(serverResponse){
                                let response = JSON.parse(serverResponse);
                                if(response.data=="success"){
                                    Swal.fire(
                                    {
                                        type: "success",
                                        title: "Group Removed",
                                        showConfirmButton: false,
                                        timer: 2500
                                    });
                                }else if(response.data=="fail"){
                                    Swal.fire(
                                    {
                                        type: "error",
                                        title: "Action Failed",
                                        showConfirmButton: false,
                                        timer: 2500
                                    });
                                }else{
                                    Swal.fire(
                                    {
                                        type: "error",
                                        title: "Internal Error!",
                                        showConfirmButton: false,
                                        timer: 2500
                                    });
                                }
                            }
                        });

                        // demo only below:
                        events.prepend('<p class="text-danger fw-500">' + JSON.stringify(rowdata, null, 4) + '</p>');
                    },
                });


                $('.select2').select2();

                $(".select2-placeholder-multiple").select2(
                {
                    placeholder: "Select State"
                });
                $(".js-hide-search").select2(
                {
                    minimumResultsForSearch: 1 / 0
                });
                $(".js-max-length").select2(
                {
                    maximumSelectionLength: 2,
                    placeholder: "Select maximum 2 items"
                });
                $(".select2-placeholder").select2(
                {
                    placeholder: "Select a state",
                    allowClear: true
                });

                $(".js-select2-icons").select2(
                {
                    minimumResultsForSearch: 1 / 0,
                    templateResult: icon,
                    templateSelection: icon,
                    escapeMarkup: function(elm)
                    {
                        return elm
                    }
                });

                function icon(elm)
                {
                    elm.element;
                    return elm.id ? "<i class='" + $(elm.element).data("icon") + " mr-2'></i>" + elm.text : elm.text
                }

            });
            
        </script>
            <!-- Modal center Small -->
            
    </body>

</html>
