<?php
$cs = Yii::app()->getClientScript();
$cs->registerScript("DisableCategory", "
function Disable(id, status){
    var r = confirm((status == 'Y') ? 'Are you sure to disable this?' : 'Are you sure to enable this?');
    if(r==true)
    {
        jQuery.ajax({
          type  : 'post',
          datatype : 'json',
          data  : 'id=' + id + '&status=' + status,
          url  : $('#liveSite').val()+'index.php?r=mascategory/Disable',
          cache: false,
          success:function(data){
            if(data.status=='fail'){
                t8.error('Error while Disable');
            } else {
                reloadTable('#tbl_Category');
                t8.success('Success!');
            }
           }
        });
    }
};
", CClientScript::POS_HEAD);
?>

<script>
$(document).ready(function () {

    var table_id = "#tbl_Category";

    var table = $(table_id).DataTable({

        scrollY: "645px",
        pageLength: 50,
        processing: true,
        serverSide: true,
        fixedColumns: true,

        ajax: {
            url: "<?php echo Yii::app()->createUrl('mascategory/GetList') ?>",
            type: "POST",
            cache: false
        },

        fnCreatedRow: function (nRow) {
            $(nRow).addClass("context-menu");
        },

        fnRowCallback: function (nRow, aData) {

            $(nRow).removeClass('bg-success bg-danger');

            if (aData[2].status === "N") { 
                $(nRow).addClass('bg-danger'); 
            }

            AddContextMenu(nRow, aData);
        }
    });
});

/* Context Menu */
function AddContextMenu(row, datas) {

    $(row).contextMenu({
        selector: 'td',

        items: {

            "edit": {
                name: "Edit",
                icon: "edit",
                callback: function () {
                    loadPopupModal(
                        "mascategory/create&id=" + datas[2].id,
                        datas[2].code,
                        "lg"
                    );
                }
            },

            "Disable_Enable": {
                name: (datas[2].status == "Y") ? "Disable" : "Enable",
                icon: "edit",
                callback: function () {
                    Disable(datas[2].id, datas[2].status);
                }
            }
        }
    });
}
</script>

<section class="panel">
    <header class="panel-heading clearfix panel-theame">
        <i class="fa fa-list"></i> Category

        <div class="pull-right">
          <?php
          $user_id   = (int) Yii::app()->user->id;
          $modeluser = User::model()->findByPk($user_id);

          if ($modeluser->user_group_id == 1) {   
            ?>
             <a class="ad-btn" onclick="loadPopupModal('mascategory/create','Add New Expense Type','lg')">
                <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"></circle>
                    <line x1="12" y1="7" x2="12" y2="17" stroke="#5db5a4" stroke-width="2" stroke-linecap="round"></line>
                    <line x1="7" y1="12" x2="17" y2="12" stroke="#5db5a4" stroke-width="2" stroke-linecap="round"></line>
                </svg>
              </a>
          <!--   <button class="btn btn-sm btn-theme"
            onclick="loadPopupModal('masexpensetype/create', 'Add New Expense Type', 'lg')">
            <i class="fa fa-plus"></i> ADD
        </button> -->
         <?php } ?>

        </div>
    </header>

    <div class="panel-body">
        <div class="custom-data-table">
            <table class="table table-hover table-bordered personal-task"
                   id="tbl_Category" style="font-size:13px;">
                <thead>
                    <tr>
                        <th>SI No</th>
                        <th>Category</th>
                    </tr>
                </thead>
            </table>
        </div>
    </div>
</section>
