<template>
    <popup
        class="active-upload-popup"
        v-model:show="show"
        closeable
        :close-on-click-overlay="false"
        @closed="handleClosed"
    >
        <div class="header">
            <div class="title">
                <span class="required">*</span>
                {{ problemTitle }}
            </div>
            <div class="date-input">
                <el-date-picker v-model="uploadDate" size="large" style="width: 100%" type="date" placeholder="请选择日期" />
            </div>
        </div>
        <div class="tips-text">上传照片，诊断更准确哦~</div>
        <upload :textShow="true" class="upload-wrap" exampleImg>
            <img class="example" src="@/assets/img/home/example-4.png" alt="" />
            <img class="example" src="@/assets/img/home/plus.png" alt="">
        </upload>
        <div class="btn" @click="handleUpload">确认</div>
    </popup>

    <!-- 上传成功提示弹窗 -->
    <popup class="success-popup" v-model:show="successShow" :close-on-click-overlay="false">
        <div class="success-wrap">
            <img class="success-icon" src="@/assets/img/home/right.png" alt="" />
            <div class="success-title">好的，感谢您的配合</div>
            <div class="success-sub">请您耐心等待农事确认</div>
            <div class="btn" @click="successShow = false">我知道了</div>
        </div>
    </popup>
</template>

<script setup>
import { Popup } from "vant";
import { onMounted, onUnmounted, ref } from "vue";
import upload from "@/components/upload";
import eventBus from "@/api/eventBus";
import { ElMessage } from "element-plus";

const show = ref(false);
const gardenId = ref(null);
const images = ref([]);
const uploadDate = ref("");
const problemTitle = ref("请选择问题");
const successShow = ref(false);
onMounted(() => {
    eventBus.off("upload:changeArr", uploadChange);
    eventBus.on("upload:changeArr", uploadChange);
    eventBus.on("activeUpload:show", handleShow);
    eventBus.on("activeUpload:success", handleSuccess);
});

function uploadChange(arr) {
    images.value = arr;
}

function formatDate(date) {
    let year = date.getFullYear();
    let month = String(date.getMonth() + 1).padStart(2, "0");
    let day = String(date.getDate()).padStart(2, "0");
    return `${year}-${month}-${day}`;
}

const type = ref(null);
function handleShow({ gardenIdVal, problemTitleVal,typeVal }) {
    images.value = [];
    gardenId.value = gardenIdVal;
    problemTitle.value = problemTitleVal || "请选择问题";
    uploadDate.value = formatDate(new Date());
    show.value = true;
    type.value = typeVal;
}

function handleSuccess() {
    successShow.value = true;
}

const emit = defineEmits(['handleUploadSuccess']);

const handleUpload = () => {
    if (images.value.length === 0) return ElMessage.warning("请上传图片");
    const params = {
        gardenId: gardenId.value,
        images: images.value,
        uploadDate: uploadDate.value,
    };
    // 提交成功后的反馈弹窗（示例直接展示）
    if(type.value === 'question'){
        show.value = false;
        emit("handleUploadSuccess",params);
    }else{
        show.value = false;
        successShow.value = true;
    }
    // VE_API.ali.uploadImg(params).then((res) => {
    //     if (res.success) {
    //         show.value = false;
    //         successShow.value = true;
    //     }
    // });
};

function handleClosed() {
    eventBus.emit("upload:reset");
}

onUnmounted(() => {
    eventBus.off("activeUpload:show", handleShow);
    eventBus.off("activeUpload:success", handleSuccess);
    eventBus.off("upload:changeArr", uploadChange);
    show.value = false;
});
</script>

<style lang="scss" scoped>
.active-upload-popup {
    width: 90%;
    box-sizing: border-box;
    padding: 24px 18px 20px;
    background: linear-gradient(0deg, #ffffff 70%, #d1ebff 100%);
    border-radius: 10px;
    ::v-deep {
        .van-popup__close-icon {
            color: #000;
        }
    }
    .header {
        .title {
            font-size: 16px;
            font-weight: 500;
            display: flex;
            align-items: center;
            .required {
                color: #ff4d4f;
                margin-right: 4px;
            }
        }
        .date-input {
            margin: 12px 0;
        }
    }
    .tips-text {
        font-weight: 500;
    }
    .upload-wrap {
        margin: 12px 0 24px;
    }
    .example {
        width: 80px;
        height: 80px;
    }
    .example + .example {
        margin-left: 12px;
    }
}

.btn {
    padding: 8px;
    background: #2199f8;
    border-radius: 25px;
    color: #fff;
    font-size: 16px;
    text-align: center;
}

.success-popup {
    width: 300px;
    border-radius: 14px;
    padding: 28px 15px 20px;
    box-sizing: border-box;
    .success-wrap {
        text-align: center;
    }
    .success-icon {
        width: 68px;
        height: 68px;
    }
    .success-title {
        font-size: 24px;
        margin-top: 12px;
    }
    .success-sub {
        margin: 8px 0 32px;
    }
}
</style>
