<script>
  <?php if ($page_name == 'security_deposits') { ?>
    $("#security_deposit_work_add").select2({
      placeholder: "Select Work",
      dropdownParent: $("#form_security_deposit_add"),
      templateResult: format_project_dropdown
    })
    $('body').on('change', '#security_deposit_work_add', function() {
      var work_id = $('#security_deposit_work_add :selected').val();
      $.post("<?php echo base_url() ?>work/select_details", {
        work_id: work_id
      }, function(data) {
        var obj = $.parseJSON(data);
        var i = 0;
        if (obj.count == 1) {
          $("#security_deposit_client_name_add").val(obj.data['client_name']);
          $("#security_deposit_client_id_add").val(obj.data['client_id']);
        }
      })
    })
    $("#security_deposit_work_edit").select2({
      placeholder: "Select Work",
      dropdownParent: $("#form_security_deposit_edit"),
      templateResult: format_project_dropdown
    })
    $('body').on('change', '#security_deposit_work_edit', function() {
      var work_id = $('#security_deposit_work_edit :selected').val();
      $.post("<?php echo base_url() ?>work/select_details", {
        work_id: work_id
      }, function(data) {
        var obj = $.parseJSON(data);
        var i = 0;
        if (obj.count == 1) {
          $("#security_deposit_client_name_edit").val(obj.data['client_name']);
          $("#security_deposit_client_id_edit").val(obj.data['client_id']);
        }
      })
    })
  <?php } ?>
  $("#security_deposit_date_add,#security_deposit_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>'
    },
    endDate: '+0d',
    <?php if ($this->session->userdata('user_role') != 'admin' && $this->config->item('User_calendar_date_restriction_enabled') == 1) { ?>
      startDate: '<?php echo $this->config->item('User_calendar_date_restriction_start_days'); ?>d'
    <?php } ?>
  })
  $('body').on('click', '#security_deposit_edit_btn', function() {
    var no = $(this).closest('tr').children('td');
    $('#deposit_id_edit').val(no.eq(2).text());
    $('#security_deposit_edit').val(no.eq(7).text());
    $("#security_deposit_date_edit").datepicker("setDate", no.eq(4).text());
    $('#retention_money_edit').val(no.eq(8).text());
    $('#ld_amount_edit').val(no.eq(9).text());
    $('#income_tax_edit').val(no.eq(10).text());
    $('#gst_tds_edit').val(no.eq(11).text());
    $('#security_deposit_work_edit').val(no.eq(14).text()).trigger('change');
  })
  $('body').on('click', '#security_deposit_delete_btn', function() {
    var no = $(this).closest('tr').children('td');
    $('#deposit_id_delete').val(no.eq(2).text());
  })

  function get_branch_id() {
    return $('#company_branch :selected').val();
  }
  <?php if ($page_name == 'work_summary') { ?>
    function GetStartDate() {
      return $('#table_filter_start_date').val();
    }
    function GetEndDate() {
      return $('#table_filter_end_date').val();
    }
    <?php } ?>
  var security_deposits_view_table = $('#security_deposits_view_table').DataTable({
    "ajax": {
      url: "<?php echo base_url() . 'accounts/select_security_deposit' ?>", // json datasource
      type: "post", // type of method  , by default would be get
      data: function(d) {
        <?php if ($page_name == 'work_profile' || $page_name == 'projects_profile' || $page_name == 'work_summary') { ?>
          d.work = "<?php echo $work_data->work_id ?>";
          d.clients = "<?php echo $work_data->client_id ?>";
        <?php } elseif ($page_name == 'security_deposits') { ?>
          <?php if (!empty($this->config->item('company_branches'))) { ?>
            d.branch_id = get_branch_id();
          <?php } ?>
        <?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 == 'work_summary') { ?>
          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',
        title: 'Security Deposits'
      },
      {
        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>>",
    "lengthMenu": [
      [10, 15, 20, -1],
      [10, 15, 20, "All"] // change per page values here
    ],
    <?php } ?>
    "columnDefs": [{
      "targets": [2, 3, 14 <?php if ($page_name == 'work_profile') {
                              echo ",5,6";
                            } ?><?php if ($page_name == 'work_summary') {
                              echo ",5,6,13";
                            } ?> <?php if (empty($this->config->item('company_branches'))) {
                                    echo ",1";
                                  } ?>],
      className: "hidden"
    }],
    'order': false
  });

  $(".table_filter").on("change", function() {
    security_deposits_view_table.ajax.reload();
  });
</script>
