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 qso = $(qs[j]); var question = qso.data('question'); var section = qso.data('section'); 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 && checkConditional(_this.report.Template, q.Conditional, answers) && checkConditional(_this.report.Template, section.Conditional, answers)) { 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 && (q.QuestionType !== Question.types.FuelRecords || q.SubType !== Question.fueltypes.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); }); } function checkConditional(template, cond, answers) { if (!cond) return true; var qid = cond.QuestionId.toLowerCase(); if (qid == "and") { for (let scond of cond.SubItems) { var r = checkConditional(template, scond, answers); if (!r) return false; } return true; } else if (qid == "or") { for (let scond of cond.SubItems) { var r = checkConditional(template, scond, answers); if (r) return true; } return false; } else { var condquestion = getQuestionAndSection(template, cond.QuestionId); if (!condquestion) return false; if (!condquestion.Question || !checkConditional(template, condquestion.Question.Conditional, answers)) return false; if (!condquestion.Section || !checkConditional(template, condquestion.Section.Conditional, answers)) return false; if (!cond.Value) return cond.Operator == "noteq" ? true : false; var ans = answers.find(c => c.QuestionId.toLowerCase() == cond.QuestionId.toLowerCase()); if (ans == null) return false; var condValue = Number(cond.Value); switch (cond.Operator) { case "gt"://> return ans.AsDouble > condValue; case "gteq"://>= return ans.AsDouble >= condValue; case "lt"://< return ans.AsDouble < condValue; case "lteq"://<= return ans.AsDouble <= condValue; case "between"://between var condValue1 = Number(cond.Value1); return ans.AsDouble >= condValue && ans.AsDouble <= condValue1; case "in"://in if (!ans.SelectedItems || ans.SelectedItems.Count == 0) return false; var condvs = cond.Value.toLowerCase().split(';'); for (let item of ans.SelectedItems) { var sv = item.Value; if (condquestion.Question.QuestionType == Question.types.AssetStatus || condquestion.Question.QuestionType == Question.types.YesOrNo) sv = item.Text || ans.SelectedText; if (!sv || condquestion.Question.LookupSource != 0) sv = item.Text; if (condvs.indexOf(sv.toLowerCase()) >= 0) return true; } return false; case "noteq": var v = ""; if (condquestion.Question.QuestionType == 5// QuestionTypes.YesOrNo || condquestion.Question.QuestionType == 8//QuestionTypes.DropDown || condquestion.Question.QuestionType == 9//QuestionTypes.List || condquestion.Question.QuestionType == 19) {//QuestionTypes.AssetStatus) if (!ans.SelectedItems || ans.SelectedItems.length == 0) return false; if (ans.SelectedItems.length > 1) return true; v = ans.SelectedItems[0].Value; if (!v || condquestion.Question.QuestionType == 19 || condquestion.Question.QuestionType == 5 || condquestion.Question.LookupSource != 0) v = ans.SelectedItems[0].Text; } else v = ans.Result; return cond.Value.toLowerCase() != v.toLowerCase() && ans.AsDouble != condValue; default: //= var v1 = ""; if (condquestion.Question.QuestionType == 5// QuestionTypes.YesOrNo || condquestion.Question.QuestionType == 8//QuestionTypes.DropDown || condquestion.Question.QuestionType == 9//QuestionTypes.List || condquestion.Question.QuestionType == 19) {//QuestionTypes.AssetStatus) if (!ans.SelectedItems || ans.SelectedItems.length != 1) return false; v1 = ans.SelectedItems[0].Value; if (!v1 || condquestion.Question.QuestionType == 19 || condquestion.Question.QuestionType == 5 || condquestion.Question.LookupSource != 0) v1 = ans.SelectedItems[0].Text; } else v1 = ans.Result; return cond.Value.toLowerCase() == v1.toLowerCase() || ans.AsDouble == condValue; } } } function getQuestionAndSection(template, questionid) { for (var p of template.Pages) { for (var s of p.Sections) { for (var q of s.Questions) { if (q.Id.toLowerCase() == questionid.toLowerCase()) return { Question: q, Section: s }; } } } return null; } __proto.createContent = function () { var _this = this; var content = $('
'); this.content = content; var funcs = $(''); var btn = $(' ').click(function () { onSave(_this); }); funcs.append(btn); btn = $(' ').click(function () { onSave(_this, true); }); funcs.append(btn); btn = $(' ').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 = $('