<style>
    .back-icon {

        color: rgb(120, 164, 128);
        /* green arrow */
        cursor: pointer;
        box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
        font-size: 14px;

    }
</style>
<script>
    $(document).on('click', '.back', function () {
        window.location.href = "index.php?r=site/_ipage";
    });
</script>
<?php
$cs = Yii::app()->getClientScript();
$baseUrl = Yii::app()->baseUrl;
$cs->registerScript("loadCountry", '$(document).ready(function() {
        var table_id ="#tbl_usergroup_details";
        var table = $(table_id).DataTable({
        scrollY         : "645px",
        // scrollX         : "true",
        // scrollCollapse  : true,
        pageLength      : 50,
        processing: true,
        order: [[0, "ASC"]],
        ajax: {
        url: "' . Yii::app()->createUrl('usergroup/GetUserGroupList') . '",
        type: "POST",
        cache : false,
        },
        serverSide:     true,
        fixedColumns:   true,
        processing: true,
        fixedHeader: {
        headerOffset: $("header.main").outerHeight()
        }
        } );
        $(".dataTables_scrollFoot").hide();
        });', CClientScript::POS_HEAD);
/* $cs->registerScript("deleteUserGroup",  "", CClientScript::POS_HEAD); */
?>
<script>
    $(document).ready(function () {
        var table_id = "#tbl_usergroup_details1";
        var table = $(table_id).DataTable({
            scrollY: "215px",
            pageLength: 50,
            processing: true,
            order: [[0, "ASC"]],
            ajax: {
                url: "<?php echo Yii::app()->createUrl('usergroup/GetUserGroupList') ?>",
                type: "POST",
                cache: false,
            },
            serverSide: true,
            fixedColumns: true,
            "fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
                AddContextMenu(nRow, aData);
            }



        });
    });
    function AddContextMenu(row, datas) {
        console.log(datas[2].isActive);
        

        $(row).contextMenu({
            selector: 'td',
            items: {
                <?php if (isset(Yii::app()->user->havePermissionOn['admin_usergroup_edit']) || Yii::app()->user->authorised_user == 1) { ?>
                                 "edit": {
                        name: "Edit",
                        icon: "edit",
                        accesskey: "e",
                        callback: function () {
                            loadPopupModal("usergroup/create_new&id=" + datas[2].id, datas[2].code, "xl");
                        }
                    },
                <?php } ?>
<?php //if (isset(Yii::app()->user->havePermissionOn['admin_usergroup_edit']) || Yii::app()->user->authorised_user == 1) { ?>
                 "copy": {
                    name: "Copy",
                    icon: "copy",
                    accesskey: "c",
                    callback: function () {
                        loadPopupModal("usergroup/CopyUsergroup&id=" + datas[2].id, datas[2].code, "xl");
                    }
                },
                <?php //} ?>
<?php if (isset(Yii::app()->user->haveHrPermissionOn['admin_usergroup_edit']) || Yii::app()->user->authorised_user == 1) { ?>
                                "HR": {
                        name: "HR Permission",
                        icon: "edit",
                        accesskey: "e",
                        callback: function () {
                            loadPopupModal("usergroup/hrcreate&id=" + datas[2].id, datas[2].code, "xl");
                        }
                    },
                <?php } ?>

<?php if (isset(Yii::app()->user->havePermissionOn['admin_usergroup_delete']) || Yii::app()->user->authorised_user == 1) { ?>
                                "delete": {



                        name: (datas[2].isActive == 'Y') ? "Disable" : "Enable",
                        icon: "edit",
                        accesskey: "d",
                        callback: function () {
                            disableUserGroup(datas[2].id, datas[2].isActive);
                        }
                    },
                <?php } ?>

            }
        });
    }
    function deleteUserGroup(id) {
        var r = confirm('Are you sure to delete this?');
        if (r == true) {
            jQuery.ajax({
                'type': 'post',
                'data': 'id=' + id,
                'url': $('#liveSite').val() + 'index.php?r=usergroup/DeleteUserGroup',
                'cache': false,
                'success': function (data) {
                    if (data == 'fail') {
                        alert('Error while Delete', 'Alert');
                    } else {
                        location.href = $('#liveSite').val() + 'index.php?r=usergroup/index';
                        notification('User Group Deleted SuccessFully!', 'success');
                    }
                }
            });
        }


    };

    function disableUserGroup(id, status) {
        
        let msg, title;

        if (status === 'Y') {
            msg = 'Are you sure to Disable this?';
            title = 'Disabled';
        } else if (status === 'N') {
            msg = 'Are you sure to Enable this?';
            title = 'Enabled';
        }

        if (confirm(msg)) {
            jQuery.ajax({
                type: 'post',
                data: {
                    id: id,
                    status: status
                },
                url: $('#liveSite').val() + 'index.php?r=usergroup/DisableUserGroup',
                cache: false,
                success: function (data) {
                    if (data === 'fail') {
                        alert('Error while deleting', 'Alert');
                    } else {
                        notification('User Group "' + title + '" updated successfully!', 'success');
                       location.reload(true);
                    }
                }
            });
        }
    }

</script>





<section class="panel" style="background-color:#F6F3EE;">
    <header class="panel-heading clearfix panel-theame">
        <!-- <i class="fa fa-users"></i> -->
        User Groups
        <div class="pull-right" style="display: flex; gap: 8px; align-items: center;">
            <?php if (isset(Yii::app()->user->havePermissionOn['admin_usergroup_add']) || Yii::app()->user->authorised_user == 1) { ?>
                <button class="btn btn-sm btn-theme" title="Back to Console"
                    onclick="window.location.href='index.php?r=site/_ipage';" style="display:none">
                    <i class="fa fa-arrow-left"></i>
                </button>
                <!--   <button class="btn btn-sm btn-theme" onclick="loadPopupModal('usergroup/create', 'Add User Group', 'lg')"><i class="fa fa-plus"></i> ADD</button>-->
                <a class="ad-btn" onclick="loadPopupModal('usergroup/create_new','Add User Group','xl')" title="Add">
                    <svg width="30" height="30" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"
                        class="pluss-ad">
                        <circle cx="12" cy="12" r="11" stroke="white" stroke-width="2" fill="none" />
                        <line x1="12" y1="7" x2="12" y2="17" stroke="#5db5a4" stroke-width="2" stroke-linecap="round" />
                        <line x1="7" y1="12" x2="17" y2="12" stroke="#5db5a4" stroke-width="2" stroke-linecap="round" />
                    </svg>
                </a>
            <?php } ?>
        </div>

    </header>
    <div class="panel-body">
        <div class="custom-data-table">
            <table class="table table-hover table-bordered personal-task" id="tbl_usergroup_details1"
                style="font-size:13px;">
                <thead>
                    <tr>
                        <th>Group Name</th>
                    </tr>
                </thead>

            </table>
        </div>
    </div>
</section>