<template>
    <div class="container">
        <!-- 顶部信息 -->
        <div class="containertop">
            <div class="topleft">
                <img src="../../../assets/images/detailsicon/1.png" alt="">
                <span>基本信息</span>
            </div>
            <div class="topright">
                <el-button type="primary" size="mini" plain style="border: none;background-color: rgba(43,98,241,0.1);color: #2B62F1;" @click="edit">
                    <img src="../../../assets/images/detailsicon/icon-bj@2x.png" alt="编辑" style="width: 0.6rem; height: 0.6rem; margin-right: 4px;">
                    编辑
                </el-button> 
                <el-button type="primary" size="mini" plain style="border: none;background-color: rgba(43,98,241,0.1);color: #2B62F1;" @click="exportData">
                    <img src="../../../assets/images/detailsicon/icon-dc@2x.png" alt="编辑" style="width: 0.6rem; height: 0.6rem; margin-right: 4px;">
                    导出
                </el-button> 
            </div>
        </div>
        <!-- 内容区 -->
        <div class="content">
            <div class="picturediv"></div>
            <div class="descriptionsdiv">
                <el-descriptions class="margin-top" :column="3" border>
                    <el-descriptions-item 
                    v-for="(item, index) in descriptions" 
                    :key="index"
                    :span="item.label === '项目法人单位简介' ? 3 : 1">
                        <template slot="label">
                            {{ item.label }}
                        </template>
                        {{ item.value }}
                    </el-descriptions-item>
                </el-descriptions>
            </div>
        </div>
        <!-- 添加或编辑项目对话框 -->
        <el-dialog :title="title" :visible.sync="dialogVisible" width="500px" append-to-body>
            <el-form :model="form" label-width="120px">
                <el-form-item label="项目名称">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="项目法人单位">
                    <el-input v-model="form.xmfrdwxz"></el-input>
                </el-form-item>
                <el-form-item label="项目法人单位性质">
                    <el-input v-model="form.nature"></el-input>
                </el-form-item>
                <el-form-item label="施工单位">
                    <el-input v-model="form.sgdw"></el-input>
                </el-form-item>
                <el-form-item label="设计单位">
                    <el-input v-model="form.sjdw"></el-input>
                </el-form-item>
                <el-form-item label="总投资额（万元）">
                    <el-input v-model="form.ztze"></el-input>
                </el-form-item>
                <el-form-item label="所属功能区">
                    <el-input v-model="form.ssgnq"></el-input>
                </el-form-item>
                <el-form-item label="建设起止时间">
                    <el-input v-model="form.begainTime"></el-input>
                </el-form-item>
                <el-form-item label="现状分类">
                    <el-input v-model="form.xzfl"></el-input>
                </el-form-item>
                <el-form-item label="建设地点">
                    <el-input v-model="form.jsdd"></el-input>
                </el-form-item>
                <el-form-item label="重点发展产业">
                    <el-input v-model="form.prioritize"></el-input>
                </el-form-item>
                <el-form-item label="建设模式">
                    <el-input v-model="form.jsms"></el-input>
                </el-form-item>
                <el-form-item label="项目标签">
                    <el-input v-model="form.label"></el-input>
                </el-form-item>
                <el-form-item label="项目负责人">
                    <el-input v-model="form.projectLeader"></el-input>
                </el-form-item>
                <el-form-item label="联系方式">
                    <el-input v-model="form.phone"></el-input>
                </el-form-item>
                <el-form-item label="施工许可证发放时间">
                    <el-input v-model="form.issuingTime"></el-input>
                </el-form-item>
                <el-form-item label="竣工验收时间">
                    <el-input v-model="form.acceptanceTime"></el-input>
                </el-form-item>
                <el-form-item label="项目法人单位简介">
                    <el-input v-model="form.unitIntroduction" type="textarea"></el-input>
                </el-form-item>
                <el-form-item label="项目简介">
                    <el-input v-model="form.introduction" type="textarea"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveForm">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { getBasicInformationById, updateBasicInformation } from '@/api/ManageApi/index';

export default {
    props: {
        size: {
            type: String,
            default: 'mini'
        },
        id: {
            type: Number,
            required: true
        }
    },
    data() {
        return {
            descriptions: [],
            dialogVisible: false,
            title: '编辑项目',
            form: {
                name: '',
                xmfrdwxz: '',
                nature: '',
                sgdw: '',
                sjdw: '',
                ztze: '',
                ssgnq: '',
                begainTime: '',
                xzfl: '',
                jsdd: '',
                prioritize: '',
                jsms: '',
                label: '',
                projectLeader: '',
                phone: '',
                issuingTime: '',
                acceptanceTime: '',
                unitIntroduction: '',
                introduction: ''
            }
        };
    },
    created() {
        this.fetchData();
    },
    methods: {
        fetchData() {
            getBasicInformationById(this.id)
                .then(response => {
                    const data = response.data;
                    this.descriptions = [
                        { label: '项目名称', value: data.name },
                        { label: '项目法人单位', value: data.xmfrdwxz },
                        { label: '项目法人单位性质', value: data.nature },
                        { label: '施工单位', value: data.sgdw },
                        { label: '设计单位', value: data.sjdw },
                        { label: '总投资额（万元）', value: data.ztze },
                        { label: '所属功能区', value: data.ssgnq },
                        { label: '建设起止时间', value: data.begainTime },
                        { label: '现状分类', value: data.xzfl },
                        { label: '建设地点', value: data.jsdd },
                        { label: '重点发展产业', value: data.prioritize },
                        { label: '建设模式', value: data.jsms },
                        { label: '项目标签', value: data.label },
                        { label: '项目负责人', value: data.projectLeader },
                        { label: '联系方式', value: data.phone },
                        { label: '施工许可证发放时间', value: data.issuingTime },
                        { label: '竣工验收时间', value: data.acceptanceTime },
                        { label: '项目法人单位简介', value: data.unitIntroduction },
                        { label: '项目简介', value: data.introduction }
                    ];
                    // 初始化表单数据
                    this.form = { ...data };
                })
                .catch(error => {
                    console.error('There was an error fetching the data!', error);
                });
        },
        edit() {
            this.dialogVisible = true;
        },
        exportData() {
            // 导出逻辑
        },
        saveForm() {
            updateBasicInformation(this.form)
                .then(response => {
                    console.log('Data saved successfully:', response.data);
                    this.$message.success('数据保存成功！');
                    this.dialogVisible = false;
                    // 刷新数据
                    this.fetchData();
                })
                .catch(error => {
                    console.error('There was an error saving the data!', error);
                    this.$message.error('数据保存失败，请重试！');
                });
        }
    }
};
</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: .7rem 0;
    padding: .5rem;
    border-bottom: 1px solid #E5E5E5;
}

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

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

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

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

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

.two-row-item {
    height: auto; 
}
</style>