<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="medium" 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="medium" plain
                    style="border: none;background-color: rgba(43,98,241,0.1);color: #2B62F1;" @click="handleExport">
                    <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">
                <!-- 图片展示 -->
                <img v-if="basicInfo.fj" :src="basicInfo.fj" alt="项目代表性照片">
            </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="70rem" append-to-body>
            <el-form :model="form" :rules="rules" ref="form" label-width="180px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="项目名称" prop="name">
                            <el-input v-model="form.name" maxlength="50"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="建设地点" prop="jsdd">
                            <el-row :gutter="10">
                                <el-col :span="20">
                                    <el-input v-model="form.jsdd" maxlength="50"></el-input>
                                </el-col>
                                <el-col :span="3">
                                    <span @click="showMap" style="color: #2B62F1;cursor: pointer;">落图</span>
                                </el-col>
                            </el-row>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="项目法人单位" prop="xmfrdwxz">
                            <el-input v-model="form.xmfrdwxz" maxlength="50"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="重点发展产业" prop="prioritize">
                            <el-input v-model="form.prioritize" maxlength="50"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="项目法人单位性质" prop="nature">
                            <el-select v-model="form.nature" placeholder="请选择" value-key="value"
                                style="width: 22.5rem;">
                                <el-option v-for="dict in dict.type.xmfrdwxz" :key="dict.value" :label="dict.label"
                                    :value="parseInt(dict.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="建设模式" prop="jsms">
                            <el-select v-model="form.jsms" placeholder="请选择" value-key="value" style="width: 22.5rem;">
                                <el-option v-for="dict in dict.type.jsms" :key="dict.value" :label="dict.label"
                                    :value="parseInt(dict.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="施工单位" prop="sgdw">
                            <el-input v-model="form.sgdw" maxlength="50"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="项目标签" prop="label">
                            <el-input v-model="form.label" maxlength="50"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="设计单位" prop="sjdw">
                            <el-input v-model="form.sjdw" maxlength="50"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="项目负责人" prop="projectLeader">
                            <el-input v-model="form.projectLeader" maxlength="50"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="总投资额（万元）" prop="ztze">
                            <el-input v-model.number="form.ztze" maxlength="50"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="联系方式" prop="phone">
                            <el-input v-model="form.phone" maxlength="50"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="所属功能区" prop="ssgnq">
                            <el-select v-model="form.ssgnq" placeholder="请选择" value-key="value" style="width: 22.5rem;">
                                <el-option v-for="dict in dict.type.ssgnq" :key="dict.value" :label="dict.label"
                                    :value="parseInt(dict.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="施工许可证发放时间" prop="issuingTime">
                            <el-input v-model="form.issuingTime" maxlength="50"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="建设起止时间" prop="begainTime">
                            <el-date-picker v-model="form.begainTime" type="daterange" range-separator="至"
                                start-placeholder="开始日期" end-placeholder="结束日期"
                                style="width: 22.5rem;"></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="竣工验收时间" prop="acceptanceTime">
                            <el-input v-model="form.acceptanceTime" maxlength="50"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="现状分类" prop="xzfl">
                            <el-select v-model="form.xzfl" placeholder="请选择" value-key="value" style="width: 22.5rem;">
                                <el-option v-for="dict in dict.type.xzfl" :key="dict.value" :label="dict.label"
                                    :value="parseInt(dict.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="建设进度" prop="jsjd">
                            <el-input v-model="form.jsjd" maxlength="50"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-form-item label="项目法人单位简介" prop="unitIntroduction">
                    <el-input v-model="form.unitIntroduction" type="textarea" rows="4" maxlength="1000"></el-input>
                </el-form-item>
                <el-form-item label="项目简介" prop="introduction">
                    <el-input v-model="form.introduction" type="textarea" rows="4" maxlength="1000"></el-input>
                </el-form-item>
                <el-form-item label="项目代表性照片">
                    <ImageUpload v-model="form.fj" />
                </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>
        <!-- 地图弹窗 -->
        <el-dialog :title="mapDialogTitle" :visible.sync="mapDialogVisible" width="1000px" height="1000px"
            style="height: 1000;" append-to-body>
            <luotu v-if="mapDialogVisible" />
        </el-dialog>
    </div>
</template>


<script>
import ImageUpload from '@/components/ImageUpload2/index.vue';
import luotu from '@/views/components/ProjectDetails/luotu.vue'

export default {
    components: {
        ImageUpload, luotu
    },
    dicts: [
        'ssgnq', 'xmfrdwxz', 'xzfl', 'jsms'
    ],
    props: {
        size: {
            type: String,
            default: ''
        },
        basicInfo: {
            type: Object,
            required: true
        }
    },
    data() {
        return {
            descriptions: [],
            dialogImageUrl: '',
            dialogVisibletwo: false,
            dialogVisible: false,
            mapDialogVisible: false,
            mapDialogTitle: '地图',
            title: '编辑基本信息',
            form: {
                name: '',
                xmfrdwxz: '',
                nature: 0,
                sgdw: '',
                sjdw: '',
                ztze: 0,
                ssgnq: 0,
                begainTime: '',
                endtime: '',
                xzfl: 0,
                jsdd: '',
                prioritize: '',
                jsms: 0,
                label: '',
                projectLeader: '',
                phone: '',
                issuingTime: '',
                acceptanceTime: '',
                unitIntroduction: '',
                introduction: '',
                fj: '',
                jsjd: '',
            },
            rules: {
            },
            // select映射
            ssgnqMap: {
                1: '高端制造与国际贸易区',
                2: '阳澄湖半岛旅游度假区',
                3: '金鸡湖商务区',
                4: '苏相合作区'
            },
            xmfrdwxzMap: {
                1: '国企',
                2: '外资企业',
                3: '民营企业',
                4: '其他'
            },
            jsmsMap: {
                1: '工地实施',
                2: '利用存量用地改扩建',
            },
            xzflMap: {
                1: '已建',
                2: '再建',
                3: '拟建',
            }
        };
    },
    watch: {
        basicInfo: {
            handler(newVal) {
                this.descriptions = this.formatDescriptions(newVal);
                this.form = {
                    ...newVal,
                    begainTime: [new Date(newVal.begainTime), new Date(newVal.endTime)],
                };
            },
            immediate: true,
            deep: true
        }
    },
    methods: {
        // 格式化描述信息
        formatDescriptions(data) {
            return [
                { label: '项目名称', value: data.name },
                { label: '项目法人单位', value: data.xmfrdwxz },
                { label: '项目法人单位性质', value: this.xmfrdwxzMap[data.nature] || '未知' },
                { label: '施工单位', value: data.sgdw },
                { label: '设计单位', value: data.sjdw },
                { label: '总投资额（万元）', value: data.ztze },
                { label: '所属功能区', value: this.ssgnqMap[data.ssgnq] || '未知' },
                { label: '建设起止时间', value: `${data.begainTime} 至 ${data.endTime}` },
                { label: '现状分类', value: this.xzflMap[data.xzfl] || '未知' },
                { label: '建设地点', value: data.jsdd },
                { label: '重点发展产业', value: data.prioritize },
                { label: '建设模式', value: this.jsmsMap[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.jsjd },
                { label: '项目法人单位简介', value: data.unitIntroduction },
                { label: '项目简介', value: data.introduction }
            ];
        },
        // 打开编辑对话框
        edit() {
            this.dialogVisible = true;
        },
        // 保存表单
        saveForm() {
            const formData = this.prepareFormData(this.form);
            // 触发事件，将数据传递给父组件
            this.$emit('update-data', formData);
            this.dialogVisible = false;
        },
        // 准备表单数据
        prepareFormData() {
            const [startDate, endDate] = this.formatDateRange(this.form.begainTime);
            return {
                id: this.id,
                name: this.form.name,
                xmfrdwxz: this.form.xmfrdwxz,
                nature: parseInt(this.form.nature),
                sgdw: this.form.sgdw,
                sjdw: this.form.sjdw,
                ztze: parseFloat(this.form.ztze),
                ssgnq: parseInt(this.form.ssgnq),
                begainTime: startDate,
                endTime: endDate,
                xzfl: parseInt(this.form.xzfl),
                jsdd: this.form.jsdd,
                prioritize: this.form.prioritize,
                jsms: parseInt(this.form.jsms),
                label: this.form.label,
                projectLeader: this.form.projectLeader,
                phone: this.form.phone,
                issuingTime: this.form.issuingTime,
                acceptanceTime: this.form.acceptanceTime,
                unitIntroduction: this.form.unitIntroduction,
                jsjd: this.form.jsjd,
                introduction: this.form.introduction,
                fj: this.form.fj,
                createBy: '',
                createId: 0,
                createTime: '',
                updateBy: '',
                updateId: 0,
                updateTime: '',
            };
        },
        // 格式化日期范围
        formatDateRange(dateRange) {
            if (!dateRange || dateRange.length !== 2) return ['', ''];
            const startDate = dateRange[0].toISOString().split('T')[0];
            const endDate = dateRange[1].toISOString().split('T')[0];
            return [startDate, endDate];
        },
        // 处理附件上传成功
        handleAttachmentSuccess(response, file) {
            this.form.fj = response.url;
        },
        // 上传前验证
        beforeAttachmentUpload(file) {
            const isImage = file.type.startsWith('image/');
            const isLt2M = file.size / 1024 / 1024 < 2;
            if (!isImage) this.$message.error('只能上传图片文件!');
            if (!isLt2M) this.$message.error('上传图片大小不能超过 2MB!');
            return isImage && isLt2M;
        },
        // 删除附件
        removeAttachment() {
            this.form.fj = '';
        },
        // 显示地图弹窗
        showMap() {
            this.mapDialogVisible = true;
        }
    }
};
</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;
    position: relative;
    overflow: hidden;
}

.picturediv img {
    width: 100%;
    height: auto;
    object-fit: cover;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
}

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

.two-row-item {
    height: auto;
}

.tanchuang {
    width: 70rem;
    height: auto;
    padding: 2rem;
}
</style>
