easyappointments/assets/js/pages/customers.min.js

1 line
9.0 KiB
JavaScript
Executable File

"use strict";App.Pages.Customers=function(){function display(customer){$id.val(customer.id),$firstName.val(customer.first_name),$lastName.val(customer.last_name),$email.val(customer.email),$phoneNumber.val(customer.phone_number),$address.val(customer.address),$city.val(customer.city),$zipCode.val(customer.zip_code),$notes.val(customer.notes),$timezone.val(customer.timezone),$language.val(customer.language||"english"),$ldapDn.val(customer.ldap_dn),$customField1.val(customer.custom_field_1),$customField2.val(customer.custom_field_2),$customField3.val(customer.custom_field_3),$customField4.val(customer.custom_field_4),$customField5.val(customer.custom_field_5),$customerAppointments.empty(),customer.appointments.length||$("<p/>",{text:lang("no_records_found")}).appendTo($customerAppointments),customer.appointments.forEach(function(appointment){if((vars("role_slug")!==App.Layouts.Backend.DB_SLUG_PROVIDER||parseInt(appointment.id_users_provider)===vars("user_id"))&&(vars("role_slug")!==App.Layouts.Backend.DB_SLUG_SECRETARY||-1!==vars("secretary_providers").indexOf(appointment.id_users_provider))){var start=App.Utils.Date.format(moment(appointment.start_datetime).toDate(),vars("date_format"),vars("time_format"),!0),end=App.Utils.Date.format(moment(appointment.end_datetime).toDate(),vars("date_format"),vars("time_format"),!0);$("<div/>",{class:"appointment-row","data-id":appointment.id,html:[$("<a/>",{href:App.Utils.Url.siteUrl("calendar/reschedule/".concat(appointment.hash)),html:[$("<i/>",{class:"fas fa-edit me-1"}),$("<strong/>",{text:appointment.service.name+" - "+appointment.provider.first_name+" "+appointment.provider.last_name}),$("<br/>")]}),$("<small/>",{text:start}),$("<br/>"),$("<small/>",{text:end}),$("<br/>"),$("<small/>",{text:vars("timezones")[appointment.provider.timezone]})]}).appendTo("#customer-appointments")}})}function filter(keyword){var selectId=1<arguments.length&&void 0!==arguments[1]?arguments[1]:null,show=!!(2<arguments.length&&void 0!==arguments[2])&&arguments[2];App.Http.Customers.search(keyword,filterLimit).then(function(response){filterResults=response,$filterCustomers.find(".results").empty(),response.forEach(function(customer){$("#filter-customers .results").append(App.Pages.Customers.getFilterHtml(customer)).append($("<hr/>"))}),response.length?response.length===filterLimit&&$("<button/>",{type:"button",class:"btn btn-outline-secondary w-100 load-more text-center",text:lang("load_more"),click:function click(){filterLimit+=20,App.Pages.Customers.filter(keyword,selectId,show)}}).appendTo("#filter-customers .results"):$filterCustomers.find(".results").append($("<em/>",{text:lang("no_records_found")})),selectId&&App.Pages.Customers.select(selectId,show)})}function select(id){var show=!!(1<arguments.length&&void 0!==arguments[1])&&arguments[1];if($("#filter-customers .selected").removeClass("selected"),$("#filter-customers .entry[data-id=\""+id+"\"]").addClass("selected"),show){var customer=filterResults.find(function(filterResult){return+filterResult.id===+id});App.Pages.Customers.display(customer),$("#edit-customer, #delete-customer").prop("disabled",!1)}}var $customers=$("#customers"),$filterCustomers=$("#filter-customers"),$id=$("#customer-id"),$firstName=$("#first-name"),$lastName=$("#last-name"),$email=$("#email"),$phoneNumber=$("#phone-number"),$address=$("#address"),$city=$("#city"),$zipCode=$("#zip-code"),$timezone=$("#timezone"),$language=$("#language"),$ldapDn=$("#ldap-dn"),$customField1=$("#custom-field-1"),$customField2=$("#custom-field-2"),$customField3=$("#custom-field-3"),$customField4=$("#custom-field-4"),$customField5=$("#custom-field-5"),$notes=$("#notes"),$formMessage=$("#form-message"),$customerAppointments=$("#customer-appointments"),moment=window.moment,filterResults={},filterLimit=20;return document.addEventListener("DOMContentLoaded",function initialize(){App.Pages.Customers.resetForm(),App.Pages.Customers.addEventListeners(),App.Pages.Customers.filter("")}),{filter:filter,save:function save(customer){App.Http.Customers.save(customer).then(function(response){App.Layouts.Backend.displayNotification(lang("customer_saved")),App.Pages.Customers.resetForm(),$("#filter-customers .key").val(""),App.Pages.Customers.filter("",response.id,!0)})},remove:function remove(id){App.Http.Customers.destroy(id).then(function(){App.Layouts.Backend.displayNotification(lang("customer_deleted")),App.Pages.Customers.resetForm(),App.Pages.Customers.filter($("#filter-customers .key").val())})},validate:function validate(){$formMessage.removeClass("alert-danger").hide(),$(".is-invalid").removeClass("is-invalid");try{var missingRequired=!1;if($(".required").each(function(index,requiredField){""===$(requiredField).val()&&($(requiredField).addClass("is-invalid"),missingRequired=!0)}),missingRequired)throw new Error(lang("fields_are_required"));var email=$email.val();if(email&&!App.Utils.Validation.email(email))throw $email.addClass("is-invalid"),new Error(lang("invalid_email"));var phoneNumber=$phoneNumber.val();if(phoneNumber&&!App.Utils.Validation.phone(phoneNumber))throw $phoneNumber.addClass("is-invalid"),new Error(lang("invalid_phone"));return!0}catch(error){return $formMessage.addClass("alert-danger").text(error.message).show(),!1}},getFilterHtml:function getFilterHtml(customer){var name=(customer.first_name||"[No First Name]")+" "+(customer.last_name||"[No Last Name]"),info=customer.email||"[No Email]";return info=customer.phone_number?info+", "+customer.phone_number:info,$("<div/>",{class:"customer-row entry","data-id":customer.id,html:[$("<strong/>",{text:name}),$("<br/>"),$("<small/>",{class:"text-muted",text:info}),$("<br/>")]})},resetForm:function resetForm(){$customers.find(".record-details").find("input, select, textarea").val("").prop("disabled",!0),$customers.find(".record-details .form-label span").prop("hidden",!0),$customers.find(".record-details #timezone").val(vars("default_timezone")),$customers.find(".record-details #language").val(vars("default_language")),$customerAppointments.empty(),$customers.find("#edit-customer, #delete-customer").prop("disabled",!0),$customers.find("#add-edit-delete-group").show(),$customers.find("#save-cancel-group").hide(),$customers.find(".record-details .is-invalid").removeClass("is-invalid"),$customers.find(".record-details #form-message").hide(),$filterCustomers.find("button").prop("disabled",!1),$filterCustomers.find(".selected").removeClass("selected"),$filterCustomers.find(".results").css("color","")},display:display,select:select,addEventListeners:function addEventListeners(){$customers.on("submit","#filter-customers form",function(event){event.preventDefault();var key=$filterCustomers.find(".key").val();$filterCustomers.find(".selected").removeClass("selected"),filterLimit=20,App.Pages.Customers.resetForm(),App.Pages.Customers.filter(key)}),$customers.on("click",".customer-row",function(event){if(!$filterCustomers.find(".filter").prop("disabled")){var customerId=$(event.currentTarget).attr("data-id"),customer=filterResults.find(function(filterResult){return+filterResult.id===+customerId});display(customer),$("#filter-customers .selected").removeClass("selected"),$(event.currentTarget).addClass("selected"),$("#edit-customer, #delete-customer").prop("disabled",!1)}}),$customers.on("click","#add-customer",function(){App.Pages.Customers.resetForm(),$customers.find("#add-edit-delete-group").hide(),$customers.find("#save-cancel-group").show(),$customers.find(".record-details").find("input, select, textarea").prop("disabled",!1),$customers.find(".record-details .form-label span").prop("hidden",!1),$filterCustomers.find("button").prop("disabled",!0),$filterCustomers.find(".results").css("color","#AAA")}),$customers.on("click","#edit-customer",function(){$customers.find(".record-details").find("input, select, textarea").prop("disabled",!1),$customers.find(".record-details .form-label span").prop("hidden",!1),$customers.find("#add-edit-delete-group").hide(),$customers.find("#save-cancel-group").show(),$filterCustomers.find("button").prop("disabled",!0),$filterCustomers.find(".results").css("color","#AAA")}),$customers.on("click","#cancel-customer",function(){var id=$id.val();App.Pages.Customers.resetForm(),id&&select(id,!0)}),$customers.on("click","#save-customer",function(){var customer={first_name:$firstName.val(),last_name:$lastName.val(),email:$email.val(),phone_number:$phoneNumber.val(),address:$address.val(),city:$city.val(),zip_code:$zipCode.val(),notes:$notes.val(),timezone:$timezone.val(),language:$language.val()||"english",custom_field_1:$customField1.val(),custom_field_2:$customField2.val(),custom_field_3:$customField3.val(),custom_field_4:$customField4.val(),custom_field_5:$customField5.val(),ldap_dn:$ldapDn.val()};$id.val()&&(customer.id=$id.val()),App.Pages.Customers.validate()&&App.Pages.Customers.save(customer)}),$customers.on("click","#delete-customer",function(){var customerId=$id.val(),buttons=[{text:lang("cancel"),click:function click(event,messageModal){messageModal.hide()}},{text:lang("delete"),click:function click(event,messageModal){App.Pages.Customers.remove(customerId),messageModal.hide()}}];App.Utils.Message.show(lang("delete_customer"),lang("delete_record_prompt"),buttons)})}}}();