<script>
<?php if ($page_name == 'subcontract_payments') { ?>

  function GetSearchDate() {
    return $('#table_filter_date_range').val();
  }

  function GetSearchWork() {
    return $('#table_filter_work').val();
  }
  function GetSearchPaymentStatus() {
    return $('#table_filter_subcontract_bill_payment_status').val();
  }
  function GetSearchWorktype() {
    return $('#table_filter_work_type').val();
  }
  function GetSearchContractor() {
    return $('#table_filter_contractor').val();
  }
  function get_branch_id() {
    return $('#company_branch :selected').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();
    }
    <?php } ?>
    <?php if ($page_name == 'work_summary') { ?>
      function GetStartDate() {
        return $('#table_filter_start_date').val();
      }
      function GetEndDate() {
        return $('#table_filter_end_date').val();
      }
      <?php } ?>
    <?php
    $page_type = '';
    if ($page_name == 'data_entry_approval') {
      $page_type = 'approval_entries';
    }
    ?>
    var subcontract_bills_view_table = $('#subcontract_bills_view_table').DataTable({
      "ajax": {
        url: "<?php echo base_url() . 'subcontracts/select_subcontract_bills/' . $page_type ?>", // json datasource
        type: "post", // type of method  , by default would be get
        data: function(d) {
          <?php if ($page_name == 'subcontract_profile') { ?>
            d.contract_id = "<?php echo $subcontract_data->contract_id ?>";
            <?php } ?>
            <?php if ($page_name == 'subcontract_payments') { ?>
              d.date = GetSearchDate();
              d.work = GetSearchWork();
              d.contractor = GetSearchContractor();
              d.work_type = GetSearchWorktype();
              d.payment_status = GetSearchPaymentStatus();
              <?php if (!empty($this->config->item('company_branches'))) { ?>
                d.branch_id = get_branch_id();
                <?php } ?>
                <?php } ?>
                <?php if ($page_name == 'data_entry_approval') { ?>
                  d.start_date = GetStartDate();
                  d.end_date = GetEndDate();
                  d.work = GetSearchWork();
                  d.approval_status = GetSearchApprovalStatus();
                  d.user_id = GetSearchUser();
                  <?php } ?>
                  <?php if ($page_name == 'work_summary') { ?>
                    d.work = "<?php echo $work_data->work_id ?>";
                    d.start_date = GetStartDate();
                    d.end_date = GetEndDate();
                    <?php } ?>
                }
              },
              "bProcessing": true,
              "deferRender": true,
              "bDeferRender": true,
              <?php if ($page_name == 'work_summary') { ?> "scrollX": false,
              bFilter: false,
              "bLengthChange": false,
              "bPaginate": false,
              "bInfo": false,
              buttons: [{
                extend: 'colvis',
                className: 'btn dark btn-outline',
                text: 'Columns'
              }],
              "dom": "<'row' <'col-md-12'B>><'row'<'col-md-6 col-sm-12'l><'col-md-6 col-sm-12'f>r><'table-scrollable't><'row'<'col-md-5 col-sm-12'i><'col-md-7 col-sm-12'p>>", // horizobtal scrollable datatable
              <?php } else { ?>
              "scrollX": true,
              buttons: [{
                extend: 'print',
                className: 'btn dark btn-outline',
                exportOptions: {
                  columns: ':visible'
                },
                title: 'Subcontract Bills',
                <?php if($this->config->item('company_letter_head')!=''){ ?>
                  customize: function ( win ) {
                    $(win.document.body).prepend(
                      '<img src="<?php echo base_url().'uploads/company/'.$this->config->item('company_letter_head')?>" style="top:0; left:0;width:100%;" />'
                    );
                  }
                  <?php } ?>
                },
                {
                  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
              ],
              <?php } ?>
              "columnDefs": [{
                "targets": [34, 35, 36,37,38 <?php if ($this->config->item('Tax_tax_splitting') == 0) {
                  echo ",13,14,15,16,17,18";
                } ?> <?php if (empty($this->config->item('company_branches'))) { echo ",1"; }?><?php if ($page_name == 'work_summary') {
                  echo ",5,33";
                } ?>
              ],
              className: "hidden"
            }],
            'order': false,
            "fnRowCallback": function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
              if (aData[38] == '0') {
                $('td', nRow).css('background-color', '#ffcccc');
              } else if (aData[38] == '2') {
                $('td', nRow).css('background-color', '#ffcccc');
              }
            }
          });


          $("#subcontract_bill_tax_percent_edit").select2({
            placeholder: "Select Tax %",
            dropdownParent: $("#form_subcontract_bill_edit")
          })

          function calculate_subcontract_bill_balance(form_type = '') {
            if (form_type == 'edit') {
              var payable_amount = ($("#subcontract_bill_payable_amount_edit").val() == '') ? 0 : parseFloat($("#subcontract_bill_payable_amount_edit").val());
              var paid_amount = ($("#subcontract_bill_total_paid_edit").val() == '') ? 0 : parseFloat($("#subcontract_bill_total_paid_edit").val());
              $("#subcontract_bill_total_balance_edit").val(payable_amount - paid_amount);
            } else {
              var payable_amount = ($("#subcontract_bill_payable_amount_add").val() == '') ? 0 : parseFloat($("#subcontract_bill_payable_amount_add").val());
              var paid_amount = ($("#subcontract_bill_paid_amount_add").val() == '') ? 0 : parseFloat($("#subcontract_bill_paid_amount_add").val());
              $("#subcontract_bill_balance_amount_add").val(payable_amount - paid_amount);
            }
          }

          $('body').on('change keyup', '.form_subcontract_bill_amount_fields_edit', function() {
            var value = $(this).val();
            if (value == "" || value == null) {
              $(this).val(0);
            }
            var bill_gross_amount = parseFloat($('#subcontract_bill_gross_amount_edit').val());
            var tax_percent = parseFloat($('#subcontract_bill_tax_percent_edit :selected').data('tax-percent'));
            var bill_tax_amount = parseFloat(bill_gross_amount * tax_percent / 100);
            $('#subcontract_bill_tax_amount_edit').val(bill_tax_amount.toFixed(<?php echo $this->config->item('Currency_amount_decimals_2') ?>));
            var bill_tds_amount = parseFloat($('#subcontract_bill_tds_amount_edit').val());
            var bill_deduction_amount = parseFloat($('#subcontract_bill_deduction_amount_edit').val());
            var bill_retention_percent = parseFloat($('#subcontract_bill_retention_percent_edit').val());
            var payable_amount = bill_gross_amount + bill_tax_amount;
            var retention_amount = payable_amount*bill_retention_percent/100;
            payable_amount = payable_amount-retention_amount-bill_tds_amount-bill_deduction_amount;
            $('#subcontract_bill_retention_amount_edit').val(retention_amount.toFixed(<?php echo $this->config->item('Currency_amount_decimals_2')?>));
            $('#subcontract_bill_payable_amount_edit').val(payable_amount.toFixed(<?php echo $this->config->item('Currency_amount_decimals_2') ?>));

            calculate_subcontract_bill_balance('edit');
          })
          $('body').on('click', '#subcontract_bill_edit_btn', function() {
            var no = $(this).closest('tr').children('td');
            var bill_type = $(this).attr('data-bill-type');
            var bill_id_column_no = $(this).attr('data-bill-id-column');
            var bill_id = no.eq(bill_id_column_no).text();
            $.post("<?php echo base_url() ?>bills/get_details", {
              bill_id: bill_id,
              bill_type: bill_type
            }, function(data) {
              var obj = $.parseJSON(data);
              if (obj.count == 1) {
                $('#contract_id_bill_edit').val("#" + obj.contract_id);
                $('#subcontract_bill_project_edit').val(obj.project_name);
                $('#subwork_name_bill_edit').val(obj.subwork_name);
                $('#subwork_type_bill_edit').html(obj.subwork_types);
                $('#subcontract_id_bill_edit').val(no.eq(35).text());
                $('#bill_id_edit').val(no.eq(34).text());
                $('#subcontract_bill_consignee_edit').val(no.eq(9).text());
                $('#subcontract_bill_description_edit').val(no.eq(27).text());
                $('#subcontract_bill_no_edit').val(no.eq(8).text());
                $('#subcontract_bill_date_edit').val(no.eq(3).text());
                $('#subcontract_bill_gross_amount_edit').val(no.eq(10).text());
                $('#subcontract_bill_tax_amount_edit').val(no.eq(12).text());
                $('#subcontract_bill_tds_amount_edit').val(no.eq(19).text());
                $('#subcontract_bill_retention_percent_edit').val(no.eq(20).text());
                $('#subcontract_bill_retention_amount_edit').val(no.eq(21).text());
                $('#subcontract_bill_deduction_amount_edit').val(no.eq(22).text());
                $('#subcontract_bill_payable_amount_edit').val(no.eq(23).text());
                $('#subcontract_bill_total_paid_edit').val(no.eq(24).text());
                $('#subcontract_bill_total_balance_edit').val(no.eq(25).text());
                $('#subcontract_bill_remarks_edit').val(no.eq(30).text());
                $('#subcontract_labours_no_edit').val(no.eq(29).text());
                $('#subcontract_bill_tax_percent_edit').val(no.eq(37).text()).trigger('change');
              }
            })
            // $('#subcontract_id_bill_add').val(no.eq(20).text());
            // $('#contract_id_bill_add').val(no.eq(1).text());
            // $('#subcontract_bill_project_add').val(no.eq(2).text());
            // $('#subwork_name_bill_add').val(no.eq(5).text());
            // $('#subwork_type_bill_add').html(no.eq(3).html());
            // $('#subcontract_bill_consignee_add').val(no.eq(6).text());
            // $('#subcontract_bill_description_add').val(no.eq(7).text());
          })
        </script>
