<?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_country').DataTable({

        scrollY: "500px",
        pageLength: 25,
        processing: true,
        serverSide: true,

        ordering: true,
        order: [[1, 'asc']],

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

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

        fnRowCallback: function (nRow, aData) {
            AddContextMenu(nRow, aData);
        },

        columns: [
        { title: "SI No", orderable: false },
        { title: "Country Name", orderable: true },
        {
            title: "Status",
            orderable: false,
            render: function (data) {
                if(data.status == 'Y'){
                    return '<span class="label label-success">Active</span>';
                }else{
                    return '<span class="label label-warning">Inactive</span>';
            }
        }
    }
]
    });
});

function AddContextMenu(row, datas) {

    $(row).contextMenu({
        selector: 'td',
        items: {
            edit: {
                name: "Edit",
                icon: "edit",
                callback: function () {
                    loadPopupModal(
                        "country/create&id=" + datas[2].id,
                        "Edit Country",
                        "md"
                    );
                }
            },

            disable: {
                name: datas[2].status == 'Y' ? "Disable" : "Enable",
                icon: "delete",
                callback: function () {
                    disableCountry(datas[2].id, datas[2].status);
                }
            }
        }
    });
}

function disableCountry(id,status)
{
    $.ajax({
        url: "<?php echo Yii::app()->createUrl('country/Disable'); ?>",
        type: "POST",
        data: {id:id,status:status},
        success: function(res){
            table.ajax.reload(null,false);
        }
    });
}

</script>

<section class="panel">

<header class="panel-heading clearfix panel-theame">
    <i class="fa fa-globe"></i> Country Master

    <?php if ($isAdmin): ?>
    <div class="pull-right">
    <a class="ad-btn" onclick="loadPopupModal('country/create','Add Country','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_country"
               style="font-size:13px; width:100%;">
            <thead>
                <tr>
                    <th>SI No</th>
                    <th>Country Name</th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody></tbody>
        </table>
    </div>
</div>

</section>