<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>
        </div>
        <!-- 上传附件弹窗 -->
        <el-dialog title="上传附件" :visible.sync="dialogVisible" width="50%">
            <el-upload
                ref="upload"
                action="/gysl/xmxl/add"
                list-type="picture-card"
                :on-preview="handlePictureCardPreview"
                :on-remove="handleRemove"
                :on-success="handleSuccess"
                :before-upload="beforeUpload"
                :auto-upload="false"
            >
                <i class="el-icon-plus"></i>
            </el-upload>
            <el-dialog :visible.sync="previewDialogVisible">
                <img width="100%" :src="previewImageUrl" alt="">
            </el-dialog>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="submitUpload">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

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

export default {
    data() {
        return {
            images: [],
            dialogVisible: false,
            previewDialogVisible: false,
            previewImageUrl: '',
            fileList: []
        };
    },
    created() {
        this.fetchImages();
    },
    methods: {
        async fetchImages() {
            try {
                const response = await getXmxlPage();
                if (response.data && response.data.list) {
                    this.images = response.data.list.map(item => ({
                        src: item.fj // 假设 fj 字段包含图片 URL
                    }));
                }
            } catch (error) {
                this.$message.error('获取图片失败');
                console.error(error);
            }
        },
        handleAdd() {
            this.dialogVisible = true;
        },
        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 formData = new FormData();
            this.fileList.forEach((file, index) => {
                formData.append(`fj[${index}]`, file.raw);
            });

            // 添加其他表单数据
            formData.append('createBy', '');
            formData.append('createId', 0);
            formData.append('createTime', '');
            formData.append('id', 0);
            formData.append('jkmc', '');
            formData.append('sj', '');
            formData.append('status', 0);
            formData.append('updateBy', '');
            formData.append('updateId', 0);
            formData.append('updateTime', '');

            try {
                const response = await addXmxl(formData);
                this.$message.success('新增成功');
                console.log(response);
                this.fetchImages(); // 刷新图片列表
            } catch (error) {
                this.$message.error('新增失败');
                console.error(error);
            }
        },
        removeImage(index) {
            this.images.splice(index, 1);
        }
    }
};
</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;
}
</style>