<script type="text/javascript">

$("#customer_add").select2({
  placeholder: "Select Service",
  dropdownParent: $("#form_enquiry_add")
})
$("#enquiry_mode_add").select2({
  placeholder: "Select Mode",
  dropdownParent: $("#form_enquiry_add")
})
$("#service_type_add").select2({
  placeholder: "Select Type",
  dropdownParent: $("#form_enquiry_add")
})
$("#service_add").select2({
  placeholder: "Select Service",
  dropdownParent: $("#form_enquiry_add")
})
$("#assign_to_add").select2({
  placeholder: "Select User",
  dropdownParent: $("#form_enquiry_add")
})

$("#enquiry_assign_to_add").select2({
  placeholder: "Select User",
  dropdownParent: $("#form_enquiry_assign_add")
})

$("#customer_edit").select2({
  placeholder: "Select Customer",
  dropdownParent: $("#form_enquiry_edit")
})
$("#service_edit").select2({
  placeholder: "Select Service",
  dropdownParent: $("#form_enquiry_edit")
})
$("#service_type_edit").select2({
  placeholder: "Select Service",
  dropdownParent: $("#form_enquiry_edit")
})
$("#enquiry_mode_edit").select2({
  placeholder: "Select Service",
  dropdownParent: $("#form_enquiry_edit")
})
$("#enquiry_id_response_add").select2({
  placeholder: "Select Enquiry",
  dropdownParent: $("#form_enquiry_response_add")
})
$("#enquiry_id_followup_add").select2({
  placeholder: "Select Enquiry",
  dropdownParent: $("#form_enquiry_followup_add")
})
$("#followup_status_edit").select2({
  placeholder: "Select Status",
  dropdownParent: $("#form_enquiry_followup_edit")
})
$("#table_filter_assigned_to").select2({
  placeholder: "Select Staff"
})
$("#table_filter_customer").select2({
  placeholder: "Select Customer"
})
$("#table_filter_service").select2({
  placeholder: "Select Service"
})
$("#table_filter_status").select2({
  placeholder: "Select Status"
})

$("#transfer_to_branch_add").select2({
  placeholder: "Select Branch",
  dropdownParent: $("#form_enquiry_transfer_add")
})

$("#enquiry_date_add,#enquiry_date_edit").datepicker( {
  todayHighlight:!0, orientation:"bottom right", templates: {
    leftArrow: '<i class="la la-angle-left"></i>', rightArrow: '<i class="la la-angle-right"></i>'
  }
})

var customer_id = '';
<?php if($page_name=='customer_profile'){?>
  customer_id = '<?php echo $customer_data->customer_id?>';
  <?php } ?>
  select_customers('customer_add','',customer_id);
  select_customers('customer_edit','',customer_id);
</script>
<script>
select_customers('table_filter_customer','filter');

$('body').on('click', '#enquiry_edit_btn', function() {
  var no = $(this).closest('tr').children('td');
  var id = $(this).data('id');
  $.post("<?php echo base_url() ?>enquiries/select_details",{id:id},function(data) {
    var obj = $.parseJSON(data);
    var i=0;
    if(obj.count==1) {
      $('#enquiry_id_edit').val(id);
      $('#enquiry_date_edit').datepicker("setDate",obj.data['enquiry_date']);
      $('#customer_edit').val(obj.data['customer_id']).trigger('change');
      $('#service_edit').val(obj.data['service']).trigger('change');
      $('#service_type_edit').val(obj.data['service_type']).trigger('change');
      $('#enquiry_mode_edit').val(obj.data['enquiry_mode']).trigger('change');
      $('input[type=radio][name=status_edit][value="'+obj.data['enquiry_status']+'"]').prop('checked',true);
      $('input[type=radio][name=priority_edit][value='+obj.data['enquiry_priority']+']').prop('checked',true);
      $('#remarks_edit').val(obj.data['remarks']);
      $('#service_details_edit').val(obj.data['service_details']);
      $('#observations_edit').val(obj.data['observations']);
      $('#actions_taken_edit').val(obj.data['actions_taken']);
      $('#nature_of_enquiry_edit').val(obj.data['nature_of_enquiry']);
      $('#reference_no_edit').val(obj.data['enquiry_reference_no']);
      $('#work_place_edit').val(obj.data['work_place']);
      $('#customer_requested_person_edit').val(obj.data['enq_requested_person']);
      $('#customer_requested_mobile_no_edit').val(obj.data['enq_requested_person_mobile']);
      $('#customer_contact_person_edit').val(obj.data['enq_contact_person']);
      $('#customer_contact_mobile_no_edit').val(obj.data['enq_contact_person_mobile']);
    }
  })



})
$('body').on('click', '#enquiry_delete_btn', function() {
  var no = $(this).closest('tr').children('td');
  $('#enquiry_delete_id').val(no.eq(25).text());
})
function select_users(select_id,branch_id='',user_role=''){
  $('#'+select_id).html('');
  $('#'+select_id).append("<option disabled selected >Select User</option>");

  $.post("<?php echo base_url() ?>user/select_users",{user_role:user_role,branch_id:branch_id},function(data) {
    var obj = $.parseJSON(data);
    var i=0;
    if(obj.count>0) {
      while (i < obj.data.length) {
        $('#'+select_id).append("<option value=" + obj.data[i]['user_id'] + ">" + obj.data[i]['user_alias'] + "</option>");
        i++;
      }
      if(selected_value!=''){
        $("#"+select_id).val(selected_value).trigger("change");
      }
    }
  })
}
$('body').on('click', '#enquiry_assign_btn', function() {
  var no = $(this).closest('tr').children('td');
  $('#enquiry_id_assign').val(no.eq(25).text());
  $('#enquiry_name_assign').val(no.eq(1).text()+' '+no.eq(6).text());
  $('#current_branch_assign').val(no.eq(2).text());
  $('#current_staff_assign').val(no.eq(5).text());
  var branch_id = no.eq(27).text();
  select_users('enquiry_assign_to_add',branch_id,'sales_staff')
})

$("#followup_date_add").datetimepicker({ startDate:new Date(),todayHighlight:!0,autoclose:!0,format:"dd-mm-yyyy hh:ii"})

$('body').on('click', '#enquiry_followup_edit_btn', function() {
  var no = $(this).closest('tr').children('td');
  $('#followup_id_edit').val(no.eq(1).text());
  $('#enquiry_details_followup_edit').val(no.eq(3).text()+" - "+no.eq(2).text());
  $('#remarks_followup_edit').val(no.eq(6).text());
  $('#followup_status_edit').val(no.eq(10).text()).trigger('change');
  $('#followup_date_edit').val(no.eq(5).text());
  $('#followup_date_edit').attr('readonly',true);
})
$('body').on('click', '#enquiry_followup_delete_btn', function() {
  var no = $(this).closest('tr').children('td');
  $('#enquiry_followup_delete_id').val(no.eq(1).text());
})

$('body').on('click', '#enquiry_response_edit_btn', function() {
  var no = $(this).closest('tr').children('td');
  $('#response_id_edit').val(no.eq(1).text());
  $('#enquiry_details_response_edit').val(no.eq(3).text()+" - "+no.eq(2).text());
  $('#response_edit').val(no.eq(5).text());
})
$('body').on('click', '#enquiry_response_delete_btn', function() {
  var no = $(this).closest('tr').children('td');
  $('#enquiry_response_delete_id').val(no.eq(1).text());
})
$('body').on('change', '#customer_add', function() {
  var contact_person = $('#customer_add :selected').data('contact-person');
  var phone = $('#customer_add :selected').data('phone');
  $("#customer_contact_person_add").val(contact_person);
  $("#customer_contact_mobile_no_add").val(phone);
})
function select_enquiries(select_id,selected_value=''){
  $('#'+select_id).html('');
  $("#"+select_id).append("<option disabled selected >Select Enquiry</option>");
  var customer_id = '';
  <?php if($page_name=='customer_profile'){?>
    customer_id = '<?php echo $customer_data->customer_id?>';
    <?php } ?>
    $.post("<?php echo base_url() ?>enquiries/select_enquiry",{customer_id:customer_id}
    ,function(data) {
      var obj = $.parseJSON(data);
      var i=0;
      if(obj.count>0) {
        while (i < obj.count) {
          $("#"+select_id).append("<option value=" + obj.data[i]['enquiry_id'] + ">" + obj.data[i]['customer_name'] +" [ "+ obj.data[i]['service_name'] +" ]"+ "</option>");
          i++;
        }
        if(selected_value!=''){
          $("#"+select_id).val(selected_value).trigger("change");
        }
      }
    })
  }
  select_enquiries("enquiry_id_response_add");
  select_enquiries("enquiry_id_followup_add");
</script>


<script>
var Datatable_enquiries_view_table=function() {
  var t=function() {
    function get_branch_id() {
      return $('#table_filter_company_branch :selected').val();
    }
    function get_assigned_to() {
      return $('#table_filter_assigned_to :selected').val();
    }
    function get_service() {
      return $('#table_filter_service :selected').val();
    }
    function get_customer() {
      return $('#table_filter_customer :selected').val();
    }
    function get_status() {
      return $('#table_filter_status :selected').val();
    }
    var enquiries_view_table = $('#enquiries_view_table').DataTable({
      "ajax":{
        url :"<?php echo base_url().'enquiries/select' ?>", // json datasource
        type: "post", // type of method  , by default would be get
        data: function(d){
          <?php if($page_name=='customer_profile'){?>
            d.customer_id = "<?php echo $customer_data->customer_id?>";
            <?php } ?>
            <?php if (!empty($this->config->item('company_branches'))) { ?>
              d.branch_id = get_branch_id();
              <?php } ?>
              d.assigned_to = get_assigned_to();
              d.status = get_status();
              d.customer_id = get_customer();
              d.service = get_service();
            }
          },
          "bProcessing": true,
          "deferRender": true,
          "bDeferRender": true,
          "scrollX" : true,
          buttons: [
            { extend: 'print', className: 'btn dark btn-outline', title: 'List of Sales Enquiries', },
            { 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": [22,23,24,25,26,27<?php if($page_name=='customer_profile'){ echo ",5"; } ?><?php if($this->session->userdata('user_role') == 'auditor'){ echo ",21"; }?><?php if (empty($this->config->item('company_branches'))) { echo ",2"; }?> ],
              className: "hidden"
            }
          ],
          'order': false
        });
        $(".table_filter").on("change", function() {
          enquiries_view_table.ajax.reload();
        });

      };
      return {
        init:function() {
          t()
        }
      }
    }
    ();
    var Datatable_enquiry_response_view_table=function() {
      var a=function() {
        var enquiry_response_view_table = $('#enquiry_response_view_table').DataTable({
          "ajax":{
            url :"<?php echo base_url().'enquiries/select_enquiry_responses' ?>", // json datasource
            type: "post", // type of method  , by default would be get
            data: function(d){
              <?php if($page_name=='customer_profile'){?>
                d.customer_id = "<?php echo $customer_data->customer_id?>";
                <?php } ?>
                <?php if($page_name=='enquiries_profile'){?>
                  d.enquiry_id = "<?php echo $enquiry_data->enquiry_id?>";
                  <?php } ?>
                }
              },
              "bProcessing": true,
              "deferRender": true,
              "bDeferRender": true,
              "scrollX" : true,
              buttons: [
                { extend: 'print', className: 'btn dark btn-outline',title: 'Enquiry Reponses' },
                { 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": [1<?php if($page_name=='customer_profile'){ echo ",3"; } ?><?php if (empty($this->config->item('company_branches'))){ echo ",2"; }?>],
                  className: "hidden"
                }
              ],
              'order': false
            });
            $(".table_filter").on("change", function() {
              enquiry_response_view_table.ajax.reload();
            });
          };
          return {
            init:function() {
              a()
            }
          }
        }
        ();
        var Datatable_enquiry_followups_view_table=function() {
          var a=function() {
            var enquiry_followups_view_table = $('#enquiry_followups_view_table').DataTable({
              "ajax":{
                url :"<?php echo base_url().'enquiries/select_enquiry_followups' ?>", // json datasource
                type: "post", // type of method  , by default would be get
                data: function(d){
                  <?php if($page_name=='customer_profile'){?>
                    d.customer_id = "<?php echo $customer_data->customer_id?>";
                    <?php } ?>
                    <?php if($page_name=='enquiries_profile'){?>
                      d.enquiry_id = "<?php echo $enquiry_data->enquiry_id?>";
                      <?php } ?>
                    }
                  },
                  "bProcessing": true,
                  "deferRender": true,
                  "bDeferRender": true,
                  "scrollX" : true,
                  buttons: [
                    { extend: 'print', className: 'btn dark btn-outline',title: 'Enquiry Followups' },
                    { 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": [1,10,<?php if($page_name=='customer_profile'){?>,2 <?php } ?>],
                      className: "hidden"
                    }
                  ],
                  'order': false
                });
                $(".table_filter").on("change", function() {
                  enquiry_followups_view_table.ajax.reload();
                });
              };
              return {
                init:function() {
                  a()
                }
              }
            }
            ();
            var Datatable_enquiry_assign_logs_view_table=function() {
              var a=function() {
                var enquiry_assign_logs_view_table = $('#enquiry_assign_logs_view_table').DataTable({
                  "ajax":{
                    url :"<?php echo base_url().'enquiries/select_enquiry_assign_logs' ?>", // json datasource
                    type: "post", // type of method  , by default would be get
                    data: function(d){
                      <?php if($page_name=='customer_profile'){?>
                        d.customer_id = "<?php echo $customer_data->customer_id?>";
                        <?php } ?>
                        <?php if($page_name=='enquiries_profile'){?>
                          d.enquiry_id = "<?php echo $enquiry_data->enquiry_id?>";
                          <?php } ?>
                        }
                      },
                      "bProcessing": true,
                      "deferRender": true,
                      "bDeferRender": true,
                      "scrollX" : true,
                      buttons: [
                        { extend: 'print', className: 'btn dark btn-outline',title: 'Enquiry Transfers' },
                        { 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": [],
                          className: "hidden"
                        }
                      ],
                      'order': false
                    });
                    $(".table_filter").on("change", function() {
                      enquiry_transfer_view_table.ajax.reload();
                    });
                  };
                  return {
                    init:function() {
                      a()
                    }
                  }
                }
                ();
                var Datatable_enquiry_transfer_view_table=function() {
                  var a=function() {
                    var enquiry_transfer_view_table = $('#enquiry_transfer_view_table').DataTable({
                      "ajax":{
                        url :"<?php echo base_url().'enquiries/select_enquiry_transfer' ?>", // json datasource
                        type: "post", // type of method  , by default would be get
                        data: function(d){
                          <?php if($page_name=='customer_profile'){?>
                            d.customer_id = "<?php echo $customer_data->customer_id?>";
                            <?php } ?>
                            <?php if($page_name=='enquiries_profile'){?>
                              d.enquiry_id = "<?php echo $enquiry_data->enquiry_id?>";
                              <?php } ?>
                            }
                          },
                          "bProcessing": true,
                          "deferRender": true,
                          "bDeferRender": true,
                          "scrollX" : true,
                          buttons: [
                            { extend: 'print', className: 'btn dark btn-outline',title: 'Enquiry Transfers' },
                            { 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": [1],
                              className: "hidden"
                            }
                          ],
                          'order': false
                        });
                        $(".table_filter").on("change", function() {
                          enquiry_transfer_view_table.ajax.reload();
                        });
                      };
                      return {
                        init:function() {
                          a()
                        }
                      }
                    }
                    ();
                    jQuery(document).ready(function() {
                      <?php if($page_name!='enquiries_profile'){?>
                        Datatable_enquiries_view_table.init();
                        <?php } ?>
                        <?php if($page_name!='enquiries_view'){?>
                          Datatable_enquiry_response_view_table.init();
                          Datatable_enquiry_followups_view_table.init();
                          Datatable_enquiry_transfer_view_table.init();
                          <?php } ?>
                          $(".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: {
                                "mobile_number": {
                                  required: !0,
                                  number: !0,
                                  minlength: 8,
                                  maxlength: 12
                                }
                              }, 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");
                                  <?php if($page_name!='enquiries_profile'){?>
                                    $('#enquiries_view_table').dataTable().fnDestroy();
                                    Datatable_enquiries_view_table.init();
                                    <?php } ?>
                                    <?php if($page_name!='enquiries_view'){?>
                                      $('#enquiry_response_view_table').dataTable().fnDestroy();
                                      $('#enquiry_followups_view_table').dataTable().fnDestroy();
                                      $('#enquiry_transfer_view_table').dataTable().fnDestroy();
                                      Datatable_enquiry_response_view_table.init();
                                      Datatable_enquiry_followups_view_table.init();
                                      Datatable_enquiry_transfer_view_table.init();
                                      <?php } ?>
                                      <?php if($page_name=='enquiries_profile'){ ?>
                                        location.reload();
                                        <?php } ?>
                                        select_customers('customer_add');
                                        select_customers('customer_edit');
                                        if(form_id=='form_enquiry_transfer_add'){
                                          window.location.replace("<?php echo base_url()?>enquiries");
                                        }
                                      } 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");
                                    }
                                  })
                                )
                              });
                            });


                            </script>
