<?php
$cs = Yii::app()->getClientScript();
$cs->registerScript("DisableSubCategory", "
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: 'index.php?r=subcategory/Disable',
            cache    : false,
            success  : function(data) {
                if(data.status == 'fail') {
                    t8.error('Error while updating status');
                } else {
                    reloadTable('#tbl_subcategory');
                    t8.success('Success!');
                }
            },
            error: function(xhr){
                t8.error('Request failed: ' + xhr.status);
            }
        });
    }
};
", CClientScript::POS_HEAD);
?>

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

    var table_id = "#tbl_subcategory";

    var table = $(table_id).DataTable({
        scrollY      : "645px",
        pageLength   : 50,
        processing   : true,
        serverSide   : true,
        fixedColumns : true,

        columnDefs: [
            { targets: [4], visible: false, searchable: false }
        ],

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

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

        fnRowCallback: function (nRow, aData) {
            $(nRow).removeClass('bg-success bg-danger');

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

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

function AddContextMenu(row, datas) {
    $(row).contextMenu({
        selector: 'td',
        items: {
            "edit": {
                name    : "Edit",
                icon    : "edit",
                callback: function () {
                    loadPopupModal(
                        "subcategory/create&id=" + datas[4].id,
                        datas[4].code,
                        "lg"
                    );
                }
            },
            "Disable_Enable": {
                name    : (datas[4].status == "Y") ? "Disable" : "Enable",
                icon    : "edit",
                callback: function () {
                    Disable(datas[4].id, datas[4].status);
                }
            }
        }
    });
}
</script>

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

        <?php if ($isAdmin): ?>
        <div class="col-sm-2 pull-right" style="margin-right:10px;">
            <?php
            $allUsers = User::model()->findAll(array('order' => 'username ASC'));
            $users    = CHtml::listData($allUsers, 'id', 'username');
            echo CHtml::dropDownList('user_id', '', $users, array(
                'id'          => 'user_id',
                'class'       => 'form-control',
                'prompt'      => 'All users',
                'data-search' => 'true'
            ));
            ?>
        </div>
        <?php endif; ?>

        <div class="col-sm-2 pull-right" style="margin-right:10px;">
            <?php
            echo CHtml::dropDownList('category_id', '', Category::getCategories(), array(
                'id'     => 'category_id',
                'class'  => 'form-control',
                'prompt' => 'Categories'
            ));
            ?>
        </div>

        <div class="pull-right">
            <a class="ad-btn" onclick="loadPopupModal('subcategory/create','Add Sub Category','md')">
                <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>
        </div>
    </header>

    <div class="panel-body">
        <div class="custom-data-table">
            <table class="table table-hover table-bordered"
                   id="tbl_subcategory"
                   style="font-size:13px; width:100%;">
                <thead>
                    <tr>
                        <th>SI No</th>
                        <th>Category</th>
                        <th>Sub Category</th>
                        <th>Status</th>
                        <?php if ($isAdmin): ?>
                        <th>Created By</th>
                        <?php endif; ?>
                    </tr>
                </thead>
                <tbody></tbody>
            </table>
        </div>
    </div>
</section>