<script>
<?php if($page_name=='ta_list_view'){?>
  function GetSearchDate(){
    return $('#date_range').val();
  }
  function GetSearchEmployee(){
    return $('#employee_filter').val();
  }
  function GetSearchVehicleType(){
    return $('#ta_vehicle_type_filter').val();
  }
  function GetSearchVehicle(){
    return $('#vehicle_filter').val();
  }
  function GetSearchProject(){
    return $('#project_filter').val();
  }
  function GetStartDate(){
    return $('#table_filter_start_date').val();
  }
  function GetEndDate(){
    return $('#table_filter_end_date').val();
  }
  <?php } ?>
  <?php if($page_name=='data_entry_approval'){?>
    function GetStartDate(){
      return $('#table_filter_start_date').val();
    }
    function GetEndDate(){
      return $('#table_filter_end_date').val();
    }
    function GetSearchWork(){
      return $('#table_filter_work').val();
    }
    function GetSearchUser(){
      return $('#table_filter_user').val();
    }
    function GetSearchApprovalStatus(){
      return $('#table_filter_approval_status').val();
    }
    function GetSearchAccount() {
      return $('#table_filter_account').val();
    }
    <?php } ?>
    <?php
    $page_type='';
    if($page_name=='data_entry_approval'){
      $page_type='approval_entries';
    }
    ?>
    var ta_view_table = $('#ta_view_table').DataTable({
      "ajax":{
        url :"<?php echo base_url().'travel_allowance/select_ta/'.$page_type ?>", // json datasource
        type: "post", // type of method  , by default would be get
        data: function(d){
          <?php if($page_name=='user_profile') { ?>
            d.user_id = <?= $user_data->user_id ?>;
            d.start_date = GetStartDate();
            d.end_date = GetEndDate();
            <?php } ?>
            <?php if($page_name=='ta_list_view'){?>
              d.vehicle_type = GetSearchVehicleType();
              d.vehicle = GetSearchVehicle();
              d.project = GetSearchProject();
              d.employee_id = GetSearchEmployee();
              d.date = GetSearchDate();
              <?php } ?>
              <?php if($page_name=='data_entry_approval'){?>
                d.start_date = GetStartDate();
                d.end_date = GetEndDate();
                d.work = GetSearchWork();
                d.approval_status = GetSearchApprovalStatus();
                d.created_by = GetSearchUser();
                d.acc_head_id = GetSearchAccount();
                <?php } ?>
              }
            },
            "bProcessing": true,
            "deferRender": true,
            "bDeferRender": true,
            "scrollX" : true,
            buttons: [
              { extend: 'print', className: 'btn dark btn-outline',exportOptions: {columns: ':visible'},title: 'Travel Allowance' },
              { extend: 'csv', className: 'btn purple btn-outline ' },
              { extend: 'colvis', className: 'btn dark btn-outline', text: 'Columns'}
            ],
            dom:"<'row'<'col-sm-6 text-left'B><'col-sm-6 text-right'f>>\n\t\t\t<'row'<'col-sm-12'tr>>\n\t\t\t<'row'<'col-sm-12 col-md-5'i><'col-sm-12 col-md-7 dataTables_pager'lp>>", // horizobtal scrollable datatable
            "lengthMenu": [
              [25, 50, 100, -1],
              [25, 50, 100, "All"] // change per page values here
            ],
            "columnDefs": [
              {
                "targets": [1,17,18,19,20,<?php if($this->session->userdata('user_role')=='auditor'){ echo "16,"; } ?>  <?php if($this->config->item('app_module_vehicle_management')==0){ echo "6,";}?>],
                className: "hidden"
              },
              {
                render: function (data, type, full, meta) {
                  return "<div class='text-wrap width-150'>" + data + "</div>";
                },
                targets: 12
              },
            ],
            'ordering': false,
            "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
              if(aData[20]=='0'){
                $('td', nRow).css('background-color', '#ffcccc');
              }else if(aData[20]=='2'){
                $('td', nRow).css('background-color', '#ffcccc');
              }
            }
          });
          <?php if($page_name=='ta_list_view') { ?>
            var ta_summary_view_table = $('#ta_summary_view_table').DataTable({
              "ajax":{
                url :"<?php echo base_url().'travel_allowance/select_ta_summary/' ?>", // json datasource
                type: "post", // type of method  , by default would be get
                data: function(d){
                  d.vehicle_type = GetSearchVehicleType();
                  d.date = GetSearchDate();
                  d.vehicle = GetSearchVehicle();
                  d.project = GetSearchProject();
                  <?php if($page_name=='user_profile') { ?>
                    d.user_id = <?= $user_data->user_id ?>;
                    <?php }else{ ?>
                      d.employee_id = GetSearchEmployee();
                      <?php } ?>
                    }
                  },
                  "bProcessing": true,
                  "deferRender": true,
                  "bDeferRender": true,
                  "scrollX" : true,
                  buttons: [
                    { extend: 'print', className: 'btn dark btn-outline',exportOptions: {columns: ':visible'},title: 'Travel Allowance Summary' },
                    { extend: 'csv', className: 'btn purple btn-outline ' },
                    { extend: 'colvis', className: 'btn dark btn-outline', text: 'Columns'}
                  ],
                  dom:"<'row'<'col-sm-6 text-left'B><'col-sm-6 text-right'f>>\n\t\t\t<'row'<'col-sm-12'tr>>\n\t\t\t<'row'<'col-sm-12 col-md-5'i><'col-sm-12 col-md-7 dataTables_pager'lp>>", // horizobtal scrollable datatable
                  "lengthMenu": [
                    [25, 50, 100, -1],
                    [25, 50, 100, "All"] // change per page values here
                  ],
                  "columnDefs": [
                    {
                      "targets": [<?php if($this->config->item('app_module_vehicle_management')==0){ echo "4,";}?>],
                      className: "hidden"
                    }
                  ],
                  'ordering': false
                });
                <?php } ?>
                $(".table_filter").on("change", function() {
                  ta_view_table.ajax.reload();
                  <?php if($page_name=='ta_list_view') { ?>
                    ta_summary_view_table.ajax.reload();
                    <?php } ?>
                  });
                  $(".hide_column").hide();

                  <?php if($page_name=='ta_list_view') { ?>
                    // Date Range Picker
                    var start = moment();
                    var end = moment();

                    function cb(start, end) {
                      $('#date_range').val(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
                      ta_view_table.ajax.reload();
                      ta_summary_view_table.ajax.reload();
                    }

                    $('#date_range').daterangepicker({
                      buttonClasses: "m-btn btn",
                      applyClass: "btn-primary",
                      cancelClass: "btn-secondary",
                      startDate: start,
                      endDate: end,
                      locale: {
                        format: 'MMMM D, YYYY'
                      },
                      ranges: {
                        'Today': [moment(), moment()],
                        'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
                        'Last 7 Days': [moment().subtract(6, 'days'), moment()],
                        'Last 30 Days': [moment().subtract(29, 'days'), moment()],
                        'This Month': [moment().startOf('month'), moment().endOf('month')],
                        'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')],
                        'All time': [moment('1970-01-01'), moment().add(9999, 'days')],
                      }
                    }, cb);

                    cb(start, end);

                    $(".form_submit_btn").click(function (e) {
                      e.preventDefault();
                      var a = $(this),
                      l = $(this).closest("form"),
                      action = l.attr('action');
                      var form_id = l.attr('id');
                      l.validate({
                        rules: {
                          ta_km: {
                            number: !0,
                            required: !0,
                            min: 1
                          },
                          ta_amount: {
                            number: !0,
                            required: !0,
                            min: 1
                          },

                        }, invalidHandler: function (e, r) {
                          var i = $("#" + form_id + "_msg");
                          i.removeClass("m--hide").show(), mUtil.scrollTo(i, -200)
                        }, submitHandler: function (e) {
                        }
                      }), l.valid() && (a.addClass("m-loader m-loader--right m-loader--light").attr("disabled", !0),
                      l.ajaxSubmit({
                        url: action,
                        success: function (e, t, r, s) {
                          var modal_id = l.attr('data-modal-id');
                          var form_type = l.attr('data-form-type');
                          $("#" + form_id + "_msg").addClass("m--hide");
                          // Block UI
                          mApp.blockPage({
                            overlayColor: "#000000",
                            type: "loader",
                            state: "success",
                            message: "Please wait..."
                          }), setTimeout(function () {
                            mApp.unblockPage()
                          }, 2e3);
                          var obj = $.parseJSON(e);
                          if (obj.status == 1) {
                            a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1);
                            toastr[obj.flashdata_type](obj.flashdata_msg, obj.flashdata_title);
                            $("#"+modal_id).modal('toggle');
                            $("."+form_id+"_fields").val('');
                            $("."+form_id+"_select").val('').trigger("change");

                            ta_view_table.ajax.reload();
                            ta_summary_view_table.ajax.reload();
                          } else {
                            a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1);
                            toastr[obj.flashdata_type](obj.flashdata_msg, obj.flashdata_title);
                          }
                          if (obj == null) {
                            setTimeout(function () {
                              a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1);
                              toastr["warning"]("Your Attempt is Timed Out. Please Try Again.", "Timed Out");
                            }, 2e4)
                          }
                        },error: function() {
                          a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1);
                          toastr["error"]("Sorry. There Has Been Some Error Occurred. Please Try Again.", "Error");
                        }
                      })
                    )
                  });
                  <?php } ?>
                  </script>
