<template>
    <div class="carousel-container">
        <!-- 图片列表 -->
        <div class="carousel-wrapper" :style="carouselStyle">
            <!-- <photo-provider v-if="images" :photo-closable="true" @visibleChange="handleVisibleChange">
                
                <template  v-for="(photo, index) in images"
                   :key="photo.id">
                    <album-draw-box :isShowNum="0" :farmId="766" :photo="photo" :current="currentIndex" :index="index" :length="images.length"
                    ></album-draw-box>
                    </template>
            </photo-provider> -->
            <div
                class="carousel-img"
                :class="{ noFit: isAchievementImgs }"
                v-for="(photo, index) in images"
                :key="index"
            >
                <div class="label-text" v-if="labelText">{{ labelText }}</div>
                <img class="img-dom" :index="index" @click="clickPhoto(photo)" :src="getPhotoSrc(photo)" alt="" />
                <div class="carousel-img-mask" v-if="!isAchievementImgs">
                    <div class="mask-content">
                        <div class="mask-line line-top">
                            <span class="date-text">2025.12.05</span>
                            <span class="line-separator">|</span>
                            <span class="executor-text">执行人：张某某、张某某</span>
                        </div>
                        <div class="mask-line line-middle">
                            <span class="work-name">梢期杀虫</span>
                            <span class="line-separator">|</span>
                            <span class="prescription-text">药物处方：乙烯利乙烯利</span>
                        </div>
                        <div class="mask-line line-bottom">
                            <span class="location-text">
                                <img src="@/assets/watermark/address.png" alt="location" class="location-icon" />
                                荔博园(广东省广州市从化区思想街道思)
                            </span>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 左右箭头 -->
        <div @click.stop="prev" v-if="currentIndex !== 0" class="arrow left-arrow">
            <el-icon color="#F0D09C"><ArrowLeftBold /></el-icon>
        </div>
        <div @click.stop="next" v-if="images && currentIndex !== images.length - 1" class="arrow right-arrow">
            <el-icon color="#F0D09C"><ArrowRightBold /></el-icon>
        </div>

        <div class="curren-img" v-if="currentPhoto">
            <div ref="currentImgRef" class="carousel-img" :class="{ noFit: isAchievementImgs }">
                <img class="img-dom" :src="currentPhoto" alt="" />
                <img src="@/assets/img/home/qrcode.png" alt="" class="code-icon" />
                <div class="carousel-img-mask" v-if="!isAchievementImgs">
                    <div class="mask-content">
                        <div class="mask-line line-top">
                            <span class="date-text">2025.12.05</span>
                            <span class="line-separator">|</span>
                            <span class="executor-text">执行人：张某某、张某某</span>
                        </div>
                        <div class="mask-line line-middle">
                            <span class="work-name">梢期杀虫</span>
                            <span class="line-separator">|</span>
                            <span class="prescription-text">药物处方：乙烯利乙烯利</span>
                        </div>
                        <div class="mask-line line-bottom">
                            <span class="location-text">
                                <img src="@/assets/watermark/address.png" alt="location" class="location-icon" />
                                荔博园(广东省广州市从化区思想街道思)
                            </span>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <popup class="cavans-popup" v-model:show="showPopup">
            <div class="cavans-content">
                <img class="current-img" :src="previewCanvas" alt="" />
            </div>
            <!-- 底部操作按钮 -->
            <div class="bottom-actions" @click.stop="showPopup = false">
                <div class="action-buttons">
                    <div class="action-btn green-btn" @click.stop="handleWechat">
                        <div class="icon-circle">
                            <img src="@/assets/img/home/wechat.png" alt="" />
                        </div>
                        <span class="btn-label">微信</span>
                    </div>
                    <div class="action-btn orange-btn" @click.stop="handleSaveImage">
                        <div class="icon-circle">
                            <el-icon :size="24"><Download /></el-icon>
                        </div>
                        <span class="btn-label">保存图片</span>
                    </div>
                </div>
                <div class="cancel-btn" @click="handleCancel">取消</div>
            </div>
        </popup>
    </div>
</template>

<script setup>
import { Popup } from "vant";
import { toRefs, ref, computed, onMounted, onUnmounted, nextTick } from "vue";
import AlbumDrawBox from "./albumDrawBox.vue";
import html2canvas from "html2canvas";
import { base_img_url2 } from "@/api/config";
import "./cacheImg.js";
const props = defineProps({
    images: {
        type: Array,
        required: true,
    },
    labelText: {
        type: String,
        default: "",
    },
    isAchievementImgs: {
        type: Boolean,
        default: false,
    },
});
const { images, labelText, isAchievementImgs } = toRefs(props);
let timer = null;
const currentIndex = ref(0);

onMounted(() => {
    updateImagePosition();
    clearAndRestartTimer();
});
onUnmounted(() => {
    clearInterval(timer);
});

const updateImagePosition = () => {
    carouselStyle.value.transform = `translateX(-${currentIndex.value * 100}%)`;
};

const clickPhotoShow = () => {
    if (timer) {
        clearInterval(timer);
    }
};

// 图片显隐切换回调
const handleVisibleChange = ({ visible }) => {
    if (visible.value) {
        if (timer) {
            clearInterval(timer);
        }
    } else {
        clearAndRestartTimer();
    }
};

// 计算轮播图样式
const carouselStyle = computed(() => {
    return {
        transform: `translateX(-${currentIndex.value * 100}%)`,
    };
});

// 下一张图片
const next = () => {
    // 图片总数
    const totalImages = images.value.length;
    currentIndex.value = (currentIndex.value + 1) % totalImages;
    updateImagePosition();
    clearAndRestartTimer();
};

// 上一张图片
const prev = () => {
    // 图片总数
    const totalImages = images.value.length;
    currentIndex.value = (currentIndex.value - 1 + totalImages) % totalImages;
    updateImagePosition();
    clearAndRestartTimer();
};

const clearAndRestartTimer = () => {
    if (timer) {
        clearInterval(timer);
    }
    // timer = setInterval(next, 5000);
};

const showPopup = ref(false);
const currentPhoto = ref(null);
const previewCanvas = ref(null);

const currentImgRef = ref(null);
const clickPhoto = (photo) => {
    currentPhoto.value = getPhotoSrc(photo);
    nextTick(async () => {
        const canvas = await html2canvas(currentImgRef.value, {
            backgroundColor: "#ffffff00",
            scrollY: -window.scrollY, // 处理滚动条位置
            allowTaint: true, // 允许跨域图片
            useCORS: true, // 使用CORS
            scale: 2, // 提高分辨率（2倍）
            height: currentImgRef.value.scrollHeight, // 设置完整高度
            width: currentImgRef.value.scrollWidth, // 设置完整宽度
            logging: true, // 开启日志（调试用）
        });

        // 转换为图片并下载
        const image = canvas.toDataURL("image/png");
        setTimeout(() => {
            previewCanvas.value = image;
            showPopup.value = true;
        }, 100);
    });
};

const handleSaveImage = () => {
    downloadImage(previewCanvas.value, '执行照片');
};

function downloadImage(dataUrl, filename) {
    const link = document.createElement("a");
    link.href = dataUrl;
    link.download = filename;
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
}

const getPhotoSrc = (photo) => {
    if (isAchievementImgs.value) {
        return photo;
    }
    return base_img_url2 + (photo.cloudFilename ? photo.cloudFilename : photo);
};
</script>

<style lang="scss" scoped>
@import "src/styles/index";
.carousel-container {
    position: relative;
    width: 100%;
    overflow: hidden;
    margin: 0 auto;
    .curren-img {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        width: 80%;
        height: 100%;
        margin: 0 auto;
        z-index: -1;
        pointer-events: none;
        .carousel-img {
            width: 100%;
            position: relative;
            overflow: hidden;
        }
        .img-dom {
            width: 100%;
            height: 100%;
            object-fit: cover;
            border-radius: 8px;
        }
    }
    .carousel-wrapper {
        display: flex;
        transition: transform 0.5s ease;
        width: 100%;
        .carousel-img {
            width: calc(100vw - 48px);
            min-width: calc(100vw - 48px);
            // min-width: 312px;
            height: 255px;
            object-fit: cover;
            position: relative;
            overflow: hidden;
            &.noFit {
                height: auto;
                object-fit: contain;
            }
            .img-dom {
                width: 100%;
                height: 100%;
                object-fit: cover;
                border-radius: 8px;
            }
            &.noFit {
                .img-dom {
                    height: auto;
                    object-fit: contain;
                }
            }
        }
    }
    .code-icon {
        position: absolute;
        right: 12px;
        top: 12px;
        width: 40px;
    }
    .carousel-img-mask {
        position: absolute;
        bottom: -2px;
        left: -2px;
        width: calc(100% + 2px);
        height: 80%;
        background: linear-gradient(360deg, rgba(0, 0, 0, 0.7) 0%, rgba(0, 0, 0, 0.4) 25%, rgba(0, 0, 0, 0) 40%);
        z-index: 1;
        pointer-events: none;
        display: flex;
        align-items: flex-end;
        padding: 8px 12px;
        box-sizing: border-box;
        border-radius: 8px 8px 12px 12px;
        .mask-content {
            width: 100%;
            color: #ffffff;
            font-size: 10px;
            line-height: 15px;
        }
        .mask-line {
            display: flex;
            align-items: center;
            flex-wrap: wrap;
        }
        .line-middle {
            margin-top: 4px;
            .work-name {
                font-family: "PangMenZhengDao", system-ui, -apple-system, BlinkMacSystemFont, "Helvetica Neue", Arial,
                    sans-serif;
                font-size: 17px;
            }
        }
        .line-bottom {
            margin-top: 4px;
        }
        .date-text {
            font-family: "PangMenZhengDao";
            font-size: 12px;
        }
        .date-text,
        .executor-text,
        .prescription-text,
        .location-text {
            white-space: nowrap;
        }
        .location-icon {
            width: 9px;
            height: 10px;
            margin-right: 2px;
        }
        .line-separator {
            margin: 0 6px;
        }
    }
    .label-text {
        position: absolute;
        top: 0;
        left: 0;
        padding: 4px 10px;
        background: rgba(54, 52, 52, 0.8);
        color: #fff;
        font-size: 12px;
        border-radius: 8px 0 8px 0;
        z-index: 1;
    }
    .blur-bg {
        position: absolute;
        top: 0;
        width: 100%;
        height: 100%;
        backdrop-filter: blur(1.4px);
        .blur-content {
            border-radius: 8px;
            background: rgba(0, 0, 0, 0.5);
            width: 100%;
            height: 100%;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            font-size: 12px;
            color: #fff;
            .blur-img {
                img {
                    width: 54px;
                    position: relative;
                    left: 4px;
                    top: 4px;
                }
            }
            .blur-text {
                padding: 8px 0;
                text-align: center;
                line-height: 1.5;
            }
            .blur-btn {
                padding: 0 40px;
                box-shadow: 0 -2px 2px #86c9ff;
                height: 28px;
                line-height: 28px;
                border-radius: 50px;
                background: rgba(33, 153, 248, 0.7);
                // background: linear-gradient(#86C9FF, rgba(255, 255, 255, 0));
            }
        }
    }

    .arrow {
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        background: rgba(0, 0, 0, 0.5);
        width: rpx(72);
        height: rpx(72);
        border-radius: 50%;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        pointer-events: all;
    }

    .left-arrow {
        left: rpx(32);
    }

    .right-arrow {
        right: rpx(32);
    }
}

.cavans-popup {
    width: 100%;
    max-width: 100%;
    max-height: 90vh;
    background: none;
    border-radius: 12px;
    overflow: auto;
    display: flex;
    flex-direction: column;
    backdrop-filter: 4px;
    .cavans-content {
        text-align: center;
        padding: 16px;
        .current-img {
            width: 100%;
        }
    }

    // 底部操作按钮
    .bottom-actions {
        flex-shrink: 0;

        .action-buttons {
            padding: 16px;
            display: flex;
            justify-content: space-around;

            .action-btn {
                display: flex;
                flex-direction: column;
                align-items: center;
                cursor: pointer;

                .icon-circle {
                    width: 48px;
                    height: 48px;
                    border-radius: 50%;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    color: #fff;
                    margin-bottom: 4px;

                    .el-icon {
                        color: #fff;
                    }
                    img {
                        width: 50px;
                    }
                }

                &.blue-btn .icon-circle {
                    background: #2199f8;
                }

                &.green-btn .icon-circle {
                    background: #07c160;
                }

                &.orange-btn .icon-circle {
                    background: #ff790b;
                }

                .btn-label {
                    font-size: 12px;
                    color: #fff;
                }
            }
        }

        .cancel-btn {
            text-align: center;
            font-size: 18px;
            color: #fff;
            cursor: pointer;
        }
    }
}
</style>
