<script setup>
import { onActivated, reactive,ref } from 'vue'
import Search from '@/components/Search.vue'
import SelfList from '@/components/SelfList.vue'
import SortClassify from './components/SortClassify.vue'
import {apiMyChartList,apiMyChartClassifyList} from '@/api/myChart'
import { useRouter } from 'vue-router'

const router=useRouter()


let classifyOpts=ref([])//我的分类数据
async function getMyClassifyOpt(){
    const res=await apiMyChartClassifyList()
    if(res.code===200){
        classifyOpts.value=res.data||[]
    }
}
getMyClassifyOpt()

// 分类选择变化
function classifyChange(){
    listState.page=1
    listState.finished=false
    listState.list=[]
    getChartList()
}

let listState=reactive({
    loading:false,
    finished:false,
    page:1,
    pageSize:20,
    list:[],
    sClassifyId:'',

})
// 获取列表数据
async function getChartList(){
    listState.loading=true
    const res=await apiMyChartList({
        keyword:'',
        classify_id:listState.sClassifyId,
        curr_page:listState.page,
        page_size:listState.pageSize
    })
    listState.loading=false
    if(res.code===200){
        const arr=res.data.list||[]
        listState.list=[...listState.list,...arr]
        listState.finished=res.data.paging.is_end
    }
}
getChartList()

// 加载更多
function onLoad(){
    listState.page++
    getChartList()
}

//查看详情
function goDetail(item){
    router.push({
        path:'/mychart/detail',
        query:{
            chartInfoId:item.chart_info_id,
            chartSource: item.chart_info_source,
            chartCode:item.unique_code,
            classifyId:listState.sClassifyId
        }
    })
}

// onActivated(()=>{
//     getMyClassifyOpt()
// })


</script>


<template>
    <div class="mychart-list-page">
        <div class="flex top-wrap">
            <div class="flex">
            <Search @click="$router.push('/mychart/search')" :disabled="true" placeholder="关键词搜索" style="background-color:#fff"/>
            <el-select 
                v-model="listState.sClassifyId" 
                class="select-box" 
                placeholder="选择分类" 
                clearable
                size="large"
                @change="classifyChange"
            >
                <el-option
                    v-for="item in classifyOpts"
                    :key="item.my_chart_classify_id"
                    :label="item.my_chart_classify_name"
                    :value="item.my_chart_classify_id"
                />
            </el-select>
            </div>
            <SortClassify @change="getMyClassifyOpt"/>
                
        </div>
        <SelfList 
            :finished="listState.finished" 
            :isEmpty="listState.list.length===0&&listState.finished"
            :loading="listState.loading"
            :count="listState.list.length"
            emptyMsg="暂无图表收藏的信息"
            @listOnload="onLoad"
        >
            <ul class="flex list-wrap">
                <li class="item" v-for="item in listState.list" :key="item.chart_info_id" @click="goDetail(item)">
                    <div class="multi-ellipsis-l2 title">{{item.chart_name}}</div>
                    <img class="img" :src="item.chart_image" alt="">
                </li>
                <li class="last-add-item"></li>
                <li class="last-add-item"></li>
                <li class="last-add-item"></li>
            </ul>
        </SelfList>
    </div>
</template>


<style lang="scss" scoped>

.mychart-list-page{
    .top-wrap{
        position: sticky;
        top: 60px;
        z-index: 10;
        padding-top: 20px;
        padding-bottom: 20px;
        margin-top: -20px;
        background-color: #fff;
        justify-content: space-between;
    }
    .select-box{
        width: 228px;
        margin-left: 30px;
        :deep(.el-input__wrapper){
            border-radius: 40px;
        }
    }


    .list-wrap{
        flex-wrap: wrap;
        justify-content: center;
        .last-add-item{
            height:0;
            flex-shrink: 0;
            width: 285px;
            margin-left: 10px;
            margin-right: 10px;
        }
        .item{
            flex-shrink: 0;
            width: 285px;
            background: #FFFFFF;
            border: 1px solid #EBEBEB;
            box-shadow: 0px 0px 12px rgba(167, 167, 167, 0.25);
            border-radius: 4px;
            padding: 10px;
            margin-bottom: 20px;
            margin-left: 10px;
            margin-right: 10px;
            overflow: hidden;
            cursor: pointer;
            .title{
                margin-bottom: 20px;
            }
            .img{
                width: 100%;
                height: 229px;
                object-fit: contain;
            }
        }
    }
}
</style>