<template>
    <el-dialog
        v-model="winDialogVisible"
        lock-scroll
        modal-class="album-detail-modal"
        :showClose="false"
        width="86%"
        align-center
        @close="closeDialog"
    >
        <div class="detail-log">
            <div class="congratulation-wrap">
                <div class="congratulation-box">
                    <div class="win-des">
                        <!-- <img src="@/assets/img/weather_index/box-top.png" class="win-icon" /> -->
                    </div>
                    <div class="album-detail-box">
                        <div class="detail-title">{{ dialogData.name }}</div>
                        <div class="detail-desc-box">
                            <!-- <div class="desc-item van-ellipsis" v-if="dialogData?.conditionList && dialogData.conditionList.length">
                                <span class="item-name">{{ $t('触发条件') }}</span>
                                {{
                                    dialogData.condition ||
                                    dialogData.conditionList[0].name + dialogData.conditionList[0].value
                                }}
                            </div> -->
                            <div class="desc-item van-ellipsis">
                                <span class="item-name">{{ $t('农事编号') }}</span>
                                {{ dialogData.code }}
                            </div>
                            <div class="desc-item">
                                <span class="item-name">{{ $t('推荐时间') }}</span>
                                {{ dialogData.executeDate }}
                            </div>
                            <div class="desc-item">
                                <div class="item-name">药物处方
                                    <span class="no-prescription" v-if="!hasPrescription">{{ $t('暂无处方') }}</span>
                                </div>
                                <div class="item-table" v-if="hasPrescription">
                                    <div class="table">
                                        <div class="th">
                                            <div class="td">{{ $t('类型') }}</div>
                                            <div class="td width">{{ $t('名称') }}</div>
                                            <div class="td">{{ $t('配比') }}</div>
                                            <div class="td">{{ $t('方式') }}</div>
                                        </div>
                                        <div
                                            class="tr"
                                            v-for="(subP, subI) in dialogData.prescription?.pesticideFertilizerList"
                                            :key="subI"
                                        >
                                            <div class="td">{{ subP.typeName }}</div>
                                            <div class="td width">{{ subP.name }}</div>
                                            <div class="td">{{ subP.ratio }}</div>
                                            <div class="td">{{ $t('人工') }}</div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="card-link">
                                <img :src="dialogData.expertIcon || dialogData.expertUserIcon" />
                                <div class="expert-name">
                                    {{ dialogData.expertUserName || dialogData.expertName }}
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="bottom-btn" v-if="showBtn">
                <div class="btn-item secondary-btn" @click="toPage">{{ btnText }}</div>
                <div class="btn-item primary-btn" @click="triggerClick">{{ curRole == 0 ? "触发农事" : "下发农事" }}</div>
            </div>
        </div>
        <template #footer>
            <div class="dialog-footer">
                <div class="close-btn">
                    <!-- <el-icon size="32" color="#fff"><CircleCloseFilled /></el-icon> -->
                    <!-- <el-button type="primary" class="one-btn" @click="toShare">{{ $t('立即分享') }}</el-button> -->
                </div>
            </div>
        </template>
    </el-dialog>

    <Popup v-model:show="noShow" class="no-popup">
        <div class="right-icon">
            <img class="right-img" src="@/assets/img/home/right.png" alt="" />
        </div>
        <div class="no-popup-title">
            <span>{{ curRole == 0 ? "好的，感谢您的配合" : "农事下发成功" }}</span>
            <div class="no-popup-title-sub" v-if="curRole == 0">{{ $t('请您耐心等待 农事确认') }}</div>
        </div>
        <div class="no-popup-btn" @click="handleNoPopupBtn">{{ curRole == 0 ? "我知道了" : "我知道了" }}</div>
    </Popup>
</template>
<script setup>
import { ref, computed } from "vue";
import eventBus from "@/api/eventBus";
import { Popup } from "vant";
import { useRoute, useRouter } from "vue-router";
import { ElMessage } from "element-plus";

const props = defineProps({
    showSuccessOnly: {
        type: Boolean,
        default: false,
    },
});

const curRole = localStorage.getItem("SET_USER_CUR_ROLE");

const winDialogVisible = ref(false);
const noShow = ref(false);
const btnText = ref('');
const route = useRoute();
const router = useRouter();

const triggerClick = () => {
    winDialogVisible.value = false;
    // 如果只显示成功弹窗（来自 album_recognize 页面）
    if (props.showSuccessOnly) {
        noShow.value = true;
    }
    emit("triggerFarmWork");
};

const emit = defineEmits(["triggerFarmWork", "noPopupBtn"]);

const dialogData = ref({})
const showBtn = ref(true);
const showDialog = (id,btnTextVal = '转发', showBtnVal = true) => {
    btnText.value = btnTextVal;
    showBtn.value = showBtnVal;
    if (id) {
        VE_API.farm
            .getFarmWorkLib({ id })
            .then(({ code, data }) => {
                if (code === 0 && data) {
                    // 将接口返回的数据更新到 dialogData
                    dialogData.value = data || {};
                    settingData();
                }
            })
            .catch((error) => {
                console.error("获取农事详情失败:", error);
            });
    }
    winDialogVisible.value = true;
};

defineExpose({ showDialog });

//立即激活
const handleAct = () => {
    winDialogVisible.value = false;
    eventBus.emit("uploadUopup:show", {
        gardenIdVal: dialogData.value.farmId,
        orderIdVal: dialogData.value.orderId,
        textVal: dialogData.value.consequenceText,
    });
};

const pesticideFertilizers = ref([]);

// 判断是否有处方数据
const hasPrescription = computed(() => {
    const pesticideFertilizerList = dialogData.value.prescription?.pesticideFertilizerList;
    return pesticideFertilizerList && Array.isArray(pesticideFertilizerList) && pesticideFertilizerList.length > 0;
});

const settingData = () => {
    const pesticideFertilizerList = dialogData.value.prescription?.pesticideFertilizerList;
    pesticideFertilizers.value = pesticideFertilizerList || [];
};

function closeDialog() {
    winDialogVisible.value = false;
}

function toPage() {
    if(btnText.value!='转发'){
        router.push("/expert_list");
    }
    ElMessage.warning("该功能正在升级中，敬请期待");
}

const handleNoPopupBtn = () => {
    noShow.value = false;
    // if(curRole == 0){
    //     noShow.value = false;
    // }else{
    //     noShow.value = false;
    //     emit("noPopupBtn");
    // }
};
</script>

<style lang="scss" scoped>
.congratulation-wrap {
    border-radius: 12px;
    background: #f4f5f4;
    width: 100%;
}
.detail-log {
    width: 100%;
}
.close-btn {
    text-align: center;
    margin-top: 20px;
}
.bottom-btn {
    position: absolute;
    width: 100%;
    text-align: center;
    margin-top: 20px;
    display: flex;
    .btn-item {
        flex: 1;
        text-align: center;
        height: 38px;
        line-height: 38px;
        border-radius: 30px;
        font-size: 14px;
        &.secondary-btn {
            background: #ffffff;
            color: #000000;
        }
        &.primary-btn {
            background: linear-gradient(180deg, #76c3ff, #2199f8);
            color: #ffffff;
        }
    }
    .btn-item + .btn-item {
        margin-left: 12px;
    }
}
.congratulation-box {
    border-radius: 12px;
    background: url("@/assets/img/home/sd-bg.png") no-repeat top center / contain;
    // background: url("@/assets/img/home/box-top.png") no-repeat top center / contain;
    .el-message-box__message {
        padding: 12px 0 24px 0;
    }
    .win-title {
        color: #1d1e1f;
        font-family: "PangMenZhengDao", sans-serif; /* 使用自定义字体 */
        text-align: center;
        font-size: 24px;
        line-height: 32px;
    }
    .win-detail {
        text-align: center;
        color: #252525;
        padding-top: 6px;
        font-size: 16px;
        span {
            font-size: 22px;
            color: #2199f8;
            padding: 0 6px;
            font-weight: bold;
        }
    }
    .win-des {
        height: 180px;
        text-align: center;
    }
    .win-icon {
        width: 100%;
        border-radius: 12px 12px 0 0;
    }
}
.album-detail-box {
    padding: 0 10px 16px 10px;
    color: #000;
    position: relative;
    // top: -58px;
    .detail-title {
        font-size: 24px;
        font-weight: bold;
        padding-bottom: 8px;
        letter-spacing: 1.6px;
    }
    .detail-desc-box {
        position: relative;
        .desc-item {
            font-size: 14px;
            &.van-ellipsis{
                max-width: calc(100% - 86px);
            }
            .item-name {
                color: #999999;
                margin-right: 12px;
            }
            .no-prescription {
                margin-left: 12px;
                color: #000;
            }
            .item-table {
                margin-top: 8px;
                .table {
                    border: 1px solid rgba(196, 196, 196, 0.8);
                    border-radius: 4px;
                    font-size: 13px;
                    .th {
                        background: #f5f5f5;
                        color: #999999;
                        display: flex;
                        justify-content: space-between;
                        border-radius: 4px 4px 0 0;
                        padding: 4px 0;
                    }
                    .tr {
                        display: flex;
                        justify-content: space-between;
                        align-items: center;
                        color: #333333;
                        padding: 6px 0;
                    }

                    .tr + .tr {
                        border-top: 1px solid rgba(0, 0, 0, 0.08);
                    }
                    .td {
                        width: 25%;
                        padding: 0 2px;
                        text-align: center;
                    }
                    .width {
                        width: 55%;
                    }
                }
            }
        }
        .desc-item + .desc-item {
            padding-top: 4px;
        }
        .card-link {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            color: #2199f8;
            font-size: 12px;
            position: absolute;
            right: 6px;
            top: 0px;
            .expert-name {
                background: #d3e8ff;
                border-radius: 4px;
                padding: 1px 12px;
                margin-top: 4px;
                display: flex;
                align-items: center;
            }
            img {
                width: 48px;
                height: 48px;
                border-radius: 50%;
                object-fit: cover;
            }
            .icon {
                padding-right: 2px;
            }
        }
    }
}
.dialog-footer {
    position: relative;
    .close-btn {
        position: absolute;
        bottom: -58px;
        left: 0;
        right: 0;
        margin: 0 auto;
        text-align: center;
    }
}

.no-popup {
    width: 300px;
    border-radius: 14px;
    padding: 28px 15px 20px;
    box-sizing: border-box;
    .right-icon {
        text-align: center;
        padding-bottom: 12px;
        .right-img {
            width: 68px;
        }
    }
    .no-popup-title {
        font-size: 24px;
        font-weight: 500;
        text-align: center;
        .no-popup-title-sub {
            font-size: 14px;
            margin-top: 8px;
        }
    }
    .no-popup-btn {
        background-color: #2199f8;
        padding: 8px;
        border-radius: 20px;
        color: #fff;
        font-size: 16px;
        margin-top: 32px;
        text-align: center;
    }
}
</style>

<style lang="scss">
.album-detail-modal {
    .el-overlay-dialog {
        .el-dialog {
            padding: 0;
            border-radius: 12px;
            background: none;
            box-shadow: none;
            margin-bottom: 70px;
            margin-top: 10px;
            overflow: auto;
            scrollbar-width: none;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            pointer-events: none;
            .el-dialog__header {
                padding: 0;
            }
            .el-dialog__body {
                pointer-events: all;
                width: 100%;
            }
        }

        .one-btn {
            width: 210px;
            height: 40px;
            line-height: 40px;
        }
    }
}
</style>
