define(['modules/inspects/editsection', 'modules/inspects/editquestion'], function (Section, Question) {
var edit = function (inspection, id) {
this.reportId = id;
this.instance = inspection;
};
var __proto = edit.prototype;
function onSave(_this, exit) {
var alerttitle = GetTextByKey("P_IPT_EDITINSPECTION", 'Edit Inspection');
if (!_this || !_this.report) {
showAlert(GetTextByKey("P_IPT_PAGEERROR", 'An unknown error occurred. Please refresh page.'), alerttitle);
return;
}
_this.loading && _this.loading.fadeIn(100);
var pages = _this.content.children('div[data-page]');
var answers = [];
for (var i = 0; i < pages.length; i++) {
var qs = $(pages[i]).find('.question-item');
for (var j = 0; j < qs.length; j++) {
var question = $(qs[j]).data('question');
var q = question.question;
var a = question.getAnswer();
var flag;
var msg;
var isChoice =
q.QuestionType === Question.types.List ||
q.QuestionType === Question.types.DropDown ||
q.QuestionType === Question.types.YesOrNo ||
q.QuestionType === Question.types.EmailList ||
q.QuestionType === Question.types.AssetStatus ||
(q.QuestionType === Question.types.FuelRecords
&& (q.SubType === Question.fueltypes.State ||
q.SubType === Question.fueltypes.FuelType));
if (q.IsRequired) {
if (isChoice) {
flag = a.SelectedItems == null || a.SelectedItems.length == 0;
} else if (a.Result == null || a.Result.length == 0) {
if (q.QuestionType === Question.types.Picture) {
flag = false;
} else if (q.QuestionType === Question.types.FuelRecords
&& (q.SubType === Question.fueltypes.TotalCost ||
q.SubType === Question.fueltypes.Picture)) {
flag = false;
} else {
flag = true;
}
}
} else if (a.Comment == null || a.Comment.length == 0) {
if (isChoice) {
if (a.SelectedItems == null || a.SelectedItems.length == 0) {
continue;
}
} else if (a.Result == null || a.Result.length == 0) {
if (q.QuestionType !== Question.types.Picture) {
continue;
}
}
}
// check
if (!flag && a.Result != null && a.Result.length > 0) {
switch (q.QuestionType) {
case Question.types.DateAndTime:
if (isNaN(new Date(a.Result.replace(' ', 'T')))) {
msg = GetTextByKey("P_IPT_SUPPLIEDINPUTISNOTAVALIDTIME", 'Supplied input is not a valid time.');
}
break;
case Question.types.Email:
var emails = a.Result.split(';');
for (var k = 0; k < emails.length; k++) {
if (!/^[0-9a-zA-Z][^@]*@[0-9a-zA-Z.]+$/.test(emails[k].trim())) {
msg = GetTextByKey("P_IPT_VALIDEMAILADDRESSFORMATREQUIRED", 'Valid email address format required.');
}
}
break;
case Question.types.EngingHours:
case Question.types.Number:
case Question.types.Odometer:
case Question.types.FuelUsed:
if (isNaN(Number(a.Result))) {
msg = GetTextByKey("P_IPT_SUPPLIEDINPUTISNOTAVALIDNUMBER", 'Supplied input is not a valid number.');
}
break;
case Question.types.FuelRemaining:
var d = Number(a.Result);
if (isNaN(d) || d < 0 || d > 100) {
msg = GetTextByKey("P_IPT_SUPPLIEDINPUTISNOTAVALIDPERCENTVALUE", 'Supplied input is not a valid percent value.');
}
break;
case Question.types.Integer:
if (isNaN(Number(a.Result)) || a.Result.indexOf('.') >= 0) {
msg = 'Supplied input is not a valid integer.';
}
break;
case Question.types.FuelRecords:
switch (q.SubType) {
case Question.fueltypes.Odometer:
case Question.fueltypes.Quantity:
if (isNaN(Number(a.Result))) {
msg = GetTextByKey("P_IPT_SUPPLIEDINPUTISNOTAVALIDNUMBER", 'Supplied input is not a valid number.');
}
break;
}
break;
}
}
if (flag || msg) {
showAlert(flag ? GetTextByKey("P_IPT_THEQUESTIONISREQUIRED", 'The question is required.') : (msg || GetTextByKey("P_IPT_QUESTIONRESULTISINVALID", 'Question result is invalid.')), alerttitle, null, function () {
if (typeof question.focus === 'function') {
question.focus();
}
});
_this.loading && _this.loading.fadeOut(100);
return;
}
//if (q.QuestionType === Question.types.AssetStatus) {
// a.Result = a.Comment;
// a.Comment = "";
//}
answers.push(a);
}
}
_this.report.Answers = answers;
_this.report.Medias = [];
console.log(answers);
var p = JSON.stringify(_this.report);
inspectionrequest('UpdateInspectionReport', htmlencode(p), function (result) {
if (typeof result === 'string') {
showAlert(GetTextByKey("P_IPT_PAGEERROR1", 'An unknown error occurred. ') + result, alerttitle);
_this.loading && _this.loading.fadeOut(100);
return;
} else if (!result) {
showAlert(GetTextByKey("P_IPT_FAILEDTOUPDATEINSPECTION", 'Failed to update inspection.'), alerttitle);
_this.loading && _this.loading.fadeOut(100);
return;
} else {
_this.changed = true;
if (exit) {
showRightPopup(false);
if (typeof _this.instance === 'function') {
showAlert(GetTextByKey("P_IPT_UPDATEINSPECTIONSUCCESSFULLY", 'Update inspection successfully.'), alerttitle, null, function () {
_this.instance();
});
return;
} else {
_this.instance.refresh();
}
}
}
showAlert(GetTextByKey("P_IPT_UPDATEINSPECTIONSUCCESSFULLY", 'Update inspection successfully.'), alerttitle);
_this.loading && _this.loading.fadeOut(100);
}, function () {
showAlert(GetTextByKey("P_IPT_PAGEERROR", 'An unknown error occurred. Please refresh page.'), alerttitle);
_this.loading && _this.loading.fadeOut(100);
});
}
__proto.createContent = function () {
var _this = this;
var content = $('
');
this.content = content;
var funcs = $('');
var btn = $('' + GetTextByKey("P_IPT_SAVE", "Save") + '').click(function () {
onSave(_this);
});
funcs.append(btn);
btn = $('' + GetTextByKey("P_IPT_SAVE1", "Save and Exit") + '').click(function () {
onSave(_this, true);
});
funcs.append(btn);
btn = $('' + GetTextByKey("P_IPT_SAVE2", "Exit Without Saving") + '').click(function () {
showRightPopup(false);
if (_this.changed) {
if (typeof _this.instance === 'function') {
_this.instance();
} else {
_this.instance.refresh();
}
}
});
funcs.append(btn);
content.append(funcs);
// committed information
var info = $('');
var templatename = $('');
info.append(templatename);
var committedby = $('');
info.append(committedby);
content.append(info);
// asset info
var assetpanel = $('').hide();
content.append(assetpanel);
var ul = $('');
content.append(ul);
//var tabIndex = 0;
//for (var i = 0; i < this.pages.length; i++) {
// var p = this.pages[i];
// addTab(p, false);
//}
var p = JSON.stringify([teamintelligence, this.reportId]);
inspectionrequest('GetInspectionReportForEdit', htmlencode(p), function (data) {
console.log(data);
if (typeof data === 'string') {
showAlert(data + GetTextByKey("P_IPT_REFRESHPAGE", ' Please refresh page.'), GetTextByKey("P_IPT_EDITINSPECTION", 'Edit Inspection'));
return;
}
_this.report = data;
//if (data.AssetList) {
// window.AssetList = data.AssetList;
//}
if (data.EmailList) {
window.EmailList = data.EmailList;
}
if (data.JobSiteList) {
window.JobSiteList = data.JobSiteList;
}
templatename.text(data.Template.Name);
committedby.text(data.CommitTimeLocalStr + ' by ' + data.CommitedByUserName);
if (data.Target === 0 && data.Asset) {
var asset = data.Asset;
var tb = $('');
var tr = $('
');
var td = $('' + GetTextByKey("P_IPT_ASSETNAME_COLON", "Asset Name:") + ' | ');
td.append($('').text(asset.DisplayName));
tr.append(td);
td = $('' + GetTextByKey("P_IPT_ASSETNAME2_COLON", "Asset Name(Custom):") + ' | ');
td.append($('').text(asset.Name2 || asset.Name));
tr.append(td);
tb.append(tr);
tr = $('
');
td = $('' + GetTextByKey("P_IPT_VINSN_COLON", "VIN/SN:") + ' | ');
td.append($('').text(asset.VIN));
tr.append(td);
td = $('' + GetTextByKey("P_IPT_MAKE_COLON", "Make:") + ' | ');
td.append($('').text(asset.MakeName));
tr.append(td);
td = $('' + GetTextByKey("P_IPT_MODEL_COLON", "Model:") + ' | ');
td.append($('').text(asset.ModelName));
tr.append(td);
td = $('' + GetTextByKey("P_IPT_ASSETTYPE_COLON", "Asset Type:") + ' | ');
td.append($('').text(asset.TypeName));
tr.append(td);
tb.append(tr);
if (data.ForWorkOrder) {
tr = $('
');
td = $('' + GetTextByKey("P_IPT_WORKORDER_COLON", "Work Order:") + ' | ');
var selwo = $('');
selwo.change(function () {
_this.report.WorkOrderId = selwo.find("option:selected").val();
});
td.append(selwo);//WorkOrderId
tr.append(td);
td = $(' | ');
var chkcv = $('');
chkcv.change(function () {
_this.report.VisibleToCustomer = chkcv.prop("checked");
});
chkcv.prop("checked", _this.report.VisibleToCustomer);
td.append(chkcv);//VisibleToCustomer
tr.append(td);
tb.append(tr);
selwo.append($("").val(-1).text(""));
inspectionrequest('GetAssetWorkOrders', data.AssetId, function (list) {
if (typeof list === 'string') {
} else if (list && list.length > 0) {
var txt = "";
for (var i = 0; i < list.length; i++) {
var wo = list[i];
txt = wo.Id;
if (wo.CustomerName && $.trim(wo.CustomerName) != "")
txt += " - " + wo.CustomerName;
selwo.append($("").val(wo.Id).text(txt));
}
}
selwo.val(data.WorkOrderId);
});
}
assetpanel.empty().show().append(tb);
}
// pages
var hasAssets = false;
for (var i = 0; i < data.Template.Pages.length; i++) {
var page = data.Template.Pages[i];
var name = 'page_' + i;
var li = $('').attr('data-href', name);
if (i == 0) {
li.addClass('selected');
}
ul.append(li);
li.append($(''));
li.append($('').text(page.DisplayText));
// content
var p = $('').attr('data-page', name);
var change = (function (panel) {
return function (e) {
var v = e.data.getAnswer();
var left = parseFloat(v && v.Result);
if (isNaN(left)) {
return;
}
var items = panel.find('.question-item');
var total;
var rightObj;
var right = e.data.question.SubType === Question.fueltypes.Quantity
? Question.fueltypes.UnitCost : Question.fueltypes.Quantity;
for (var n = 0; n < items.length; n++) {
var q = $(items[n]).data('question');
if (q.question.QuestionType === Question.types.FuelRecords) {
if (total == null && q.question.SubType === Question.fueltypes.TotalCost) {
total = q;
if (rightObj != null) {
break;
}
} else if (rightObj == null && q.question.SubType === right) {
rightObj = q;
if (total != null) {
break;
}
}
}
}
if (total != null && rightObj != null) {
var a = rightObj.getAnswer();
var val = parseFloat(a && a.Result);
if (!isNaN(val)) {
total.fillResult((left * val).toFixed(2));
}
}
};
})(p);
for (var j = 0; j < page.Sections.length; j++) {
var section = new Section(page.Sections[j], data);
if (!hasAssets && section.section.Questions.filter(function (q) {
return q.QuestionType === Question.types.DropDown && (
q.LookupSource === Question.sources.Assets ||
q.LookupSource === Question.sources.AssetCustomerName);
}).length > 0) {
hasAssets = true;
inspectionrequest('GetAssetBasicInfoForEdit', '', function (list) {
if (typeof list === 'string') {
console.log(list);
} else if (list && list.length > 0) {
window.AssetList = list;
var pages = _this.content.children('div[data-page]');
for (var i = 0; i < pages.length; i++) {
var qs = $(pages[i]).find('.question-item');
for (var j = 0; j < qs.length; j++) {
var question = $(qs[j]).data('question');
var q = question.question;
if (q.QuestionType === Question.types.DropDown && (
q.LookupSource === Question.sources.Assets ||
q.LookupSource === Question.sources.AssetCustomerName)) {
question.setLoaded && question.setLoaded();
}
}
}
console.log(GetTextByKey("P_IPT_LOADEDASSETLIST", 'loaded asset list: ') + list.length);
} else {
console.log(GetTextByKey("P_IPT_FAILEDTODOWNLOADASSETLIST", 'failed to download asset list.'));
}
});
}
p.append(section.createContent(change));
}
content.append(p);
var items = p.find('.question-item');
for (var n = 0; n < items.length; n++) {
var q = $(items[n]).data('question');
if (q.question.QuestionType === Question.types.FuelRecords
&& (q.question.SubType === Question.fueltypes.Quantity ||
q.question.SubType === Question.fueltypes.UnitCost)) {
change({ data: q });
break;
}
}
}
ul.append($(''));
content.tab();
_this.loading && _this.loading.fadeOut(100);
});
var loading = $('');
_this.loading = loading;
content.append(loading);
return content;
};
return {
EditInspection: edit
}
});