<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.farmWorkName }}</div>
                        <div class="detail-desc-box">
                            <div class="desc-item" v-if="dialogData?.conditionList && dialogData.conditionList.length">
                                <span class="item-name">触发条件</span>
                                {{ dialogData.condition || dialogData.conditionList[0].name + dialogData.conditionList[0].value }}
                            </div>
                            <div class="desc-item">
                                <span class="item-name">农事编号</span>
                                {{ dialogData.code }}
                            </div>
                            <div class="desc-item">
                                <span class="item-name">推荐时间</span>
                                {{ dialogData.executeDate }}
                            </div>
                            <!-- <div class="desc-item">
                                <span class="item-name">农事宗旨</span>
                                {{ dialogData.purpose || dialogData.condition }}
                            </div> -->
                            <div class="desc-item">
                                <div class="item-name">药物处方</div>
                                <div class="item-table">
                                    <div class="table">
                                        <div class="th">
                                            <div class="td">功效</div>
                                            <div class="td width">名称</div>
                                            <div class="td">配比</div>
                                            <div class="td">方式</div>
                                        </div>
                                        <div v-for="(prescriptionItem, prescriptionI) in dialogData.prescriptionList" :key="prescriptionI">
                                            <div class="tr" v-for="(subP, subI) in prescriptionItem.pesticideFertilizerList" :key="subI">
                                                <div class="td">{{ subP.typeName }}</div>
                                                <div class="td width">{{ subP.defaultName }}</div>
                                                <div class="td">{{ subP.ratio }}</div>
                                                <div class="td">人工</div>
                                            </div>
                                        </div>
                                        <!-- <div class="tr">
                                            <div class="td width">80%代森锰锌</div>
                                            <div class="td">山德生</div>
                                            <div class="td">1:2000</div>
                                            <div class="td">人工</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">
                <div class="btn-item secondary-btn" @click="toPage">咨询专家</div>
                <div class="btn-item primary-btn" @click="triggerClick">触发农事</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"> 立即分享 </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>好的，感谢您的配合</span>
            <div class="no-popup-title-sub">请您耐心等待 专家确认农事</div>
        </div>
        <div class="no-popup-btn" @click="noShow = false">我知道了</div>
    </Popup>
</template>
<script setup>
import { ref } from "vue";
import eventBus from "@/api/eventBus";
import wx from "weixin-js-sdk";
import { Popup } from "vant";
import { useRoute, useRouter } from "vue-router";

const winDialogVisible = ref(false);
const noShow = ref(false);

const route = useRoute();
const router = useRouter();
const sampleId = route.query.sampleId
const farmId = route.query.farmId;

const triggerClick = () => {
    console.log('triggerClick called, emitting activeUpload:show', { farmId: dialogData.value.farmId, farmWorkName: dialogData.value.farmWorkName })
    winDialogVisible.value = false;
    // 打开同级的激活上传弹窗
    eventBus.emit("activeUpload:show", { 
        gardenIdVal: dialogData.value.farmId,
        problemTitleVal: dialogData.value.farmWorkName
    });
}

const dialogData = ref({
        targetId: "part2",
        title: "梢期防虫",
        parentTitle: "秋梢期",
        consequenceText: "如果不做本次农事，会导致您的产量、质量下降30%，管理得分降低10分",
        id: "274654",
        reCheckText: "本次农事复核成效优异，作物产量潜力实现大幅增长，树体营养较充足，土壤肥力增加",
        farmName: "荔枝博览园",
        farmPoint: "POINT(113.61702297075017 23.584863449735067)",
        orderId: "745923632567422976",
        area: 2.719998598098755,
        expert: 91356,
        orderStatus: 4,
        activeStatus: 0,
        farmId: 766,
        regionId: 2,
        speciesId: "1",
        speciesName: "荔枝",
        agriculturalId: 24,
        farmWorkId: "699343457444958208",
        farmWorkLibId: "699343457444958208",
        farmWorkLibName: "梢期营养",
        farmWorkName: "梢期营养",
        expertIcon:
            "https://birdseye-img.sysuimars.com/birdseye-look-vue/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20250411150343.png",
        expertName: "韦帮稳",
        expertUserIcon: "",
        expertUserName: "韦帮稳",
        icon: 4,
        indexChart: [],
        indexName: "",
        beforeExecuteDate: "2025-05-26",
        checkDate: null,
        executeDate: "2025-08-01",
        indexJson: "",
        code: "BZ-YY-04-SQYY-20",
        expertPrescription: "",
        condition: "园区叶芽率大于20.0%",
        solarName: "",
        reCheck: null,
        executeBlueZones: [
            {
                id: "ws0y1m6x7cjz",
                level: null,
            },
            {
                id: "ws0y1md9v3ht",
                level: null,
            },
            {
                id: "ws0y1mdspbk7",
                level: null,
            },
            {
                id: "ws0y1mdvvdsz",
                level: null,
            },
            {
                id: "ws0y1me545tg",
                level: null,
            },
        ],
        menu: 1,
        isEdit: 0,
        isMaster: null,
        num: null,
        purpose: "",
        selfExec: null,
        defaultFarmWork: 0,
        farmWorkType: 2,
        farmWorkTypeName: "营养",
        type: 1,
        execute: 4,
        updateDate0: "2025-08-20",
        updateDate1: null,
        updateDate2: null,
        updateDate3: null,
        updateDate4: null,
        updateDate5: null,
        usageMode: "根部施",
        serviceMain: "广州泽秾丰农资有限公司",
        updateDate6: null,
        confirmPicture: [],
        executeMain: "广州泽秾丰农资有限公司",
        storeShortName: "泽秾丰",
        weatherWarningMsg: "",
        executeEvidence: [],
        userEvaluation: null,
        reviewDate: null,
        reviewDate2: null,
        reviewImage: [],
        reviewImage2: [],
        serviceRegion: "广州市从化区荔枝博览园",
        users: [
            {
                id: null,
                orderId: null,
                serviceType: null,
                userType: null,
                userId: 81881,
                joinStatus: null,
                icon: "https://birdseye-img.sysuimars.com/birdseye-look-mini/Group%201321316260.png",
                userName: "飞鸟种植助手",
                area: "",
                point: "",
                farmName: "",
                selected: null,
            },
        ],
        cost: null,
        prescriptionList: [
            {
                name: "营养",
                pesticideFertilizerList: [
                    {
                        defaultDroneRatio: null,
                        defaultName: "尿素",
                        defaultRatio: 0,
                        id: null,
                        muPrice: null,
                        muUsage: 15000.0,
                        muUsage2: 15000.0,
                        ratio: 0,
                        ratio2: 0,
                        remark: "",
                        usageMode: "",
                        usageModeList: ["叶面施、根部施"],
                        orderId: null,
                        pesticideFertilizerCode: "1001",
                        pesticideFertilizerId: "1",
                        pesticideFertilizerName: "尿素",
                        brand: "山东联盟",
                        typeName: "营养",
                        price: 132,
                        unit: "g",
                        executeStyle: null,
                    },
                    {
                        defaultDroneRatio: null,
                        defaultName: "15-15-15复合肥",
                        defaultRatio: 0,
                        id: null,
                        muPrice: null,
                        muUsage: 45000.0,
                        muUsage2: 45000.0,
                        ratio: 0,
                        ratio2: 0,
                        remark: "",
                        usageMode: "",
                        usageModeList: ["根部施"],
                        orderId: null,
                        pesticideFertilizerCode: "1002",
                        pesticideFertilizerId: "2",
                        pesticideFertilizerName: "15-15-15复合肥",
                        brand: "金正大",
                        typeName: "营养",
                        price: 220,
                        unit: "g",
                        executeStyle: null,
                    },
                ],
            },
        ],
        conditionList: [
            {
                index: "1-2-001-02-02-02-01-0008",
                name: "园区叶芽率",
                type: 1,
                value: "0.2",
            },
        ],
    });
const currentCard = ref({});
const showDialog = (pageParams) => {
    // dialogData.value = pageParams.card;
    // currentCard.value = {
    //     activeIndex: pageParams.activeIndex,
    //     farmWorkName: dialogData.value.farmWorkName,
    //     farmId: farmId,
    //     sampleId: sampleId,
    // };
    // settingData()
    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 settingData = () => {
    pesticideFertilizers.value = flattenDomains(dialogData.value.prescriptionList)
};
function flattenDomains(data) {
  return data.reduce((acc, item) => {
    return acc.concat(item.pesticideFertilizerList);
  }, []);
}

function closeDialog() {
    winDialogVisible.value = false
}

function toPage() {
    router.push("/expert_list")
}

</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/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;
            .item-name {
                color: #999999;
                margin-right: 12px;
            }
            .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: 62px;
                height: 62px;
                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>
