<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="dialogVisible = true">
                    <img src="../../../assets/images/detailsicon/icon-xz@2x.png" alt="编辑"
                        style="width: 0.6rem; height: 0.6rem; margin-right: 4px;">
                    新增
                </el-button>
            </div>
        </div>
        <!-- 展示的区域 -->
        <div class="image-container">
            <div v-for="(image, index) in images" :key="index" class="image-box">
                <img :src="image.src" alt="项目图片" class="image">
                <div class="close-button" @click="removeImage(index)">
                    <img src="../../../assets/images/detailsicon/icon-关闭@2x.png" alt="">
                </div>
                <div class="bottombox">
                    <div class="bottomtext"><span>新科大厦项目正在施工</span></div>
                    <div class="bottomtime"><span>2025-02-11</span></div>
                </div>
            </div>
        </div>
        <!-- 弹窗 -->
        <el-dialog title="上传附件" :visible.sync="dialogVisible" width="50%">
            <el-form :model="form" label-width="100px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="项目名称">
                            <el-input v-model="form.xmmc" placeholder="请输入项目名称"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="年度">
                            <el-input v-model="form.year" placeholder="请输入年度"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-form-item label="网页地址">
                    <el-input v-model="form.webUrl" placeholder="请输入网页地址"></el-input>
                </el-form-item>
                <el-form-item label="时间">
                    <el-date-picker v-model="form.sj" type="datetime" placeholder="选择日期时间" style="width: 51rem;"
                        value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
                </el-form-item>
                <el-form-item label="图片上传">
                    <el-upload action="https://jsonplaceholder.typicode.com/posts/" list-type="picture-card"
                        :on-preview="handlePictureCardPreview" :on-remove="handleRemove" :before-upload="beforeUpload">
                        <i class="el-icon-plus"></i>
                    </el-upload>
                    <el-dialog :visible.sync="dialogImageUrlVisible">
                        <img width="100%" :src="dialogImageUrl" alt="">
                    </el-dialog>
                </el-form-item>
                <el-form-item label="说明">
                    <el-input v-model="form.content" type="textarea" :rows="4" placeholder="请输入说明"></el-input>
                </el-form-item>
                <el-form-item label="大事记">
                    <el-input v-model="form.bigEvent" type="textarea" :rows="4" placeholder="请输入大事记"></el-input>
                </el-form-item>
                <el-form-item label="新闻事件">
                    <el-input v-model="form.newsEvent" type="textarea" :rows="4" placeholder="请输入新闻事件"></el-input>
                </el-form-item>
                <el-form-item label="附件上传">
                    <el-upload action="https://jsonplaceholder.typicode.com/posts/" :on-preview="handleFilePreview"
                        :on-remove="handleRemove" :before-upload="beforeUpload">
                        <el-button size="small" type="primary">点击上传</el-button>
                    </el-upload>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="handleAddXmxl">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { addXmxl, getXmxlPage } from '@/api/ManageApi/index.js';

export default {
    data() {
        return {
            images: [
                { src: 'https://picsum.photos/300/200?random=1', },
                { src: 'https://picsum.photos/300/200?random=2', },
                { src: 'https://picsum.photos/300/200?random=3', },
            ],
            dialogVisible: false,
            previewDialogVisible: false,
            previewImageUrl: '',
            fileList: [],
            form: {
                xmmc: '',
                year: '',
                webUrl: '',
                sj: '',
                content: '',
                bigEvent: '',
                newsEvent: '',
                fj: '',
                img: '',
                createBy: '',
                createId: 0,
                createTime: '',
                id: 0,
                updateBy: '',
                updateId: 0,
                updateTime: '',
                xmId: 0 // 确保 xmId 存在
            }
        };
    },
    created() {
        const xmid = this.$route.query.xmid; // 假设 xmid 通过路由参数传递
        this.form.xmId = xmid; // 设置 xmId
        this.fetchImages(xmid);
    },
    methods: {
        async fetchImages(xmid) {
            try {
                const response = await getXmxlPage({ xmid });
                if (response.data && response.data.list) {
                    this.images = response.data.list.map(item => ({
                        src: item.fj
                    }));
                }
            } catch (error) {
                // this.$message.error('获取图片失败');
                console.error(error);
            }
        },
        handleAdd() {
            this.dialogVisible = true;
        },
        removeImage(index) {
            if (confirm('确定要删除吗？')) {
                this.images.splice(index, 1);
            }
        },
        handleRemove(file, fileList) {
            this.fileList = fileList;
        },
        handlePictureCardPreview(file) {
            this.previewImageUrl = file.url;
            this.previewDialogVisible = true;
        },
        handleSuccess(response, file, fileList) {
            this.fileList = fileList;
            this.images.push({ src: URL.createObjectURL(file.raw) });
        },
        beforeUpload(file) {
            const isJPG = file.type === 'image/jpeg';
            const isPNG = file.type === 'image/png';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG && !isPNG) {
                this.$message.error('上传图片只能是 JPG 或 PNG 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传图片大小不能超过 2MB!');
            }
            return (isJPG || isPNG) && isLt2M;
        },
        submitUpload() {
            this.$refs.upload.submit();
            this.dialogVisible = false;
            this.handleAddXmxl();
        },
        async handleAddXmxl() {
            // 收集表单数据
            const requestData = {
                bigEvent: this.form.bigEvent,
                content: this.form.content,
                createBy: this.form.createBy,
                createId: this.form.createId,
                createTime: this.form.createTime,
                fj: this.form.fj,
                id: this.form.id,
                img: this.form.img,
                newsEvent: this.form.newsEvent,
                sj: this.form.sj,
                updateBy: this.form.updateBy,
                updateId: this.form.updateId,
                updateTime: this.form.updateTime,
                webUrl: this.form.webUrl,
                xmId: this.form.xmId,
                xmmc: this.form.xmmc,
                year: this.form.year
            };

            try {
                const response = await addXmxl(requestData);
                this.$message.success('新增成功');
                console.log(response);
                this.fetchImages(); // 刷新图片列表
            } catch (error) {
                this.$message.error('新增失败');
                console.error(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;
    padding: 1rem;
    gap: 1rem;
    overflow: auto;
}

.containertop {
    height: auto;
    display: flex;
    justify-content: space-between;
    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;
}

.image-container {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
}

.image-box {
    position: relative;
    width: 21.75rem;
    height: 12.44rem;
    background-color: #f0f0f0;
    border-radius: 0.5rem;
    overflow: hidden;
}

.image {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.close-button {
    width: 1.25rem;
    height: 1.25rem;
    position: absolute;
    top: 0.2rem;
    right: 0.2rem;
}

.close-button img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.bottombox {
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: absolute;
    bottom: .5rem;
    padding: .2rem;
    width: 97%;
}

.bottomtext {
    font-family: AlibabaPuHuiTi, AlibabaPuHuiTi;
    font-weight: 500;
    font-size: 1.13rem;
    color: #FFFFFF;
    line-height: 1.56rem;
    text-align: left;
    font-style: normal;
    text-transform: none;
}

.bottomtime {
    font-family: AlibabaPuHuiTi, AlibabaPuHuiTi;
    font-weight: 400;
    font-size: 0.75rem;
    color: rgba(255, 255, 255, 0.8);
    line-height: 1rem;
    text-align: left;
    font-style: normal;
    text-transform: none;
}
</style>