text: ﻿$(function () {
  
      //To solve Synchronous XMLHttpRequest warning
      global.AjaxPrefilter();
  
      //Get appSetting.json
      var appSetting = global.getAppSettings('AppSettings');
  
      $("#success-alert").hide();
  
      //Grid Table Config
      var page = 0;
  
      deptVM = {
          dtDept: null,
          init: function () {
              dtDept = $('#tblDept').DataTable({
                  dom: "<'row'<'col-sm-4'B><'col-sm-2'l><'col-sm-6'f>>" +
                       "<'row'<'col-sm-12'tr>>" +
                       "<'row'<'col-sm-6'i><'col-sm-6'p>>",
                  buttons: [
                      {
                          text: '<i class="fa fa-refresh">&nbsp;<p class="setfont">Refresh</p></i>',
                          titleAttr: 'Refresh',
                          action: function (e, dt, node, config) {
                              dt.ajax.reload(null, false);
                          }
                      },
                      {
                          extend: 'collection',
                          text: '<i class="fa fa-file-o">&nbsp;<p class="setfont">Export</p></i>',
                          titleAttr: 'Export Option',
                          autoClose: true,
                          buttons: [
                              {
                                  extend: 'excelHtml5',
                                  text: '<i class="fa fa-file-excel-o">&nbsp;<p class="setfont">Export XLS</p></i>',
                                  title: 'Department Master',
                                  titleAttr: 'Excel'
                              },
                              {
                                  extend: 'csvHtml5',
                                  text: '<i class="fa fa-file-text-o">&nbsp;<p class="setfont">Export CSV</p></i>',
                                  title: 'Department Master',
                                  titleAttr: 'CSV'
                              }
                          ]
                      }
                  ],
                  initComplete: function () {
                      var btns = $('.dt-button');
                      btns.addClass('btn btn-default btn-sm');
                      btns.removeClass('dt-button');
                  },
                  processing: true, // for show progress bar
                  autoWidth: false,
                  ajax: {
                      url: $('#IndexData').data('dept-get-url'),  
                      type: "GET",
                      async: true,
                      datatype: "json",
                      data: null,
                      error: function (xhr, txtStatus, errThrown) {
  
                          var reponseErr = JSON.parse(xhr.responseText);
  
                          toastr.error('Error: ' + reponseErr.message, 'Get Department Error', { timeOut: appSetting.toastrErrorTimeout, extendedTimeOut: appSetting.toastrExtenTimeout });
                      }
                  },
                  columns: [
                      { "data": "DeptCode", "className": "boldColumn", "autoWidth": false },
                      { "data": "DeptName", "autoWidth": false },
                      { "data": "DeptDesc", "autoWidth": false },
                      { "data": "CompanyCode", "className": "boldColumn", "autoWidth": false },
                      {
                          "data": "Is_Active",
                          "autoWidth": false,
                          render: function (data, type, row, meta) {
                              if (type === 'display') {
                                  if (data) {
                                      return '<img src="' + $('#IndexData').data('image-url') + '/mswitch/isavtive_yes.png" />';
                                  } else {
                                      return '<img src="' + $('#IndexData').data('image-url') + '/mswitch/isavtive_no.png" />';
                                  }
                              }
                              return data;
                          }
                      },
                      {
                          "autoWidth": true,
                          "render": function (data, type, dept, meta) {
                              return '<a id="viewDept" class="btn btn-view btn-sm" data-toggle="tooltip" title="View" href="Department/Details/' + dept.Id + '"><span class="glyphicon glyphicon-search" aria-hidden="true"></span></a>&nbsp;' +
                                     '<a id="editDept" class="btn btn-edit btn-sm" data-toggle="tooltip" title="Edit" href="Department/Edit/' + dept.Id + '"><span class="glyphicon glyphicon-edit" aria-hidden="true"></span></a>&nbsp;' +
                                     '<a id="delDept" class="btn btn-delete btn-sm" data-toggle="tooltip" title="Remove" href="Department/Delete/"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></a>';
                          }
                      }
                  ],
                  columnDefs: [
                      { "width": "12%", "targets": 0 },
                      { "width": "27%", "targets": 1 },
                      { "width": "30%", "targets": 2 },
                      { "width": "14%", "targets": 3 },
                      { "className": "dt-center", "width": "7%", "targets": 4, "orderable": false },
                      { "width": "10%", "targets": 5, "orderable": false }
                  ],
                  order: [],
                  lengthMenu: [[5, 10, 25, 50, 100, -1], [5, 10, 25, 50, 100, "All"]],
                  iDisplayLength: appSetting.tableDisplayLength,
                  stateSave: true,
                  stateDuration: -1 //force the use of Session Storage
              });
  
              //dt.on('draw', function () {
              //    global.applyIcheckStyle();
              //});
  
              //keep the current page after sorting
              dtDept.on('order', function () {
                  if (dtDept.page() !== page) {
                      dtDept.page(page).draw('page');
                  }
              });
  
              dtDept.on('page', function () {
                  page = dtDept.page();
              });
  
              $('div.dataTables_filter input').addClass('form-control');
              $('div.dataTables_length select').addClass('form-control');
  
  
          },
  
          refresh: function () {
              dtDept.ajax.reload();
          },
  
          removeSorting: function () {  //remove order/sorting
              dtDept.order([]).draw(false);
          }
      }
  
      // initialize the datatables
      deptVM.init();
  
      deptVM.removeSorting();
  
      if (appSetting.defaultFirstPage == 1) {
          setTimeout(function () {
              if (dtDept.page.info().page != 0) {
                  dtDept.page('first').draw('page');
              }
          }, 200);
      }
  
      //set width of input search.
      //$('.dataTables_filter input[type="search"]').css({ 'width': '350px' });
  
      function addRequestVerificationToken(data) {
          data.__RequestVerificationToken = $('input[name=__RequestVerificationToken]').val();
          return data;
      };
  
      //Add
      $("#btnCreateDept").on("click", function (event) {
  
          event.preventDefault();
  
          var api = $(this).data("url");
  
          $.ajax({
              type: "GET",
              url: api,
              async: true,
              success: function (data) {
                  if (data) {
                      $('#newDeptContainer').html(data);
                      $('#newDeptModal').modal('show');
                  } else {
                      global.authenExpire();
                  }
              },
              error: function (xhr, txtStatus, errThrown) {
  
                  var reponseErr = JSON.parse(xhr.responseText);
                  
                  toastr.error('Error: ' + reponseErr.message, 'Create Department', { timeOut: appSetting.toastrErrorTimeout, extendedTimeOut: appSetting.toastrExtenTimeout });
              }
          });
      });
  
      $("#newDeptModal").on("shown.bs.modal", function (event) {
  
          event.preventDefault();
  
          $('#DeptCode').focus();
      });
  
      $("#newDeptModal").on("hidden.bs.modal", function (event) {
  
          event.preventDefault();
  
          $('#newDeptContainer').html("");
      });
  
      //view
      $('#tblDept').on("click", "#viewDept", function (event) {
  
          event.preventDefault();
  
          var api = $(this).attr("href");
  
          $.ajax({
              type: "GET",
              url: api,
              async: true,
              success: function (data) {
                  if (data) {
                      $('#viewDeptContainer').html(data);
                      $('#viewDeptModal').modal('show');
                  } else {
                      global.authenExpire();
                  }
              },
              error: function (xhr, txtStatus, errThrown) {
  
                  var reponseErr = JSON.parse(xhr.responseText);
                  
                  toastr.error('Error: ' + reponseErr.message, 'View Department', { timeOut: appSetting.toastrErrorTimeout, extendedTimeOut: appSetting.toastrExtenTimeout });
              }
          });
  
      });
  
      //clear html data for View;
      $("#viewDeptModal").on("hidden.bs.modal", function () {
          $('#viewDeptContainer').html("");
      });
  
      //Edit
      $('#tblDept').on("click", "#editDept", function (event) {
  
          event.preventDefault();
  
          var api = $(this).attr("href");
  
          $.ajax({
              type: "GET",
              url: api,
              async: true,
              success: function (data) {
                  if (data) {
                      $('#editDeptContainer').html(data);
                      $('#editDeptModal').modal('show');
                  } else {
                      global.authenExpire();
                  }
              },
              error: function (xhr, txtStatus, errThrown) {
  
                  var reponseErr = JSON.parse(xhr.responseText);
                  
                  toastr.error('Error: ' + reponseErr.message, 'Edit Department', { timeOut: appSetting.toastrErrorTimeout, extendedTimeOut: appSetting.toastrExtenTimeout });
              }
          });
      });
  
      $("#editDeptModal").on("shown.bs.modal", function () {
  
          $('#DeptCode').focus().select();
  
      });
  
      //clear html data for edit;
      $("#editDeptModal").on("hidden.bs.modal", function () {
          $('#editDeptContainer').html("");
      });
  
  
  
      //Delete
      $('#tblDept').on('click', '#delDept', function (event) {
  
          event.preventDefault();
  
          var api = $(this).attr("href");
          var rowSelect = $(this).parents('tr')[0];
          var deptData = (dtDept.row(rowSelect).data());
          var deptId = deptData["Id"];
          var deptName = deptData["DeptName"];
  
          $.confirm({
              title: 'Please Confirm!',
              content: 'Are you sure you want to delete this \"' + deptName + '\"',
              buttons: {
                  confirm: {
                      text: 'Confirm',
                      btnClass: 'btn-confirm',
                      keys: ['shift', 'enter'],
                      action: function () {
  
                          $.ajax({
                              type: 'POST',
                              url: api,
                              async: true,
                              data: addRequestVerificationToken({ id: deptId }),
                              success: function (response) {
  
                                  if (response.success) {
  
                                      //deptVM.refresh();
                                      dtDept.row(rowSelect).remove().draw(false);
  
                                      toastr.success(response.message, 'Delete Department', { timeOut: appSetting.toastrSuccessTimeout, extendedTimeOut: appSetting.toastrExtenTimeout });
                                  }
                                  else {
                                      toastr.error(response.message, 'Delete Department', { timeOut: appSetting.toastrErrorTimeout, extendedTimeOut: appSetting.toastrExtenTimeout });
                                  }
                              },
                              error: function (xhr, txtStatus, errThrown) {
  
                                  var reponseErr = JSON.parse(xhr.responseText);
                                  
                                  toastr.error('Error: ' + reponseErr.message, 'Delete Department', { timeOut: appSetting.toastrErrorTimeout, extendedTimeOut: appSetting.toastrExtenTimeout });
                              }
                          });
                      }
                  },
                  cancel: {
                      text: 'Cancel',
                      btnClass: 'btn-cancel',
                      keys: ['enter'],
                      action: function () {
                          //nothing
                      }
                  }
              }
          });
  
      });
  });