jQuery(document).ready(function(){
    jQuery('<div class="overlay"></div>').insertBefore(".content-wrapper");
});

function setImg(input,id) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {
            $('#'+id).attr('src', e.target.result);
        };
        reader.readAsDataURL(input.files[0]);
    }
}

function setMultiImg(input,thisObj){
  if (input.files && input.files[0]) {
    var reader = new FileReader();

    reader.onload = function (e) {
      var count = thisObj.attr('count');
      thisObj.attr('count',count+1);
      jQuery('[id="multipleImageInputCntr"]').append(jQuery('[id="multipleImageInput"]').html().replace(/{:count}/g,count+1));

      thisObj.addClass('prevent-click');
      jQuery('[id="multiImageClose_'+count+'"]').removeClass('hide');
      jQuery('[id="multiImageImg_'+count+'"]').attr('src', e.target.result);
      jQuery('[id^="multiImageImg_"]').removeClass('errorBorder');
    };
    reader.readAsDataURL(input.files[0]);
  }
}

function removeImage(count){
  jQuery('[id="multiImageCntr_'+count+'"]').remove();
}

function setModal(header_msg,body_msg){
    jQuery('[id="modal_body_msg"]').html(body_msg);
    jQuery('[id="modal_header_msg"]').html(header_msg);
    jQuery('[id="errModal"]').modal('show');
}

function slideTo(id){
    jQuery('html, body').animate({
        scrollTop: jQuery('[id="'+id+'"]').offset().top
    }, 800);
}

function modalTrigger(header,body_html){
    jQuery('[id="modal_content"]').html(body_html);
    jQuery('[id="modal_header"]').html(header);

    jQuery('[id="popup_modal"]').modal('show');
}

function modalHide(){
    jQuery('[id="popup_modal"]').modal('hide');
}

function addModalLoader(){
    jQuery("[id='modal_content']").addClass('relative height_200');
    jQuery("[id='modal_content']").prepend("<div id='modal_loader_body' class='loader'></div>");
}

function remModalLoader(){
    jQuery("[id='modal_loader_body']").remove();
    jQuery("[id='modal_content']").removeClass('relative height_200');
}

function showFullScreenLoader(){
    var thisObj = jQuery('.overlay');
    thisObj.css("display",'block');

    thisObj.addClass('relative');
    thisObj.prepend("<div id='fullScreenLoaderBody' class='loader'></div>");
}

function remFullScreenLoader(){
    var thisObj = jQuery('.overlay');
    thisObj.css("display",'none');

    jQuery('[id="fullScreenLoaderBody"]').remove();
    thisObj.removeClass('relative');
}

function viewImageModal(title,img_src){
	if(title=='' || title==undefined || title=='undefined' || title==null || title=='null'||
	   img_src=='' || img_src==undefined || img_src=='undefined' || img_src==null || img_src=='null'){
		return false;
	}
	body_html = '<div style="text-align:center">'+
				  '<img src="'+img_src+'" onerror="this.src=\''+base_url+'assets/images/no_image.png\';" height="400px" width="auto">'+
   		  		'</div>';
	modalTrigger(title,body_html);
}

function initLocSearch_1() {
 var input = document.getElementById('loc_search_1');
 var options = {componentRestrictions: {country: country_flag}};
 var autocomplete = new google.maps.places.Autocomplete(input, options);
}
google.maps.event.addDomListener(window,'load',initLocSearch_1);
function initLocSearch_2() {
 var input = document.getElementById('loc_search_2');
 var options = {componentRestrictions: {country: country_flag}};
 var autocomplete = new google.maps.places.Autocomplete(input, options);
}
google.maps.event.addDomListener(window,'load',initLocSearch_2);
function initLocSearch_3() {
 var input = document.getElementById('loc_search_3');
 var options = {componentRestrictions: {country: country_flag}};
 var autocomplete = new google.maps.places.Autocomplete(input, options);
}
google.maps.event.addDomListener(window,'load',initLocSearch_3);

jQuery('[id="viewMechanic"]').on('click',function() {
    var mechanic_id = jQuery(this).attr('mechanic_id');

    if(mechanic_id=='' || mechanic_id==undefined || mechanic_id=='undefined' || mechanic_id==null || mechanic_id=='null'){
        return true;
    }
    modalTrigger('Mechanic Details','');
    addModalLoader();
    jQuery.ajax({
        url  : base_url+"Mechanic/getMechanicData", 
        type : 'POST',
        data : {'mechanic_id':mechanic_id,'view_all':'1'},
        success: function(resp){
            if(resp == '' || resp == undefined || resp == 'undefined' || resp == null || resp == 'null'){
                remModalLoader();
                jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');
                return false;
            }
            var resp_data = jQuery.parseJSON(resp);
            if(resp_data['status'] == '0'){
                remModalLoader();
                jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');
                return false;
            }
            var mechanic_data = resp_data['data'];
            jQuery.each(mechanic_data, function (index, value) {
                if(value == '' || value == null || value == undefined || value == 'null' || value == 'undefined'){
                    mechanic_data[index] = ' -- ';
                }
            });

            var shopHtml = '';
            if(mechanic_data['shop_id'] != 0){
                shopHtml = '<br><div class="row"><label>Shop Details</label></div>'+
                            '<div class="row">'+
                              '<div class="col-md-4">Shop Name</div>'+
                              '<div class="col-md-1">:</div>'+
                              '<div class="col-md-6"><label>'+mechanic_data['shop_name']+'</label></div>'+
                            '</div>'+
                            '<div class="row">'+
                              '<div class="col-md-4">Shop Phone</div>'+
                              '<div class="col-md-1">:</div>'+
                              '<div class="col-md-6"><label>'+mechanic_data['shop_phone']+'</label></div>'+
                            '</div>'+
                            '<div class="row">'+
                              '<div class="col-md-4">Shop Email</div>'+
                              '<div class="col-md-1">:</div>'+
                              '<div class="col-md-6"><label>'+mechanic_data['shop_email']+'</label></div>'+
                            '</div>'+
                            '<div class="row">'+
                              '<div class="col-md-4">Shop Address</div>'+
                              '<div class="col-md-1">:</div>'+
                              '<div class="col-md-6"><label>'+mechanic_data['shop_address']+'</label></div>'+
                            '</div>';
            }

            var html = '<div class="col-xs-12">'+
                          '<div class="col-md-2">'+
                            '<div class="row">'+
                              '<img  id="driverProfileImg" src="'+base_url+mechanic_data['profile_image']+'" height="100" width="100" />'+
                            '</div>'+
                          '</div>'+
                          '<div class="col-md-5">'+
                            '<div class="row"><label>Basic Details</label></div>'+
                            '<div class="row">'+
                              '<div class="col-md-4">Display Name</div>'+
                              '<div class="col-md-1">:</div>'+
                              '<div class="col-md-6"><label>'+ mechanic_data['display_name']+'</label></div>'+
                            '</div> '+
                            '<div class="row">'+
                              '<div class="col-md-4">User Name</div>'+
                              '<div class="col-md-1">:</div>'+
                              '<div class="col-md-6"><label>'+ mechanic_data['username']+'</label></div>'+
                            '</div> '+
                            '<div class="row">'+
                              '<div class="col-md-4">First Name</div>'+
                              '<div class="col-md-1">:</div>'+
                              '<div class="col-md-6"><label>'+mechanic_data['first_name']+'</label></div>'+
                            '</div> '+
                            '<div class="row">'+
                              '<div class="col-md-4">Last Name</div>'+
                              '<div class="col-md-1">:</div>'+
                              '<div class="col-md-6"><label>'+mechanic_data['last_name']+'</label></div>'+
                            '</div> '+
                            '<div class="row"> '+
                              '<div class="col-md-4">Email ID</div>'+
                              '<div class="col-md-1">:</div>'+
                              '<div class="col-md-6"><label>'+mechanic_data['email_id']+'</label></div>'+
                            '</div> '+
                            '<div class="row"> '+
                              '<div class="col-md-4">Phone</div>'+
                              '<div class="col-md-1">:</div>'+
                              '<div class="col-md-6"><label>'+mechanic_data['phone']+'</label></div> '+
                            '</div> '+
                            '<div class="row"> '+
                              '<div class="col-md-4">Address</div>'+
                              '<div class="col-md-1">:</div>'+
                              '<div class="col-md-6"><label>'+mechanic_data['address']+'</label></div> '+
                            '</div> '+
                            '<div class="row"> '+
                              '<div class="col-md-4">City</div>'+
                              '<div class="col-md-1">:</div>'+
                              '<div class="col-md-6"><label>'+mechanic_data['city']+'</label></div> '+
                            '</div> '+
                            '<div class="row"> '+
                              '<div class="col-md-4">State</div>'+
                              '<div class="col-md-1">:</div>'+
                              '<div class="col-md-6"><label>'+mechanic_data['state']+'</label></div> '+
                            '</div> '+
                            '<div class="row"> '+
                              '<div class="col-md-4">Working Hours</div>'+
                              '<div class="col-md-1">:</div>'+
                              '<div class="col-md-6">'+
                                '<label>'+
                                  mechanic_data['start_time']+' - '+mechanic_data['end_time']+
                                '</label>'+
                              '</div> '+
                            '</div> '+
                            '<div class="row"> '+
                              '<div class="col-md-4">Location</div>'+
                              '<div class="col-md-1">:</div>'+
                              '<div class="col-md-6"><label>'+mechanic_data['location']+'</label></div> '+
                            '</div> '+
                            shopHtml+
                          '</div> '+
                          '<div class="col-md-5"> '+
                            '<div class="row"><label>Licence Details</label></div>'+
                            '<div class="row"> '+
                              '<div class="col-md-4">Licence No.</div>'+
                              '<div class="col-md-1">:</div>'+
                              '<div class="col-md-6"><label>'+mechanic_data['licence_number']+' </label></div>'+
                            '</div> '+
                            '<div class="row"> '+
                              '<div class="col-md-4">Licence Expiry</div>'+
                              '<div class="col-md-1">:</div>'+
                              '<div class="col-md-6"><label>'+mechanic_data['licence_exp_date']+'</label></div>'+
                            '</div> '+
                            '<div class="row"> '+
                              '<div class="col-md-4">Licence Proof</div>'+
                              '<div class="col-md-1">:</div>'+
                              '<div class="col-md-5" style="text-align:center;"> '+
                                '<img id="driverLicenceImg" src="'+base_url+mechanic_data['licence']+'"'+
                                  'style="margin-top:10px;max-width:205px;height:auto;max-height:130px;" />'+
                              '</div> '+
                            '</div> '+
                          '</div> '+
                        '</div>';

                remModalLoader();
                jQuery('[id="modal_content"]').html(html);

                jQuery('[id="driverLicenceImg"]').error(function() {
                    jQuery('[id="driverLicenceImg"]').attr('src',base_url+'assets/images/no_image.png');
                });

                jQuery('[id="driverProfileImg"]').error(function() {
                    jQuery('[id="driverProfileImg"]').attr('src',base_url+'assets/images/user_avatar.jpg');
                });

            },
            fail: function(xhr, textStatus, errorThrown){
                remModalLoader();
                jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');
            },
            error: function (ajaxContext) {
                remModalLoader();
                jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');
        }
    });
});

function changeMechanic(){
    jQuery('[id="chooseMechForm"]').submit();
}

jQuery('[id="viewCustomer"]').on('click',function() {
  customer_id = jQuery(this).attr('customer_id');

  if(customer_id=='' || customer_id==undefined || customer_id=='undefined' || customer_id==null || customer_id=='null'){
    return true;
  }
  modalTrigger('Customer Details','');
  addModalLoader();
  jQuery.ajax({
    url  : base_url+"Customer/getCustomerData", 
    type : 'POST',
    data : {'customer_id':customer_id},
    success: function(resp){
      if(resp == '' || resp == undefined || resp == 'undefined' || resp == null || resp == 'null'){
        remModalLoader();
        jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');
        return false;
      }
      var resp_data = jQuery.parseJSON(resp);
      if(resp_data['status'] == '0'){
        remModalLoader();
        jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');
        return false;
      }
      var customer_data = resp_data['customer_data'], html = '', veh_html = '',
          vehicle_data = resp_data['customer_data']['vehicle_data'];

      if(vehicle_data != '' && vehicle_data != undefined && vehicle_data != 'undefined' && vehicle_data != null && vehicle_data != 'null'){

        jQuery.each(vehicle_data, function (index, value) {
          veh_html += '<span class="vechile-body disp-block marginBottom-5">'+
                        '<i class="fa fa-fw fa-car padRight-8p"></i>'
                          +value['car_name']+
                      '</span>';
        });
        if(veh_html != ''){
          veh_html =  '<div class="col-xs-12"><div class="col-xs-2"></div><div class="col-xs-9">'+
                      '<label>Vehicles Added</label>'+veh_html+'</div></div>';
        }
      }

      html = '<div class="col-xs-12">'+
              '<div class="col-md-2"> '+
                '<div class="form-group has-feedback"> '+
                  '<img id="customerProfileImg" src="'+base_url+customer_data['profile_image']+'"'+
                    'height="100" width="100" /> '+
                '</div> '+
              '</div> '+
              '<div class="col-md-5"> '+
                '<div class="form-group has-feedback"> '+
                  '<span style="padding-right: 38px;">First Name </span> : '+
                  '<label style="padding-left: 10px;">'+
                    customer_data['first_name']+
                  '</label> '+
                '</div> '+
                '<div class="form-group has-feedback"> '+
                  '<span style="padding-right: 68px;">Email </span> : '+
                  '<label style="padding-left: 10px;">'+
                    customer_data['email']+
                  '</label> '+
                '</div> '+
                '<div class="form-group has-feedback"> '+
                  '<span style="padding-right: 55px;">Address </span> : '+
                  '<label style="padding-left: 10px;">'+
                    customer_data['address']+
                  '</label> '+
                '</div> '+
              '</div> '+
              '<div class="col-md-5"> '+
                '<div class="form-group has-feedback"> '+
                  '<span style="padding-right: 56px;">Last Name </span> : '+
                  '<label style="padding-left: 10px;">'+
                    customer_data['last_name']+
                  '</label> '+
                '</div> '+
                '<div class="form-group has-feedback"> '+
                  '<span style="padding-right: 80px;">Phone </span> : '+
                  '<label style="padding-left: 10px;">'+
                    customer_data['phone']+
                  '</label> '+
                '</div> '+
                '<div class="form-group has-feedback"> '+
                  '<span style="padding-right: 43px;">Date Of Dirth </span> : '+
                  '<label style="padding-left: 10px;">'+
                    customer_data['date_of_birth']+
                  '</label> '+
                '</div> '+
              '</div> '+
            '</div>'+veh_html;

      remModalLoader();
      jQuery('[id="modal_content"]').html(html);

      jQuery('[id="customerProfileImg"]').error(function() {
        jQuery('[id="customerProfileImg"]').attr('src',base_url+'assets/images/user_avatar.jpg');
      });

    },
    fail: function(xhr, textStatus, errorThrown){
      remModalLoader();
      jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');
    },
    error: function (ajaxContext) {
      remModalLoader();
      jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');
    }
  });
});

jQuery('[name="search_key_type"]').on('change',function() {
    var searchTypId = jQuery(this).val(); 
    if(searchTypId == '' || searchTypId == null || searchTypId == 'null' || searchTypId == undefined || searchTypId == 'undefined'){
        return false;
    }
    jQuery('[id^="searchTyp_"]').addClass('hide');
    jQuery('[id="searchTyp_'+searchTypId+'"]').removeClass('hide');

    jQuery('[id^="searchTyp_"]').find('[input="search_params"]').each(function (index,element) {
        jQuery(this).val('');
        jQuery(this).removeAttr('required');
        jQuery(this).removeClass('required');
    });

    jQuery('[id="searchTyp_'+searchTypId+'"]').find('[input="search_params"]').each(function (index,element) {
        jQuery(this).attr('required','');
        jQuery(this).addClass('required');
    });
});

jQuery('[id="createCustomerSubmit"]').on('click',function() {
  jQuery('[id="createCustomerForm"]').submit();
});

jQuery('[id="vehSearch"]').on('click',function(event) {
  event.preventDefault();
  if(!jQuery('[id="CarSearchForm"]').parsley().validate()){
    return false;
  }
  var searchTypId = jQuery('input[name=search_key_type]:checked').val(),
      passArr = {'searchType':'','vehModel':'','vehMaker':'','vehYear':'','vehVin':''};

  if(searchTypId == '' || searchTypId == null || searchTypId == 'null' || searchTypId == undefined || searchTypId == 'undefined'){
      modalTrigger('Vechile Search Failed','Vechile Search Failed, Provide proper data and try again..!');
      return false;
  }
  var vehVin = jQuery('[name="vehVin"]').val(),
      vehTrim = jQuery('[id="trim"]').val(),
      vehYear = jQuery('[name="vehYear"]').val(),
      vehModel = jQuery('option:selected', jQuery('[name="vehModel"]')).attr('model'),
      vehMaker = jQuery('[name="vehMaker"]').val(),
      vehLocVin = jQuery('[name="vehLocationVin"]').val(),
      vehEngine = jQuery('[id="engine"]').val(),
      vehLocDetls = jQuery('[name="vehLocationDetails"]').val();

  if((searchTypId == 1 && (vehLocDetls == '' || vehLocDetls == null || vehLocDetls == 'null' || vehLocDetls == undefined || vehLocDetls == 'undefined') && 
        (vehYear == '' || vehYear == null || vehYear == 'null' || vehYear == undefined || vehYear == 'undefined' || 
         vehModel == '' || vehModel == null || vehModel == 'null' || vehModel == undefined || vehModel == 'undefined' || 
         vehMaker == '' || vehMaker == null || vehMaker == 'null' || vehMaker == undefined || vehMaker == 'undefined')
     ) || 
     (searchTypId == 2 && (vehLocVin == '' || vehLocVin == null || vehLocVin == 'null' || vehLocVin == undefined || vehLocVin == 'undefined') &&
        (vehVin == '' || vehVin == null || vehVin == 'null' || vehVin == undefined || vehVin == 'undefined')
     )){
    return false;
  }
  showFullScreenLoader();

  passArr.vehVin = vehVin;
  passArr.vehYear = vehYear;
  passArr.vehModel = vehModel;
  passArr.vehMaker = vehMaker;
  passArr.searchType = searchTypId;
  passArr.vehLocation = (searchTypId == 1)?vehLocDetls:vehLocVin;
  passArr.vehTrim = (vehTrim != undefined && vehTrim != 'undefined' && vehTrim != null && vehTrim != 'null' && vehTrim != '')?vehTrim:'';
  passArr.vehEngine = (vehEngine != undefined && vehEngine != 'undefined' && vehEngine != null && vehEngine != 'null' && vehEngine != '')?vehEngine:'';

  jQuery.ajax({
    url  : base_url+"Vehicle/vehicleSearch", 
    type : 'POST',
    data : passArr,
    success: function(resp){
      if(resp == '' || resp == undefined || resp == 'undefined' || resp == null || resp == 'null'){
        remFullScreenLoader();
        setModal('Vechile Search','Something went wrong, please try again later...!');
        return false;
      }
      var resp_data = jQuery.parseJSON(resp);

      if(resp_data['status'] != '1' || (resp_data['status'] == 1 && resp_data['veh_data'] == '' || resp_data['veh_data'] == null || resp_data['veh_data'] == undefined || resp_data['veh_data'] == 'null' || resp_data['veh_data'] == 'undefined' || resp_data['car_id'] == '' || resp_data['car_id'] == null || resp_data['car_id'] == undefined || resp_data['car_id'] == 'null' || resp_data['car_id'] == 'undefined')){
        var msg = 'Something went wrong, please try again later...!';
        if(resp_data['status'] == '2')
          msg = 'No Data Found, Try again with proper search parameters..!';
        if(resp_data['status'] == '3')
          msg = 'Provide a valid Location..!';
        remFullScreenLoader();
        setModal('Vechile Search',msg);
        return false;
      }
      var vehHtmlBody = '', car_id = resp_data['car_id'], vehicleData = resp_data['veh_data'];
      vehHtmlBody = '<span class="vechile-body disp-block marginBottom-10" style="border:1px solid #e2e2e2;padding: 12px;">'
                      +vehicleData['vehicle']+
                      '<div class="float-right">'+
                        '<div class="float-right margin-all" style="margin-top:0px;"><i class="fa fa-fw fa-close cpoint"></i></div>'+
                        '<div class="float-right margin-all"><button onclick="searchAction('+car_id+',2,0,\''+vehicleData['vehicle']+'\');" type="button" class="btn btn-primary float-right margin-all" style="margin-top: -10px">Add</button></div>'+
                      '</div>'+
                    '</span>';

      jQuery('[id="carSearchResult"]').html('<div id="search_result_'+car_id+'" class="box-body">'+vehHtmlBody+'</div>');
      jQuery('[id="carSearchResult"]').removeClass('hide');
      remFullScreenLoader();
      slideTo('carSearchResult');
    },
    fail: function(xhr, textStatus, errorThrown){
      remFullScreenLoader();
      setModal('Vechile Search','Something went wrong, please try again later...!');
    },
    error: function (ajaxContext) {
      remFullScreenLoader();
      setModal('Vechile Search','Something went wrong, please try again later...!');
    }
  });
});

function searchAction(car_id, action, saved, name){
  if(car_id == '' || car_id == null || car_id == 'null' || car_id == undefined || car_id == 'undefined' || action == '' || action == null || action == 'null' || action == undefined || action == 'undefined'){
    return false;
  }
  var html = '';

  if(action == 0 || action == 1 || action == 3){
    if(saved == 1){
      jQuery('[id="saved_vehicle_'+car_id+'"]').remove();
      if(jQuery('[id="savedVehiclesCntr"]').children().length <= 0){
        jQuery('[id="savedVehicleBox"]').addClass('hide');
      }
    }
    slideTo('CarSearchForm');
    jQuery.ajax({
      url  : base_url+"Vehicle/updateUserCarStatus", 
      type : 'POST',
      data : {'car_id':car_id,'action':action}
    });
  }
  if(action == 2){
    html = '<span id="saved_vehicle_'+car_id+'" class="vechile-body disp-block marginBottom-5">'+
              '<i class="fa fa-fw fa-car padRight-8p"></i>'
              +name+
              '<div id="remove_saved_'+car_id+'" car_id="'+car_id+'" class="float-right">'+
                '<i class="fa fa-fw fa-close cpoint"></i>'+
              '</div>'+
              '<input type="hidden" name="saved_vehicles[]" value="'+car_id+'" >'+
            '</span>';
    jQuery('[id="savedVehicleBox"]').removeClass('hide');
    jQuery('[id="savedVehiclesCntr"]').append(html);
    slideTo('createCustomerForm');
  }
  jQuery('[id="search_result_'+car_id+'"]').remove();
}

jQuery('div').on('click','[id^="remove_saved_"]',function(event) {
  var thisObj = jQuery(this), car_id = thisObj.attr('car_id');
  searchAction(car_id,'3','1','');
});

jQuery('[id^="addSubIssue_"]').on('click',function(event) {
  var thisObj = jQuery(this), 
      count = thisObj.attr('count'),
      html = jQuery('[id="subIssueAdd"]').html();

  if(count == '' || count == null || count == 'null' || count == undefined || count == 'undefined' || html == '' || html == null || html == 'null' || html == undefined || html == 'undefined'){
    return false;
  }
  count = parseInt(count)+1;
  thisObj.attr('count',count);
  html = html.replace(/{:count}/g, count);
  jQuery('[id="subIssueCntr"]').append('<div id="newSubIssue_'+count+'">'+html+'</div>');
});

jQuery('[id^="addPrdtCar"]').on('click',function(event) {
  var year = jQuery('[name="vehYear"]').val(),
      make =  jQuery("#veh_make option:selected").text(),
      model = jQuery("#veh_model option:selected").text(),
      modelVal = jQuery("#veh_model").val(),
      count = parseInt((jQuery(this).attr('count'))) + 1;

      jQuery(this).attr('count',count);
  if(year == '' || year == null || year == 'null' || year == undefined || year == 'undefined' || model == '' || model == null || model == 'null' || model == undefined || model == 'undefined'  || make == '' || make == null || make == 'null' || make == undefined || make == 'undefined' || count == '' || count == null || count == 'null' || count == undefined || count == 'undefined'  || modelVal == '' || modelVal == null || modelVal == 'null' || modelVal == undefined || modelVal == 'undefined'){
    return false;
  }
  vehHtmlBody = '<span class="disp-block" style="border:1px solid #e2e2e2;padding: 12px;">'
                  +year+' '+make+' '+model+
                  '<div class="float-right">'+
                    '<div class="float-right margin-all" style="margin-top:0px;"><i class="fa fa-fw fa-close cpoint" onclick="removeProduct('+count+')"></i></div>'+
                  '</div>'+
                  '<input type="hidden" value="'+year+'" name="prdVehYear[]">'+
                  '<input type="hidden" value="'+modelVal+'" name="prdVehModel[]">'+
                '</span>';
  jQuery('[id="addCarResult"]').append('<div id="add_result_'+count+'" class="col-md-6 form-group">'+vehHtmlBody+'</div>');
  jQuery('[id="addCarResult"]').removeClass('hide');
  
  jQuery('[name="vehYear"]').prop('selectedIndex',0);
  jQuery("#veh_make").prop('selectedIndex',0);
  jQuery("#veh_model").prop('selectedIndex',0);
});

function removeProduct(count){
  if(count == '' || count == null || count == 'null' || count == undefined || count == 'undefined'){
    return false;
  }
  jQuery('[id="add_result_'+count+'"]').remove();
}

function remSubIssue(count){
  if(count == '' || count == null || count == 'null' || count == undefined || count == 'undefined'){
    return false;
  }
  jQuery('[id="newSubIssue_'+count+'"]').remove();
}

function viewIssueDetls(issue_id){
  if(issue_id == '' || issue_id == null || issue_id == 'null' || issue_id == undefined || issue_id == 'undefined'){
    return false;
  }
  var head = '', body = '', 
      msg = 'Something went wrong, please try again later...!';

  jQuery('.modal-dialog').css('width', '60%');
  modalTrigger('Issue Sub Types','');
  addModalLoader();

  jQuery.ajax({
    url  : base_url+"Issue/getIssueDetails", 
    type : 'POST',
    data : {'issue_id':issue_id},
    success: function(resp){
      if(resp == '' || resp == undefined || resp == 'undefined' || resp == null || resp == 'null'){
        remModalLoader();
        jQuery('[id="modal_content"]').html(msg);
        return false;
      }
      var resp_data = jQuery.parseJSON(resp);
      if(resp_data['status'] != '1' || (resp_data['status'] == 1 && resp_data['data'] == '' || resp_data['data'] == null || resp_data['data'] == undefined || resp_data['data'] == 'null' || resp_data['data'] == 'undefined')){
        remModalLoader();
        jQuery('[id="modal_content"]').html(msg);
        return false;
      }
      if(resp_data['data']['sub_issue'] == '' || resp_data['data']['sub_issue'] == undefined || resp_data['data']['sub_issue'] == 'undefined' || resp_data['data']['sub_issue'] == null || resp_data['data']['sub_issue'] == 'null'){
        remModalLoader();
        jQuery('[id="modal_content"]').html('Current Issue doesn\'t have a sub category...');
        return false;
      }

      head = '<ul class="list_custom">'+
               '<li class="marginTop10" style="width:25%;"><label>Type</label></li>'+
               '<li class="marginTop10" style="width:40%;"><label>Description</label></li>'+
               '<li class="marginTop10" style="width:15%;"><label>Service Fee</label></li>'+
               '<li class="padBottom10 padLeft50"><label>Image</label></li>'+
             '</ul>';

      
      jQuery.each(resp_data['data']['sub_issue'], function (index, value) {
        body += '<ul class="list_custom">'+   
                  '<li class="issue-box" style="width:25%;"><label>'+value['issue_category']+'</label></li>'+
                  '<li class="issue-box" style="width:40%;">'+value['default_description']+'</li>'+
                  '<li class="issue-box padLeft40" style="width:15%;"><label>'+value['default_service_fee']+'</label></li>'+
                  '<li class="padBottom10 padLeft20 issue-box">'+
                    '<img src="'+base_url+value['issue_cat_image']+'" height="75" width="auto" style="max-width: 125px;"'+
                      'onerror="this.src=\''+base_url+'assets/images/no_image_text.png\'";/>'+
                  '</li>'+
                '</ul>';
      });
      jQuery('[id="modal_content"]').html('<div class="subIssueCntr">'+head+body+'</div>');
      remModalLoader();
      return false;
    },
    fail: function(xhr, textStatus, errorThrown){
      remModalLoader();
      jQuery('[id="modal_content"]').html(msg);
      return false;
    },
    error: function (ajaxContext) {
      remModalLoader();
      jQuery('[id="modal_content"]').html(msg);
      return false;
    }
  });
}

jQuery('[name="issue_id"]').on('change',function() {
  var issue_id = jQuery(this).val();
  getIssueDetails(issue_id);
});

function getIssueDetails(issue_id){
  if(issue_id == '' || issue_id == null || issue_id == 'null' || issue_id == undefined || issue_id == 'undefined'){
    return false;
  }
  showFullScreenLoader();
  var head = '', body = '', 
      msg = 'Something went wrong, please try again later...!';

  jQuery.ajax({
    url  : base_url+"Issue/getIssueDetails", 
    type : 'POST',
    data : {'issue_id':issue_id},
    success: function(resp){
      if(resp == '' || resp == undefined || resp == 'undefined' || resp == null || resp == 'null'){
        remFullScreenLoader();
        modalTrigger('Issue Mapping',msg);
        return false;
      }
      var resp_data = jQuery.parseJSON(resp);
      if(resp_data['status'] != '1' || (resp_data['status'] == 1 && resp_data['data'] == '' || resp_data['data'] == null || resp_data['data'] == undefined || resp_data['data'] == 'null' || resp_data['data'] == 'undefined')){
        remFullScreenLoader();
        modalTrigger('Issue Mapping',msg);
        return false;
      }
      if(resp_data['data']['sub_issue'] == '' || resp_data['data']['sub_issue'] == undefined || resp_data['data']['sub_issue'] == 'undefined' || resp_data['data']['sub_issue'] == null || resp_data['data']['sub_issue'] == 'null'){
        jQuery('[id="showNoSubTypeMsg"]').removeClass('hide');
        jQuery('[id="mapTypeCntr"],[id="subTypeDtlsCntr"]').addClass('hide');
        jQuery('[id="subTypeDtlsCntrData"]').html('');
        remFullScreenLoader();
        return false;
      }
      var html = '';
      jQuery.each(resp_data['data']['sub_issue'], function (index, value) {
        html += '<div class="col-md-12">'+
                  '<div class="col-md-1"></div>'+
                  '<div class="col-md-1 marginTop5">'+
                    '<i class="fa fa-arrow-circle-right fav-add-icon"></i>'+
                  '</div>'+
                  '<div class="col-md-2">'+
                    '<label>'+value['issue_category']+'</label>'+
                  '</div>'+
                  '<div class="col-md-2 marginBottom-10">'+
                    '<img class="small-icon" src="'+base_url+value['issue_cat_image']+'"'+
                      'onerror="this.src=\''+base_url+'assets/images/no_image_text.png\'";/>'+
                  '</div>'+
                  '<div class="col-md-4 marginBottom-10">'+
                    '<textarea name="default_description_'+value['issue_cat_id']+'" disabled required class="custom-textarea custom-textarea-style">'+value['default_description']+'</textarea>'+
                  '</div>'+
                  '<div class="col-md-2">'+
                    'Service Fee : '+
                    '<input name="default_service_fee_'+value['issue_cat_id']+'" disabled required data-parsley-pattern="^[0-9\ . \/]+$" class="custom-input custom-input-style" maxlength="6" value="'+value['default_service_fee']+'">'+
                  '</div>'+
                '</div>'+
                '<input type="hidden" name="issue_cat_ids[]" value="'+value['issue_cat_id']+'">';
      });
      jQuery('[id="showNoSubTypeMsg"]').addClass('hide');
      jQuery('[id="mapTypeCntr"],[id="subTypeDtlsCntr"]').removeClass('hide');
      jQuery('[id="subTypeDtlsCntrData"]').html(html);
      remFullScreenLoader();
      return false;
    },
    fail: function(xhr, textStatus, errorThrown){
      remFullScreenLoader();
      modalTrigger('Issue Mapping',msg);
      return false;
    },
    error: function (ajaxContext) {
      remFullScreenLoader();
      modalTrigger('Issue Mapping',msg);
      return false;
    }
  });
}

jQuery('[name="mapping_type"]').on('click',function() {
  var issue_id = '',
      mapping_type = jQuery(this).val();

  if(mapping_type == 1){
    jQuery('[name^="default_description_"]').removeClass('custom-textarea');
    jQuery('[name^="default_service_fee_"]').removeClass('custom-input');
    jQuery('[name^="default_description_"],[name^="default_service_fee_"]').prop("disabled",false);
  } else if(mapping_type == 0){
    jQuery('[name^="default_description_"]').addClass('custom-textarea');
    jQuery('[name^="default_service_fee_"]').addClass('custom-input');
    jQuery('[name^="default_description_"],[name^="default_service_fee_"]').prop("disabled",true);

    issue_id = jQuery('[name="issue_id"]').val();
    getIssueDetails(issue_id);
  }
});

function viewMapIssueDetails(issue_id) {
  if(issue_id == '' || issue_id == null || issue_id == 'null' || issue_id == undefined || issue_id == 'undefined'){
    return false;
  }
  var head = '', body = '', issueData = jQuery.parseJSON(mapIssueData);

  jQuery('.modal-dialog').css('width', '60%');
  modalTrigger('Issue Sub Types','');
  addModalLoader();

  head = '<ul class="list_custom">'+
               '<li class="marginTop10" style="width:25%;"><label>Type</label></li>'+
               '<li class="marginTop10" style="width:40%;"><label>Description</label></li>'+
               '<li class="marginTop10" style="width:15%;"><label>Service Fee</label></li>'+
               '<li class="padBottom10 padLeft50"><label>Image</label></li>'+
             '</ul>';

  jQuery.each(issueData[issue_id]['sub_issue'], function (index, value) {
    body += '<ul class="list_custom">'+   
              '<li class="issue-box" style="width:25%;"><label>'+value['issue_category']+'</label></li>'+
              '<li class="issue-box" style="width:40%;">'+value['description']+'</li>'+
              '<li class="issue-box padLeft40" style="width:15%;"><label>'+value['service_fee']+'</label></li>'+
              '<li class="padBottom10 padLeft20 issue-box">'+
                '<img src="'+base_url+value['issue_cat_image']+'" height="75" width="auto" style="max-width: 125px;"'+
                  'onerror="this.src=\''+base_url+'assets/images/no_image_text.png\'";/>'+
              '</li>'+
            '</ul>';
  });
  jQuery('[id="modal_content"]').html('<div class="subIssueCntr">'+head+body+'</div>');
  remModalLoader();
  return false;
}

jQuery('[id="customQuote"]').on('click',function() {
  var thisObj = jQuery(this);

  if(thisObj.attr('view') == '0'){
    customQuote(thisObj);
  } else {
    viewCustomQuote(thisObj);
  }
});

quoteThisObj = '';
function customQuote(thisObj){
  quoteThisObj = thisObj;
  var booking_id = thisObj.attr('booking_id');

  if(booking_id == '' || booking_id==undefined || booking_id == 'undefined' || booking_id == null || booking_id=='null'){
    return true;
  }

  modalTrigger('Generate Custom Description','');
  addModalLoader();
  jQuery.ajax({
    url : base_url+"Bookings/getBooking",
    type : 'POST',
    data : {'booking_id':booking_id,'view_all':'1'},
    success : function(resp){
      if(resp == '' || resp == undefined || resp == 'undefined' || resp == null || resp == 'null'){
        remModalLoader();
        jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');
        return false;
      }

      var resp_data = jQuery.parseJSON(resp);
      if(resp_data['status'] == '0'){
        remModalLoader();
        jQuery('[id="modal_content"]').htnl('Something went wrong, Please try again later...!');
        return false;
      }

      var booking_data = resp_data['data'];
      jQuery.each(booking_data, function(index, value){
        if(value == '' || value == null || value == 'null' || value == undefined || value == 'undefined'){
          booking_data[index] = ' -- ';
        }
      });

      var html ='',
          imgCount = 0,
          issueHtml = '',
          optionalHtml = '',
          issues_selected = jQuery.parseJSON(booking_data['issues_selected']);

      if(issues_selected != ''){
        var comma = '';
        issueHtml +=  '<form id="customQuote" role="form" method="post">'+
                       '<div class="col-md-12" style="padding-top:10px">'+
                        '<div class="col-md-3"><div class="row"><label>Selected Issue</label></div></div>'+
                        '<div class="col-md-6"><div class="row"><label>Description</label></div></div>'+
                        '<div class="col-md-3"><div class="row"><label>Amount</label></div></div>'+
                          '<input type="hidden" name="booking_id" value="'+booking_id+'">'+
                          '<div class="row">';
        jQuery.each(issues_selected, function (index, value) {
          issueHtml +=      '<div class="col-md-12">'
          issueHtml +=        '<div class="col-md-3 marginTop10">';
          issueHtml += comma+' '+value['issue_category']+' ';
          issueHtml +=        '</div>'+
                              '<div class="col-md-6">'+
                                '<textarea placeholder="Description" id="description_'+index+'" rows="2" cols="60" name="description[]"></textarea>'+
                              '</div>'+
                              '<div class="col-md-3">'+
                                '<input class="marginTop10" placeholder="Amount" id="amount_'+index+'" type="number" name="amount[]">'+
                              '</div>'+
                              '<input type="hidden" name="issue_id[]" value="'+value['issue_id']+'">'+
                              '<input type="hidden" name="sub_issue_id[]" value="'+value['sub_issue_id']+'">'+
                            '</div>'+
                            '<input type="hidden" name="issue_category[]" value="'+value['issue_category']+'">';
        });
        issueHtml +=      '<input type="hidden" id="array_count" name="count" value="'+issues_selected.length+'">'+ 
                          '</div>'+
                        '</div>'+
                        '<div class="col-md-12">'+
                          '<div class="box-footer textCenterAlign">'+
                            '<button type="button" onclick="submitCustQuote(event);" class="btn btn-primary">Submit</button>'+
                          '</div>'+        
                        '</div>'+
                      '</form>';
      }
      remModalLoader();
      jQuery('[id="modal_content"]').html(issueHtml);
          
      jQuery('[id^="optionalImage_"]').error(function() {
        jQuery('[id^="optionalImage_"]').attr('src',base_url+'assets/images/no_image_text.png');
      });
    },
    fail: function(xhr, textStatus, errorThrown){
        remModalLoader();
        jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');
    },
    error: function (ajaxContext) {
        remModalLoader();
        jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');
    }
  })
}

function submitCustQuote(e){
  e.preventDefault();
  var count = jQuery('[id="array_count"]').val(), errFlag = '1';

  jQuery('[id^="amount_"]').removeClass('errInput');
  jQuery('[id^="description_"]').removeClass('errInput');

  for (var i=0;i<count;i++){
    var descrptn = jQuery('[id="description_'+i+'"]').val();
    var amount = jQuery('[id="amount_'+i+'"]').val();

    if((descrptn != '' && amount == '') || (descrptn == '' && amount != '')){
      if(amount == ''){
        jQuery('[id="amount_'+i+'"]').addClass('errInput');
      }
      if(descrptn == ''){
        jQuery('[id="description_'+i+'"]').addClass('errInput');
      }
      return false;
    }

    if(descrptn != '' && amount != ''){
      errFlag = '0';
    }
  }
  if(errFlag == '1'){
    jQuery('[id^="amount_"]').addClass('errInput');
    jQuery('[id^="description_"]').addClass('errInput');
    return false;
  }

  var form_data = new FormData();
  form_data.append('data',jQuery('[id="customQuote"]').serialize());
  if(form_data == '' || form_data==undefined || form_data == 'undefined' || form_data == null || form_data=='null'){
    return true;
  }

  jQuery.ajax({
      url  : base_url+"Bookings/insertCustomQuote", 
      type : 'POST',
      data : form_data,
      async : false,
      contentType: false, 
      processData: false,
      success: function(resp){
         if(resp == '' || resp == undefined || resp == 'undefined' || resp == null || resp == 'null'){
                remModalLoader();
                jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');
                return false;
            }
            var resp_data = jQuery.parseJSON(resp);
            if(resp_data['status'] == 'error'){
                remModalLoader();
                jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');
                return false;
            }
            else{
              quoteThisObj.attr('view','1');
              quoteThisObj.find('span').html('View Custom Quote');
              quoteThisObj.attr('booking_id',resp_data['data']);
              remModalLoader();
              jQuery('[id="modal_content"]').html('Custom Quote Inserted Successfully.');
              return false;
            }
      },
        fail: function(xhr, textStatus, errorThrown){
            remModalLoader();
            jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');
        },
        error: function (ajaxContext) {
            remModalLoader();
            jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');
      }
  })
}

function viewCustomQuote(thisObj){
   var custom_id = thisObj.attr('booking_id');

  if(custom_id == '' || custom_id==undefined || custom_id == 'undefined' || custom_id == null || custom_id=='null'){
    return true;
  }

  modalTrigger('View Custom Description','');
  addModalLoader();
  jQuery.ajax({
    url : base_url+"Bookings/getCustomData",
    type : 'POST',
    data : {'custom_id':custom_id,'view_all':'1'},
    success : function(resp){
       if(resp == '' || resp == undefined || resp == 'undefined' || resp == null || resp == 'null'){
            remModalLoader();
            jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');
            return false;
        }
        var resp_data = jQuery.parseJSON(resp);
        if(resp_data['status'] == '0'){
            remModalLoader();
            jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');
            return false;
        }
        var booking_data = resp_data['data'];

        jQuery.each(booking_data, function(index, value){
        if(value == '' || value == null || value == 'null' || value == undefined || value == 'undefined'){
          booking_data[index] = ' -- ';
        }
      });

      var html ='',
          imgCount = 0,
          issueHtml = '',
          optionalHtml = '',
          issues_selected = jQuery.parseJSON(booking_data['issues_selected']),
          custdescription = jQuery.parseJSON(booking_data['custom_service_quote']);
         // console.log(custdescription);
      if(issues_selected != ''){
        var comma = '';
        issueHtml +=  '<div class="col-md-12" style="padding-top:10px">'+
                        '<div class="col-md-4"><div class="row"><label>Selected Issue</label></div></div>'+
                        '<div class="col-md-4"><div class="row"><label>Description</label></div></div>'+
                        '<div class="col-md-4"><div class="row"><label>Amount</label></div></div>'+
                          '<div class="row">';
        jQuery.each(issues_selected, function (index, value) {
          issueHtml +=      '<div class="col-md-12">'+
                             '<div class="col-md-4 marginTop10">';
          issueHtml += comma+' '+value['issue_category']+' ';
          issueHtml +=        '</div>'+
                              '<div class="col-md-4 marginTop10">'+
                                custdescription[index]['description']+
                              '</div>'+
                              '<div class="col-md-4 marginTop10">'+
                                custdescription[index]['amount']+
                              '</div>'+
                            '</div>';
        });
        issueHtml +=      '</div>'+
                        '</div>'+
                        '<div class="col-md-12">'+
                          '<div class="col-md-6"></div>'+
                          '<div class="col-md-6"><br><br>'+
                            '<div class="col-md-4"><label>Total Amount</label></div>'+
                            '<div class="col-md-1">:</div>'+
                            '<div class="col-md-5"><b>'+booking_data['custom_amount']+'</b></div>'+
                          '</div>'+
                        '</div>';
      }
      remModalLoader();
      jQuery('[id="modal_content"]').html(issueHtml);
          
      jQuery('[id^="optionalImage_"]').error(function() {
        jQuery('[id^="optionalImage_"]').attr('src',base_url+'assets/images/no_image_text.png');
      });
    },
    fail: function(xhr, textStatus, errorThrown){
          remModalLoader();
          jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');
    },
    error: function (ajaxContext) {
        remModalLoader();
        jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');
    }
  })
}

jQuery('[id="showBookinDetails"]').on('click',function() {
    var booking_id = jQuery(this).attr('booking_id');

    if(booking_id=='' || booking_id==undefined || booking_id=='undefined' || booking_id==null || booking_id=='null'){
        return true;
    }

    modalTrigger('Booking Details','');
    addModalLoader();
    jQuery.ajax({
        url  : base_url+"Bookings/getBooking", 
        type : 'POST',
        data : {'booking_id':booking_id,'view_all':'1'},
        success: function(resp){
            if(resp == '' || resp == undefined || resp == 'undefined' || resp == null || resp == 'null'){
                remModalLoader();
                jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');
                return false;
            }
            var resp_data = jQuery.parseJSON(resp);
            if(resp_data['status'] == '0'){
                remModalLoader();
                jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');
                return false;
            }
            var booking_data = resp_data['data'];
            jQuery.each(booking_data, function (index, value) {
                if(value == '' || value == null || value == undefined || value == 'null' || value == 'undefined'){
                    booking_data[index] = ' -- ';
                }
            });
            var html = '',
                imgCount = 0,
                issueHtml = '',
                optionalHtml = '',
                optional_data = jQuery.parseJSON(booking_data['custom_issue_data']),
                issues_selected = jQuery.parseJSON(booking_data['issues_selected']);
            
            if(issues_selected != ''){
              var comma = '';
              issueHtml =  '<div class="col-md-12" style="padding-top:10px;">'+
                              '<div class="row"><label>Selected Issue</label></div>'+
                              '<div class="row">'+
                                '<div class="col-md-1"></div>'+
                                '<div class="col-md-11">';
              jQuery.each(issues_selected, function (index, value) {
                issueHtml += comma+' '+value['issue_category']+' ';
                comma = ',';
              });
              issueHtml += '</div></div></div>';
            }

            if(optional_data != ''){
              optionalHtml =  '<div class="col-md-12" style="padding-top:20px;">'+
                                  '<div class="row"><label>Additional Information</label></div>';

              if(optional_data['optionlaDescription'] != ''){
                optionalHtml +=  '<div class="row">'+
                                    '<div class="col-md-2">Issue Details</div>'+
                                    '<div class="col-md-1">:</div>'+
                                    '<div class="col-md-9"><p>'+optional_data['optionlaDescription']+'</p></div>'+
                                 '</div>';
              }

              if(optional_data['optionalImages'] != ''){
                optionalHtml += '<div class="row">'+
                                  '<div class="col-md-2">Issue Images</div>'+
                                  '<div class="col-md-1">:</div>'+
                                  '<div class="col-md-9">';

                jQuery.each(optional_data['optionalImages'], function (index, image) {
                  imgCount += 1;
                  optionalHtml += '<img id="optionalImage_'+imgCount+'" src="'+base_url+image+'" height="100" width="100" /> ';
                });
                optionalHtml += '</div>';
              }
              optionalHtml += '</div>';
            }

            html = '<div class="col-xs-12">'+
                    '<div class="col-md-6">'+
                      '<div class="row"><label>Basic Details</label></div>'+
                      '<div class="row">'+
                        '<div class="col-md-4">Customer</div>'+
                        '<div class="col-md-1">:</div>'+
                        '<div class="col-md-6"><label>'+booking_data['custFirstName']+' '+booking_data['custLastName']+'</label></div>'+
                      '</div> '+
                      '<div class="row">'+
                        '<div class="col-md-4">Current Milage</div>'+
                        '<div class="col-md-1">:</div>'+
                        '<div class="col-md-6"><label>'+booking_data['mileage']+'</label></div>'+
                      '</div> '+
                      '<div class="row"> '+
                        '<div class="col-md-4">Service Free</div>'+
                        '<div class="col-md-1">:</div>'+
                        '<div class="col-md-6"><label>'+booking_data['cost']+'</label></div>'+
                      '</div> '+
                      '<div class="row"> '+
                        '<div class="col-md-4">Scheduled Date</div>'+
                        '<div class="col-md-1">:</div>'+
                        '<div class="col-md-6"><label>'+booking_data['scheduled_date']+' '+booking_data['scheduled_time']+'</label></div> '+
                      '</div> '+
                      '<div class="row"> '+
                        '<div class="col-md-4">Customer Phone</div>'+
                        '<div class="col-md-1">:</div>'+
                        '<div class="col-md-6"><label>'+booking_data['phone']+'</label></div> '+
                      '</div> '+
                      '<div class="row"> '+
                        '<div class="col-md-4">Customer Mail</div>'+
                        '<div class="col-md-1">:</div>'+
                        '<div class="col-md-6"><label>'+booking_data['email']+'</label></div> '+
                      '</div> '+
                    '</div> '+
                    '<div class="col-md-6"> '+
                      '<div class="row"><label>Customer Vehicle Details</label></div>'+
                      '<div class="row"> '+
                        '<div class="col-md-4">Car Name</div>'+
                        '<div class="col-md-1">:</div>'+
                        '<div class="col-md-6"><label>'+booking_data['car_name']+' </label></div>'+
                      '</div> '+
                      '<div class="row"> '+
                        '<div class="col-md-4">Car Model</div>'+
                        '<div class="col-md-1">:</div>'+
                        '<div class="col-md-6"><label>'+booking_data['car_model']+'</label></div>'+
                      '</div> '+
                      '<div class="row"> '+
                        '<div class="col-md-4">Car Model Year</div>'+
                        '<div class="col-md-1">:</div>'+
                        '<div class="col-md-6"><label>'+booking_data['car_model_year']+'</label></div>'+
                      '</div> '+
                      '<div class="row"> '+
                        '<div class="col-md-4">Car Location</div>'+
                        '<div class="col-md-1">:</div>'+
                        '<div class="col-md-6"><label>'+booking_data['car_location']+'</label></div>'+
                      '</div> '+
                    '</div> '+issueHtml+
                    '<div class="col-md-12"> '+
                      '<div class="row"><label>Mechanic Details</label></div>'+
                      '<div class="col-md-3"><div class="row"><label>Mechanic Name</label></div></div>'+
                      '<div class="col-md-3"><div class="row"><label>Amount</label></div></div>'+
                      '<div class="col-md-2"><div class="row"><label>status</label></div></div>'+
                      '<div class="row">';
                        jQuery.each(booking_data['mechanic_data'], function (index1, value1) {
                          if(value1['custom_amount'] == '' || value1['custom_amount'] == null || value1['custom_amount'] == undefined || value1['custom_amount'] == 'null' || value1['custom_amount'] == 'undefined'){
                              value1['custom_amount'] = booking_data['cost'];
                          }
                          html+= '<div class="col-md-12">'+
                                  '<div class="col-md-3">'+value1['first_name']+' '+value1['last_name']+'</div>'+
                                  '<div class="col-md-3">'+value1['custom_amount']+'</div>'+
                                  '<div class="col-md-2">'+value1['status']+'</div>'+
                                '</div>';
                          });
                        html+= '</div>'+
                      '</div>'+
                    optionalHtml+
                  '</div>';

              remModalLoader();
              jQuery('[id="modal_content"]').html(html);
              
              jQuery('[id^="optionalImage_"]').error(function() {
                jQuery('[id^="optionalImage_"]').attr('src',base_url+'assets/images/no_image_text.png');
              });
            },
            fail: function(xhr, textStatus, errorThrown){
                remModalLoader();
                jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');
            },
            error: function (ajaxContext) {
                remModalLoader();
                jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');
        }
    });
});

jQuery('[id="addProductButton"]').on('click',function(event) {
  event.preventDefault();
  var validation = jQuery('[name="productAddForm"]').parsley().validate();

  var error = 0;
  var count = jQuery('[id="multipleImageInputCntr"]').children().first().attr('count');
  if(jQuery('[id="product_image_'+count+'"]').val() == ''){
    error = 1;
    jQuery('[id="multiImageImg_'+count+'"]').addClass('errorBorder');
  }

  if(validation && error == 0){
    jQuery('[name="productAddForm"]').submit();
  }
});

jQuery('[id="viewProductDetails"]').on('click',function() {
    var product_id = jQuery(this).attr('product_id');

    if(product_id=='' || product_id==undefined || product_id=='undefined' || product_id==null || product_id=='null'){
        return true;
    }

    modalTrigger('Product Details','');
    addModalLoader();
    jQuery.ajax({
        url  : base_url+"Product/getProductData", 
        type : 'POST',
        data : {'product_id':product_id,'view_all':'1'},
        success: function(resp){
            if(resp == '' || resp == undefined || resp == 'undefined' || resp == null || resp == 'null'){
                remModalLoader();
                jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');
                return false;
            }
            var resp_data = jQuery.parseJSON(resp);
            console.log(resp_data);
            if(resp_data['status'] == '0'){
                remModalLoader();
                jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');
                return false;
            }
            var html = '',
                imgCount = 0,
                optionalHtml = '';
                
            if(resp_data.product_image.length > 0){
              optionalHtml =  '<div class="col-md-12" style="padding-top:20px;">'+
                                  '<div class="row"><label>Product Images</label></div>';

                optionalHtml += '<div class="row">'+
                                  '<div class="col-md-2">Images</div>'+
                                  '<div class="col-md-1">:</div>'+
                                  '<div class="col-md-9">';

                jQuery.each(resp_data.product_image, function (index, image) {
                  console.log(image.image);
                  imgCount += 1;
                  optionalHtml += '<img id="optionalImage_'+imgCount+'" src="'+base_url+image.image+'" height="100" width="100" /> ';
                });
                optionalHtml += '</div>';
              optionalHtml += '</div>';
            }

            html = '<div class="col-xs-12">'+
                    '<div class="row"><label>Product Details</label></div>'+
                    '<div class="col-md-6">'+
                      '<div class="row">'+
                        '<div class="col-md-4">Brand Name</div>'+
                        '<div class="col-md-1">:</div>'+
                        '<div class="col-md-6"><label>'+resp_data.product_data.brand_name+'</label></div>'+
                      '</div> '+
                      '<div class="row">'+
                        '<div class="col-md-4">Product Name</div>'+
                        '<div class="col-md-1">:</div>'+
                        '<div class="col-md-6"><label>'+resp_data.product_data.product_name+'</label></div>'+
                      '</div> '+
                    '</div>'+
                    '<div class="col-md-6">'+
                      '<div class="row"> '+
                        '<div class="col-md-4">Amount</div>'+
                        '<div class="col-md-1">:</div>'+
                        '<div class="col-md-6"><label>'+resp_data.product_data.amount+'</label></div>'+
                      '</div> '+
                      '<div class="row"> '+
                        '<div class="col-md-4">Short Description</div>'+
                        '<div class="col-md-1">:</div>'+
                        '<div class="col-md-6"><label>'+resp_data.product_data.short_description+'</label></div> '+
                      '</div> '+
                    '</div> '+
                    '<div class="col-md-12"> '+
                      '<br>'+
                      '<div class="row"><label>Product Description</label></div>'+
                      '<div class="row"> '+
                        '<div class="col-md-2">Description</div>'+
                        '<div class="col-md-1">:</div>'+
                        '<div class="col-md-9"><label>'+resp_data.product_data.description+'</label></div> '+
                      '</div> '+
                    '</div> '+
                    '<div class="col-md-12"> '+
                      '<br>'+
                      '<div class="row"><label>About Product</label></div>'+
                      '<div class="row"> '+
                        '<div class="col-md-2">Description</div>'+
                        '<div class="col-md-1">:</div>'+
                        '<div class="col-md-9"><label>'+resp_data.product_data.about+'</label></div> '+
                      '</div> '+
                    '</div> '+
                    optionalHtml+
                  '</div>';

              remModalLoader();
              jQuery('[id="modal_content"]').html(html);
              
              jQuery('[id^="optionalImage_"]').error(function() {
                jQuery('[id^="optionalImage_"]').attr('src',base_url+'assets/images/no_image_text.png');
              });
            },
            fail: function(xhr, textStatus, errorThrown){
                remModalLoader();
                jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');
            },
            error: function (ajaxContext) {
                remModalLoader();
                jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');
        }
    });
});


jQuery('[id="viewOrderDetails"]').on('click',function() {
    var order_id = jQuery(this).attr('order_id');

    if(order_id=='' || order_id==undefined || order_id=='undefined' || order_id==null || order_id=='null'){
        return true;
    }

    modalTrigger('Order Details','');
    addModalLoader();
    jQuery.ajax({
        url  : base_url+"Orders/getOrderData", 
        type : 'POST',
        data : {'order_id':order_id,'view_all':'1'},
        success: function(resp){
            if(resp == '' || resp == undefined || resp == 'undefined' || resp == null || resp == 'null'){
                remModalLoader();
                jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');
                return false;
            }
            var resp_data = jQuery.parseJSON(resp);
            if(resp_data['status'] == '0'){
                remModalLoader();
                jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');
                return false;
            }
            var html = '',
                imgCount = 0,
                optionalHtml = '';
                
            if(resp_data.product_image.length > 0){
              optionalHtml =  '<div class="col-md-12" style="padding-top:20px;">'+
                                  '<div class="row"><label>Product Images</label></div>';

                optionalHtml += '<div class="row">'+
                                  '<div class="col-md-12">';

                jQuery.each(resp_data.product_image, function (index, image) {
                  imgCount += 1;
                  optionalHtml += '<img id="optionalImage_'+imgCount+'" src="'+base_url+image.image+'" height="100" width="100" /> ';
                });
                optionalHtml += '</div>';
              optionalHtml += '</div>';
            }

            html = '<div class="col-xs-12">'+
                    '<div class="row"><label>Order Details</label></div>'+
                    '<div class="col-md-6">'+
                     '<div class="row">'+
                        '<div class="col-md-4">Order Id</div>'+
                        '<div class="col-md-1">:</div>'+
                        '<div class="col-md-6"><label>'+resp_data.order_data.format_order_id+'</label></div>'+
                      '</div> '+
                      '<div class="row">'+
                        '<div class="col-md-4">Customer Name</div>'+
                        '<div class="col-md-1">:</div>'+
                        '<div class="col-md-6"><label>'+resp_data.order_data.customer_name+'</label></div>'+
                      '</div> '+
                      '<div class="row">'+
                        '<div class="col-md-4">Product Name</div>'+
                        '<div class="col-md-1">:</div>'+
                        '<div class="col-md-6"><label>'+resp_data.order_data.product_name+'</label></div>'+
                      '</div> '+
                      '<div class="row">'+
                        '<div class="col-md-4">Quantity</div>'+
                        '<div class="col-md-1">:</div>'+
                        '<div class="col-md-6"><label>'+resp_data.order_data.quantity+'</label></div>'+
                      '</div> '+
                    '</div>'+
                    '<div class="col-md-6">'+
                      '<div class="row"> '+
                        '<div class="col-md-4">Brand Name</div>'+
                        '<div class="col-md-1">:</div>'+
                        '<div class="col-md-6"><label>'+resp_data.order_data.brand_name+'</label></div>'+
                      '</div> '+
                      '<div class="row"> '+
                        '<div class="col-md-4">Short Description</div>'+
                        '<div class="col-md-1">:</div>'+
                        '<div class="col-md-6"><label>'+resp_data.order_data.short_description+'</label></div> '+
                      '</div> '+
                      '<div class="row"> '+
                        '<div class="col-md-4">Amount</div>'+
                        '<div class="col-md-1">:</div>'+
                        '<div class="col-md-6"><label>'+resp_data.order_data.amount+'</label></div> '+
                      '</div> '+
                      '<div class="row"> '+
                        '<div class="col-md-4">Status</div>'+
                        '<div class="col-md-1">:</div>'+
                        '<div class="col-md-6"><label>'+resp_data.order_data.ord_status+'</label></div> '+
                      '</div> ';
                      if(resp_data.order_data.status == '3' || resp_data.order_data.status == '4'){
                        html += '<div class="row"> '+
                                  '<div class="col-md-4">Expected Delivery Date</div>'+
                                  '<div class="col-md-1">:</div>'+
                                  '<div class="col-md-6"><label>'+resp_data.order_data.expected_delivery+'</label></div> '+
                                '</div> ';
                      }else if(resp_data.order_data.status == '5'){
                        html += '<div class="row"> '+
                                  '<div class="col-md-4">Delivered On</div>'+
                                  '<div class="col-md-1">:</div>'+
                                  '<div class="col-md-6"><label>'+resp_data.order_data.delivered+'</label></div> '+
                                '</div> ';
                      }
                   html += '</div> '+
                    optionalHtml+
                  '</div>';

              remModalLoader();
              jQuery('[id="modal_content"]').html(html);
              
              jQuery('[id^="optionalImage_"]').error(function() {
                jQuery('[id^="optionalImage_"]').attr('src',base_url+'assets/images/no_image_text.png');
              });
            },
            fail: function(xhr, textStatus, errorThrown){
                remModalLoader();
                jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');
            },
            error: function (ajaxContext) {
                remModalLoader();
                jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');
        }
    });
});

jQuery('[id="changeOrderStatus"]').on('click',function() {
    var order_id = jQuery(this).attr('order_id');
    var order_status = jQuery(this).attr('order_status');
    if(order_id=='' || order_id==undefined || order_id=='undefined' || order_id==null || order_id=='null'){
        return true;
    }
    modalTrigger('Change Order Detail Status','');
    addModalLoader();

    var stat = '', 
        dropOption = '<option selected disabled value="">--Change Status--</option>';

    switch (order_status){
      case '2': stat = 'Order Places';
              dropOption += '<option value="3">Order Packed</option>';
              dropOption += '<option value="4">Order Shipped</option>';
              dropOption += '<option value="5">Order Delivered</option>';
              break;
      case '3': stat = 'Order Packed';
              dropOption += '<option value="4">Order Shipped</option>';
              dropOption += '<option value="5">Order Delivered</option>';
              break;
      case '4': stat = 'Ordered Shipped';
              dropOption += '<option value="5">Order Delivered</option>';
              break;
    }

    var html =   '<form id="changeOrderStatus" role="form" method="post">'+
                  '<div class="col-md-12" style="padding-top:10px">'+
                    '<div class="col-md-3"><div class="row"><label>Current Status</label></div></div>'+
                    '<div class="col-md-1"> : </div>'+
                    '<div class="col-md-3"><div class="row"><label>'+stat+'</label></div></div>'+
                  '</div>'+
                  
                  '<div class="col-md-12" style="padding-top:10px">'+
                    '<div class="col-md-3"><div class="row"><label>Change Status</label></div></div>'+
                    '<div class="col-md-1"> : </div>'+
                    '<div class="col-md-3">'+
                      '<div class="row">'+
                        '<select id="orderStatus" onchange="statusChangeFun()" name="status" class="form-control required">'+
                          dropOption+
                        '</select>'+
                      '</div>'+
                    '</div>'+
                    '<div class="col-md-5" id="deliverydatediv"></div>'+
                  '</div>'+
                  '<input type="hidden" name="order_id" id="order_id" value="'+order_id+'">'+
                  '<div class="col-md-12"  style="padding-top:10px">'+
                    '<div class="box-footer textCenterAlign">'+
                      '<button type="button" onclick="changeOrderStatus(event);" class="btn btn-primary">Submit</button>'+
                    '</div>'+        
                  '</div>'+
                '</form>';
      remModalLoader();
      jQuery('[id="modal_content"]').html(html);
});

function statusChangeFun(){
  var status = jQuery('[id="orderStatus"]').val();
  if(status == '3' || status == '4'){
    jQuery('[id="deliverydatediv"]').html('<div class="col-md-4">'+
                                            '<div class="row">'+
                                              '<label>Deliver On</label>'+
                                            '</div>'+
                                          '</div>'+
                                          '<div class="col-md-1"> : </div>'+
                                          '<div class="col-md-6">'+
                                            '<div class="row">'+
                                              '<input type="date" id="expected_delivery" class="form-control required" name="expected_delivery">'+
                                            '</div>'+
                                          '</div>');
  }else if(status == '5'){
    jQuery('[id="deliverydatediv"]').html('<div class="col-md-4">'+
                                            '<div class="row">'+
                                              '<label>Delivered on</label>'+
                                            '</div>'+
                                          '</div>'+
                                          '<div class="col-md-1"> : </div>'+
                                          '<div class="col-md-6">'+
                                            '<div class="row">'+
                                              '<input type="date" id="delivery" class="form-control required" name="expected_delivery">'+
                                            '</div>'+
                                          '</div>');
  }
}

function changeOrderStatus(e){
  e.preventDefault();
  var errFlag = '1';

  jQuery('[id^="orderStatus"]').removeClass('errInput');

  var status = jQuery('[id="orderStatus"]').val();
  var order_id = jQuery('[id="order_id"]').val();

  if(status == '' || status == 'null' || status == 'NULL' || status == null){
    jQuery('[id="orderStatus"]').addClass('errInput');
    return false;
  }

  if(status != '' || status != 'null' || status != 'NULL' || status != null){
    errFlag = '0';
    if(status == '3' || status == '4'){
      var expected_delivery = jQuery('[id="expected_delivery"]').val();
      if(expected_delivery == '' || expected_delivery == 'null'){
        jQuery('[id="expected_delivery"]').addClass('errInput');
        errFlag = '1';
      }
    }else if(status == '5'){
      var expected_delivery = jQuery('[id="delivery"]').val();
      if(expected_delivery == '' || expected_delivery == 'null'){
        jQuery('[id="delivery"]').addClass('errInput');
        errFlag = '1';
      }
    }
  }
  if(errFlag == '1'){
    return false;
  }

  jQuery.ajax({
      url  : base_url+"Orders/changeOrderStatus", 
      type : 'POST',
      data : {'order_id':order_id,'status':status,'expected_date':expected_delivery},
      success: function(resp){
       if(resp == '' || resp == undefined || resp == 'undefined' || resp == null || resp == 'null'){
            remModalLoader();
            jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');
            return false;
        }
        var resp_data = jQuery.parseJSON(resp);
        if(resp_data['status'] == '0'){
            remModalLoader();
            jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');
            return false;
        }
        else{
          remModalLoader();
          if(status == '3'){
            var html = 'Order Packed <br>(Deliver by '+expected_delivery+')';
          }else if(status == '4'){
            var html = 'Order Shipped <br>(Deliver by '+expected_delivery+')';
          }else if(status == '5'){
            var html = 'Order Delivered <br>(Delivered on '+expected_delivery+')';
          }
          jQuery('[id="orderStatus_'+order_id+'"]').html(html);
          jQuery('[id="modal_content"]').html('Status Changed Successfully.');

          setTimeout(function(){
            modalHide();
          }, 1000);
          return false;
        }
      },
        fail: function(xhr, textStatus, errorThrown){
          remModalLoader();
          jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');

          setTimeout(function(){
            modalHide();
          }, 1000);
        },
        error: function (ajaxContext) {
          remModalLoader();
          jQuery('[id="modal_content"]').html('Something went wrong, please try again later...!');

          setTimeout(function(){
            modalHide();
          }, 1000);
      }
  });
}

jQuery('#veh_make').on('change', function () {
  var id = jQuery('option:selected', this).attr('maker'),
      type = jQuery(this).attr('type');

  if(id == '' || id == undefined || id == 'undefined' || id == null || id == 'null'){
    return false;
  }
  type = (type == undefined || type == 'undefined' || type == null || type == 'null' || type == '')?0:type;
  showFullScreenLoader();
  jQuery.ajax({
    type: "POST",
    url: base_url + 'Vehicle/getVehModel',
    data:'id=' + id +'&type=' + type,
    success: function (data) {
      jQuery('#veh_model').html(data);
      remFullScreenLoader();
    }
  });
});

jQuery('[id="import_id"]').on('change',function() {
  var id = jQuery(this).val();
  if(id != '' || id != undefined || id != 'undefined' || id != null || id != 'null'){
    $('#import_file').removeClass('hide');
  }
})
