<script>
<?php if($page_name=='material_issue_view'){?>
  $("#table_filter_location").select2({
    placeholder: "Select  Location",
    templateResult: format_project_dropdown
  })
  $("#table_filter_material").select2({
    placeholder: "Select  Material"
  })
  $("#table_filter_subcontract").select2({
    placeholder: "Select Subcontract",
    // dropdownParent: $("#form_vehicle_work_slip_add")
  })

  function select_subcontracts(select_id,selected_value='',project){
    $('#'+select_id).html('');
    $('#'+select_id).append("<option selected value='all' >All Subcontracts</option>");
    var project = $("#table_filter_location option:selected").val();
    if(project!='' && project!=null && project!='Select Location' && project!='All'){
      $.post("<?php echo base_url() ?>subcontracts/select_subcontracts_list",{work:project},function(data) {
        var obj = $.parseJSON(data);
        var i=0;
        if(obj.count>0) {
          while (i < obj.data.length) {
            $('#'+select_id).append("<option  value=" + obj.data[i]['contract_id'] + ">" + obj.data[i]['sub_work_name'] + " [" + obj.data[i]['contractor_name'] + " - " + obj.data[i]['contractor_phone'] + "]</option>");
            i++;
          }
          if(selected_value!=''){
            $("#"+select_id).val(selected_value).trigger("change");
          }
        }
      })
    }
  }
  select_subcontracts("table_filter_subcontract");
  $('body').on('change', '#table_filter_location', function() {
    select_subcontracts("table_filter_subcontract");
  })
  <?php } ?>

  function GetSearchLocation(){
    return $('#table_filter_location').val();
  }
  function GetSearchSubcontract(){
    return $('#table_filter_subcontract').val();
  }
  function GetSearchMaterial(){
    return $('#table_filter_material').val();
  }
  <?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();
    }
    <?php } ?>
  <?php
  $page_type='';
  if($page_name=='data_entry_approval'){
    $page_type='approval_entries';
  }
  ?>
  var material_issue_items_view_table = $('#material_issue_items_view_table').DataTable({
    "ajax":{
      url :"<?php echo base_url().'subcontracts/select_material_issue_items/'.$page_type ?>", // json datasource
      type: "post", // type of method  , by default would be get
      data: function(d){
          <?php if($page_name=='material_issue_view'){?>
        d.location = GetSearchLocation();
        d.subcontract = GetSearchSubcontract();
        d.material_id = GetSearchMaterial();
        <?php } ?>
        <?php if($page_name=='work_profile'){?>
          d.work_id = "<?php echo $work_data->work_id?>";
          <?php } ?>
          <?php if($page_name=='work_profile_master' || $page_name=='work_summary_master'){?>
            d.work_ids = "<?php echo implode(',',$sub_project_ids)?>";
            <?php } ?>
        <?php if($page_name=='data_entry_approval'){?>
          d.start_date = GetStartDate();
          d.end_date = GetEndDate();
          d.work_id = GetSearchWork();
          d.approval_status = GetSearchApprovalStatus();
          d.user_id = GetSearchUser();
          <?php } ?>
      }
    },
    "bProcessing": true,
    "deferRender": true,
    "bDeferRender": true,
    "scrollX" : true,
    scrollX:!0, scrollCollapse:!0,
    buttons: [
      { extend: 'print', className: 'btn dark btn-outline',exportOptions: {columns: ':visible'},title: 'Stock Transfer History' },
      { 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,<?php if($page_name!='data_entry_approval'){ echo "16";}?> ],
        className: "hidden"
      }
    ],
    'order': false
  });
  var material_issue_bills_view_table = $('#material_issue_bills_view_table').DataTable({
    "ajax":{
      url :"<?php echo base_url().'subcontracts/select_material_issue_bills' ?>", // json datasource
      type: "post", // type of method  , by default would be get
      data: function(d){
        d.location = GetSearchLocation();
        d.subcontract = GetSearchSubcontract();
        <?php if($page_name=='work_profile'){?>
          d.work_id = "<?php echo $work_data->work_id?>";
          <?php } ?>
          <?php if($page_name=='work_profile_master' || $page_name=='work_summary_master'){?>
            d.work_ids = "<?php echo implode(',',$sub_project_ids)?>";
            <?php } ?>
      }
    },
    "bProcessing": true,
    "deferRender": true,
    "bDeferRender": true,
    "scrollX" : true,
    scrollX:!0, scrollCollapse:!0,
    buttons: [
      { extend: 'print', className: 'btn dark btn-outline',exportOptions: {columns: ':visible'},title: 'Stock Transfer Bills' },
      { 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": [10],
        className: "hidden"
      }
    ],
    'order': false
  });
  var material_issue_returns_view_table = $('#material_issue_returns_view_table').DataTable({
    "ajax":{
      url :"<?php echo base_url().'subcontracts/select_material_issue_returns/'.$page_type ?>", // json datasource
      type: "post", // type of method  , by default would be get
      data: function(d){
          <?php if($page_name=='material_issue_view'){?>
        d.location = GetSearchLocation();
        d.subcontract = GetSearchSubcontract();
        d.material_id = GetSearchMaterial();
        <?php } ?>
        <?php if($page_name=='work_profile'){?>
          d.work_id = "<?php echo $work_data->work_id?>";
          <?php } ?>
          <?php if($page_name=='work_profile_master' || $page_name=='work_summary_master'){?>
            d.work_ids = "<?php echo implode(',',$sub_project_ids)?>";
            <?php } ?>
        <?php if($page_name=='data_entry_approval'){?>
          d.start_date = GetStartDate();
          d.end_date = GetEndDate();
          d.work_id = GetSearchWork();
          d.approval_status = GetSearchApprovalStatus();
          d.user_id = GetSearchUser();
          <?php } ?>
      }
    },
    "bProcessing": true,
    "deferRender": true,
    "bDeferRender": true,
    "scrollX" : true,
    scrollX:!0, scrollCollapse:!0,
    buttons: [
      { extend: 'print', className: 'btn dark btn-outline',exportOptions: {columns: ':visible'},title: 'Stock Transfer History' },
      { 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 ],
        className: "hidden"
      }
    ],
    'order': false
  });
  $(".table_filter").on("change", function() {
    material_issue_items_view_table.ajax.reload();
    material_issue_bills_view_table.ajax.reload();
    material_issue_returns_view_table.ajax.reload();
  });
  $(".hide_column").hide();
  $('body').on('click', '#material_issue_bill_delete_btn', function() {
    var no = $(this).closest('tr').children('td');
    $('#material_issue_bill_delete_id').val(no.eq(10).text());
  })
<?php if($page_name=='material_issue_view'){?>
  $(".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: {
        "mobile_number": {
          required: !0,
          number: !0,
          minlength: 8,
          maxlength: 12
        }
      }, 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');

        material_issue_items_view_table.ajax.reload();
        material_issue_bills_view_table.ajax.reload();
        material_issue_returns_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>
