2016-04-02 15:59:31 +03:00
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Easy!Appointments - Open Source Web Scheduler
|
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2021-12-18 19:43:45 +03:00
|
|
|
* @copyright Copyright (c) Alex Tselegidis
|
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link https://easyappointments.org
|
2022-01-06 11:53:16 +03:00
|
|
|
* @since v1.5.0
|
2016-04-02 15:59:31 +03:00
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
2022-01-06 11:53:16 +03:00
|
|
|
/**
|
|
|
|
* Customers page module.
|
|
|
|
*
|
|
|
|
* This class contains the methods that are used in the backend customers page.
|
|
|
|
*/
|
|
|
|
App.Pages.Customers = (function () {
|
|
|
|
const $customers = $('#customers');
|
|
|
|
let filterResults = {};
|
|
|
|
let filterLimit = 20;
|
2016-04-02 15:59:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Binds the default event handlers of the backend customers page.
|
|
|
|
*/
|
2022-01-06 11:53:16 +03:00
|
|
|
function bindEventHandlers() {
|
2016-04-02 15:59:31 +03:00
|
|
|
/**
|
|
|
|
* Event: Filter Customers Form "Submit"
|
2020-12-08 14:23:37 +03:00
|
|
|
*
|
|
|
|
* @param {jQuery.Event} event
|
2016-04-02 15:59:31 +03:00
|
|
|
*/
|
2022-01-06 11:53:16 +03:00
|
|
|
$customers.on('submit', '#filter-customers form', function (event) {
|
2020-12-08 14:23:37 +03:00
|
|
|
event.preventDefault();
|
2022-01-06 11:53:16 +03:00
|
|
|
const key = $('#filter-customers .key').val();
|
2016-05-20 10:08:41 +03:00
|
|
|
$('#filter-customers .selected').removeClass('selected');
|
2022-01-06 11:53:16 +03:00
|
|
|
filterLimit = 20;
|
|
|
|
resetForm();
|
|
|
|
filter(key);
|
2016-04-02 15:59:31 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Filter Customers Clear Button "Click"
|
|
|
|
*/
|
2022-01-06 11:53:16 +03:00
|
|
|
$customers.on('click', '#filter-customers .clear', function () {
|
2016-04-02 15:59:31 +03:00
|
|
|
$('#filter-customers .key').val('');
|
2022-01-06 11:53:16 +03:00
|
|
|
filterLimit = 20;
|
|
|
|
filter('');
|
|
|
|
resetForm();
|
2016-04-02 15:59:31 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2016-05-20 10:08:41 +03:00
|
|
|
* Event: Filter Entry "Click"
|
2016-04-02 15:59:31 +03:00
|
|
|
*
|
|
|
|
* Display the customer data of the selected row.
|
|
|
|
*/
|
2022-01-06 11:53:16 +03:00
|
|
|
$customers.on('click', '.customer-row', function () {
|
2016-04-02 15:59:31 +03:00
|
|
|
if ($('#filter-customers .filter').prop('disabled')) {
|
|
|
|
return; // Do nothing when user edits a customer record.
|
|
|
|
}
|
|
|
|
|
2016-07-15 21:52:21 +03:00
|
|
|
var customerId = $(this).attr('data-id');
|
2022-01-06 11:53:16 +03:00
|
|
|
var customer = filterResults.find(function (filterResult) {
|
2020-05-12 21:52:32 +03:00
|
|
|
return Number(filterResult.id) === Number(customerId);
|
2016-04-02 15:59:31 +03:00
|
|
|
});
|
|
|
|
|
2022-01-06 11:53:16 +03:00
|
|
|
display(customer);
|
2016-05-20 10:08:41 +03:00
|
|
|
$('#filter-customers .selected').removeClass('selected');
|
|
|
|
$(this).addClass('selected');
|
2016-04-02 15:59:31 +03:00
|
|
|
$('#edit-customer, #delete-customer').prop('disabled', false);
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Add Customer Button "Click"
|
|
|
|
*/
|
2022-01-06 11:53:16 +03:00
|
|
|
$customers.on('click', '#add-customer', function () {
|
|
|
|
resetForm();
|
2016-04-02 15:59:31 +03:00
|
|
|
$('#add-edit-delete-group').hide();
|
|
|
|
$('#save-cancel-group').show();
|
2021-11-06 19:38:37 +03:00
|
|
|
$('.record-details').find('input, select, textarea').prop('disabled', false);
|
2016-04-02 15:59:31 +03:00
|
|
|
$('#filter-customers button').prop('disabled', true);
|
|
|
|
$('#filter-customers .results').css('color', '#AAA');
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Edit Customer Button "Click"
|
|
|
|
*/
|
2022-01-06 11:53:16 +03:00
|
|
|
$customers.on('click', '#edit-customer', function () {
|
2021-11-06 19:38:37 +03:00
|
|
|
$('.record-details').find('input, select, textarea').prop('disabled', false);
|
2016-04-02 15:59:31 +03:00
|
|
|
$('#add-edit-delete-group').hide();
|
|
|
|
$('#save-cancel-group').show();
|
|
|
|
$('#filter-customers button').prop('disabled', true);
|
|
|
|
$('#filter-customers .results').css('color', '#AAA');
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Cancel Customer Add/Edit Operation Button "Click"
|
|
|
|
*/
|
2022-01-06 11:53:16 +03:00
|
|
|
$customers.on('click', '#cancel-customer', function () {
|
2016-04-02 15:59:31 +03:00
|
|
|
var id = $('#customer-id').val();
|
2022-01-06 11:53:16 +03:00
|
|
|
resetForm();
|
2020-05-06 20:15:11 +03:00
|
|
|
if (id) {
|
2022-01-06 11:53:16 +03:00
|
|
|
select(id, true);
|
2016-04-02 15:59:31 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Save Add/Edit Customer Operation "Click"
|
|
|
|
*/
|
2022-01-06 11:53:16 +03:00
|
|
|
$customers.on('click', '#save-customer', function () {
|
2016-04-02 15:59:31 +03:00
|
|
|
var customer = {
|
2016-07-15 21:52:21 +03:00
|
|
|
first_name: $('#first-name').val(),
|
|
|
|
last_name: $('#last-name').val(),
|
|
|
|
email: $('#email').val(),
|
|
|
|
phone_number: $('#phone-number').val(),
|
|
|
|
address: $('#address').val(),
|
|
|
|
city: $('#city').val(),
|
|
|
|
zip_code: $('#zip-code').val(),
|
2020-03-29 15:10:49 +03:00
|
|
|
notes: $('#notes').val(),
|
2020-09-23 13:48:07 +03:00
|
|
|
timezone: $('#timezone').val(),
|
|
|
|
language: $('#language').val() || 'english'
|
2016-04-02 15:59:31 +03:00
|
|
|
};
|
|
|
|
|
2020-05-06 20:15:11 +03:00
|
|
|
if ($('#customer-id').val()) {
|
2016-04-02 15:59:31 +03:00
|
|
|
customer.id = $('#customer-id').val();
|
|
|
|
}
|
|
|
|
|
2022-01-06 11:53:16 +03:00
|
|
|
if (!validate()) {
|
2017-11-14 15:52:59 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-04-02 15:59:31 +03:00
|
|
|
|
2022-01-06 11:53:16 +03:00
|
|
|
save(customer);
|
2016-04-02 15:59:31 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Delete Customer Button "Click"
|
|
|
|
*/
|
2022-01-06 11:53:16 +03:00
|
|
|
$customers.on('click', '#delete-customer', function () {
|
2016-07-15 21:52:21 +03:00
|
|
|
var customerId = $('#customer-id').val();
|
2017-06-16 09:51:05 +03:00
|
|
|
var buttons = [
|
|
|
|
{
|
2021-12-13 09:52:09 +03:00
|
|
|
text: App.Lang.cancel,
|
2018-01-23 12:08:37 +03:00
|
|
|
click: function () {
|
2020-09-07 11:08:18 +03:00
|
|
|
$('#message-box').dialog('close');
|
2017-06-16 09:51:05 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2021-12-13 09:52:09 +03:00
|
|
|
text: App.Lang.delete,
|
2018-01-23 12:08:37 +03:00
|
|
|
click: function () {
|
2022-01-06 11:53:16 +03:00
|
|
|
remove(customerId);
|
2020-09-07 11:08:18 +03:00
|
|
|
$('#message-box').dialog('close');
|
2017-06-16 09:51:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
];
|
2016-04-02 15:59:31 +03:00
|
|
|
|
2022-01-06 11:53:16 +03:00
|
|
|
App.Utils.Message.show(App.Lang.delete_customer, App.Lang.delete_record_prompt, buttons);
|
2016-04-02 15:59:31 +03:00
|
|
|
});
|
2022-01-06 11:53:16 +03:00
|
|
|
}
|
2016-04-02 15:59:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Save a customer record to the database (via ajax post).
|
|
|
|
*
|
2016-05-15 13:17:12 +03:00
|
|
|
* @param {Object} customer Contains the customer data.
|
2016-04-02 15:59:31 +03:00
|
|
|
*/
|
2022-01-06 11:53:16 +03:00
|
|
|
function save(customer) {
|
|
|
|
App.Http.Customers.save(customer).then((response) => {
|
|
|
|
Backend.displayNotification(App.Lang.customer_saved);
|
|
|
|
resetForm();
|
|
|
|
$('#filter-customers .key').val('');
|
|
|
|
filter('', response.id, true);
|
|
|
|
});
|
|
|
|
}
|
2016-04-02 15:59:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a customer record from database.
|
|
|
|
*
|
2016-05-15 13:17:12 +03:00
|
|
|
* @param {Number} id Record id to be deleted.
|
2016-04-02 15:59:31 +03:00
|
|
|
*/
|
2022-01-06 11:53:16 +03:00
|
|
|
function remove(id) {
|
|
|
|
App.Http.Customers.destroy(id).then(() => {
|
|
|
|
Backend.displayNotification(App.Lang.customer_deleted);
|
|
|
|
resetForm();
|
|
|
|
filter($('#filter-customers .key').val());
|
|
|
|
});
|
|
|
|
}
|
2016-04-02 15:59:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate customer data before save (insert or update).
|
|
|
|
*/
|
2022-01-06 11:53:16 +03:00
|
|
|
function validate() {
|
2021-11-06 19:38:37 +03:00
|
|
|
$('#form-message').removeClass('alert-danger').hide();
|
2021-11-23 12:10:09 +03:00
|
|
|
$('.is-invalid').removeClass('is-invalid');
|
2016-04-02 15:59:31 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
// Validate required fields.
|
|
|
|
var missingRequired = false;
|
|
|
|
|
2020-05-12 21:52:32 +03:00
|
|
|
$('.required').each(function (index, requiredField) {
|
|
|
|
if ($(requiredField).val() === '') {
|
2021-11-23 12:10:09 +03:00
|
|
|
$(requiredField).addClass('is-invalid');
|
2016-04-02 15:59:31 +03:00
|
|
|
missingRequired = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (missingRequired) {
|
2021-12-13 09:52:09 +03:00
|
|
|
throw new Error(App.Lang.fields_are_required);
|
2016-04-02 15:59:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate email address.
|
|
|
|
if (!GeneralFunctions.validateEmail($('#email').val())) {
|
2021-11-23 12:10:09 +03:00
|
|
|
$('#email').addClass('is-invalid');
|
2021-12-13 09:52:09 +03:00
|
|
|
throw new Error(App.Lang.invalid_email);
|
2016-04-02 15:59:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2020-05-06 20:15:11 +03:00
|
|
|
} catch (error) {
|
2021-11-06 19:38:37 +03:00
|
|
|
$('#form-message').addClass('alert-danger').text(error.message).show();
|
2016-04-02 15:59:31 +03:00
|
|
|
return false;
|
|
|
|
}
|
2022-01-06 11:53:16 +03:00
|
|
|
}
|
2016-04-02 15:59:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Bring the customer form back to its initial state.
|
|
|
|
*/
|
2022-01-06 11:53:16 +03:00
|
|
|
function resetForm() {
|
2021-11-06 19:38:37 +03:00
|
|
|
$('.record-details').find('input, select, textarea').val('').prop('disabled', true);
|
2020-12-14 21:40:10 +03:00
|
|
|
$('.record-details #timezone').val('UTC');
|
2016-04-02 15:59:31 +03:00
|
|
|
|
2020-09-23 13:48:07 +03:00
|
|
|
$('#language').val('english');
|
2020-06-16 18:01:27 +03:00
|
|
|
|
2017-09-23 02:27:07 +03:00
|
|
|
$('#customer-appointments').empty();
|
2016-04-02 15:59:31 +03:00
|
|
|
$('#edit-customer, #delete-customer').prop('disabled', true);
|
|
|
|
$('#add-edit-delete-group').show();
|
|
|
|
$('#save-cancel-group').hide();
|
|
|
|
|
2021-11-23 12:10:09 +03:00
|
|
|
$('.record-details .is-invalid').removeClass('is-invalid');
|
2016-05-20 10:08:41 +03:00
|
|
|
$('.record-details #form-message').hide();
|
2016-04-02 15:59:31 +03:00
|
|
|
|
|
|
|
$('#filter-customers button').prop('disabled', false);
|
2016-05-20 10:08:41 +03:00
|
|
|
$('#filter-customers .selected').removeClass('selected');
|
2016-04-02 15:59:31 +03:00
|
|
|
$('#filter-customers .results').css('color', '');
|
2022-01-06 11:53:16 +03:00
|
|
|
}
|
2016-04-02 15:59:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a customer record into the form.
|
|
|
|
*
|
2016-05-15 13:17:12 +03:00
|
|
|
* @param {Object} customer Contains the customer record data.
|
2016-04-02 15:59:31 +03:00
|
|
|
*/
|
2022-01-06 11:53:16 +03:00
|
|
|
function display(customer) {
|
2016-04-02 15:59:31 +03:00
|
|
|
$('#customer-id').val(customer.id);
|
|
|
|
$('#first-name').val(customer.first_name);
|
|
|
|
$('#last-name').val(customer.last_name);
|
|
|
|
$('#email').val(customer.email);
|
|
|
|
$('#phone-number').val(customer.phone_number);
|
|
|
|
$('#address').val(customer.address);
|
|
|
|
$('#city').val(customer.city);
|
|
|
|
$('#zip-code').val(customer.zip_code);
|
|
|
|
$('#notes').val(customer.notes);
|
2020-03-29 15:10:49 +03:00
|
|
|
$('#timezone').val(customer.timezone);
|
2020-09-23 13:48:07 +03:00
|
|
|
$('#language').val(customer.language || 'english');
|
2020-06-16 18:01:27 +03:00
|
|
|
|
2016-04-02 15:59:31 +03:00
|
|
|
$('#customer-appointments').empty();
|
2020-09-08 10:42:58 +03:00
|
|
|
|
|
|
|
if (!customer.appointments.length) {
|
|
|
|
$('<p/>', {
|
2021-12-13 09:52:09 +03:00
|
|
|
'text': App.Lang.no_records_found
|
2021-11-06 19:38:37 +03:00
|
|
|
}).appendTo('#customer-appointments');
|
2020-09-08 10:42:58 +03:00
|
|
|
}
|
|
|
|
|
2020-05-12 21:52:32 +03:00
|
|
|
customer.appointments.forEach(function (appointment) {
|
2021-11-06 19:38:37 +03:00
|
|
|
if (
|
2022-01-06 11:53:16 +03:00
|
|
|
App.Vars.role_slug === Backend.DB_SLUG_PROVIDER &&
|
|
|
|
parseInt(appointment.id_users_provider) !== App.Vars.user_id
|
2021-11-06 19:38:37 +03:00
|
|
|
) {
|
2020-05-12 21:52:32 +03:00
|
|
|
return;
|
2018-07-29 16:59:18 +03:00
|
|
|
}
|
|
|
|
|
2021-11-06 19:38:37 +03:00
|
|
|
if (
|
2022-01-06 11:53:16 +03:00
|
|
|
App.Vars.role_slug === Backend.DB_SLUG_SECRETARY &&
|
|
|
|
App.Vars.secretary_providers.indexOf(appointment.id_users_provider) === -1
|
2021-11-06 19:38:37 +03:00
|
|
|
) {
|
2020-05-12 21:52:32 +03:00
|
|
|
return;
|
2018-07-29 16:59:18 +03:00
|
|
|
}
|
|
|
|
|
2021-11-06 19:38:37 +03:00
|
|
|
var start = GeneralFunctions.formatDate(
|
2021-11-24 10:34:26 +03:00
|
|
|
moment(appointment.start_datetime).toDate(),
|
2022-01-06 11:53:16 +03:00
|
|
|
App.Vars.date_format,
|
2021-11-06 19:38:37 +03:00
|
|
|
true
|
|
|
|
);
|
|
|
|
var end = GeneralFunctions.formatDate(
|
2021-11-24 10:34:26 +03:00
|
|
|
moment(appointment.end_datetime).toDate(),
|
2022-01-06 11:53:16 +03:00
|
|
|
App.Vars.date_format,
|
2021-11-06 19:38:37 +03:00
|
|
|
true
|
|
|
|
);
|
2020-05-07 19:47:14 +03:00
|
|
|
|
|
|
|
$('<div/>', {
|
|
|
|
'class': 'appointment-row',
|
|
|
|
'data-id': appointment.id,
|
|
|
|
'html': [
|
2020-09-08 10:42:58 +03:00
|
|
|
// Service - Provider
|
|
|
|
|
|
|
|
$('<a/>', {
|
2022-01-06 11:53:16 +03:00
|
|
|
'href': App.Utils.Url.siteUrl(`backend/index/${appointment.hash}`),
|
2020-09-08 10:42:58 +03:00
|
|
|
'html': [
|
|
|
|
$('<i/>', {
|
2021-11-23 10:43:40 +03:00
|
|
|
'class': 'fas fa-edit me-1'
|
2020-09-08 10:42:58 +03:00
|
|
|
}),
|
|
|
|
$('<strong/>', {
|
2021-11-06 19:38:37 +03:00
|
|
|
'text':
|
|
|
|
appointment.service.name +
|
|
|
|
' - ' +
|
|
|
|
appointment.provider.first_name +
|
|
|
|
' ' +
|
|
|
|
appointment.provider.last_name
|
2020-09-08 10:42:58 +03:00
|
|
|
}),
|
2021-11-06 19:38:37 +03:00
|
|
|
$('<br/>')
|
2020-09-08 10:42:58 +03:00
|
|
|
]
|
|
|
|
}),
|
|
|
|
|
|
|
|
// Start
|
|
|
|
|
|
|
|
$('<small/>', {
|
|
|
|
'text': start
|
2020-05-07 19:47:14 +03:00
|
|
|
}),
|
|
|
|
$('<br/>'),
|
2020-09-08 10:42:58 +03:00
|
|
|
|
|
|
|
// End
|
|
|
|
|
|
|
|
$('<small/>', {
|
|
|
|
'text': end
|
2020-05-07 19:47:14 +03:00
|
|
|
}),
|
2020-09-08 10:42:58 +03:00
|
|
|
$('<br/>'),
|
|
|
|
|
|
|
|
// Timezone
|
|
|
|
|
|
|
|
$('<small/>', {
|
2022-01-06 11:53:16 +03:00
|
|
|
'text': App.Vars.timezones[appointment.provider.timezone]
|
2020-05-07 19:47:14 +03:00
|
|
|
})
|
|
|
|
]
|
2021-11-06 19:38:37 +03:00
|
|
|
}).appendTo('#customer-appointments');
|
2016-04-02 15:59:31 +03:00
|
|
|
});
|
2022-01-06 11:53:16 +03:00
|
|
|
}
|
2016-04-02 15:59:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter customer records.
|
|
|
|
*
|
2021-11-18 08:34:07 +03:00
|
|
|
* @param {String} keyword This keyword string is used to filter the customer records.
|
2016-05-15 13:17:12 +03:00
|
|
|
* @param {Number} selectId Optional, if set then after the filter operation the record with the given
|
|
|
|
* ID will be selected (but not displayed).
|
|
|
|
* @param {Boolean} display Optional (false), if true then the selected record will be displayed on the form.
|
2016-04-02 15:59:31 +03:00
|
|
|
*/
|
2022-01-06 11:53:16 +03:00
|
|
|
function filter(keyword, selectId = null, display = false) {
|
|
|
|
App.Http.Customers.search(keyword, filterLimit).then((response) => {
|
|
|
|
filterResults = response;
|
2016-04-02 15:59:31 +03:00
|
|
|
|
2022-01-06 11:53:16 +03:00
|
|
|
$('#filter-customers .results').empty();
|
2020-05-06 20:15:11 +03:00
|
|
|
|
2022-01-06 11:53:16 +03:00
|
|
|
response.forEach(
|
|
|
|
function (customer) {
|
|
|
|
$('#filter-customers .results').append(getFilterHtml(customer)).append($('<hr/>'));
|
|
|
|
}.bind(this)
|
|
|
|
);
|
2020-05-12 21:52:32 +03:00
|
|
|
|
2022-01-06 11:53:16 +03:00
|
|
|
if (!response.length) {
|
|
|
|
$('#filter-customers .results').append(
|
|
|
|
$('<em/>', {
|
|
|
|
'text': App.Lang.no_records_found
|
|
|
|
})
|
2021-11-06 19:38:37 +03:00
|
|
|
);
|
2022-01-06 11:53:16 +03:00
|
|
|
} else if (response.length === filterLimit) {
|
|
|
|
$('<button/>', {
|
|
|
|
'type': 'button',
|
|
|
|
'class': 'btn btn-outline-secondary w-100 load-more text-center',
|
|
|
|
'text': App.Lang.load_more,
|
|
|
|
'click': function () {
|
|
|
|
filterLimit += 20;
|
|
|
|
filter(keyword, selectId, display);
|
|
|
|
}.bind(this)
|
|
|
|
}).appendTo('#filter-customers .results');
|
|
|
|
}
|
2020-05-06 20:15:11 +03:00
|
|
|
|
2022-01-06 11:53:16 +03:00
|
|
|
if (selectId) {
|
|
|
|
select(selectId, display);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-04-02 15:59:31 +03:00
|
|
|
|
|
|
|
/**
|
2016-05-15 13:17:12 +03:00
|
|
|
* Get the filter results row HTML code.
|
|
|
|
*
|
|
|
|
* @param {Object} customer Contains the customer data.
|
2016-04-02 15:59:31 +03:00
|
|
|
*
|
2016-05-15 13:17:12 +03:00
|
|
|
* @return {String} Returns the record HTML code.
|
2016-04-02 15:59:31 +03:00
|
|
|
*/
|
2022-01-06 11:53:16 +03:00
|
|
|
function getFilterHtml(customer) {
|
2016-04-02 15:59:31 +03:00
|
|
|
var name = customer.first_name + ' ' + customer.last_name;
|
2020-05-07 19:47:14 +03:00
|
|
|
|
2016-04-02 15:59:31 +03:00
|
|
|
var info = customer.email;
|
|
|
|
|
2020-05-07 19:47:14 +03:00
|
|
|
info = customer.phone_number ? info + ', ' + customer.phone_number : info;
|
2016-04-02 15:59:31 +03:00
|
|
|
|
2020-05-07 19:47:14 +03:00
|
|
|
return $('<div/>', {
|
|
|
|
'class': 'customer-row entry',
|
|
|
|
'data-id': customer.id,
|
|
|
|
'html': [
|
|
|
|
$('<strong/>', {
|
|
|
|
'text': name
|
|
|
|
}),
|
|
|
|
$('<br/>'),
|
|
|
|
$('<span/>', {
|
|
|
|
'text': info
|
|
|
|
}),
|
2021-11-06 19:38:37 +03:00
|
|
|
$('<br/>')
|
2020-05-07 19:47:14 +03:00
|
|
|
]
|
|
|
|
});
|
2022-01-06 11:53:16 +03:00
|
|
|
}
|
2016-04-02 15:59:31 +03:00
|
|
|
|
|
|
|
/**
|
2016-05-15 13:17:12 +03:00
|
|
|
* Select a specific record from the current filter results.
|
|
|
|
*
|
|
|
|
* If the customer id does not exist in the list then no record will be selected.
|
2016-04-02 15:59:31 +03:00
|
|
|
*
|
2016-05-15 13:17:12 +03:00
|
|
|
* @param {Number} id The record id to be selected from the filter results.
|
2022-01-06 11:53:16 +03:00
|
|
|
* @param {Boolean} show Optional (false), if true then the method will display the record
|
2016-04-02 15:59:31 +03:00
|
|
|
* on the form.
|
|
|
|
*/
|
2022-01-06 11:53:16 +03:00
|
|
|
function select(id, show = false) {
|
2016-05-20 10:08:41 +03:00
|
|
|
$('#filter-customers .selected').removeClass('selected');
|
2016-04-02 15:59:31 +03:00
|
|
|
|
2020-05-12 21:52:32 +03:00
|
|
|
$('#filter-customers .entry[data-id="' + id + '"]').addClass('selected');
|
2016-04-02 15:59:31 +03:00
|
|
|
|
2022-01-06 11:53:16 +03:00
|
|
|
if (show) {
|
|
|
|
const customer = filterResults.find(function (filterResult) {
|
2020-05-12 21:52:32 +03:00
|
|
|
return Number(filterResult.id) === Number(id);
|
|
|
|
});
|
|
|
|
|
2022-01-06 11:53:16 +03:00
|
|
|
display(customer);
|
2020-05-12 21:52:32 +03:00
|
|
|
|
|
|
|
$('#edit-customer, #delete-customer').prop('disabled', false);
|
2016-04-02 15:59:31 +03:00
|
|
|
}
|
2022-01-06 11:53:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function init() {
|
|
|
|
resetForm();
|
|
|
|
bindEventHandlers();
|
|
|
|
filter('');
|
|
|
|
}
|
2016-04-02 15:59:31 +03:00
|
|
|
|
2022-01-06 11:53:16 +03:00
|
|
|
document.addEventListener('DOMContentLoaded', init);
|
|
|
|
|
|
|
|
return {
|
|
|
|
filter,
|
|
|
|
save,
|
|
|
|
remove,
|
|
|
|
getFilterHtml,
|
|
|
|
resetForm,
|
|
|
|
select
|
|
|
|
};
|
2016-04-02 15:59:31 +03:00
|
|
|
})();
|