DetectionTemplatePreview.vue 10.3 KB
<template>
    <div>
        <div class="pageBox_title">
            检测模板预览
            <a-button style="float: right" @click="handleBack()"
                >返回列表</a-button
            >
        </div>

        <div class="InformationBox">
            <a-card class="tabs-card" :bordered="false">
                <a-descriptions :column="3" size="small" layout="vertical">
                    <template #title>模板基本信息</template>
                    <a-descriptions-item label="模板名称">
                        {{ pageData.templateName || "-" }}
                    </a-descriptions-item>
                    <a-descriptions-item label="适配车型">
                        {{ templateTypeText }}
                    </a-descriptions-item>
                    <a-descriptions-item label="备注说明">
                        {{ pageData.description || "-" }}
                    </a-descriptions-item>
                </a-descriptions>
            </a-card>
        </div>

        <div class="optionBox" v-if="nodeList.length">
            <div
                v-for="item in nodeList"
                @click="handleNodeList(item)"
                :key="item.id"
                :class="{ optionBut_active: item.id == activeNodeId }"
                class="optionBut"
            >
                {{ item.nodeName }}
            </div>
        </div>

        <div class="customizeFormBox">
            <a-form layout="vertical">
                <div v-for="group in activeColumns" :key="group.id">
                    <title-divider :title="group.nodeName"></title-divider>
                    <a-row>
                        <a-col
                            v-for="item in group.childrenNodes || []"
                            :key="item.id"
                            :span="8"
                        >
                            <a-form-item :label="item.nodeName" class="calc_24">
                                <div class="preview-text-line">
                                    类型:{{
                                        interactionTypeText(
                                            item.interactionType,
                                        )
                                    }}
                                </div>
                                <div
                                    v-if="hasOptions(item)"
                                    class="preview-text-line preview-options-line"
                                >
                                    <span class="preview-options-label"
                                        >可选项:</span
                                    >
                                    <a-popover
                                        trigger="hover"
                                        placement="topLeft"
                                        :overlay-class-name="'preview-options-popover'"
                                    >
                                        <template #content>
                                            <div class="options-tag-list-full">
                                                <a-tag
                                                    v-for="(
                                                        opt, i
                                                    ) in parseOptions(
                                                        item.options,
                                                    )"
                                                    :key="i"
                                                    class="options-tag"
                                                    >{{ opt }}</a-tag
                                                >
                                            </div>
                                        </template>
                                        <div class="options-tag-list-inline">
                                            <a-tag
                                                v-for="(opt, i) in parseOptions(
                                                    item.options,
                                                )"
                                                :key="i"
                                                class="options-tag"
                                                >{{ opt }}</a-tag
                                            >
                                        </div>
                                    </a-popover>
                                </div>
                            </a-form-item>
                        </a-col>
                    </a-row>
                </div>
            </a-form>
        </div>
    </div>
</template>

<script lang="ts">
import { Component, Vue } from "vue-facing-decorator";
import TitleDivider from "@/components/TitleDivider.vue";
import { DetectionController } from "@/api/DetectionController";

@Component({
    components: { TitleDivider },
})
export default class DetectionTemplatePreview extends Vue {
    pageData: any = {};
    nodeList: any[] = [];
    activeNodeId: number | string = null;

    created(): void {
        this.getDetailData();
    }

    getDetailData() {
        const routeData = this.$route.query || {};
        if (!routeData.id) return;
        DetectionController.inspectionTemplateDetail(this.axios, {
            id: routeData.id,
        }).then((response) => {
            const data = response?.data?.data || {};
            this.pageData = data;
            const roots =
                data.nodes ||
                data.rootNodes ||
                data.nodeList ||
                data.childrenNodes ||
                [];
            this.nodeList = roots;
            this.activeNodeId = roots[0]?.id ?? null;
        });
    }

    handleNodeList(item: any) {
        this.activeNodeId = item.id;
    }

    handleBack() {
        this.$router.back();
    }

    interactionTypeMap: Record<string, string> = {
        text_input: "文字输入",
        textarea_input: "长文本输入",
        number_input: "数字输入",
        date_picker: "日期选择",
        dropdown: "下拉",
        cascade: "级联选项",
        button_select: "按钮选择(可多选)",
        radio_button_select: "单选按钮",
        image_upload: "图片上传",
        video_upload: "视频上传",
        image_upload_and_button_select: "图片和按钮选择",
    };

    interactionTypeText(type: string): string {
        if (!type) return "-";
        return this.interactionTypeMap[type] || type;
    }

    hasOptions(item: any): boolean {
        return this.parseOptions(item?.options).length > 0;
    }

    parseOptions(raw: any): string[] {
        if (raw == null) return [];
        if (Array.isArray(raw)) return raw.map((x) => String(x));
        if (typeof raw === "string") {
            const trimmed = raw.trim();
            if (trimmed === "" || trimmed === "[]") return [];
            try {
                const parsed = JSON.parse(trimmed);
                if (Array.isArray(parsed)) return parsed.map((x) => String(x));
                return [String(parsed)];
            } catch (e) {
                return [trimmed];
            }
        }
        return [];
    }

    get templateTypeText(): string {
        const map: Record<number, string> = {
            1: "燃油车",
            2: "新能源车",
        };
        const t = Number(this.pageData?.templateType);
        return map[t] || "-";
    }

    /**
     * 当前选中根节点下的分区数据
     * - 如果子节点本身还有 childrenNodes(三层结构),原样返回作为分区
     * - 如果子节点已经是叶子(两层结构),就用当前根节点本身包一层作为分区
     */
    get activeColumns(): any[] {
        const active = this.nodeList.find((n) => n.id == this.activeNodeId);
        if (!active) return [];
        const children = active.childrenNodes || [];
        if (children.length === 0) return [];
        const firstHasChildren =
            Array.isArray(children[0]?.childrenNodes) &&
            children[0].childrenNodes.length > 0;
        if (firstHasChildren) {
            return children;
        }
        return [
            {
                id: active.id,
                nodeName: active.nodeName,
                childrenNodes: children,
            },
        ];
    }
}
</script>

<style scoped lang="less">
.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;
}

.InformationBox {
    width: 100%;
    padding: 0 24px;
    float: left;
    border-radius: 8px;
    background: linear-gradient(
        56.9deg,
        #e6f6ff 0%,
        #f0f6ff 28.999999999999996%,
        #f0f6ff 60%,
        #e3f1ff 100%
    );
}

.tabs-card {
    background: linear-gradient(
        56.9deg,
        #e6f6ff 0%,
        #f0f6ff 28.999999999999996%,
        #f0f6ff 60%,
        #e3f1ff 100%
    );
}

.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 {
    width: 100%;
    float: left;
}

.calc_24 {
    width: -webkit-calc(100% - 24px);
    width: -moz-calc(100% - 24px);
    width: calc(100% - 24px);
    position: relative;
}

.calc_24 :deep(.ant-form-item-label > label) {
    font-weight: 700;
}

.preview-text-line {
    color: #2f3133;
    font-size: 14px;
    line-height: 22px;
    word-break: break-all;
}

.preview-options-line {
    display: flex;
    align-items: center;
    overflow: hidden;
}

.preview-options-label {
    flex-shrink: 0;
    white-space: nowrap;
}

.options-tag-list-inline {
    flex: 1;
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    cursor: pointer;
}

.options-tag-list-inline :deep(.options-tag) {
    margin-right: 4px;
    margin-bottom: 0;
}

.options-tag-list-full {
    max-width: 360px;
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
}

.options-tag-list-full :deep(.options-tag) {
    margin: 0;
}
</style>