<template>
    <van-popup class="histroy-page" v-model:show="show" round position="bottom" :style="{ maxHeight: '70%' }">
        <div class="sticky">
            <div class="title text-white flex overflow-hidden whitespace-nowrap">
                <div class="van-ellipsis">{{ seriesInfo.title }}</div>
                <div>-第{{ curEpInfo.episodeNum }}集</div>
            </div>
            <div class="btns">
                <van-button
                    type="primary"
                    :color="i === page ? '#7968FB' : '#3C328A'"
                    size="mini"
                    round
                    v-for="i in curPage"
                    :key="i"
                    @click="changePage(i)"
                    >{{ 15 * (i - 1) + 1 + '-' + 15 * i }}</van-button
                >

                <van-button
                    @click="changePage(curPage + 1)"
                    :color="curPage + 1 === page ? '#7968FB' : '#3C328A'"
                    type="primary"
                    size="mini"
                    round
                    v-if="laNum > 0"
                >
                    {{ 15 * curPage + 1 + '-' + (15 * curPage + laNum) }}
                </van-button>
            </div>
        </div>

        <div class="video-list px-[18px] pt-[15px]">
            <div
                class="video-info flex flex-col items-center"
                :class="{ active: item.episodeNum === curEpInfo.episodeNum }"
                v-for="(item, index) in episodesList"
                :key="index"
                @click="play(item)"
            >
                <div class="video-content">
                    <van-image block width="calc(33vw - 20.66px)" height="140" fit="cover" radius="12" :src="file" />
                    <div class="video-box" v-if="item.episodeNum === curEpInfo.episodeNum">
                        <img src="@/assets/icon_bofang.png" alt="" />
                        <div>正在播放</div>
                    </div>
                </div>

                <div class="text-xs text-white pt-[8px] pb-[10px]">第{{ item.episodeNum }}集</div>
            </div>
        </div>
    </van-popup>
</template>

<script setup>
import { ref, computed } from 'vue'
import { http } from '@/plugins/http'
import resolveUrl from 'resolve-url'
const fileURL = import.meta.env.VITE_HTTP_FILE_URL
const show = ref(false)
const props = defineProps({
    curEpInfo: {
        type: Object,
        default: () => {
            return {}
        }
    },
    seriesInfo: {
        type: Object,
        default: () => {
            return {}
        }
    }
})
const emit = defineEmits(['playVideo'])
const page = ref(0)
function changeShow(val) {
    show.value = val

    if (val) {
        page.value = Math.ceil(props.curEpInfo.episodeNum / 15)
        getEpisodes(page.value)
    }
}
defineExpose({ changeShow })

const total = ref(0)
const episodesList = ref([])
function getEpisodes(page = 1) {
    http.get('/api/episodes', {
        seriesId: props.seriesInfo.id,
        orderBy: 'episodeNum',
        order: 'episodeNum',
        page: page,
        pageSize: 15
    }).then(res => {
        total.value = res.meta.total
        episodesList.value = res.data
    })
}

const curPage = computed(() => {
    return Math.floor(total.value / 15)
})
const laNum = computed(() => {
    return total.value % 15
})

const file = computed(() => {
    return resolveUrl(fileURL, props.seriesInfo.cover)
})

function changePage(_page) {
    if (page.value !== _page) {
        page.value = _page
        getEpisodes(_page)
    }
}

function play(playInfo) {
    show.value = false
    emit('playVideo', playInfo.episodeNum)
}
</script>

<style lang="less" scoped>
.histroy-page {
    background: linear-gradient(180deg, rgba(17, 19, 30, 0.8) 0%, rgba(17, 19, 30, 0) 100%);
    border-radius: 16px 16px 0px 0px;
    backdrop-filter: blur(4px);
    // padding: 21px 18px;
    padding-bottom: 21px;
    .sticky {
        position: sticky;
        top: 0;
        z-index: 20;
        background: rgba(17, 19, 30, 0.8);
        padding: 21px 18px 10px;
    }
}

.btns {
    .van-button {
        width: 72px;
        font-size: 14px;
        margin-right: 16px;
        margin-bottom: 5px;
    }
    // .van-button + .van-button {
    //     margin-left: 16px;
    // }
}

.video-info {
    width: calc(33vw - 20.66px);
    display: inline-flex;
    margin-right: 13px;
    &:nth-child(3n) {
        margin-right: 0;
    }
    .video-content {
        border-radius: 12px;
        border: 1px solid #7968fb;
        overflow: hidden;
    }

    &.active {
        .video-content {
            position: relative;
            .video-box {
                position: absolute;
                top: 0;
                left: 0;
                right: 0;
                bottom: 0;
                background: rgba(17, 19, 30, 0.5);

                display: flex;
                flex-direction: column;
                justify-content: center;
                align-items: center;

                img {
                    width: 24px;
                    height: 24px;
                }
                div {
                    font-size: 12px;
                    font-weight: 400;
                    color: #ffffff;
                    line-height: 18px;
                    text-shadow: 0px 1px 1px rgba(0, 0, 0, 0.5);
                }
            }
        }
    }
}
</style>
