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

  $("#table_filter_user,#table_filter_user_2").select2({
    placeholder: "Select  User"
  })
  $("#state_id_add").select2({
    placeholder: "Select State Code",
    dropdownParent: $("#form_branch_add")
  })
  $("#state_id_edit").select2({
    placeholder: "Select State Code",
    dropdownParent: $("#form_branch_edit")
  })

  $("#sale_price_category_add").select2({
    placeholder: "Select Sale Price",
    dropdownParent: $("#form_branch_add")
  })
  $("#sale_price_category_edit").select2({
    placeholder: "Select Sale Price",
    dropdownParent: $("#form_branch_edit")
  })

  function GetSearchUser() {
    return $('#table_filter_user').val();
  }

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

  function GetSearchUser2() {
    return $('#table_filter_user_2').val();
  }

  function GetSearchwork2() {
    return $('#table_filter_work_2').val();
  }

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

  <?php } ?>


  var branches_view_table = $('#branches_view_table').DataTable({
    "ajax": {
      url: "<?php echo base_url() . 'branches/select_branches' ?>", // json datasource
      type: "post", // type of method  , by default would be get
      data: function(d) {

      }

    },
    "bProcessing": true,
    "deferRender": true,
    "bDeferRender": true,
    "scrollX": true,
    buttons: [{
      extend: 'print',
      className: 'btn dark btn-outline',
      exportOptions: {
        columns: ':visible'
      },title: 'Branches List'
    },
    {
      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": [18,19],
    className: "hidden"
  },
  {
    render: function (data, type, full, meta) {
      return "<div class='text-wrap width-200'>" + data + "</div>";
    },
    targets: [ 13 ],
  },
],
  'order': false
});

$(".table_filter").on("change", function() {
  branches_view_table.ajax.reload();
  // document_revisions_view_table.ajax.reload();
});
$(".hide_column").hide();
<?php if ($page_name == 'branches_view') { ?>
  // Date Range Picker
  var start = moment();
  var end = moment();

  function cb(start, end) {
    $('#table_filter_date_range').val(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
    branches_view_table.ajax.reload();
  }

  $('#table_filter_date_range').daterangepicker({
    buttonClasses: "m-btn btn",
    applyClass: "btn-primary",
    cancelClass: "btn-secondary",
    startDate: start,
    endDate: end,
    locale: {
      format: 'MMMM D, YYYY'
    },
    ranges: {
      'Today': [moment(), moment()],
      'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
      'Last 7 Days': [moment().subtract(6, 'days'), moment()],
      'Last 30 Days': [moment().subtract(29, 'days'), moment()],
      'This Month': [moment().startOf('month'), moment().endOf('month')],
      'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')],
      'All time': [moment('1970-01-01'), moment().add(9999, 'days')],
    }
  }, cb);

  cb(start, end);

  $(".form_submit_btn").click(function(e) {

    console.log('TEST');

    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');
        console.log(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");
          branches_view_table.ajax.reload();
          console.log(modal_id);
        } 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 } ?>
$("[data-switch=true]").bootstrapSwitch()


$('body').on('click', '#branch_edit_btn', function() {
  var no = $(this).closest('tr').children('td');
  // $('#branch_name_edit').val(no.eq(1).text());
  // $('#branch_email_edit').val(no.eq(2).text());
  // $('#branch_id_edit').val(no.eq(15).text());
  // $('#branch_status_edit').bootstrapSwitch('state', parseInt(no.eq(16).text()), true);
  // $("#quotation_header_edit").attr('src', no.eq(5).find('img').attr('src'));
  // $("#quotation_footer_edit").attr('src', no.eq(6).find('img').attr('src'));
  // $("#workorder_header_edit").attr('src', no.eq(7).find('img').attr('src'));
  // $("#workorder_logo_edit").attr('src', no.eq(8).find('img').attr('src'));
  // $("#workorder_footer_edit").attr('src', no.eq(9).find('img').attr('src'));
});

$('body').on('click', '#branch_delete_btn', function() {
    var no = $(this).closest('tr').children('td');
    $('#branch_id_delete').val(no.eq(18).text());
})

$('#branch_add_modal').on('hidden.bs.modal', function() {
  $(this).find('form').trigger('reset');
})
</script>
