<template>
    <div class="agricultural-detail">
        <custom-header name="农事详情"></custom-header>
        <div class="detail-content">
            <div class="card-wrap" v-if="route.query.title !== '气象风险'">
                <div class="card-box photo-card">
                    <div class="card-title">
                        <span>农情照片</span>
                        <span class="date">{{ imgInfo.samplingTime }}</span>
                    </div>
                    <div class="ratio-tip" :class="archiveColorObj[route.query.title]">
                        <div class="title">{{ route.query.title }}</div>
                        <span class="content van-ellipsis">{{ route.query.content }}</span>
                    </div>
                    <!-- <div class="location-tip">
                        由于系统审核，某些照片拍摄位置与农场相差超 3 公里，请在农场现场使用水印相机重新拍摄上传，谢谢配合
                    </div> -->
                    <div class="photo-grid">
                        <div v-for="(photo, index) in imgInfo.imageList" :key="photo.id" class="photo-item"
                            @click="handlePhotoClick(index)">
                            <img :src="photo.url" alt="农情照片" />
                        </div>
                    </div>
                </div>
                <div class="card-box region-card">
                    <div class="card-title">区域勾选</div>
                    <div class="map-container" ref="mapContainer"></div>
                </div>
            </div>
            <div class="card-wrap" v-else>
                <div class="card-box risk-card">
                    <div class="card-title">
                        <span>农情详情</span>
                        <span class="date">2025.05.06</span>
                    </div>
                    <img class="risk-image" src="" alt="">
                    <div class="risk-content">
                        <p class="risk-highlight">
                            <span class="risk-title">高湿风险：</span>
                            <span class="risk-text">预计未来7天平均相对湿度≥85%，伴随间歇性小雨或雾天。</span>
                        </p>
                        <p>潜在危害：田间湿度过高极易诱发霜疫霉病、炭疽病等真菌病害，病菌侵染幼果后导致腐烂、脱落。影响幼果正常发育和果皮着色，可能造成裂果、烂果。</p>
                        <p>高湿环境利于蒂蛀虫等害虫繁殖，成虫活动增加，增加后期虫果率。</p>
                        <div class="warning-tip">预警等级：黄色预警（中等风险），建议在未来48小时内启动预防措施。</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { useRoute } from "vue-router";
import { ref, onActivated, computed } from "vue";
import customHeader from "@/components/customHeader.vue";
import { showImagePreview } from 'vant';
import IndexMap from "@/views/old_mini/common_map/index.js";
const route = useRoute();
const mapContainer = ref(null);
const indexMap = new IndexMap();

const previewImages = computed(() => {
    const list = imgInfo.value?.imageList || [];
    return list.map((item) => item?.url).filter(Boolean);
});

const archiveColorObj = {
    '管理信息': "green",
    '异常发现': "red",
    '气象风险': "orange",
    // '气象风险': "yellow",
};

const handlePhotoClick = (index) => {
    const images = previewImages.value;
    const safeIndex = Math.min(Math.max(Number(index) || 0, 0), Math.max(images.length - 1, 0));
    showImagePreview({
        images,
        startPosition: safeIndex,
        closeable: true,
        showIndex: true,
    });
};

onActivated(() => {
    // getFarmImagePage();
    if(route.query.title !== '气象风险') {
        indexMap.initMap('POINT(113.6142086995688 23.585836479509055)', mapContainer.value);
    } 
});

const imgInfo = ref({});
const getFarmImagePage = () => {
    VE_API.monitor.getFarmImagePage({
        id: route.query.id
    }).then((res) => {
        if (res.code === 0) {
            imgInfo.value = res.data;
        }
    });
};
</script>

<style lang="scss" scoped>
.agricultural-detail {
    min-height: 100vh;
    width: 100%;
    background: #f2f3f5;

    .detail-content {
        padding: 12px;
        padding-bottom: 24px;
        box-sizing: border-box;

        .card-wrap {
            .card-box {
                background: #fff;
                border-radius: 8px;
                padding: 12px 16px;
                box-sizing: border-box;

                .card-title {
                    font-size: 16px;
                    display: flex;
                    align-items: center;
                    justify-content: space-between;
                    margin-bottom: 12px;

                    .date {
                        font-size: 12px;
                        color: rgba(0, 0, 0, 0.2);
                    }
                }
            }

            .sampling-card {
                .sampling-title {
                    font-size: 16px;
                    font-weight: 500;
                    color: #333;
                    margin-bottom: 6px;
                }

                .sampling-desc {
                    font-size: 14px;
                    color: rgba(0, 0, 0, 0.5);
                }
            }

            .photo-card {
                .ratio-tip {
                    font-size: 14px;
                    padding: 5px 8px;
                    background: rgba(33, 153, 248, 0.08);
                    border-radius: 5px;
                    display: flex;
                    align-items: center;
                    
                    .title {
                        font-size: 12px;
                        color: #fff;
                        margin-right: 12px;
                        background: #2199f8;
                        padding: 2px 6px;
                        border-radius: 2px;
                    }

                    .content {
                        max-width: 210px;
                    }

                    &.green {
                        background: rgba(71, 184, 129, 0.1);
                        .title {
                            background: #47B881;
                        }
                    }
                    &.red {
                        background: rgba(255, 78, 78, 0.1);
                        .title {
                            background: #FF4E4E;
                        }
                    }
                    &.orange {
                        background: rgba(255, 148, 61, 0.1);
                        .title {
                            background: #FF943D;
                        }
                    }
                }

                .location-tip {
                    margin-top: 12px;
                    padding: 5px 10px;
                    border: 1px solid #FF953D;
                    border-radius: 5px;
                    color: #FA7406;
                }

                .photo-grid {
                    margin-top: 12px;
                    display: grid;
                    grid-template-columns: repeat(4, 1fr);
                    grid-gap: 6px;

                    .photo-item {
                        position: relative;
                        width: 100%;
                        padding-bottom: 100%;
                        border-radius: 8px;
                        overflow: hidden;
                        background: #e5f5e5;

                        img {
                            position: absolute;
                            inset: 0;
                            width: 100%;
                            height: 100%;
                            object-fit: cover;
                            display: block;
                        }
                    }
                }
            }

            .risk-card {
                .risk-image {
                    width: 100%;
                    height: 170px;
                    border-radius: 5px;
                    margin-bottom: 12px;
                    background: #ff8a3d;
                }

                .risk-content {
                    line-height: 22px;
                    border-radius: 5px;
                    padding: 5px 8px;
                    background: rgba(190, 190, 190, 0.11);

                    p {
                        margin: 0 0 12px;
                    }

                    .risk-highlight {
                        .risk-title {
                            font-weight: 500;
                            color: #FFAA14;
                        }
                    }
                }

                .warning-tip {
                    border-radius: 5px;
                    background: rgba(255, 177, 41, 0.1);
                    color: #FFAA14;
                    font-size: 14px;
                    line-height: 22px;
                    font-weight: 500;
                    padding: 5px 8px;
                }
            }

            .region-card {
                margin-top: 10px;
                .map-container {
                    width: 100%;
                    height: 170px;
                    clip-path: inset(0px round 5px);
                }
            }
        }
    }
}

.image-popup {
    width: 327px;
    border-radius: 8px;

    .popup-content {
        width: 100%;
    }
}
</style>