<template>
    <Header background="white" :show="false">
        <div class="header">
            <!-- <van-icon name="arrow-left"  class="icon" @click="back" /> -->
            <div class="h-content">
                <img src="../../static/image/subject-list-logo.png" />
                <div class="title">当前专题：{{ queryParams.tagName }}</div>
            </div>
            <div class="overlay"></div>
        </div>
    </Header>
    <div class="container">
        <van-sticky>
            <!-- <van-nav-bar title="专题列表" :left-arrow="true" fixed placeholder safe-area-inset-top
                @click-left="router.go(-1)" /> -->
            <!-- <div class="header">
                <div class="h-content">
                    <img src="../../static/image/subject-list-logo.png" />
                    <div class="title">当前专题：{{ queryParams.tagName }}</div>
                </div>
                <div class="overlay"></div>
            </div> -->
        </van-sticky>
        <div class="content">
            <van-pull-refresh v-model="refreshing" @refresh="handleRefresh">
                <van-list v-model:loading="loading" :finished="finished" finished-text="没有更多了" :offset="50"
                    @load="getList" v-if="dataList.length">
                    <div class="list">
                        <div class="item" v-for="(item, index) in dataList" :key="index"
                            @click="router.push({ name: 'home-content', params: { id: item.materialId } })">
                            <img :src="fileDomain + item.coverpicture">
                            <div class="desc">
                                <span class="title">
                                    <van-text-ellipsis :content="item.materialTitle" />
                                </span>
                                <div class="tag">
                                    <span v-for="(tagName, index) in item.tagName.split(',')" :key="index">{{
                                        tagName }}</span>
                                </div>
                                <div class="info">
                                    <div class="user">
                                        <img src="../../static/image/avatar.png">
                                        <span>{{ item.author }}</span>
                                    </div>
                                    <div class="collect">
                                    <van-icon name="star-o" color="#9F9F9F" />
                                    <!-- <van-icon name="star-o" color="#9F9F9F" v-if="index < 5" />
                                    <van-icon name="star" color="#F1C13E" v-else /> -->
                                    <span>收藏</span>
                                </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </van-list>
                <van-empty description="暂无数据" v-else />
            </van-pull-refresh>
        </div>
    </div>
</template>
<script setup name="subject-list">
import { selectSubjectList } from '@/api/subject'
import { useInitScroll } from '@/hooks/useScroll'
import { selectOrganizeList } from '@/api/prefecture'
import useUserStore from '@/store/user'

useInitScroll()
const userStore = useUserStore()
const router = useRouter()
const route = useRoute()

const refreshing = ref(false)
const loading = ref(false)
const finished = ref(true)
const fileDomain = import.meta.env.VITE_APP_FILE_URL
const page = reactive({
    pageNum: 0,
    pageSize: 10
})
const queryParams = reactive({
    tagId: null,
    tagName: '',
    materialType: 3,
    typeClassify: '',
    organizeName: '',
    organizeInt: [0]
})

onLoad((options) => {
    queryParams.tagId = options.tagId
    queryParams.tagName = options.tagName
    getOrganizeList()
})

const organizeList = ref([])
const getOrganizeList = async () => {
    const data = {
        userId: userStore.userinfo.user.id,
        sysCode: import.meta.env.VITE_APP_SYSTEM_CODE,
        moduleCode: 'practice-mediaCatalog'
    }
    const res = await selectOrganizeList(data)
    organizeList.value = res.data
    organizeList.value.forEach(item => {
        queryParams.organizeInt.push(item.id)
    })
    getList()
}

const dataList = ref([])
const handleSelectDataList = async () => {
    const res = await selectSubjectList(page, queryParams)
    setDataList(res)
}
const getList = () => {
    page.pageNum++
    loading.value = true
    handleSelectDataList()
}
const setDataList = (res) => {
    if (refreshing.value) {
        dataList.value = res.data.list
    } else {
        dataList.value.push(...res.data.list)
    }
    loading.value = false
    finished.value = dataList.value.length >= res.data.total
    refreshing.value = false
}

const handleRefresh = () => {
    window.scroll(0, 0)
    refreshing.value = true
    page.pageNum = 0
    getList()
}

const back = () => {
    xma.navigateBack();
  };


// onActivated(() => {
//     nextTick(() => { }).then(() => {
//         if (queryParams.tagId != route.query.tagId) {
//             handleRefresh()
//         }
//     })
// })
</script>
<style lang="scss" scoped>
:deep(.searchComponent .search) {
    display: contents;
}

.header {
    background-image: url('../../static/image/subject-list-bg.png');
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center;
    position: relative;
    display: flex;


    .h-content {
        position: relative;
        z-index: 999;
        width: 100%;
        height: 62px;
        display: flex;
        justify-content: center;
        align-items: center;
        gap: 10px;

        img {
            width: 23px;
            height: 23px;
        }

        .title {
            font-size: 15px;
            color: #ffffff;
            font-weight: 500;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
    }

    .overlay {
        background-color: #000000;
        opacity: 0.3;
        position: absolute;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
    }
}

.container {
    background-color: #f6f6f6;
    min-height: 100vh;



    .content {
        padding: 0 10px;

        .list {
            display: flex;
            flex-wrap: wrap;
            justify-content: space-between;
            margin-top: 20px;

            .item {
                break-inside: avoid;
                background-color: #ffffff;
                border-radius: 5px;
                margin-bottom: 10px;
                width: 49%;

                img {
                    width: 100%;
                    height: 150px;
                    object-fit: cover;
                }

                .desc {
                    padding: 5px 10px;
                    display: flex;
                    flex-direction: column;
                    gap: 5px;

                    .title {
                        font-size: 16px;

                    }

                    .tag {
                        display: flex;
                        flex-wrap: wrap;
                        gap: 3px;

                        span {
                            font-size: 14px;
                            padding: 0 5px;
                            background-color: #F3F8FF;
                            color: #4C70AC;
                            font-size: 12PX;
                            border-radius: 3px;
                        }
                    }

                    .info {
                        display: flex;
                        justify-content: space-between;
                        padding: 5px 0;

                        .user {
                            display: flex;
                            gap: 7px;
                            align-items: center;

                            img {
                                width: 23px;
                                height: 23px;
                                border-radius: 50%;
                            }

                            span {
                                color: #9F9F9F;
                                font-size: 12px;
                            }
                        }
                    }

                    .collect {
                        display: flex;
                        align-items: center;
                        gap: 3px;

                        span {
                            color: #9F9F9F;
                            font-size: 12px;
                        }
                    }
                }
            }
        }
    }

}
</style>