
<?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: "645px",
            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 ( aData[6].active == "N" ){
                    $("td", nRow).css("background-color", "#FACAC4"); //expired
                    //$("td", row).css("background-color", "#c8c8c8");

                }
                AddContextMenu(nRow, aData);
            }
        });
    });
    function AddContextMenu(row, datas) {
        
        $(row).contextMenu({
            selector: 'td',

            items: {
                "edit": {
                    
                     <?php  if(isset(Yii::app()->user->havePermissionOn['admin_users_edit']) ||  Yii::app()->user->authorised_user == 1)
                {
                 ?>
                    
                    name: "Edit",
                    icon: "edit",
                    accesskey: "e", 
                    disabled: function(key, opt) { 
    		                    // this references the trigger element
    		                    if(datas[6].active!='Y'){
    		                    	return !this.data('convertDisabled'); 
    		                    }},
                    callback: function () {
                        loadPopupModal("users/create&id=" + datas[6].id, datas[6].code, "lg");
                    }
                    <?php } ?> 
                },
                
                  //   -  -----------------------------
                /*  "edit1": {
                 <?php // if(isset(Yii::app()->user->havePermissionOn['users_mis_accessible_branches_edit']) ||  Yii::app()->user->authorised_user == 1)
               // {
                 ?>
                    name: "MIS Accessible Branches",
                    icon: "edit",
                    accesskey: "e",
                    disabled: function (key, opt) {
                        if (datas[6].active != 'Y') {
                            return !this.data('convertDisabled');
                        }
                    },
                    callback: function () {
                        loadPopupModal("users/CreateAccessibleBranch&id=" + datas[6].id, datas[6].code, "m");
                    }
                <?php //} ?>
            },*/
            
                        //   -  -----------------------------
                        
                        
                "disable": {
                    
                     <?php  if(isset(Yii::app()->user->havePermissionOn['admin_users_block']) ||  Yii::app()->user->authorised_user == 1)
                {
                 ?>
                    
                    name: "Disable",
                    icon: "fa-toggle-off",
                    disabled: function(key, opt) { 
    		                    // this references the trigger element
    		                    if(datas[6].active!='Y'){
    		                    	return !this.data('convertDisabled'); 
    		                    }},
                    callback: function () {
                        enableDisableUser(datas[6].id,'disabled');
                    }
                <?php } ?> 
                },    
                
                 "enable": {
                    
                     <?php  if(isset(Yii::app()->user->havePermissionOn['admin_users_block']) ||  Yii::app()->user->authorised_user == 1)
                {
                 ?>
                    
                    name: "Enable",
                    icon: "fa-toggle-on",
                    disabled: function(key, opt) { 
    		                    // this references the trigger element
    		                    if(datas[6].active!='N'){
    		                    	return !this.data('convertDisabled'); 
    		                    }},
                    callback: function () {
                        enableDisableUser(datas[6].id,'enabled');
                    }
                <?php } ?> 
                },    
                
                

            }
        });
    }
</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')"><i class="fa fa-plus"></i> ADD</button>
          &nbsp;&nbsp;&nbsp;
                <?php } ?>
                </div>
                  <div class="col-sm-3" width="100%">
                 <button class="btn btn-sm btn-theme"  onclick="loaduserprint();"><i class="fa fa-file-pdf-o" aria-hidden="true"></i> PDF</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>SI No</th>
                            <th>Staff ID</th>
                            <th>User Name</th>
                            <th>Name</th>
                            <th>Level</th>
                            <th>Branch</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>
