<?php
$user_id   = (int) Yii::app()->user->id;
$modeluser = User::model()->findByPk($user_id);
$isAdmin   = ($modeluser && $modeluser->user_group_id == 1);
?>

<script>
var table;

$(document).ready(function () {
    table = $('#tbl_bin').DataTable({
        scrollY: "500px",
        pageLength: 25,
        processing: true,
        serverSide: true,
        ordering: true,
        order: [[0, 'asc']],
        ajax: {
            url: "<?php echo Yii::app()->createUrl('masinvbin/GetList'); ?>",
            type: "POST"
        },
        fnCreatedRow: function (nRow, aData) {
            $(nRow).addClass("context-menu");
            // Highlight default row in green
            if (aData[4] && aData[4].is_default === 'Y') {
                $(nRow).css({
                    'background-color': '#28a745',
                    'color': '#fff'
                });
            }
        },
        fnRowCallback: function (nRow, aData) {
            AddContextMenu(nRow, aData);
        },
        columns: [
            { title: "SL No",     orderable: false },
            { title: "Bin Name",  orderable: true  },
            { title: "Rack Name", orderable: true  },
            {
                title: "Status",
                orderable: false,
                render: function (data) {
                    if (data === 'Active') {
                        return '<span class="label label-success">Active</span>';
                    } else {
                        return '<span class="label label-warning">Inactive</span>';
                    }
                }
            },
            { visible: false } 
        ]
    });
});

function AddContextMenu(row, datas) {
    $(row).contextMenu({
        selector: 'td',
        items: {
            edit: {
                name: "Edit",
                icon: "edit",
                callback: function () {
                    loadPopupModal(
                        "masinvbin/create&id=" + datas[4].id,
                        "Edit Bin",
                        "md"
                    );
                }
            },
            disable: {
                name: datas[4].status === 'Y' ? "Disable" : "Enable",
                icon: "delete",
                disabled: datas[4].is_default === 'Y' && datas[4].status === 'Y',
                callback: function () {
                    disableBin(datas[4].id, datas[4].status);
                }
            }
        }
    });
}

function disableBin(id, status) {
    $.ajax({
        url: "<?php echo Yii::app()->createUrl('masinvbin/Disable'); ?>",
        type: "POST",
        data: { id: id, status: status },
        success: function (res) {
            if (res.status === 'error') {
                t8.error(res.message || "Cannot disable this bin.");
            } else {
                table.ajax.reload(null, false);
            }
        }
    });
}
</script>

<section class="panel">
    <header class="panel-heading clearfix panel-theame">
        <i class="fa fa-archive"></i> Bin Master

        <?php if ($isAdmin): ?>
        <div class="pull-right">
            <a class="ad-btn" onclick="loadPopupModal('masinvbin/create','Add Bin','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>
        <?php endif; ?>
    </header>

    <div class="panel-body">
        <div class="custom-data-table">
            <table class="table table-hover table-bordered"
                   id="tbl_bin"
                   style="font-size:13px; width:100%;">
                <thead>
                    <tr>
                        <th>SL No</th>
                        <th>Bin Name</th>
                        <th>Rack Name</th>
                        <th>Status</th>
                    </tr>
                </thead>
                <tbody></tbody>
            </table>
        </div>
    </div>
</section>