jQuery(document).ready(function () { Date.prototype.format = function () { return this.getFullYear() + '-' + this.strPad(this.getMonth() + 1) + '-' + this.strPad(this.getDate()); }; Date.prototype.strPad = function (val) { if (val > 9) { return val.toString(); } return '0' + val; }; var Scheduler = function (options) { this.init(options); } jQuery.extend(Scheduler.prototype, { options: { 'api_url': '//user-api.simplybook.me', 'api_login': 'mib', 'api_key': 'f43618e37b82004066d60db3431f4a06392599a6cfcafa8268bf25becc0ec7d7' }, /*options: { 'api_url': '//user-api.em.dimka.notando.com.ua', 'api_login': 'recap', 'api_key': '8f5259e8e9f6d9342d40ef523b2f1fe52c0a3b543d140b5a3da906f17ab30e11' },*/ client: null, anyUnitData: null, events: {}, units: {}, attachedUnits: [], eventId: null, unitId: null, qty: 1, selectedUnitId: null, date: null, time: null, weekends: {}, groupBookingsAllowed: false, multipleBookingsAllowed: false, batchId: null, additionalFields: [], init: function (options) { this.options = jQuery.extend({}, this.options, options); this.initClient(); this.initElements(); this.initEvents(); }, initClient: function () { var instance = this; var loginClient = new JSONRpcClient({ 'url': this.getParam('api_url') + '/login', 'onerror': function (error) { instance.error(error); } }); var token = loginClient.getToken(this.getParam('api_login'), this.getParam('api_key')); this.client = new JSONRpcClient({ 'url': this.getParam('api_url'), 'headers': { 'X-Company-Login': this.getParam('api_login'), 'X-Token': token }, 'onerror': function (error) { instance.error(error); } }); this.groupBookingsAllowed = this.client.isPluginActivated('group_booking'); this.multipleBookingsAllowed = this.client.isPluginActivated('multiple_booking'); }, initElements: function () { jQuery('#qty-element').hide(); var instance = this; jQuery('#date').datepicker({ 'startDate': new Date(), 'todayHighlight': true, 'format': 'yyyy-mm-dd', 'beforeShowDay': function (date) { if (instance.weekends[date.format()] == true) { return {'enabled': false}; } } }); this.client.getEventList(function (events) { instance.events = events; for (var id in events) { var event = events[id]; jQuery('#event_id').append( jQuery('') ); if (event.unit_map) { for (var unitId in event.unit_map) { instance.attachedUnits.push(unitId); } } } }); this.client.getAnyUnitData(function (data) { instance.anyUnitData = data; instance.client.getUnitList(function (units) { instance.units = units; instance.setUnitList(units); }); }); this.client.getFirstWorkingDay(null, function (date) { instance.updateWorkCalendar(new Date(Date.parse(date)), function () { jQuery('#date').datepicker('update', date); }); }); if (!this.multipleBookingsAllowed) { jQuery('#add_booking').hide(); } jQuery('#confirm_batch').hide(); }, initEvents: function () { var instance = this; jQuery('#event_id').change(function (event, value) { instance.setEventId(jQuery(this).val()); }); jQuery('#unit_id').change(function () { instance.setUnitId(jQuery(this).val()); }); jQuery('#qty').change(function () { instance.setQty(jQuery(this).val()); }); jQuery('#date').on('changeDate', function (event) { if (event.date) { instance.setDate(event.date.format()); } }).on('changeMonth', function (event) { instance.updateWorkCalendar(event.date); }); jQuery('.time-handler').change(function () { instance.loadStartTimeMatrix(); }); jQuery(document).on('click', '.time-item', function () { jQuery('.time-item.active').removeClass('active'); jQuery(this).addClass('active'); instance.setTime(jQuery(this).data('time')); if (instance.unitId == -1) { var unitArr = instance.client.getAvailableUnits(instance.eventId, instance.date + ' ' + instance.time, instance.qty); if (unitArr.length) { instance.selectedUnitId = unitArr[0]; } } }); jQuery('#book').click(function () { if (instance.selectedUnitId) { jQuery('#unit_id').val(instance.selectedUnitId); } instance.showClientInfo(); }); jQuery('#confirm').click(function () { instance.bookAppointment(); }); jQuery('#confirm_batch').click(function () { instance.confirmBatch(); }); jQuery('#add_booking').click(function () { instance.addAppointment(true); }); jQuery('#cancel').click(function () { instance.cancel(); }); }, setQty: function (qty) { this.qty = qty; if (qty > 1) { jQuery('#add_booking').hide(); } else { jQuery('#add_booking').show(); } }, setUnitList: function (units) { var oldVal = jQuery('#unit_id').val(); jQuery('#unit_id option[value]').remove(); if (this.anyUnitData) { jQuery('#unit_id').append( // -1 means any unit only for JS. We don't send it to server. jQuery('') ); } for (var id in units) { var unit = units[id]; jQuery('#unit_id').append( jQuery('') ); } if (units[oldVal]) { jQuery('#unit_id').val(oldVal); } else { this.setUnitId(''); } }, setEventId: function (id) { this.eventId = id; var event = this.events[id]; var list = {}; if (event.unit_map) { for (var unitId in event.unit_map) { list[unitId] = this.units[unitId]; } } else { for (var unitId in this.units) { if (jQuery.inArray(unitId, this.attachedUnits) == -1) { list[unitId] = this.units[unitId]; } } } this.setUnitList(list); if (this.groupBookingsAllowed && !this.batchId) { var max = 1; for (var unitId in list) { if (list[unitId] && list[unitId].qty > max) { max = list[unitId].qty; } } this.setMaxQty(max); } else { jQuery('#qty-element').hide(); } this.loadAdditionalFields(id); }, setMaxQty: function (max) { jQuery('#qty').empty(); this.qty = 1; for (var i = 1; i <= max; i++) { jQuery('#qty').append(''); } if (max > 1) { jQuery('#qty-element').show(); } else { jQuery('#qty-element').hide(); } }, setUnitId: function (id) { var instance = this; this.unitId = id; if (id && !this.date) { if (id == -1) { id = null; } this.client.getFirstWorkingDay(id, function (date) { instance.updateWorkCalendar(new Date(Date.parse(date)), function () { instance.setDate(date); }); }); } }, updateWorkCalendar: function (date, callback) { if (!date) { date = jQuery('#date').datepicker('getDate'); } var instance = this; var params = { 'unit_group_id': this.unitId, 'event_id': this.eventId }; this.client.getWorkCalendar(date.getFullYear(), (date.getMonth() + 1), params, function (data) { instance.weekends = {}; for (var dt in data) { if (data[dt].is_day_off == '1') { instance.weekends[dt] = true; } } jQuery('#date').datepicker('update'); if (jQuery.type(callback) == 'function') { callback(); } }); }, loadAdditionalFields: function (eventId) { var instance = this; this.client.getAdditionalFields(eventId, function (data) { instance.clearAdditionalFields(); instance.additionalFields = data; for (var i = 0; i < data.length; i++) { instance.addAdditionalField(data[i]); } }); }, clearAdditionalFields: function () { jQuery('#additional-fields').empty(); this.additionalFields = []; }, addAdditionalField: function (field) { var container = jQuery('
'); var title = jQuery('