text changes to registration mail content
[namibia] / public / js / app / portal-workspace / appointmentedit.js
1 ;(function(){
2
3     window._w.appointmentedit = {
4
5         itemId : null,
6         itemData : null,
7         formMeta : null,
8         ti : null,
9         limited : false,
10         newItem : false,
11         validations : 0,
12         highestOfferId : false,
13         priceGuideId : false,
14         publicCentreList : {},
15         isModalForm : true,
16         idSource : 'appointmentedit',
17
18         onValuationAppointmentEditTemplateReady : function( template, event, eventData )
19         {
20             _w.appointmentedit.itemData = null;
21             _w.appointmentedit.ti = _t[template];
22             _w.appointmentedit.limited = false;
23
24             //console.log('_w.appointmentId',_w.appointmentId);
25
26             App.DataStore.loadSelectListData('appointmentTimeSlot', true, 'Valuation', 'ValuationTimeSlots.SelectList');
27
28             _w.appointmentedit.setCalendarValuationViewFormMeta();
29             _w.appointmentedit.ti.hydrate(_w.appointmentedit.formMeta);
30             // Retrieve initial view data.
31             _w.appointmentedit.itemId = '' != _w.appointmentId && undefined != _w.appointmentId ? _w.appointmentId : 0;
32
33             _w.appointmentedit.newItem = true;
34             _w.appointmentedit.itemData = {ValuationAppointments:{}};
35             //App.API.getTask(
36             //    'createappointmentedit', 'Valuation', 'ValuationAppointments.Create',
37             //    null, {}, function(){}, _w.taskContractError
38             //);
39
40             App.API.getTask(
41                 'calendarValuationUpdate:' + _w.appointmentedit.itemId, 'Valuation', 'ValuationAppointments.Update', null,
42                 {'id':_w.appointmentedit.itemId}, $.proxy( _w.appointmentedit._onCalendarValuationViewDataReceived, this ), _w.taskContractError
43             );
44
45             //App.API.getTask(
46             //    'calendarValuationCreate:' + _w.appointmentedit.itemId, 'Valuation', 'ValuationAppointments.Create', _w.appointmentedit.itemId,
47             //    {'id':_w.appointmentedit.itemId}, $.proxy( _w.appointmentedit._onCalendarValuationViewDataReceived, this ), _w.taskContractError
48             //);
49
50             //App.API.getTask(
51             //    'calendarValuationUpdate:' + _w.appointmentedit.itemId, 'Valuation', 'ValuationAppointments.Update', null,
52             //    {'id':_w.appointmentedit.itemId},
53             //    $.proxy( function(responce){
54             //
55             //        console.log('responce',responce);
56             //
57             //        App.API.execTask(
58             //            'calendarValuationUpdate:' + _w.appointmentedit.itemId, {'id':_w.appointmentedit.itemId}, {},
59             //            $.proxy( _w.appointmentedit._onCalendarValuationViewDataReceived, this ),
60             //            _w.customercompletevaluation._onValuationFail
61             //        );
62             //    },this),
63             //    _w.taskContractError
64             //);
65
66             //App.API.execTask(
67             //    'createCustomerValuation', data, {},
68             //    _w.customercompletevaluation._onValuationUpdated,
69             //    _w.customercompletevaluation._onValuationFail
70             //);
71         },
72
73         onValuationAppointmentEditPublished : function( template, event, eventData )
74         {
75             $('#frmModal').modal({
76                 backdrop        : 'static',
77                 show            : true
78             });
79
80             _w.appointmentedit.limited = false;
81             _w.appointmentedit._setupValidation();
82
83             $('#btnSaveAppointment').prop('disabled', true);
84             $('#btnSaveAppointment').show();
85
86         },
87
88         _onCalendarValuationViewDataReceived : function( response )
89         {
90             console.log('response.Data',response.Data);
91
92             _w.appointmentedit.itemId = response.Data.id;
93             _w.appointmentedit.itemData = response.Data;
94             _w.appointmentedit.setCalendarValuationViewFormMeta(response.Data);
95             _w.appointmentedit.ti.hydratePartial(_w.appointmentedit.formMeta);
96             if (_w.appointmentedit.ti.published)
97             {
98                 _w.appointmentedit._setupValidation();
99             }
100         },
101
102         setCalendarValuationViewFormMeta : function(data)
103         {
104             if (!data)
105             {
106                 data = {"ValuationAppointments":{},"valuation":{},"timeSlot":{}};
107             }
108
109             console.log('data'.data);
110
111             if(undefined != data.appointmentDate)
112             {
113                 _w.appointmentDateData = data.appointmentDate.replace(/ 00:00:00/g,'');
114             }
115             if(undefined != data.timeSlot && undefined != data.timeSlot.timeSlot)
116             {
117                 var appointmentTimeSlotData = data.timeSlot.timeSlot.split(' - ');
118
119                 _w.appointmentTimeSlotData = appointmentTimeSlotData[1];
120             }
121
122             _w.appointmentedit.formMeta = {
123
124                 'firstName' : {
125                     type  : 'input',
126                     id    : 'firstName',
127                     title : 'Name',
128                     value : data.valuation.firstName ? data.valuation.firstName : ''
129                 },
130                 'familyName' : {
131                     type  : 'input',
132                     id    : 'familyName',
133                     title : 'Surname',
134                     value : data.valuation.familyName ? data.valuation.familyName : ''
135                 },
136                 'email' : {
137                     type  : 'input',
138                     id    : 'email',
139                     title : 'Email address',
140                     value : data.valuation.email ? data.valuation.email : ''
141                 },
142                 'mobile' : {
143                     type  : 'mobile',
144                     id    : 'mobile',
145                     title : 'Mobile',
146                     value : data.valuation.mobile ? data.valuation.mobile : ''
147                 },
148
149                 'appointmentDate' : {
150                     type  : 'input',
151                     id    : 'appointmentDate',
152                     title : 'Appointment Date',
153                     style : 'datepicker',
154                     value : data.appointmentDate ? data.appointmentDate.replace(/ 00:00:00/g,'') : ''
155                 },
156
157                 'timeSlot' : {
158                     type        : 'select',
159                     id          : 'timeSlot',
160                     title       : 'Appointment Time',
161                     dataStoreId : 'appointmentTimeSlot',
162                     value       : data.timeSlot.id ? data.timeSlot.id : ''
163                 },
164
165                 'appointmentCancelledComments' : {
166                     type  : 'textarea-styled',
167                     disabled    : false,
168                     id    : 'appointmentCancelledComments',
169                     title : 'Cancellation Reason',
170                     style : 'damages-comments',
171                     value : data.appointmentCancelledComments ? data.appointmentCancelledComments : ''
172                 },
173
174                 'appointmentCompleted': {
175                     type: 'checkbox',
176                     id: 'appointmentCompleted',
177                     title: 'Appointment Completed',
178                     value: data.appointmentCompleted ? data.appointmentCompleted : false
179                 },
180
181                 'appointmentCancelled': {
182                     type: 'checkbox',
183                     id: 'appointmentCancelled',
184                     title: 'Cancel Appointment',
185                     value: data.appointmentCancelled ? data.appointmentCancelled : false
186                 },
187
188
189                 'customerAddressStreet' : {
190                     type  : 'input',
191                     id    : 'customerAddressStreet',
192                     title : 'Street/Unit/Building No',
193                     value : data.valuation.customerAddressStreet ? data.valuation.customerAddressStreet : ''
194                 },
195                 'customerAddressStreetName' : {
196                     type  : 'input',
197                     id    : 'customerAddressStreetName',
198                     title : 'Street Name',
199                     value : data.valuation.customerAddressStreetName ? data.valuation.customerAddressStreetName : ''
200                 },
201                 'customerAddressSuburb' : {
202                     type  : 'input',
203                     id    : 'customerAddressSuburb',
204                     title : 'Suburb / District',
205                     value : data.valuation.customerAddressSuburb ? data.valuation.customerAddressSuburb : ''
206                 },
207                 'customerAddressCity' : {
208                     type  : 'input',
209                     id    : 'customerAddressCity',
210                     title : 'City / Town',
211                     value : data.valuation.customerAddressCity ? data.valuation.customerAddressCity : ''
212                 },
213                 'customerAddressPostalCode' : {
214                     type  : 'input',
215                     id    : 'customerAddressPostalCode',
216                     title : 'Postal Code',
217                     value : data.valuation.customerAddressPostalCode ? data.valuation.customerAddressPostalCode : ''
218                 }
219             };
220         },
221
222         _listMakesModelsTypes : function( fromItemData, callback )
223         {
224
225         },
226
227         _setupValidation : function ()
228         {
229
230             $('#btnSaveAppointment').click(function () {
231                 $('#btnSaveAppointment').prop('disabled', true);
232                 $('#terms').prop('disabled', true);
233                 if (!$('#frmAppointmentView').valid())
234                 {
235                     $('#btnSaveAppointment').prop('disabled', false);
236                     return;
237                 }
238
239
240                 var data = _w.appointmentedit._prepValuationUpdateData();
241
242                 console.log('data',data);
243
244                 App.API.execTask(
245                     'calendarValuationUpdate:' + _w.appointmentedit.itemId, data, {},
246                     _w.appointmentedit._onValuationUpdated,
247                     _w.appointmentedit._onValuationFail
248                 );
249             });
250
251             $('.appointmentCancelledComments').hide();
252             $('.appointmentCompleted').hide();
253             $('.datepicker').datepicker({format:"yyyy-mm-dd"});
254
255             if($('#appointmentCancelled').is(':checked'))
256             {
257                 $('.appointmentCancelledComments').show();
258                 $('.appointmentCancelledComments').removeClass('hide');
259                 $('#appointmentCancelledComments').attr('required',true);
260             }
261
262             $('#appointmentCancelled').change(function() {
263
264                 if($('#appointmentCancelled').is(':checked'))
265                 {
266                     $('.appointmentCancelledComments').show();
267                     $('.appointmentCancelledComments').removeClass('hide');
268                     $('#appointmentCancelledComments').attr('required',true);
269                 }
270                 else
271                 {
272                     $('.appointmentCancelledComments').hide();
273                     $('#appointmentCancelledComments').removeAttr('required')
274                 }
275             });
276
277             var calendarAppointmentDateData = _w.appointmentDateData.replace(/-/g,'');
278             var calendarAppointmentTimeSlotData = _w.appointmentTimeSlotData.replace(/:/g,'');
279
280             var date = new Date();
281             var day = (date.getDate() < 10 ? '0' : '') + date.getDate();
282
283             var month = ((date.getMonth() + 1) < 10 ? '0' : '') + (date.getMonth() + 1);
284             var year = date.getFullYear();
285             var hour = (date.getHours() < 10 ? '0' : '') + date.getHours();
286             var minutes = (date.getMinutes() < 10 ? '0' : '') + date.getMinutes();
287
288             if(year+month+day+hour+minutes > calendarAppointmentDateData+calendarAppointmentTimeSlotData)
289             {
290                 $('.appointmentCompleted').show();
291                 $('.appointmentCompleted').removeClass('hide');
292             }
293
294             $('#appointmentCompleted').change(function() {
295
296                 if($('#appointmentCompleted').is(':checked'))
297                 {
298                     $('.appointmentCancelled').hide();
299                     $('.appointmentCancelledComments').hide();
300                 }
301                 else
302                 {
303                     $('.appointmentCancelled').show();
304                     if($('#appointmentCancelled').is(':checked'))
305                     {
306                         $('.appointmentCancelledComments').show();
307                     }
308                 }
309             });
310
311
312             App.DataStore.listen('regValidation', 'damages', function(id, damages) {
313                 var rules = {
314                     firstName: {required: true, minlength: 3},
315                     familyName: {required: true, minlength: 3},
316                     email: {required: true, email: true},
317                     mobile: {required: true, mobile: true},
318                     requiredPrice: {required: true, number: true, min: 0.01},
319                     amountOffered: {required: true, number: true, min: 0.01},
320
321                     customerAddressStreet: {required: true},
322                     customerAddressStreetName: {required: true},
323                     customerAddressSuburb: {required: true},
324                     customerAddressCity: {required: true},
325                     customerAddressPostalCode: { required: true, digits: true }
326
327                 };
328                 for (var i = 0; i < damages.length; i++)
329                 {
330                     rules['damage' + damages[i].value + 'Amount'] = {number: true};
331                 }
332                 $('#frmAppointmentView').validate({
333                     "rules": rules,
334                     messages: {
335                         firstName: {
336                             minlength: "This field is required."
337                         },
338                         familyName: {
339                             minlength: "This field is required."
340                         },
341                         email: {
342                             required: "This field is required.",
343                             email: "Please enter a valid<br/>email address."
344                         },
345                         customerAddressStreet : "This field is required.",
346                         customerAddressStreetName : "This field is required.",
347                         customerAddressSuburb : "This field is required.",
348                         customerAddressCity : "This field is required.",
349                         customerAddressPostalCode : {required: "This field is required."}
350                     },
351                     invalidHandler: function(event, validator) {
352                         var errors = validator.numberOfInvalids();
353                         if (0 < _w.appointmentedit.validations && errors) {
354                             var message = errors == 1
355                                 ? 'You missed 1 field. It has been highlighted'
356                                 : 'You missed ' + errors + ' fields. They have been highlighted';
357                             alert(message);
358                         }
359                         _w.appointmentedit.validations++;
360                     },
361                     submitHandler: function( form ) {
362                         return;
363                     }
364                 });
365                 try {
366                     _w.appointmentedit.validations = 0;
367                     $('#frmAppointmentView').valid();
368                 } catch(err) {}
369                 $('label.error').remove();
370                 $('.valid').removeClass('valid');
371             });
372         },
373
374         _prepValuationUpdateData : function()
375         {
376             function isValidEmailAddress(emailAddress) {
377                 var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
378                 return pattern.test(emailAddress);
379             };
380
381             var data = _w.appointmentedit.ti.harvest();
382
383
384             var postData = {ValuationAppointments:{}, Valuation:{}};
385             var valuationFields = [
386                 "firstName","familyName","mobile",
387                 "email", "customerAddressStreet", "customerAddressStreetName", "customerAddressSuburb", "customerAddressCity", "customerAddressPostalCode",
388                 "requiredPrice"
389             ];
390             for (var field in data)
391             {
392                 if (-1 == valuationFields.indexOf(field))
393                 {
394                     postData.ValuationAppointments[field] = data[field];
395                 }
396                 else
397                 {
398                     postData.Valuation[field] = data[field];
399                 }
400             }
401
402             if ('' != postData.ValuationAppointments.appointmentDate)
403             {
404                 //var appointmentDate = new Date(postData.ValuationAppointments.appointmentDate);
405                 //
406                 //postData.ValuationAppointments.appointmentDate = appointmentDate;
407
408                 postData.ValuationAppointments.appointmentDate = postData.ValuationAppointments.appointmentDate + ' 00:00:00';
409             }
410
411             if( !isValidEmailAddress( postData.Valuation.email ) )
412             {
413                 testEmail = postData.Valuation.email;
414                 //console.log("went through check", testEmail);
415                 postData.Valuation.email = null;
416             }
417
418
419             if ('' == postData.Valuation.email)
420             {
421                 postData.Valuation.email = null;
422             }
423
424             return postData;
425         },
426
427         _onValuationUpdated : function( response )
428         {
429             //alert('Your vehicle has successfully been submitted.');
430             //window.location.hash = '/valuationcalendar';
431
432             //window.location.hash = "/valuationcalendar"; //Whatever is your hash value
433             //location.reload();
434
435             $('#calendar').fullCalendar('removeEvents');
436
437             var userId = App.userData.id
438
439             App.Ajax.JSON({
440                     "url"  : 'workspace/get-calendar-events',
441                     "data" : {'userId' : userId}
442                 },
443                 $.proxy(function(response) {
444
445                     //console.log('response',response);
446
447                     var calendarEvents = [];
448
449                     $.each( response, function( responseKey, responseValue ) {
450
451                         $.each( responseValue, function( responseK, responseV ) {
452
453                             var startDate = responseV.start.replace("SAST", "T");
454                             var endDate = responseV.end.replace("SAST", "T");
455
456                             calendarEvents.push(
457                                 {
458                                     'title'                             : responseV.title,
459                                     'start'                             : startDate,
460                                     'end'                                   : endDate,
461                                     //'url'                                     : '/#/appointmentedit',
462                                     'id'                                : responseV.appointmentId,
463                                     'appointmentCompleted'      : responseV.appointmentCompleted,
464                                     'appointmentCancelled'      : responseV.appointmentCancelled
465                                 }
466                             );
467
468                         });
469                     });
470                     //this.createCalendar(calendarEvents);
471
472                     //console.log('calendarEvents',calendarEvents);
473
474                     $('#calendar').fullCalendar( 'addEventSource', calendarEvents);
475
476                 },this),
477                 $.proxy(function(response) {
478                     console.log('App.Notification.remove Error', response);
479                 },this)
480             );
481
482
483
484             $('#frmModal .close').click();
485
486         },
487         _onValuationFail : function( response )
488         {
489             alert('Oops, something went wrong, please check your data carefully and try again.');
490             //window.location.hash = '/public-landing';
491         }
492
493     };
494
495 })();