<script>
  $("#custom_requiremnt_work_add").select2({
    placeholder: "Select  Project",
    dropdownParent: $("#form_project_custom_requirements_add"),
    templateResult: format_project_dropdown
  })
  $("#custom_requiremnt_type_add").select2({
    placeholder: "Select Type",
    dropdownParent: $("#form_project_custom_requirements_add")
  })
  $("#custom_requiremnt_work_edit").select2({
    placeholder: "Select  Project",
    dropdownParent: $("#form_project_custom_requirements_edit"),
    templateResult: format_project_dropdown
  })
  $("#custom_requiremnt_type_edit").select2({
    placeholder: "Select Type",
    dropdownParent: $("#form_project_custom_requirements_edit")
  })
  $("#custom_requirement_date_add,#custom_requirement_date_edit").datepicker({
    todayHighlight: !0,
    orientation: "bottom left",
    templates: {
      leftArrow: '<i class="la la-angle-left"></i>',
      rightArrow: '<i class="la la-angle-right"></i>'
    }
  })
  $("#custom_requirement_tax_percent_add").select2({
    placeholder: "Select Tax %",
    dropdownParent: $("#form_project_custom_requirements_add")
  })
  $("#custom_requirement_tax_percent_edit").select2({
    placeholder: "Select Tax %",
    dropdownParent: $("#form_project_custom_requirements_edit")
  })

  function GetSearchwork() {
    return $('#table_filter_work').val();
  }

  function get_branch_id() {
    return $('#company_branch :selected').val();
  }
  var project_custom_requirements_view_table = $('#project_custom_requirements_view_table').DataTable({
    "ajax": {
      url: "<?php echo base_url() . 'work/select_custom_requirements' ?>", // json datasource
      type: "post", // type of method  , by default would be get
      data: function(d) {
        <?php if ($page_name == 'work_profile') { ?>
          d.work = "<?php echo $work_data->work_id ?>";
        <?php }elseif($page_name=='work_profile_master' || $page_name=='work_summary_master'){?>
          d.work_ids = "<?php echo implode(',',$sub_project_ids)?>";
          <?php }else { ?>
          // d.date = GetSearchDate();
          d.work = GetSearchwork();
          <?php if (!empty($this->config->item('company_branches'))) { ?>
            d.branch_id = get_branch_id();
          <?php } ?>
        <?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: 'Custom Requirements'
      },
      {
        extend: 'csv',
        className: 'btn purple btn-outline ',
        exportOptions: {
          columns: ':visible'
        }
      },
      {
        extend: 'pdf',
        className: 'btn purple btn-outline ',
        exportOptions: {
          columns: ':visible'
        }
      },
      {
        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": [2,14,15,16 <?php if ($page_name == 'work_profile' || $page_name == 'projects_profile') {
                                echo ",3";
                              } ?> <?php if (empty($this->config->item('company_branches'))) { echo ",1"; }?> ],
        className: "hidden"
      },
      {
        render: function(data, type, full, meta) {
          return "<div class='text-wrap width-400'>" + data + "</div>";
        },
        targets: 10
      }
    ],
    'order': false
  });
  $(".table_filter").on("change", function() {
    project_custom_requirements_view_table.ajax.reload();
  });
  $(".hide_column").hide();
  <?php if ($page_name == 'project_custom_requirements') { ?>

    $(".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: {

        },
        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");
              project_custom_requirements_view_table.ajax.reload();
              $("#" + modal_id).modal('toggle');
            } 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 } ?>
  $('body').on('keyup change', '#custom_requirement_taxable_amount_add,#custom_requirement_tax_percent_add', function() {
    var tax_percent = parseFloat($('#custom_requirement_tax_percent_add :selected').data('tax-percent'));
    var taxable_amount = parseFloat($("#custom_requirement_taxable_amount_add").val());
    var tax_amount = taxable_amount*tax_percent/100;
    var total_amount = taxable_amount+tax_amount;
    $("#custom_requirement_amount_add").val(total_amount.toFixed(<?php echo $this->config->item('Currency_amount_decimals_2')?>));
  })
  $('body').on('keyup change', '#custom_requirement_taxable_amount_edit,#custom_requirement_tax_percent_edit', function() {
    var tax_percent = parseFloat($('#custom_requirement_tax_percent_edit :selected').data('tax-percent'));
    var taxable_amount = parseFloat($("#custom_requirement_taxable_amount_edit").val());
    var tax_amount = taxable_amount*tax_percent/100;
    var total_amount = taxable_amount+tax_amount;
    $("#custom_requirement_amount_edit").val(total_amount.toFixed(<?php echo $this->config->item('Currency_amount_decimals_2')?>));
  })
  $('body').on('click', '#project_custom_requirement_edit_btn', function() {
    var no = $(this).closest('tr').children('td');
    $('#custom_requiremnt_work_edit').val(no.eq(14).text()).trigger('change');
    $('#custom_requirement_title_edit').val(no.eq(4).text());
    $('#custom_requirement_taxable_amount_edit').val(no.eq(7).text());
    $('#custom_requirement_tax_percent_edit').val(no.eq(16).text()).trigger('change');
    $("#custom_requirement_date_edit").datepicker("setDate", no.eq(5).text());
    $('#custom_requiremnt_type_edit').val(no.eq(6).text()).trigger('change');
    $('#custom_requirement_description_edit').val(no.eq(10).text());
    $('#custom_requirement_amount_edit').val(no.eq(9).text());
    $("input[name=requirement_status_edit][value=" + no.eq(15).text() + "]").prop('checked', true);
    $('#custom_requirement_id_edit').val(no.eq(2).text());
  })
  $('body').on('click', '#project_custom_requirement_delete_btn', function() {
    var no = $(this).closest('tr').children('td');
    $('#custom_requirement_id_delete').val(no.eq(2).text());
  })
</script>
