<?php
$cs = Yii::app()->getClientScript();
$cs->registerScript("Disable",  "function Disable(id){
    var r = confirm('Are you sure to disable this?');
        if(r==true)
        {
            jQuery.ajax({
              'type'  : 'post',
              'data'  : 'id=' + id,
              'url'  : $('#liveSite').val()+'index.php?r=country/Disable',
              'cache': false,
              'success':function(data){
                if(data=='fail'){
                    t8.error('Error while Delete');
                }else {
                    reloadTable('#tbl_country_details');
                    t8.success('Country Disabled SuccessFully!');
                }
               }
            });
        }

};", CClientScript::POS_HEAD);
$cs = Yii::app()->getClientScript();
$baseUrl = Yii::app()->baseUrl;


/* $cs->registerScript("loadIndustry", '
  $(document).ready(function() {

  var table_id ="#tbl_country_details";
  var table = $(table_id).DataTable({
  scrollY         : "380px",
  // scrollX         : "true",
  // scrollCollapse  : true,
  pageLength      : 50,
  processing: true,
  ajax: {
  url: "' . Yii::app()->createUrl('country/GetList') . '",
  type: "POST",
  cache : false,
  },
  serverSide:     true,
  fixedColumns:   true,
  processing: true,
  fixedHeader: {
  headerOffset: $("header.main").outerHeight()
  }
  } );
  $(".dataTables_scrollFoot").hide();
  });', CClientScript::POS_HEAD);
 */
?>
<script>
    $(document).ready(function () {
    var table_id = "#tbl_country_details";
    var table = $(table_id).on("preXhr.dt", function (e, settings, data) {
    data.display_type = $("#display_type").val();
    return data;
    }).DataTable({
    scrollY: "645px",
            pageLength: 50,
            processing: true,
            ajax: {
            url: "<?php echo Yii::app()->createUrl('country/GetList') ?>",
                    type: "POST",
                    cache: false,
            },
            serverSide: true,
            fixedColumns: true,
            processing: true,
            "fnCreatedRow": function (nRow, aData, iDataIndex) {
            $(nRow).addClass("context-menu");
            },
            "fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
                //console.log(aData);
                if ( aData[4] == "Y" )  {  
                                $("td", nRow).addClass("bg-payment");

                              }else{
                                  $("td", nRow).addClass("bg-test");
                              }
                              if(aData[5].status == "N") {
                                  $("td", nRow).addClass("bg-danger");
                              }
                    AddContextMenu(nRow, aData);
            }
    });
    });
    function AddContextMenu(row, datas) {
        var mas_edit=0;
        var mas_disable=0;
        <?php if (isset(Yii::app()->user->havePermissionOn['master_country_edit']) || Yii::app()->user->authorised_user == 1) { ?>
            var mas_edit=1;
        <?php } ?>
        <?php if (isset(Yii::app()->user->havePermissionOn['master_country_disable']) || Yii::app()->user->authorised_user == 1) { ?>
            var mas_disable=1;
        <?php } ?> 
        $(row).contextMenu({
        selector: 'td',
            items: {
            "edit": {

                name: "Edit",
                        icon: "edit",
                        accesskey: "e",
                        callback: function () {
                        loadPopupModal("country/create&id=" + datas[5].id, datas[5].code, "lg");
                        }, 
                    disabled: function(key, opt) { 
                        // this references the trigger element
                        if(mas_edit=='0'){
                            return !this.data('editDisabled'); 
                        }
                    }
                },
                "Disable_Enable":{
                        name: (datas[5].status == "Y") ?"Disable" : "Enable",
                                icon: "edit",
                                accesskey: "d",
                                callback: function () {
                                Disable(datas[5].id, datas[5].status);
                                },
                    disabled: function(key, opt) { 
                            // this references the trigger element
                            if(mas_disable=='0'){
                                return true; 
                            }
                            if (String(datas[4]).trim().toUpperCase() === 'Y') {
                                            return true;
                                            }
                                         return false;


                    }
                }

            }
    });
    }
</script>
<script>
    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=country/Disable',
            'cache': false,
            'success':function(data){
            if (data.status == 'fail'){
            t8.error('Error while Delete');
            } else {
            $('#tbl_country_details').DataTable().ajax.reload()
                    t8.success(' Success!');
            }
            }
    });
    }


    }
</script>
<section class="panel">
    <header class="panel-heading clearfix panel-theame">
        <!-- <i class="fa fa-users"></i>  -->
        Countries     
        <div class="pull-right">
            <?php if (isset(Yii::app()->user->havePermissionOn['master_country_add']) || Yii::app()->user->authorised_user == 1) { ?>

                <a class="ad-btn" onclick="loadPopupModal('country/create', 'Add New Country', 'lg')" title="Add">
                <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" />
                <line x1="12" y1="7" x2="12" y2="17" stroke="#5db5a4" stroke-width="2" stroke-linecap="round"/>
                <line x1="7" y1="12" x2="17" y2="12" stroke="#5db5a4" stroke-width="2" stroke-linecap="round"/>
                </svg>
            </a>

            <?php } ?></div>

    </header>
    <div class="panel-body">
        <div class="custom-data-table">
            <table class="table table-hover table-bordered personal-task" id="tbl_country_details">
                <thead>
                    <tr>
                        <th>Sl.No</th>
                        <th>Country name</th>
                        <th>Country Code</th>

                    </tr>
                </thead>
            </table>
        </div>
    </div>
</section>
