<script>
<?php if ($page_name == 'security_deposits') { ?>
  $("#emd_work_add").select2({
    placeholder: "Select Work",
    dropdownParent: $("#form_emd_add"),
    templateResult: format_project_dropdown
  })
  $('body').on('change', '#emd_work_add', function() {
    var work_id = $('#emd_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) {
        $("#emd_client_name_add").val(obj.data['client_name']);
        $("#emd_client_id_add").val(obj.data['client_id']);
      }
    })
  })
  $("#emd_work_edit").select2({
    placeholder: "Select Work",
    dropdownParent: $("#form_emd_edit"),
    templateResult: format_project_dropdown
  })
  $('body').on('change', '#emd_work_edit', function() {
    var work_id = $('#emd_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) {
        $("#emd_client_name_edit").val(obj.data['client_name']);
        $("#emd_client_id_edit").val(obj.data['client_id']);
      }
    })
  })
  <?php } ?>
  $("#emd_date_add,#emd_date_edit,#emd_expiry_date_add,#emd_expiry_date_edit,#emd_renewal_date_add,#emd_renewal_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>'
    },
  })
  $('body').on('click', '#emd_edit_btn', function() {
    var no = $(this).closest('tr').children('td');
    $('#emd_id_edit').val(no.eq(2).text());
    $("#emd_date_edit").datepicker("setDate", no.eq(3).text());
    $("#emd_expiry_date_edit").datepicker("setDate", no.eq(9).text());
    $("#emd_renewal_date_edit").datepicker("setDate", no.eq(10).text());
    $('#emd_remarks_edit').val(no.eq(11).text());
    $('#emd_amount_edit').val(no.eq(8).text());
    $('#bg_no_edit').val(no.eq(7).text());
    $('#emd_authority_edit').val(no.eq(4).text());
    $('#emd_project_details_edit').val(no.eq(15).text());
    $('#emd_work_edit').val(no.eq(14).text()).trigger('change');
  })
  $('body').on('click', '#emd_delete_btn', function() {
    var no = $(this).closest('tr').children('td');
    $('#emd_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 emd_view_table = $('#emd_view_table').DataTable({
      "ajax": {
        url: "<?php echo base_url() . 'accounts/select_security_deposit_emd' ?>", // 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=='work_profile_master' || $page_name=='work_summary_master'){?>
                d.work_ids = "<?php echo implode(',',$sub_project_ids)?>";
                <?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_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: 'List of EMD'
                },
                {
                  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, 14, 15<?php if (empty($this->config->item('company_branches'))) {
                  echo ",1";
                } ?><?php if ($page_name == 'work_summary') {
                  echo ",5,6,13";
                } ?>],
                className: "hidden"
              },
              {
                render: function(data, type, full, meta) {
                  return "<div class='text-wrap width-400'>" + data + "</div>";
                },
                targets: [5],
              },
            ],
            'order': false
          });

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