<?php
$cs = Yii::app()->getClientScript();

$cs->registerScript("DisableInventoryProduct", "
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=inventoryProducts/Disable',
          cache: false,
          success:function(data){
            if(data.status=='fail'){
                t8.error('Error while Disable');
            } else {
                reloadTable('#tbl_inventory_products');
                t8.success('Success!');
            }
           }
        });
    }
};
", CClientScript::POS_HEAD);
?>

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

    var table = $("#tbl_inventory_products").DataTable({
        scrollY: "645px",
        pageLength: 50,
        processing: true,
        serverSide: true,
        ajax: {
            url: "<?php echo Yii::app()->createUrl('inventoryProducts/GetList') ?>",
            type: "POST"
        },
       columns: [
            { data: "id", title: "ID" },
            { data: "sku", title: "Part No" },
            { data: "product_name", title: "Name" },
            { data: "brand", title: "Brand" },
            { data: "category", title: "Category" },
            { data: "quantity", title: "Qty" },
            { data: "price", title: "Rate" },
            { data: "isActive", title: "Status" }
        ],
        fnCreatedRow: function (nRow) {
            $(nRow).addClass("context-menu");
        },
        rowCallback: function (row, data) {
            $(row).removeClass('bg-danger');
            if (data.isActive === "N") {
                $(row).addClass('bg-danger');
            }
            AddContextMenu(row, data);
        }
    });
});

/* Context Menu */
function AddContextMenu(row, data) {
    $(row).contextMenu({
        selector: 'td',
        items: {
            "edit": {
                name: "Edit",
                icon: "edit",
                callback: function () {
                    loadPopupModal(
                        "inventoryProducts/create&id=" + data.id,
                        data.product_name,
                        "lg"
                    );
                }
            },
            "Disable_Enable": {
               name: (data.isActive == "Y") ? "Disable" : "Enable",
                    callback: function () {
                        Disable(data.id, data.isActive);
                    }
            }
        }
    });
}
</script>

<section class="panel">
    <header class="panel-heading clearfix panel-theame">
        <i class="fa fa-cubes"></i> Inventory Products

        <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('inventoryProducts/create','Add Product','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>
        <?php } ?>
        </div>
    </header>

    <div class="panel-body">
        <div class="custom-data-table">
            <table id="tbl_inventory_products" class="table table-bordered table-striped">
    <thead>
        <tr>
            <th>ID</th>
            <th>SKU</th>
            <th>Product Name</th>
            <th>Brand</th>
            <th>Category</th>
            <th>Quantity</th>
            <th>Price</th>
            <th>Action</th>
        </tr>
    </thead>
</table>
        </div>
    </div>
</section>