<script setup>
import {onActivated, onMounted, reactive, ref,computed} from 'vue'
import SelfList from '@/components/SelfList.vue'
import SharePoster from '@/components/SharePoster.vue'
import {apiReportClassify,apiSpecialColumnList} from '@/api/report'
import { useRoute,useRouter } from 'vue-router';

const route=useRoute()
const router=useRouter()

document.title=decodeURIComponent(route.query.classifyName)

// 获取分类列表数据 过滤出其中的专栏
let tabList=ref([])
let classifyId=ref(Number(route.query.classifyId)||0)
const getClassifyList=async ()=>{
    const res=await apiReportClassify()
    if(res.code===200){
        let arr=res.data||[]
        tabList.value=arr.filter(item=>item.redirect_type===1)
    }
}
getClassifyList()

// 获取专栏列表
let listState=reactive({
    list:[],
    finished:false,
    loading:false
})
const getSpecialColumnList=async ()=>{
    listState.loading=true
    const res=await apiSpecialColumnList({
        classify_id_first:Number(classifyId.value)
    })
    listState.loading=false
    document.body.scrollTop=document.documentElement.scrollTop=0
    if(res.code===200){
        listState.list=res.data||[]
        listState.finished=true
    }
}
getSpecialColumnList()


// 点击切换专栏
const handleClickTab=(item)=>{
    document.title=item.props.label
    classifyId.value=item.props.name 
    router.replace({
        query:{
            classifyId:item.props.name,
            classifyName:item.props.label
        }
    })
    getSpecialColumnList()
}

// 跳转专栏详情
const goSpecialColumnDetail=(item)=>{
    router.push({
        path:'/report/specialcolumndetail',
        query:{
            columnId:item.classify_id_second
        }
    })
}

// 向小程序发送数据
const handleDataToXcx=()=>{
  const postData={
    path:'/pages-report/specialColumn/list',
    params:{
        classifyId:classifyId.value,
        classifyName:route.query.classifyName
    },
    title:`FICC【${route.query.classifyName}】`,
    shareImg:''
  }
  wx.miniProgram.postMessage({ data: postData })
}

onMounted(()=>{
    handleDataToXcx()
})

// 组件缓存激活时
onActivated(()=>{
    const temClassifyId=route.query.classifyId
    if(temClassifyId!=classifyId.value){
        classifyId.value=Number(temClassifyId)
        getSpecialColumnList()
    }
    handleDataToXcx()
})

const code_scene=computed(()=>{
    return JSON.stringify({classifyId:classifyId.value,classifyName:route.query.classifyName})
})

const posterParams=computed(()=>{
    let obj={
        list_title:route.query.classifyName,
        stage_1:'',
        avatar_1:'',
        title_1:'',
        author_1:'',
        tag_1:'',
        stage_2:'',
        avatar_2:'',
        title_2:'',
        author_2:'',
        tag_2:'',
    }
    if(listState.list[0]){
        obj.stage_1=listState.list[0].stage.toString()
        obj.avatar_1=listState.list[0].home_img_url
        obj.title_1=listState.list[0].classify_name_second
        obj.author_1=listState.list[0].report_author
        obj.tag_1=listState.list[0].author_descript
    }
    if(listState.list[1]){
        obj.stage_2=listState.list[1].stage.toString()
        obj.avatar_2=listState.list[1].home_img_url
        obj.title_2=listState.list[1].classify_name_second
        obj.author_2=listState.list[1].report_author
        obj.tag_2=listState.list[1].author_descript
    }
    return obj
})

</script>

<template>
    <div class="report-specialColumn-list-page">
        <img :src="$store.state.globalImgUrls.specialColumnBanner" alt=""  class="top-banner">
        <el-tabs v-model="classifyId" class="tabs-wrap" @tab-click="handleClickTab">
            <el-tab-pane 
                :label="item.classify_name_first" 
                :name="item.classify_id_first"
                v-for="item in tabList" 
                :key="item.classify_name_first"
            ></el-tab-pane>
        </el-tabs>
        <SelfList 
            :finished="listState.finished" 
            :isEmpty="listState.list.length === 0 && listState.finished" 
            :count="listState.list.length"
            :loading="listState.loading"
        >
            <div class="flex list-wrap">
                <div class="item" v-for="item in listState.list" :key="item.classify_id_second" >
                    <div class="global-plain-btn btn" @click="goSpecialColumnDetail(item)">立即查看</div>
                    <div class="title">{{item.classify_name_second}}</div>
                    <div class="flex user-box">
                        <img :src="item.home_img_url" alt="" class="avatar">
                        <div :class="['name',item.vip_title&&'vip']">{{item.report_author}}</div>
                        <div class="stage">最新：{{item.stage}}期</div>
                    </div>
                    <div class="multi-ellipsis-l2 des">{{item.abstract}}</div>
                </div>
            </div>
        </SelfList>
    </div>

    <!-- 生成海报 -->
    <SharePoster  
        :shareData="{
            type:'special_column_list',
            code_page:'pages-report/specialColumn/list',
            code_scene:code_scene,
            data:posterParams
        }"
    ></SharePoster>
</template>

<style lang="scss" scoped>
.report-specialColumn-list-page{
    .top-banner{
        display: block;
        width: 100%;
        margin-bottom: 40px;
    }
    .tabs-wrap{
        :deep(.el-tabs__active-bar){
            height: 4px;
            background: #F3A52F;
            border-radius: 2px 2px 2px 2px;
        }
        :deep(.el-tabs__nav-wrap::after){
            display: none;
        }
        :deep(.el-tabs__item.is-active){
            color: #F3A52F;
        }
        :deep(.el-tabs__item){
            font-size: 20px;
            &:hover{
                color: #F3A52F;
            }
        }
        :deep(.el-tabs__nav-next){
            width: 20px;
            height: 20px;
            background-image: url('@/assets/icon-more.png');
            background-size: cover;
            top: 50%;
            transform: translateY(-50%);
            .el-icon{
                display: none;
            }
        }
        :deep(.el-tabs__nav-prev){
            width: 20px;
            height: 20px;
            background-image: url('@/assets/icon-more.png');
            background-size: cover;
            top: 50%;
            transform: translateY(-50%) rotate(180deg);
            .el-icon{
                display: none;
            }
        }
        :deep(.el-tabs__nav-wrap.is-scrollable){
            padding: 0 25px;
        }
    }

    .list-wrap{
        flex-wrap: wrap;
        .item{
            padding-top: 30px;
            padding-bottom: 30px;
            width: 50%;
            border-top: 1px solid #F2F2F2;
            border-bottom: 1px solid #F2F2F2;
            position: relative;
            .btn{
                position: absolute;
                top: 30px;
                right: 0;
                width: 100px;
                height: 28px;
                line-height: 28px;
                font-size: 14px;
                padding-top: 0;
                padding-bottom: 0;
            }
            .title{
                font-size: 20px;
            }
            .user-box{
                margin-top: 6px;
                margin-bottom: 20px;
                align-items: center;
                .avatar{
                    width: 40px;
                    height: 40px;
                    display: block;
                    object-fit: cover;
                    border-radius: 50%;
                    margin-right: 10px;
                    position: relative;
                    top: 3px;
                }
                .name{
                    margin-right: 20px;
                }
                .vip::after{
                    content: '';
                        width: 20px;
                        height: 20px;
                        display: inline-block;
                        background-image: url('@/assets/tag.png');
                        background-size: cover;
                        position: relative;
                        top:5px;
                        margin-left: 5px;
                }
                .stage{
                    font-size: 14px;
                    color: #F3A52F;
                    position: relative;
                    top: 2px;
                    background-color:#FFFBF5;
                    padding:4px 7px;
                }
            }
            .des{
                font-size: 14px;
                color: #666;
            }
        }
        .item:nth-child(odd){
            padding-right: 25px;
            border-right: 1px solid #F2F2F2;
            .btn{
                right: 25px;
            }
        }
        .item:nth-child(even){
            padding-left: 25px;
        }
    }
}
</style>