DetectionOrderEdit.vue 11.3 KB
<template>
    <div class="detection-order-edit-page">
        <div class="detection-order-edit__header">
            <div class="pageBox_title">
                {{ pageTitle }}
                <a-button style="float: right" @click="handleBack()"
                    >返回列表</a-button
                >
            </div>
            <InformationEdit
                ref="informationEditRef"
                :pageData="pageData.inspectionWORespVO || {}"
            />
            <div class="optionBox">
                <div
                    v-for="item in nodeList"
                    @click="handleNodeList(item)"
                    :key="item.id"
                    :class="{ optionBut_active: item.id == activeNodeId }"
                    class="optionBut"
                >
                    {{ item.nodeName }}
                    <CheckCircleFilled
                        v-if="item.completedFlag == '1'"
                        class="circleFilled"
                    />
                </div>
            </div>
        </div>
        <div ref="customizeFormBoxRef" class="customizeFormBox">
            <CustomizeForm
                v-if="activeNodeId != -1"
                ref="customizeFormRef"
                :readonly="readonly"
                @submitSuccess="onFormSubmitSuccess"
            />
            <TestingScore
                v-if="activeNodeId == -1"
                ref="testingScoreRef"
                :readonly="readonly"
                @submitSuccess="onFormSubmitSuccess"
            />
        </div>
    </div>
</template>

<script lang="ts">
import { Component } from "vue-facing-decorator";
import { PaginationComponent } from "@/components/PaginationComponent";
import { Vehicle } from "lib/src/entity/vehicle/entity/Vehicle";
import InformationEdit from "@/views/detection/order/components/InformationEdit.vue";
import CustomizeForm from "@/views/detection/order/components/CustomizeForm.vue";
import TestingScore from "@/views/detection/order/components/TestingScore.vue";
import { DetectionController } from "@/api/DetectionController";
import { CheckCircleFilled } from "@ant-design/icons-vue";
import { TrackHelper } from "@/common/TrackHelper";
import { Modal } from "ant-design-vue";
import {
    applyResolvedInitialValuesToLeafNodes,
    extractScalarFromDefaultData,
} from "@/views/detection/order/utils/inspectInitialValues";

@Component({
    components: {
        InformationEdit,
        CustomizeForm,
        CheckCircleFilled,
        TestingScore,
    },
})
export default class DetectionOrderEdit extends PaginationComponent<Vehicle> {
    pageData: any = {};
    defaultData: any = {};

    nodeList: any = [];
    activeNodeId: number = null;
    /** 本次编辑会话内已成功保存过的一级类目 */
    savedNodeIds = new Set<number>();

    carTypeList: any = [];
    inspectionNodeList: any = [];

    get readonly(): boolean {
        return !!(this.$route.meta as { readonly?: boolean })?.readonly;
    }

    get pageTitle(): string {
        return this.readonly ? "查看检测项" : "编辑检测项";
    }

    created(): void {
        if (!this.readonly) {
            TrackHelper.markEditStart();
        }
        const q = this.$route.query || {};
        TrackHelper.setEditContext({
            workOrderId: q.id,
            reportId: q.id,
        });
        this.getDetailData();
        this.getDefaultTemplate().finally(() => {
            this.getTemplateData("1");
        });
    }

    unmounted(): void {
        if (!this.readonly) {
            TrackHelper.clearEditStart();
            TrackHelper.clearEditContext();
        }
    }

    getDetailData() {
        const routeData = this.$route.query || {};
        let postData = { workOrderId: routeData.id };
        DetectionController.inspectOrderDetail(this.axios, postData).then(
            (response) => {
                this.pageData = response.data.data || {};
                const wo = this.pageData?.inspectionWORespVO || {};
                TrackHelper.setEditContext({
                    workOrderId: routeData.id,
                    reportId: routeData.id,
                    vehicleId:
                        wo.vehicleId ||
                        wo.vehicle?.id ||
                        this.pageData?.vehicle?.id ||
                        "",
                });
            },
        );
    }

    getTemplateData(status) {
        const routeData = this.$route.query || {};
        DetectionController.inspectionTemplate(this.axios, {
            id: routeData.id,
            status,
        }).then((response) => {
            this.nodeList = (response?.data?.data || []).map((item) => ({
                ...item,
                nodeName: item.id == -1 ? "检测评分" : item.nodeName,
            }));
            if (status == "1") {
                this.activeNodeId = this.nodeList[0].id || null;
                this.getInspectionNodeData(this.activeNodeId);
            }
        });
    }

    getDefaultTemplate() {
        const routeData = this.$route.query || {};
        return DetectionController.inspectionTemplateDefault(this.axios, {
            taskId: routeData.id,
        }).then((response) => {
            this.defaultData = response?.data?.data || {};
            const csId = extractScalarFromDefaultData(this.defaultData, "carSeriesId");
            return this.getCarTypeList(csId);
        });
    }

    getCarTypeList(id) {
        if (!id) {
            this.carTypeList = [];
            return Promise.resolve();
        }
        return DetectionController.getCarTypeList(this.axios, {
            csId: id,
        }).then((response) => {
            const data = response?.data?.data || {};
            this.carTypeList = (data.carinfos || []).map((item) => ({
                text: item.name || "",
                value: String(item.id) || "",
            }));
        });
    }

    getInspectionNodeData(taskId) {
        const routeData = this.$route.query || {};
        DetectionController.inspectionNode(this.axios, {
            id: taskId,
            taskId: routeData.id,
        }).then((response) => {
            const nodeList = response?.data?.data || [];
            const taskIdMap = {
                48: "车型信息",
                60: "车身主图",
                551: "工况视频",
            };
            if (taskIdMap[taskId]) {
                this.inspectionNodeList = [
                    {
                        id: taskId,
                        nodeName: taskIdMap[taskId],
                        childrenNodes: nodeList,
                    },
                ];
            } else {
                this.inspectionNodeList = nodeList;
            }
            this.inspectionNodeList = this.inspectionNodeList.map((item) => ({
                ...item,
                childrenNodes: this.applyLeafDefaults(item.childrenNodes || []),
            }));
            this.$refs.customizeFormRef.init({
                customizeFormList: this.inspectionNodeList,
                carTypeList: this.carTypeList,
                activeNodeId: this.activeNodeId,
                taskId: routeData.id,
            });
            this.scrollCustomizeFormToTop();
        });
    }

    scrollCustomizeFormToTop() {
        this.$nextTick(() => {
            const el = this.$refs.customizeFormBoxRef as
                | HTMLElement
                | undefined;
            if (el) {
                el.scrollTop = 0;
            }
        });
    }

    applyLeafDefaults(list: any[] = []) {
        const withDefaults = applyResolvedInitialValuesToLeafNodes(
            list,
            this.defaultData || {},
            this.carTypeList || [],
        );
        return this.mapNodeResultStatus(withDefaults);
    }

    mapNodeResultStatus(nodes: any[] = []): any[] {
        return (nodes || []).map((item) => {
            const mapped = {
                ...item,
                resultStatus:
                    item?.inspectionResultVO?.resultStatus == 3 ? true : false,
            };
            if (mapped.childrenNodes?.length) {
                mapped.childrenNodes = this.mapNodeResultStatus(
                    mapped.childrenNodes,
                );
            }
            return mapped;
        });
    }

    onFormSubmitSuccess() {
        if (this.activeNodeId != null) {
            this.savedNodeIds.add(Number(this.activeNodeId));
        }
        this.getTemplateData("0");
    }

    getActiveFormRef(): any {
        if (Number(this.activeNodeId) === -1) {
            return this.$refs.testingScoreRef;
        }
        return this.$refs.customizeFormRef;
    }

    hasUnsavedChanges(): boolean {
        const form = this.getActiveFormRef();
        return !!form?.hasUnsavedChanges?.();
    }

    isCurrentTabSaved(): boolean {
        const nodeId = Number(this.activeNodeId);
        if (this.savedNodeIds.has(nodeId)) {
            return true;
        }
        const node = (this.nodeList || []).find((n) => Number(n.id) === nodeId);
        return node?.completedFlag == "1";
    }

    needsConfirmLeave(): boolean {
        if (this.isCurrentTabSaved() && !this.hasUnsavedChanges()) {
            return false;
        }
        return this.hasUnsavedChanges() || !this.isCurrentTabSaved();
    }

    handleNodeList(item: any) {
        if (item.id == this.activeNodeId) return;
        if (this.readonly) {
            this.switchNode(item);
            return;
        }
        if (!this.needsConfirmLeave()) {
            this.switchNode(item);
            return;
        }
        Modal.confirm({
            title: "提示",
            content:
                "检测到当前页面有未保存的内容,强制离开将丢失数据。建议先点击保存,再切换 Tab 或离开页面",
            okText: "强制离开",
            cancelText: "继续填写",
            onOk: () => {
                this.switchNode(item);
            },
        });
    }

    switchNode(item: any) {
        this.activeNodeId = item.id;
        this.scrollCustomizeFormToTop();
        if (Number(item.id) > 0) {
            this.getInspectionNodeData(this.activeNodeId);
        }
    }

    handleBack() {
        this.$router.back();
    }
}
</script>

<style scoped lang="less">
.detection-order-edit-page {
    display: flex;
    flex: 1;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
}

.detection-order-edit__header {
    flex-shrink: 0;
    overflow: hidden;
}

.pageBox_title {
    width: 100%;
    height: 24px;
    float: left;
    color: #202020;
    font-size: 16px;
    font-weight: 700;
    line-height: 24px;
    margin-top: 16px;
    margin-bottom: 16px;
}

.optionBox {
    width: 100%;
    float: left;
    margin-top: 24px;
    margin-bottom: 4px;
}

.optionBut {
    min-width: 76px;
    padding: 0 24px;
    height: 36px;
    float: left;
    line-height: 36px;
    border-radius: 6px;
    background: #f7f8f9;
    color: #565d6b;
    font-size: 14px;
    font-weight: 400;
    text-align: center;
    margin-right: 12px;
    margin-bottom: 12px;
    cursor: pointer;
}

.optionBut_active {
    background: #e8f0ff !important;
    color: #1956fe !important;
}

.customizeFormBox {
    flex: 1;
    min-height: 0;
    width: 100%;
    overflow-x: hidden;
    overflow-y: auto;
}

.circleFilled {
    font-size: 12px;
    color: #1956fe;
    padding-left: 2px;
}
</style>

<style lang="less">
.inner-main > .main:has(.detection-order-edit-page) {
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
</style>