<template>
    <view class="preview-container" :class="phone === 'mi' ? 'mi-bg' : 'oppo-bg'" v-if="show">
        <template v-if="phone === 'oppo' || !phone || phone === 'iphone' || phone === 'huawei' || phone === 'vivo'">
            <view class="header" @tap.stop :style="{ 'padding-top': statusBarHeight }">
                <image class="icon-back" src="/static/image/phone-message/oppo/back-white.png" @tap="close"></image>
                <image class="icon-download" src="/static/image/phone-message/oppo/save-white.png"></image>
            </view>
            <!-- 图片显示 -->
            <swiper class="preview-swiper" :current="current" @change="onChange">
                <swiper-item v-for="(imgSrc, index) in images" :key="index">
                    <image class="preview-img" :src="imgSrc" mode="aspectFit"></image>
                </swiper-item>
            </swiper>
        </template>

        <template v-else-if="phone === 'mi'">
            <view class="mi-header" @tap.stop :style="{ 'padding-top': statusBarHeight }">
                <!-- 复用现有返回图标并通过滤镜反色 -->
                <image class="mi-icon-back" src="/static/image/phone-message/oppo/back-white.png" @tap="close"></image>
                <view class="mi-header-center">
                    <view class="mi-date">{{ currentFormatDate }}</view>
                    <view class="mi-time">{{ currentFormatTime }}</view>
                </view>
                <view class="mi-icon-right"></view> <!-- 占位符以居中 -->
            </view>
            <swiper class="preview-swiper mi-swiper" :current="current" @change="onChange">
                <swiper-item v-for="(imgSrc, index) in images" :key="index">
                    <view class="mi-img-container">
                        <image class="mi-preview-img" :src="imgSrc" mode="aspectFill"></image>
                    </view>
                </swiper-item>
            </swiper>
            <view class="mi-footer">
                <view class="mi-footer-item">
                    <image class="mi-footer-icon" src="/static/image/phone-message/mi/baocun.png"></image>
                    <view class="mi-footer-text">保存</view>
                </view>
                <view class="mi-footer-item">
                    <view class="mi-more-icon">⋯</view>
                    <view class="mi-footer-text">更多</view>
                </view>
            </view>
        </template>
    </view>
</template>

<script setup>
import { ref, watch, computed } from 'vue';

const statusBarHeight = (uni.getSystemInfoSync().statusBarHeight || 44) + 'px';

const props = defineProps({
    show: Boolean,
    images: {
        type: Array,
        default: () => []
    },
    times: {
        type: Array,
        default: () => []
    },
    currentIndex: {
        type: Number,
        default: 0
    },
    phone: {
        type: String,
        default: ''
    }
});
const emit = defineEmits(['update:show']);

const current = ref(0);

watch(() => props.currentIndex, (val) => {
    current.value = val;
});

const onChange = (e) => {
    current.value = e.detail.current;
};

const currentFormatDate = computed(() => {
    if (props.times && props.times.length > current.value) {
        const timeVal = props.times[current.value];
        if (timeVal) {
            if (typeof timeVal === 'string' && timeVal.includes('-')) {
                const parts = timeVal.split(' ');
                if (parts.length > 0) {
                    const dateParts = parts[0].split('-');
                    if (dateParts.length === 3) {
                        return `${dateParts[0]}年${parseInt(dateParts[1])}月${parseInt(dateParts[2])}日`;
                    }
                }
            }
            // Fallback
            const safeTimeStr = String(timeVal).replace(/-/g, '/');
            const d = new Date(safeTimeStr);
            if (!isNaN(d.getTime())) {
                return `${d.getFullYear()}年${d.getMonth() + 1}月${d.getDate()}日`;
            }
        }
    }
    return '';
});

const currentFormatTime = computed(() => {
    if (props.times && props.times.length > current.value) {
        const timeVal = props.times[current.value];
        if (timeVal) {
            if (typeof timeVal === 'string' && timeVal.includes(':')) {
                const parts = timeVal.split(' ');
                if (parts.length > 1) {
                    return parts[1];
                }
            }
            // Fallback
            const safeTimeStr = String(timeVal).replace(/-/g, '/');
            const d = new Date(safeTimeStr);
            if (!isNaN(d.getTime())) {
                const h = d.getHours().toString().padStart(2, '0');
                const m = d.getMinutes().toString().padStart(2, '0');
                return `${h}:${m}`;
            }
        }
    }
    return '';
});

const close = () => {
    emit('update:show', false);
};

// const download = () => {
//     const currentSrc = props.images[current.value];
//     if (!currentSrc) return;

//     // 触发下载逻辑
//     uni.saveImageToPhotosAlbum({
//         filePath: currentSrc,
//         success: function () {
//             uni.showToast({
//                 title: '保存成功',
//                 icon: 'success'
//             });
//         },
//         fail: function () {
//             uni.showToast({
//                 title: '保存失败',
//                 icon: 'none'
//             });
//         }
//     });
// };
</script>

<style scoped>
.preview-container {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    z-index: 999999;
    display: flex;
    flex-direction: column;
}

.oppo-bg {
    background-color: #000000;
}

.mi-bg {
    background-color: #FFFFFF;
}

.header {
    box-sizing: content-box;
    width: 100%;
    /* 预留状态栏高度 */
    height: 98rpx;
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 2;
    background-color: rgba(0, 0, 0, 0.6);
}

.icon-back {
    width: 48rpx;
    height: 48rpx;
    margin: 0 34rpx;
}

.icon-download {
    width: 48rpx;
    height: 48rpx;
    margin: 0 34rpx;
}

.preview-swiper {
    width: 100vw;
    height: 100vh;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 1;
}

.preview-img {
    width: 100%;
    height: 100%;
    display: block;
}

/* ================= 小米样式 ================= */
.mi-header {
    box-sizing: content-box;
    width: 100%;
    height: 98rpx;
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 2;
    background-color: #FFFFFF;
}

.mi-icon-back {
    width: 48rpx;
    height: 48rpx;
    margin: 0 34rpx;
    filter: invert(1);
    /* 白变黑 */
}

.mi-icon-right {
    width: 48rpx;
    height: 48rpx;
    margin: 0 34rpx;
}

.mi-header-center {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.mi-date {
    font-size: 32rpx;
    color: #333333;
    font-weight: 500;
}

.mi-time {
    font-size: 24rpx;
    color: #999999;
    margin-top: 4rpx;
}

.mi-swiper {
    z-index: 1;
    display: flex;
    align-items: center;
}

.mi-img-container {
    width: 100vw;
    height: 100vw;
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
}

.mi-preview-img {
    width: 100%;
    height: 100%;
    display: block;
}

.mi-footer {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 160rpx;
    background-color: #FFFFFF;
    display: flex;
    justify-content: space-around;
    align-items: flex-start;
    padding-top: 20rpx;
    z-index: 2;
}

.mi-footer-item {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.mi-footer-icon {
    width: 48rpx;
    height: 48rpx;
    margin-bottom: 8rpx;
}

.mi-more-icon {
    width: 48rpx;
    height: 48rpx;
    line-height: 38rpx;
    font-size: 40rpx;
    text-align: center;
    color: #000;
    margin-bottom: 8rpx;
    font-weight: bold;
}

.mi-footer-text {
    font-size: 24rpx;
    color: #666666;
}
</style>
