<script>
<?php if($page_name=='purchase_request_view'){?>
  $('body').on('click', '#purchase_request_approve_btn', function() {
    var no = $(this).closest('tr').children('td');
    $('#purchase_request_id').val(no.eq(1).text().replace("#", ""));
    if(no.eq(11).text()!=0){
      $('input[type=radio][name=approve_status][value='+no.eq(11).text()+']').prop('checked',true);
    }else{
        $('input[type=radio][name=approve_status]').prop('checked',false);
    }
  });

  $("#table_filter_work").select2({
    placeholder: "Select  Work",
    templateResult: format_project_dropdown
  })
  $("#table_filter_location").select2({
    placeholder: "Select  Work",
    templateResult: format_project_dropdown
  })
  $("#table_filter_status").select2({
    placeholder: "Select Status"
  })
  <?php if(isset($list_type) && $list_type=="pending"){?>
    $("#table_filter_status").val(0).trigger('change');
    <?php } ?>
  $("#table_filter_purchase_request_start_date,#table_filter_purchase_request_end_date").datepicker( {
    todayHighlight:!0, orientation:"bottom left", templates: {
      leftArrow: '<i class="la la-angle-left"></i>', rightArrow: '<i class="la la-angle-right"></i>'
    }
  })
  function GetSearchstatus(){
    return $('#table_filter_status').val();
  }
  function GetSearchDate(){
    return $('#table_filter_date_range').val();
  }
  function GetSearchLocation(){
    return $('#table_filter_location').val();
  }
  function GetSearchStartDate(){
    return $('#table_filter_purchase_request_start_date').val();
  }
  function GetSearchEndDate(){
    return $('#table_filter_purchase_request_end_date').val();
  }
  function Getdatetype(){
          return $("input[name='date_type']:checked").val();
        }
  <?php } ?>
  var purchase_request_view_table = $('#purchase_request_view_table').DataTable({
    "ajax":{
      url :"<?php echo base_url().'purchase_request/select_purchase_request' ?>", // json datasource
      type: "post", // type of method  , by default would be get
      data: function(d){
        <?php if($page_name=='purchase_request_view'){?>
          d.start_date = GetSearchStartDate();
          d.end_date = GetSearchEndDate();
          d.status = GetSearchstatus();
          d.location = GetSearchLocation();
          d.date_type = Getdatetype();
          <?php } ?>
          <?php if($page_name=='work_profile'){?>
            d.work = "<?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=='stage_profile' || $page_name=='stage_summary'){?>
              d.stage_id = "<?php echo $stage_data->stage_id?>";
              <?php } ?>

              <?php if($page_name=='reports_activity'){?>
                d.date = GetSearchDate();
                d.work = GetSearchWork();
                <?php } ?>
                <?php if($page_name=='user_profile'){?>
                  d.user_id = "<?php echo $user_data->user_id?>";
                  <?php } ?>

                }
              },
              "bProcessing": true,
              "deferRender": true,
              "bDeferRender": true,
              "scrollX" : true,
              buttons: [
                { extend: 'print', className: 'btn dark btn-outline',exportOptions: {columns: ':visible'},title: 'Purchase Requests' },
                { 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,11<?php if($page_name=='work_profile'){ echo ",3"; }?><?php if($page_name=='reports_activity'){ echo ",8"; }?> ],
                  className: "hidden"
                }
              ],
              'order': false
            });
            $(".table_filter").on("change", function() {
              purchase_request_view_table.ajax.reload();
            });

            $(".hidden").hide();

            <?php if($page_name=='purchase_request_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: {
                    "new_paid_amount": {
                      required: !0,
                      min: 0,
                      number: !0
                    }
                  }, 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("");
                      purchase_request_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>
