<template>
    <div class="container">
        <!-- 顶部信息 -->
        <div class="containertop">
            <div class="topleft">
                <img src="../../../assets/images/detailsicon/1.png" alt="">
                <span>五要素模型信息</span>
            </div>
            <div class="topright" v-if="action === 'fill' || !action || action === 'okay'">
                <el-button type="primary" size="medium" plain
                    style="border: none;background-color: rgba(43,98,241,0.1);color: #2B62F1;" @click="toggleEditAll">
                    <img src="../../../assets/images/detailsicon/icon-bj@2x.png" alt="编辑"
                        style="width: 0.6rem; height: 0.6rem; margin-right: 4px;">
                    {{ isEditingAll ? '保存' : '编辑' }}
                </el-button>
            </div>
        </div>
        <!-- 内容区 -->
        <div class="content">
            <div class="descriptionsdiv">
                <div>
                    <table class="custom-table">
                        <tr v-for="(row, rowIndex) in form" :key="rowIndex" style="border: 1px solid #E5E5E5;">
                            <td class="left-column">{{ row.ysmc }}</td>
                            <td class="right-columns" v-for="(item, itemIndex) in row.list" :key="itemIndex">
                                <div v-if="item.isEditing">
                                    <span class="label-color">{{ item.zdname || '暂无指标' }}：</span>
                                    <!-- 使用下拉框（el-select）渲染第一个数据集的所有字段，其他数据集使用输入框（el-input） -->
                                    <el-select v-if="rowIndex === 0" v-model="item.zdinfor" size="small"
                                        style="width: 12rem;">
                                        <el-option v-for="option in getOptionsForItem(rowIndex, itemIndex)"
                                            :key="option.value" :label="option.label" :value="option.value">
                                        </el-option>
                                    </el-select>
                                    <el-input v-else v-model="item.zdinfor" size="small" style="width: 12rem;"
                                        :disabled="item.zdname === '暂无指标'"
                                        :placeholder="item.zdname === '暂无指标' ? '暂无数据' : '请输入内容'">
                                    </el-input>
                                </div>
                                <div v-else>
                                    <span class="label-color">{{ item.zdname || '暂无指标' }}：</span> {{ item.zdinfor ||
                                    '无' }}
                                </div>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    name: "Models",
    props: {
        wysmxInfo: {
            type: Array,
            required: true,
        },
        action: {
            type: String,
            required: true
        }
    },
    data() {
        return {
            isEditingAll: false,
            form: [],
            optionsMap: {
                '0-0': [
                    { value: '一级', label: '一级' },
                    { value: '二级', label: '二级' },
                    { value: '三级', label: '三级' },
                    { value: '四级', label: '四级' },
                ],
                '0-1': [
                    { value: '甲', label: '甲' },
                    { value: '乙', label: '乙' },
                    { value: '丙', label: '丙' },
                    { value: '丁', label: '丁' },
                    { value: '戊', label: '戊' },
                ],
                '0-2': [
                    { value: '有', label: '有' },
                    { value: '无', label: '无' },
                ],
                // 添加更多选项数据
            }
        };
    },
    watch: {
        wysmxInfo: {
            immediate: true,
            handler(newVal) {
                // console.log('接收到的 wysmxInfo 数据:', newVal);
                this.form = newVal.map(item => ({
                    // id: item.id,
                    xmId: item.xmId,
                    ysmc: item.ysmc,
                    list: item.list.map(subItem => ({
                        id: subItem.id,
                        zdname: subItem.zdname || '暂无指标',
                        zdinfor: subItem.zdinfor || '',
                        isEditing: false
                    }))
                }));
            }
        }
    },
    methods: {
        toggleEditAll() {
            if (this.isEditingAll) {
                // 保存模式：过滤掉 isEditing 字段
                const cleanedForm = this.form.map(row => ({
                    ysmc: row.ysmc,
                    // id: row.id,
                    xmId: row.xmId,
                    list: row.list.map(item => ({
                        id: item.id,
                        zdname: item.zdname,
                        zdinfor: item.zdinfor,
                    }))
                }));
                this.$emit('updata-data', cleanedForm); // 传递过滤后的数据
            } else {
                // 编辑模式：开启所有字段的编辑状态
                this.form.forEach(row => {
                    row.list.forEach(item => {
                        item.isEditing = true;
                    });
                });
            }
            this.isEditingAll = !this.isEditingAll;
        },
        getOptionsForItem(rowIndex, itemIndex) {
            // 根据 rowIndex 和 itemIndex 获取对应的选项数据
            return this.optionsMap[`${rowIndex}-${itemIndex}`] || [];
        }
    },
    created() {
        // console.log('wysmxInfo 内容:', this.wysmxInfo); // 初始化时打印数据
    }
};
</script>

<style scoped>
.container {
    display: flex;
    flex-direction: column;
    width: 100%;
    background-color: #FFFFFF;
    box-shadow: 0rem 0.13rem 0.63rem 0rem rgba(177, 177, 177, 0.1);
    border-radius: 0.5rem 0.5rem 0.5rem 0.5rem;
}

.content {
    padding: 1rem;
    display: flex;
}

.containertop {
    height: auto;
    display: flex;
    justify-content: space-between;
    padding: .5rem;
    border-bottom: 1px solid #E5E5E5;
}

.topleft {
    height: 2rem;
    width: auto;
    display: flex;
    gap: 0.4rem;
    align-items: center;
}

.topleft img {
    width: 0.81rem;
    height: 0.81rem;
}

.topleft span {
    width: auto;
    height: 1rem;
    font-family: aliregular;
    font-weight: 500;
    font-size: 1rem;
    color: #3D424C;
    line-height: 1rem;
    text-align: right;
    font-style: normal;
    text-transform: none;
}

.picturediv {
    width: 18.31rem;
    height: 25.31rem;
    background-color: lightblue;
}

.descriptionsdiv {
    width: 100%;
    margin-left: 1rem;
    height: auto;
}

.two-row-item {
    height: 20rem;
}

.custom-table {
    width: 100%;
    border-collapse: collapse;
    border: 1px solid #E6EAF2;
}

.custom-table th,
.custom-table td {
    padding: 8px;
    text-align: left;
    border-top: 1px solid #E6EAF2;
    border-bottom: 1px solid #E6EAF2;
}

.left-column {
    border-right: 1px solid #E6EAF2;
    background-color: #F4F7FE;
}

.right-columns {
    border-right: none;
}

.label-color {
    color: #808080;
}
</style>