text: ﻿var table = null;
  var arrData = [];
  
  $(document).ready(function () {
      table = $('#report').DataTable({
          "processing": true,
          "responsive": true,
          "pagination": true,
          "stateSave": true,
          "ajax": {
              url: "/feedback/LoadData",
              type: "GET",
              dataType: "json",
              dataSrc: "",
          },
          "columns": [
              {
                  "data": "id",
                  render: function (data, type, row, meta) {
                      //console.log(row);
                      return meta.row + meta.settings._iDisplayStart + 1;
                      //return meta.row + 1;
                  }
              },
              { "data": "employee.name" },
              {
                  "sortable": false,
                  "data": "question.training.employee.name"
              },
              { "data": "question.training.title" },
              { "data": "question.questionDesc" },
              { "data": "review" },
              {
                  "sortable": false,
                  "data": "rate"
              },
              {
                  "sortable": false,
                  "render": function (data, type, row) {
                      //console.log(row);
                      $('[data-toggle="tooltip"]').tooltip();
                      return '<div class="form-button-action">'
                          + '<button class="btn btn-link btn-lg btn-warning" data-placement="left" data-toggle="tooltip" data-animation="false" title="Edit" onclick="return GetById(' + row.id + ')" ><i class="fa fa-lg fa-edit"></i></button>'
                          + '&nbsp;'
                          + '<button class="btn btn-link btn-lg btn-danger" data-placement="right" data-toggle="tooltip" data-animation="false" title="Delete" onclick="return Delete(' + row.id + ')" ><i class="fa fa-lg fa-times"></i></button>'
                          + '</div>'
                  }
              }
          ],
          initComplete: function () {
              this.api().columns(6).every(function () {
                  var column = this;
                  var select = $('<select class="form-control"><option value="">ALL</option></select>')
                      .appendTo($(column.header()).empty())
                      .on('change', function () {
                          var val = $.fn.dataTable.util.escapeRegex(
                              $(this).val()
                          );
  
                          column
                              .search(val ? '^' + val + '$' : '', true, false)
                              .draw();
                      });
  
                  column.data().unique().sort().each(function (d, j) {
                      select.append('<option value="' + d + '">' + d + '</option>')
                  });
              });
              this.api().columns(2).every(function () {
                  var column = this;
                  var select = $('<select class="form-control"><option value="">ALL</option></select>')
                      .appendTo($(column.header()).empty())
                      .on('change', function () {
                          var val = $.fn.dataTable.util.escapeRegex(
                              $(this).val()
                          );
  
                          column
                              .search(val ? '^' + val + '$' : '', true, false)
                              .draw();
                      });
  
                  column.data().unique().sort().each(function (d, j) {
                      select.append('<option value="' + d + '">' + d + '</option>')
                  });
              });
          }
      });
  
      ClearScreen();
  });
  
  function ClearScreen() {
      $('#Id').val('');
      $('#Name').val('');
      $('#trainerCek').hide();
      $('#update').hide();
      $('#add').show();
  }
  
  function LoadData(element) {
      //debugger;
      if (arrData.length === 0) {
          if (element[0].name == 'TrainOption') {
              $.ajax({
                  type: "Get",
                  url: "/account/LoadData",
                  success: function (data) {
                      arrData = data;
                      renderData(element);
                  }
              });
          } else if (element[0].name == 'UserOption') {
              $.ajax({
                  type: "Get",
                  url: "/account/LoadData",
                  success: function (data) {
                      arrData = data;
                      renderData(element);
                  }
              });
          } else if (element[0].name == 'QuestOption') {
              $.ajax({
                  type: "Get",
                  url: "/question/loaddata",
                  success: function (data) {
                      arrData = data;
                      renderData(element);
                  }
              });
          }
      }
      else {
          renderData(element);
      }
  }
  
  function renderData(element) {
      //debugger;
      var $option = $(element);
      $option.empty();
      $option.append($('<option/>').val('0').text('-- Select --').hide());
      if (element[0].name == 'QuestOption') {
          $.each(arrData, function (i, val) {
              $option.append($('<option/>').val(val.id).text(val.questionDesc))
          });
      } else {
          $.each(arrData, function (i, val) {
              $option.append($('<option/>').val(val.id).text(val.name))
          });
      }
  }
  
  LoadData($('#TrainOption'))
  LoadData($('#UserOption'))
  LoadData($('#QuestOption'))
  
  function GetById(id) {
      //debugger;
      $.ajax({
          url: "/feedback/GetById/",
          data: { id: id }
      }).then((result) => {
          //debugger;
          $('#Id').val(result.id);
          $('#Review').val(result.review);
          $('#Rate').val(result.rate);
          $('#UserOption').val(result.userId);
          $('#QuestOption').val(result.questionId);
          $('#trainerCek').show();
          $('#TrainOption').val(result.question.training.userId);
          $('#Title').val(result.question.training.title);
          $('#add').hide();
          $('#update').show();
          $('#myModal').modal('show');
      })
  }
  
  function Save() {
      //debugger;
      var Data = new Object();
      Data.Id = 0;
      Data.review = $('#Review').val();
      Data.rate = $('#Rate').val();
      Data.userId = $('#UserOption').val();
      Data.questionId = $('#QuestOption').val();
      $.ajax({
          type: 'POST',
          url: "/feedback/InsertOrUpdate/",
          cache: false,
          dataType: "JSON",
          data: Data
      }).then((result) => {
          //debugger;
          if (result.statusCode == 200) {
              Swal.fire({
                  position: 'center',
                  icon: 'success',
                  title: 'Data inserted Successfully',
                  showConfirmButton: false,
                  timer: 1500,
              })
              table.ajax.reload(null, false);
          } else {
              Swal.fire('Error', 'Failed to Input', 'error');
              ClearScreen();
          }
      })
  }
  
  function Update() {
      //debugger;
      var Data = new Object();
      Data.Id = $('#Id').val();
      Data.review = $('#Review').val();
      Data.rate = $('#Rate').val();
      Data.userId = $('#UserOption').val();
      Data.questionId = $('#QuestOption').val();
      $.ajax({
          type: 'POST',
          url: "/feedback/InsertOrUpdate/",
          cache: false,
          dataType: "JSON",
          data: Data
      }).then((result) => {
          //debugger;
          if (result.statusCode == 200) {
              Swal.fire({
                  position: 'center',
                  icon: 'success',
                  title: 'Data Updated Successfully',
                  showConfirmButton: false,
                  timer: 1500,
              });
              table.ajax.reload(null, false);
          } else {
              Swal.fire('Error', 'Failed to Input', 'error');
              ClearScreen();
          }
      })
  }
  
  function Delete(id) {
      Swal.fire({
          title: 'Are you sure?',
          text: "You won't be able to revert this!",
          showCancelButton: true,
          confirmButtonColor: '#3085d6',
          cancelButtonColor: '#d33',
          confirmButtonText: 'Yes, delete it!',
      }).then((resultSwal) => {
          if (resultSwal.value) {
              //debugger;
              $.ajax({
                  url: "/feedback/Delete/",
                  data: { id: id }
              }).then((result) => {
                  //debugger;
                  if (result.statusCode == 200) {
                      //debugger;
                      Swal.fire({
                          position: 'center',
                          icon: 'success',
                          title: 'Delete Successfully',
                          showConfirmButton: false,
                          timer: 1500,
                      });
                      table.ajax.reload(null, false);
                  } else {
                      Swal.fire('Error', 'Failed to Delete', 'error');
                      ClearScreen();
                  }
              })
          };
      });
  }