<style>
.back-icon {
   
    color: rgb(120, 164, 128);            /* green arrow */
    /* padding: 6px 10px; */
    /* border-radius: 4px; */
    cursor: pointer;
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
    font-size: 13px;
}
</style>
<script>
$(document).on('click', '.back', function () {
    window.location.href = "index.php?r=site/_ipage";
});
</script>
<?php

$cs = Yii::app()->getClientScript();
$cs->registerScript("deleteUsers",  "function deleteUsers(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=users/DeleteUsers',
              'cache': false,
              'success':function(data){
                if(data=='fail'){
                    t8.error('Error while Delete');
                }else {
                    location.href=$('#liveSite').val()+'index.php?r=users/index';
       
                    t8.success('Users deleted SuccessFully');
                }
               }
            });
        }

   
};", CClientScript::POS_HEAD);
$cs->registerScript("enableDisableUser",  "function enableDisableUser(id,type){
    
    if(type=='disabled'){
        var display_type = 'Block';
    }
    else{
        var display_type = 'Un Block';
    }
    var r = confirm('Are you sure you want to '+display_type+' this User?');
        if(r==true)
        {
            jQuery.ajax({
                'type'  : 'post',
                'dataType'  : 'json',
                'data'  : 'id=' + id+'&type='+type,
                'url'  : $('#liveSite').val()+'index.php?r=users/blockuser',
                'cache': false,
                'success':function(data){
                    if(data.status=='success'){
                        reloadTable('#tbl_user_details');
                        t8.success('User Blocked SuccessFully!');

                    }else {
                        t8.error('This User cant be Blocked');
                    }
                 }
            });
        }
};", CClientScript::POS_END);

/*$cs->registerScript("loadUsers", '
        $(document).ready(function() {

        var table_id ="#tbl_user_details";
        var table = $(table_id).DataTable({
            scrollY         : "380px",
           // scrollX         : "true",
           // scrollCollapse  : true,
            pageLength      : 50,
			processing: true,
            ajax: {
                url: "' . Yii::app()->createUrl('users/GetList') . '",
                type: "POST",
                cache : false,
            },
            serverSide:     true,
            fixedColumns:   true,
			processing: true,
            fixedHeader: {
                headerOffset: $("header.main").outerHeight()
            }
        } );
        $(".dataTables_scrollFoot").hide();
    });', CClientScript::POS_HEAD);*/
    ?>
<script>
    $(document).ready(function () {
        var table_id = "#tbl_user_details";
        var table = $(table_id).on("preXhr.dt", function (e, settings, data) {
            data.display_type = $("#display_type").val();
            data.warehouse_id = $("#warehouse_id").val();
            data.level = $("#level").val();
            return data;
        }).DataTable({
            scrollY: "200px",
            pageLength: 50,
            order: [[0, "ASC"]],
            
            processing: true,
            ajax: {
                url: "<?php echo Yii::app()->createUrl('users/GetList') ?>",
                type: "POST",
                cache: false,
            },
             "columnDefs": [{
            "targets": [1,4,5],
                    "visible": false,
            } ],
            serverSide: true,
            fixedColumns: true,
            processing: true,
            "fnCreatedRow": function (nRow, aData, iDataIndex) {
                $(nRow).addClass("context-menu");
            },
            "fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
              if (nRow[8] && nRow[8].active === "N") {
                    $("td", nRow).css("background-color", "#FACAC4"); //expired
                    //$("td", row).css("background-color", "#c8c8c8");

                }
                AddContextMenuUsers(nRow, aData);
            }
        });
    });
   function AddContextMenuUsers(row, datas) {
    const menuItems = {};

    <?php if (isset(Yii::app()->user->havePermissionOn['admin_users_edit']) || Yii::app()->user->authorised_user == 1): ?>
    menuItems.edit = {
        name: "Edit",
        icon: "edit",
        accesskey: "e",
        disabled: function (key, opt) {
            if (datas[8].active !== 'Y') {
                return !this.data('convertDisabled');
            }
        },
        callback: function () {
            loadPopupModal("users/create&id=" + datas[8].id, datas[8].code, "lg");
        }
    };
    <?php endif; ?>

    <?php /* if (isset(Yii::app()->user->havePermissionOn['users_mis_accessible_branches_edit']) || Yii::app()->user->authorised_user == 1): */?>
     <?php if (Yii::app()->user->authorised_user == 1): ?>    
    menuItems.edit1 = {
        name: "MIS Accessible Branches",
        icon: "edit",
        accesskey: "e",
        disabled: function (key, opt) {
            if (datas[8].active !== 'Y') {
                return !this.data('convertDisabled');
            }
        },
        callback: function () {
            loadPopupModal("users/CreateAccessibleBranch&id=" + datas[8].id, datas[8].code, "m");
        }
    };
    <?php endif; ?>

    <?php if (isset(Yii::app()->user->havePermissionOn['admin_users_block']) || Yii::app()->user->authorised_user == 1): ?>
    menuItems.disable = {
        name: "Disable",
        icon: "fa-toggle-off",
        disabled: function (key, opt) {
            if (datas[8].active !== 'Y') {
                return !this.data('convertDisabled');
            }
        },
        callback: function () {
            enableDisableUser(datas[8].id, 'disabled');
        }
    };
    menuItems.enable = {
        name: "Enable",
        icon: "fa-toggle-on",
        disabled: function (key, opt) {
            if (datas[8].active !== 'N') {
                return !this.data('convertDisabled');
            }
        },
        callback: function () {
            enableDisableUser(datas[8].id, 'enabled');
        }
    };
    <?php endif; ?>

    if (Object.keys(menuItems).length > 0) {
        $(row).contextMenu({
            selector: 'td',
            items: menuItems
        });
    }
}

</script>
    <section class="panel" style="background-color:#F6F3EE;">
        <header class="panel-heading clearfix panel-theame">
            <!-- <i class="fa fa-users"></i> -->
             Users 
          
            <div class="pull-right">
                 <div class="col-md-12">        
              <div class="col-sm-3" width="100%">
                <?php //echo CHtml::dropDownList('display_type','',array(1=>'--Select--','2'=>'All'),array('class'=>'form-control','onchange'=>'reloadTable("#tbl_user_details");'));?>      
                </div>
               
                
                 <!-- <div class="col-sm-4" width="100%">
                <?php  if (isset(Yii::app()->user->havePermissionOn['admin_users_add']) ||  Yii::app()->user->authorised_user == 1) {?>
                
                <button class="btn btn-sm btn-theme" onclick="loadPopupModal('users/create','Add New User','lg')" title="Add"><i class="fa fa-plus"></i></button>
          &nbsp;&nbsp;&nbsp;
                <?php } ?>
                </div> -->
                <!-- <div class="col-sm-4" style="width: 100%;">
                    <?php //if (isset(Yii::app()->user->havePermissionOn['admin_users_add']) || Yii::app()->user->authorised_user == 1) { ?>
                        <button class="btn btn-sm btn-theme pull-right" onclick="loadPopupModal('users/create','Add New User','lg')" title="Add">
                            <i class="fa fa-plus"></i>
                        </button>
                    <?php //} ?>
                </div> -->
                <div class="pull-right" style="display: flex; gap: 8px; align-items: center;">
    <?php if (isset(Yii::app()->user->havePermissionOn['admin_users_add']) || Yii::app()->user->authorised_user == 1) { ?>
        
        <!-- Back Icon Styled as Button -->
        <!-- Back Button -->
<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>

<!-- Add Button -->
<a class="ad-btn" 
   onclick="loadPopupModal('users/create','Add New User','lg')" 
   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>



                  <!-- <div class="col-sm-4" width="100%">
                 <button class="btn btn-sm btn-theme"  onclick="loaduserprint();"><i class="fa fa-file-pdf-o" aria-hidden="true" title="PDF"></i> </button>
                 </div> -->
               
                </div>
                </div>
        </header>
        <div class="panel-body">
            <div class="custom-data-table">
                <table class="table table-hover table-bordered personal-task" id="tbl_user_details" style="font-size:13px;">
                    <thead>
                        <tr>
                            <th style="width: 10px;">SI No</th>
                            <th style="width: 60px;">Staff ID</th>
                            <th style="width: 160px;">Name</th>
                            <th style="width: 80px;">User Name</th>
                            <th style="width: 60px;">Level</th>
                            <th style="width: 60px;">Branch</th>
                            <th style="width: 80px;">Usergroup</th>
                            <th style="width: 140px;">Warehouse</th>
                        </tr>

                    </thead>
                </table>
            </div>
        </div>
    </section>
    <script>
   
    function loaduserprint() {
         var display_type = $('#display_type').val();
            var warehouse_id = $('#warehouse_id').val();
            var level = $('#level').val();
           
        var url = 'index.php?r=users/PrintUsersPdf&display_type='+display_type+'&warehouse_id='+warehouse_id+'&level='+level;
        window.open(url);
    }
   
</script>
