ui-lib/lib/element/addworkorder.js
2024-07-10 12:19:52 +08:00

734 lines
38 KiB
JavaScript

import { createElement, Dropdown, Popup, showAlert, createIcon, DateSelector, showConfirm, Grid, OptionBase } from "../ui";
import { nullOrEmpty } from "../utility";
import AssetSelector from "./assetSelector";
const iconWorkOrder = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAE0AAABJCAYAAAB4mKumAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAABMISURBVHhe1VxpVBRnuubMn3vOTSbJJDHJzUxMxmhM1Dgzd5KZXHPumWgco8YFcAFxxzUGo7ihqCxuKO4at7jvkShqxF1cUBAQwr4rO7KD7Ivge9/nowq6m+qmqmnMzXPOc47prvq66ql3/4pY0W8EtfX11GfaLOo5bjKNcHWjpXv2k1/gfUpIz6DS8grpqOeD34xofkHB1NnWnt742lbw1UHD6YV+A+n9UQ40epknbTzpQ/dj4qimrk46o+PwmxFt/tYd9OrAYfRfw0Y28+3hI+nNoSPola+G0otfDqIeYyfTSFd38vG/RQ2NDdKZlsdvQrTC0ic0YO5CFkdfNEN2+tqG/sDHvMVC9pnuRFeCQ6UVLIsOES2vuISuBodYzFWuhTygLqPG0htDbBXFMiREe7H/YBrrsZoqqqulVSwHi4v2ICGRhrssFRe/8uBh6dP2wfvYSfrPfoNaiWOKL345mLacOi2tYFlYTLSGhkbac+4CvT/Sgf7AsacTW8UbQ2xo86mfpCPMQ3FZuXgICPxK4igRbtpj7CQKiIySVrEsLCJaeVUVOXOg7jTYWlywfPFwpz8OHyXEbBPPiEorKig15zFFP0ylkLgECuZseOTyNbHWW9KaaojE8PWCxVRVUyMtblm0W7SCklKyd1vRKrPJfINFfHeEPZ26cVM6owVPGxooIjlFiLpg+y4audSdvnCaS3+dNJW6jh5Hfxo+ml7nB4EYpbS2EiFup8E2tIzruI5Cu0RDwB/GrvPSv4co3oBM3HhXrqeuh4aJ80rKy2nnmXM0yNmFerIbvTn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G/gDuY5Gh4smAzdzv+hJ8y4++lf80Yal4h5E2/tI3atX7cVzE01GLWe2R5XVtOdRluhhccNIGqJZNxBCLSE83iy/nqftjUZz8dxF0wXmZ7cLSmhdQrr4W1PcODKgGP9oiH+IZ9h/bWv8ZCn8qqLJQAWUW1MrXj9F1oWIc8KThHjir5rZ9UQWltzZ0KVhqUu5K3mmOGC3PP5fiGYIvFOSW1MnWrQgFvJkeh5tS86iFbGp5MwWOeNBgngtDOMkiIaRUE51x/x/hVqD6P8AXY8jcrVVNpAAAAAASUVORK5CYII=';
/**
* @private
* @param {HTMLElement} element
* @param {boolean?} display
* @returns {HTMLElement}
*/
function displayElement(element, display) {
element.style.display = display === false ? 'none' : '';
return element;
}
/**
* @private
* @param {HTMLInputElement} element
* @param {string} pattern
* @returns {HTMLInputElement}
*/
function patternValidation(element, pattern) {
element.pattern = pattern;
return element;
}
/**
* @private
* @param {"mile" | "mi" | "m" | "kilometre" | "km" | "kilometres"} uom
* @returns {"Mile" | "Kilometre"}
*/
function getOdometerUnit(uom) {
if (nullOrEmpty(uom)) {
return 'Mile';
}
switch (uom.toLowerCase()) {
case 'mile':
case 'mi':
case 'm':
return 'Mile';
case 'kilometre':
case 'km':
case 'kilometres':
return 'Kilometre';
default:
return 'Mile';
}
}
export default class AddWorkOrder extends OptionBase {
_var = {
/**
* @private
* @type {HTMLElement}
*/
container: null,
/**
* @private
* @type {Popup}
*/
assetSelectorPopup: null,
/**
* @private
* @property {number} Id
* @property {string} VIN
* @property {string} DisplayName
* @property {boolean} OnRoad
* @property {boolean} Hide
* @property {number} [EngineHours]
* @property {number} [Odometer]
* @property {string} [OdometerUOM]
*/
asset: null,
/**
* @private
* @property {string} Id
* @property {string} Name
* @property {string} Code
*/
customer: null,
contacts: [],
followers: [],
params: {
Locations: [],
Departments: [],
Advisors: [],
OrderTypes: [],
Statuses: [],
Jobsites: [],
Codes: []
},
el: {
/**
* @private
* @type {HTMLTextAreaElement}
*/
textComplaint: null,
/**
* @private
* @type {Dropdown}
*/
dropWorkOrderType: null,
/**
* @private
* @type {Dropdown}
*/
dropStatus: null,
/**
* @private
* @type {DateSelector}
*/
dateCompleted: null,
/**
* @private
* @type {HTMLInputElement}
*/
inputHours: null,
/**
* @private
* @type {HTMLInputElement}
*/
inputOdometer: null,
/**
* @private
* @type {Dropdown}
*/
dropOdometerUnit: null,
/**
* @private
* @type {Dropdown}
*/
dropAssignedTo: null,
/**
* @private
* @type {Dropdown}
*/
dropAdvisor: null,
/**
* @private
* @type {Dropdown}
*/
dropLocation: null,
/**
* @private
* @type {Dropdown}
*/
dropDepartment: null
}
};
onAssetAdded;
onAssetSelected;
onSave;
static IconWorkOrder = iconWorkOrder;
constructor(opt = {
allowCustomer: false,
assetFullcontrol: false,
/**
* @private
* @type {(assetId: number) => Promise<{Id: number, VIN: string, DisplayName: string, OnRoad: boolean, Hide: boolean, EngineHours?: number, Odometer?: number, OdometerUOM?: string}>}
*/
requestAssetInfo: null,
/**
* @private
* @type {() => Promise<{Statuses: any[], Jobsites: any[], Codes: any[], Advisors: any[], Locations: any[], Departments: any[], OrderTypes: any[], AssetId: null}>}
*/
requestWorkOrderParams: null,
/**
* @private
* @type {(hidden: boolean, search?: string, groups?: string[], jobsites?: number[], codes?: string[]) => Promise<any[]>}
*/
requestAssets: null,
/**
* @private
* @type {() => Promise<any>}
*/
requestAddAsset: null,
/**
* @private
* @type {() => Promise<{Customer: any, Contacts: any[], Followers: any[]}>}
*/
requestCustomers: null,
/**
* @private
* @type {(assetId: number, locationId: number, departmentId: number) => Promise<any[]>}
*/
requestAssignedTo: null,
/**
* @private
* @type {(assetId: number) => Promise}
*/
requestHideAsset: null,
/**
* @private
* @type {(assetId: number) => Promise<any[]>}
*/
requestOpenedWorkorder: null,
zIndex: 999
}) {
super(opt);
}
async getItem() {
const title = this.r('P_WO_OPENWORKORDER', 'Open Work Order');
const el = this._var.el;
let status = el.dropStatus.selected;
if (status != null) {
status = {
Status: status.Id,
StatusType: status.StatusType,
StatusName: status.Name,
StatusAutoText: status.AutoText,
StatusMessage: status.Message
};
} else {
status = {
Status: -1
};
}
let machine = this._var.asset;
if (machine == null) {
showAlert(title, this.r('P_WO_ASSETNOTEMPTY', 'Asset cannot be empty.')).then(() => this._var.container.querySelector('.wo-asset>svg')?.focus());
return null;
}
const item = {
Id: -1,
Description: el.textComplaint.value,
WorkOrderType: el.dropWorkOrderType.selected?.Key ?? '',
CompleteDate: el.dateCompleted.element.value,
// MeterType:
HourMeter: el.inputHours.value || -1,
Odometer: el.inputOdometer.value || -1,
OdometerUnits: el.dropOdometerUnit.selected?.value ?? 'Mile',
AssignedTo: el.dropAssignedTo.selected?.IID ?? '',
AdvisorId: el.dropAdvisor.selected?.Key ?? '',
LocationId: el.dropLocation.selected?.ID || -1,
DepartmentId: el.dropDepartment.selected?.Id || -1,
AssetID: machine.Id,
VIN: machine.VIN,
AssetName: machine.DisplayName,
WorkOrderTotalCost: -1,
HoursToComplete: -1,
OtherCost: -1,
PartsCost: -1,
TravelTimeCost: -1,
LaborCost: -1,
HourlyRate: -1,
InspectionTemplateId: -1,
...status
};
item.CustomerId = this._var.customer?.Id ?? -1;
item.Contacts = this._var.contacts ?? [];
item.Followers = this._var.followers ?? [];
if (nullOrEmpty(el.textComplaint?.value)) {
showAlert(title, this.r('P_WO_COMPLAINTREQUIRED', 'Complaint is required.')).then(() => el.textComplaint.focus());
return null;
}
if (el.dropStatus.selected?.Completed) {
if (!el.dateCompleted.element.validity.valid) {
showAlert(title, this.r('P_WO_COMPLETEDDATECANNOTBEEMPTY', 'Completed Date cannot be empty.')).then(() => el.dateCompleted.element.focus());
return null;
}
if (machine.OnRoad) {
item.MeterType = 'Odometer';
if (nullOrEmpty(item.Odometer) || isNaN(item.Odometer) || item.Odometer < 0) {
showAlert(title, this.r('P_WO_ODOMETERFORMATERROR', 'Odometer format error.')).then(() => el.inputOdometer.focus());
return null;
}
} else {
item.MeterType = 'HourMeter';
if (nullOrEmpty(item.HourMeter) || isNaN(item.HourMeter) || item.HourMeter < 0) {
showAlert(title, this.r('P_WO_HOURMETERFORMATERROR', 'Hour Meter format error.')).then(() => el.inputHours.focus());
return null;
}
}
}
return item;
}
async show() {
const option = this._option;
const allowCustomer = option.allowCustomer === true;
const title = this.r('P_WO_OPENWORKORDER', 'Open Work Order');
const tabIndex = Math.max.apply(null, [...document.querySelectorAll('[tabindex]')].map(e => e.tabIndex ?? 0)) + 3;
const textComplaint = createElement('textarea', textarea => {
textarea.tabIndex = tabIndex + 2;
textarea.className = 'ui-text wo-complaint';
textarea.placeholder = this.r('P_WO_ENTERCOMPLAINT', 'Enter Complaint');
});
const baseOption = {
search: true,
textKey: 'Value',
valueKey: 'Key'
};
const dropWorkOrderType = new Dropdown({
tabIndex: tabIndex + 3,
input: true,
...baseOption
});
const dropStatus = new Dropdown({
tabIndex: tabIndex + 5,
htmlTemplate: it => createElement('span', 'wo-color-line',
createElement('em', em => em.style.backgroundColor = it.Color),
createElement('label', label => label.innerText = it.Name)
),
search: true,
textKey: 'Name',
valueKey: 'Id'
});
const dateCompleted = new DateSelector({
tabIndex: tabIndex + 6,
className: 'wo-status-closed'
});
const inputHours = createElement('input', input => {
input.type = 'text';
input.tabIndex = tabIndex + 7;
input.className = 'ui-input wo-hours input-hours';
});
const inputOdometer = createElement('input', input => {
input.type = 'text';
input.tabIndex = tabIndex + 8;
input.className = 'ui-input wo-odometer input-odometer';
});
const dropOdometerUnit = new Dropdown({
tabIndex: tabIndex + 9,
selected: 'Mile'
});
dropOdometerUnit.source = [
{ value: 'Mile', text: GetTextByKey('P_WO_MILE', 'Mile') },
{ value: 'Kilometre', text: GetTextByKey('P_WO_KILOMETER', 'Kilometer') }
]
const dropAssignedTo = new Dropdown({
tabIndex: tabIndex + 10,
selected: '',
search: true,
valueKey: 'IID',
textKey: 'DisplayName'
});
dropAssignedTo.source = [{ IID: '', DisplayName: '' }];
const dropAdvisor = new Dropdown({
tabIndex: tabIndex + 11,
selected: '',
...baseOption
});
const dropLocation = new Dropdown({
tabIndex: tabIndex + 12,
selected: -1,
search: true,
valueKey: 'ID',
textKey: 'Name'
});
const dropDepartment = new Dropdown({
tabIndex: tabIndex + 13,
selected: -1,
search: true,
valueKey: 'Id',
textKey: 'Name'
});
// save variables
this._var.el = {
textComplaint,
dropWorkOrderType,
dropStatus,
dateCompleted,
inputHours,
inputOdometer,
dropOdometerUnit,
dropAssignedTo,
dropAdvisor,
dropLocation,
dropDepartment
}
const container = createElement('div', 'open-wo-container',
createElement('div', 'open-wo-header',
createElement('img', img => img.src = iconWorkOrder),
createElement('h3', h3 => h3.innerText = title)
),
createElement('div', 'open-wo-content',
createElement('div', 'wo-combined wo-asset',
createElement('span', span => {
span.className = 'wo-title wo-title-required';
span.innerText = this.r('P_WO_ASSET_COLON', 'Asset:');
}),
createIcon('fa-light', 'search', svg => {
svg.tabIndex = tabIndex + 1;
svg.addEventListener('click', async () => {
let popup = this._var.assetSelectorPopup;
if (popup == null) {
const selector = new AssetSelector({
assetFullcontrol: option.assetFullcontrol,
loading: flag => this._var.assetSelectorPopup.loading = flag,
close: () => this._var.assetSelectorPopup.close(),
requestAssets: option.requestAssets,
requestAddAsset: option.requestAddAsset,
dataSource: this._var.params
});
selector.onSelected = async it => {
const el = this._var.el;
if (typeof option.requestAssetInfo === 'function') {
const a = await option.requestAssetInfo(it.Id);
if (a != null) {
it = a;
if (it.OnRoad) {
el.inputHours.value = '';
el.inputOdometer.value = it.Odometer;
el.dropOdometerUnit.select(getOdometerUnit(it.OdometerUOM));
} else {
el.inputHours.value = it.EngineHours;
el.inputOdometer.value = '';
el.dropOdometerUnit.select('Mile');
}
}
}
this._var.asset = it;
if (typeof this.onAssetSelected === 'function') {
this.onAssetSelected(it);
}
this._var.container.querySelector('.wo-asset-name').innerText = it.DisplayName;
// get assigned to
if (typeof option.requestAssignedTo === 'function') {
const data = await option.requestAssignedTo(it.Id, el.dropLocation.selected?.ID ?? -1, el.dropDepartment.selected?.Id ?? -1)
el.dropAssignedTo.source = [{ IID: '', DisplayName: '' }, ...data];
el.dropStatus.onSelected(el.dropStatus.selected);
}
};
popup = new Popup({
title: this.r('P_MA_SELECTASSET', 'Select Asset'),
content: selector.create(),
persistent: true,
buttons: [
{
key: 'ok',
text: this.r('P_GRID_OK', 'OK'),
trigger: () => selector.select()
},
{ text: this.r('P_WO_CANCEL', 'Cancel') }
]
});
this._var.assetSelectorPopup = popup;
popup.create();
popup.rect = { width: 1220 };
const mask = await popup.show();
mask.style.zIndex = option.zIndex ?? 999;
await selector.init();
} else {
await popup.show();
}
});
})
),
createElement('span', 'wo-asset-name'),
createElement('div', 'wo-line wo-combined',
createElement('span', span => {
span.className = 'wo-title wo-title-required';
span.innerText = this.r('P_WO_COMPLAINTCOLON', 'Complaint:');
})
),
createElement('div', div => {
div.className = 'wo-line wo-combined wo-sub-line';
div.style.marginBottom = '2px';
div.style.width = 'calc(100% - 10px)';
},
textComplaint
),
createElement('span', span => {
span.className = 'wo-title';
span.innerText = this.r('P_WO_WORKORDERTYPE_COLON', 'Work Order Type:');
}),
dropWorkOrderType.create(),
createElement('div', div => {
div.className = 'wo-combined wo-customer-record';
if (!allowCustomer) {
displayElement(div, false);
}
},
createElement('span', span => {
span.className = 'wo-title';
span.innerText = this.r('P_WO_COMPANYNAME_COLON', 'Company Name:');
}),
createIcon('fa-light', 'search', svg => {
svg.tabIndex = tabIndex + 4;
svg.addEventListener('click', async () => {
if (typeof option.requestCustomers === 'function') {
popup.loading = true;
const data = await option.requestCustomers();
popup.loading = false;
if (data != null) {
this._var.customer = data.Customer;
this._var.contacts = data.Contacts;
this._var.followers = data.Followers;
let name = data.Customer.Name;
if (!nullOrEmpty(data.Customer.Code)) {
name += ' / ' + data.Customer.Code;
}
this._var.container.querySelector('.wo-company-name').innerText = name;
}
}
});
})
),
displayElement(createElement('span', 'wo-company-name'), allowCustomer),
createElement('span', span => {
span.className = 'wo-title';
span.innerText = this.r('P_WO_STATUS_COLON', 'Status:');
}),
dropStatus.create(),
createElement('span', span => {
span.className = 'wo-title wo-title-required wo-sub-line wo-status-closed';
span.innerText = this.r('P_WO_COMPLETEDDATE_COLON', 'Completed Date:');
displayElement(span, false);
}),
displayElement(dateCompleted.create(), false),
createElement('span', span => {
span.className = 'wo-title wo-title-required wo-sub-line wo-hours';
span.innerText = this.r('P_WO_HOURS_COLON', 'Hours:');
displayElement(span, false);
}),
displayElement(
patternValidation(inputHours, '\\d+\\.?\\d*'),
false
),
createElement('span', span => {
span.className = 'wo-title wo-title-required wo-sub-line wo-odometer';
span.innerText = this.r('P_WO_ODOMETER_COLON', 'Odometer:');
displayElement(span, false);
}),
createElement('div', div => {
div.className = 'wo-combined wo-odometer';
displayElement(div, false);
},
patternValidation(inputOdometer, '\\d+\\.?\\d*'),
dropOdometerUnit.create()
),
createElement('span', span => {
span.className = 'wo-title';
span.innerText = this.r('P_WO_ASSIGNEDTO_COLON', 'Assigned Tech:');
}),
dropAssignedTo.create(),
createElement('span', span => {
span.className = 'wo-title wo-customer-record';
span.innerText = this.r('P_WO_ADVISOR_COLON', 'Advisor:');
if (!allowCustomer) {
displayElement(span, false);
}
}),
displayElement(dropAdvisor.create(), allowCustomer),
createElement('span', span => {
span.className = 'wo-title wo-customer-record';
span.innerText = this.r('P_WO_LOCATION_COLON', 'Location:');
if (!allowCustomer) {
displayElement(span, false);
}
}),
displayElement(dropLocation.create(), allowCustomer),
createElement('span', span => {
span.className = 'wo-title wo-customer-record';
span.innerText = this.r('P_WO_DEPARTMENT_COLON', 'Department:');
if (!allowCustomer) {
displayElement(span, false);
}
}),
displayElement(dropDepartment.create(), allowCustomer)
)
);
dropStatus.onSelected = item => {
if (item == null) {
return;
}
container.querySelectorAll('.wo-status-closed').forEach(it => it.style.display = item.Completed ? '' : 'none');
if (item.Completed) {
const onRoad = this._var.asset?.OnRoad;
container.querySelectorAll('.wo-hours').forEach(it => it.style.display = onRoad ? 'none' : '');
container.querySelectorAll('.wo-odometer').forEach(it => it.style.display = onRoad ? '' : 'none');
} else {
container.querySelectorAll('.wo-hours').forEach(it => it.style.display = 'none');
container.querySelectorAll('.wo-odometer').forEach(it => it.style.display = 'none');
}
};
dropLocation.onSelected = async loc => {
// get assigned to
const asset = this._var.asset;
if (asset != null && typeof option.requestAssignedTo === 'function') {
const data = await option.requestAssignedTo(asset.Id, loc?.ID ?? -1, this._var.el.dropDepartment.selected?.Id ?? -1)
this._var.el.dropAssignedTo.source = [{ IID: '', DisplayName: '' }, ...data];
}
};
dropDepartment.onSelected = async dep => {
// get assigned to
const asset = this._var.asset;
if (asset != null && typeof option.requestAssignedTo === 'function') {
const data = await option.requestAssignedTo(asset.Id, this._var.el.dropLocation.selected?.ID ?? -1, dep?.Id ?? -1)
this._var.el.dropAssignedTo.source = [{ IID: '', DisplayName: '' }, ...data];
}
}
dateCompleted.value = new Date();
this._var.container = container;
const popup = new Popup({
title,
content: container,
buttons: [
{
key: 'open',
text: title,
trigger: async () => {
popup.loading = true;
try {
const item = await this.getItem();
if (item == null) {
return false;
}
if (this._var.asset.Hide) {
if (!option.assetFullcontrol) {
await showAlert(title, this.r('P_WO_HIDDENCANNOTCREATE', 'The selected asset is hidden and a work order cannot be created.') + '\n\n' + this.r('P_WO_CONTACTTOUNHIDE', 'Please contact your Fleet Manager to Unhide the asset if you require a work order.'));
return false;
}
const next = await showConfirm(title, this.r('P_WO_HIDDENCANNOTCREATE', 'The selected asset is hidden and a work order cannot be created.') + '\n\n' + this.r('P_WO_PROMPTUNHIDE', 'Do you wish to "Un-Hide" the asset?'), [
{ key: 'unhide', text: this.r('P_WO_UNHIDE', 'Unhide') },
{ key: 'cancel', text: this.r('P_WO_CANCELWO', 'Cancel Work Order') }
]);
if (next.result !== 'unhide') {
return false;
}
// hide
if (typeof option.requestHideAsset === 'function') {
await option.requestHideAsset(this._var.asset.Id);
this._var.asset.Hide = false;
}
}
if (typeof option.requestOpenedWorkorder === 'function') {
const wos = await option.requestOpenedWorkorder(this._var.asset.Id);
if (wos == null) {
return false;
}
if (wos.length > 0) {
const next = await new Promise(resolve => {
const popWorkorders = new Popup({
title,
content: createElement('div', 'wo-opened-workorder',
createElement('header', header => header.innerText = this.r('P_WO_ASSETOPENEDWORKORDER', 'The selected asset has the following open work orders:')),
createElement('div', 'wo-grid-opened')
),
resolve,
buttons: [
{ key: 'create', text: this.r('P_WO_CREATEWO', 'Create Work Order'), trigger: () => resolve('create') },
{ key: 'cancel', text: this.r('P_WO_CANCELWO', 'Cancel Work Order'), trigger: () => resolve('cancel') }
]
});
popWorkorders.show().then(mask => {
const grid = new Grid(mask.querySelector('.wo-grid-opened'), this.r);
grid.columns = [
{ key: 'WorkOrderNumber', caption: 'WO #', width: 100 },
{ key: 'CreateDateStr', caption: this.r('P_WO_CREATEDDATE', 'Created Date'), width: 120 },
{ key: 'Description', caption: this.r('P_WO_COMPLAINT', 'Complaint'), width: 360 }
];
grid.init();
grid.source = wos.map(w => ({
WorkOrderNumber: w.WorkOrderNumber,
CreateDateStr: { DisplayValue: w.CreateDateStr, Value: w.CreateDate },
Description: w.Description
}));
// default focus
const button = mask.querySelector('.ui-popup-container .ui-popup-footer .ui-popup-button:last-child');
button?.focus();
});
});
if (next !== 'create') {
return false;
}
}
}
if (typeof this.onSave === 'function') {
this.onSave(item, this._var.el.dropStatus.selected);
}
} finally {
popup.loading = false;
}
}
},
{ text: this.r('P_WO_CANCEL', 'Cancel') }
]
});
popup.create();
popup.rect = { width: 600 };
const mask = await popup.show();
mask.style.zIndex = option.zIndex ?? 999;
if (typeof option.requestWorkOrderParams === 'function') {
popup.loading = true;
const data = await option.requestWorkOrderParams()
if (!isNaN(data.AssetId) && data.AssetId > 0 && typeof option.requestAssetInfo === 'function') {
const it = await option.requestAssetInfo(data.AssetId);
if (it != null) {
if (it.OnRoad) {
inputOdometer.value = it.Odometer;
dropOdometerUnit.select(getOdometerUnit(it.OdometerUOM));
} else {
inputHours.value = it.EngineHours;
}
this._var.asset = it;
// if (typeof this.onAssetSelected === 'function') {
// this.onAssetSelected(it);
// }
this._var.container.querySelector('.wo-asset-name').innerText = it.DisplayName;
// get assigned to
if (typeof option.requestAssignedTo === 'function') {
const data = await option.requestAssignedTo(it.Id, dropLocation.selected?.ID ?? -1, dropDepartment.selected?.Id ?? -1)
dropAssignedTo.source = [{ IID: '', DisplayName: '' }, ...data];
// dropStatus.onSelected(dropStatus.selected);
}
}
}
popup.loading = false;
dropWorkOrderType.source = data.OrderTypes;
dropAdvisor.source = [{ Key: '', Value: '' }, ...data.Advisors];
dropLocation.source = [{ ID: -1, Name: '' }, ...data.Locations];
dropDepartment.source = [{ Id: -1, Name: '' }, ...data.Departments];
this._var.params = data;
dropStatus.source = data.Statuses;
const defaultStatus = data.Statuses.find(s => s.DefaultOnOpen);
if (defaultStatus != null) {
dropStatus.select(defaultStatus.Id);
}
}
textComplaint.focus();
return mask;
}
}