<script setup>
import {reactive,ref,watch} from 'vue'
import apiMyETAChart from '@/api/myETA'
import { vInfiniteScroll } from '@vueuse/components'
import { useNoAuth } from '@/hooks/useNoAuth'

const emits=defineEmits(['update'])

// 获取我的图库中分类
let showClassify=ref(false)
let classifyList=[]
let activeClassify=ref('')
let activeClassifyName=ref('')
let curClassifyList=ref([])
async function getClassifyList(){
    const res=await apiMyETAChart.myClassifyList()
    if(res.Ret===200){
        classifyList=res.Data?.List??[]
        curClassifyList.value=classifyList
    }
}
getClassifyList()

let searchVal=ref('')
function handleRefreshList(){
    activeClassify=ref('')
    curClassifyList.value=classifyList.filter(item=>item.MyChartClassifyName.includes(searchVal.value))
}

function handleSave(){
    activeClassifyName.value=classifyList.filter(item=>item.MyChartClassifyId==activeClassify.value)[0]?.MyChartClassifyName
    showClassify.value=false
    getChartList()
}

const listState=reactive({
    page:1,
    pageSize:10000,
    list:[],
    finished:false,
    loading:false
})
async function getChartList(){
    const params={
        CurrentIndex:listState.page,
        PageSize:listState.pageSize,
        MyChartClassifyId:activeClassify.value
    }
    listState.loading=true
    let res=await apiMyETAChart.myChartList(params)
    listState.loading=false
    if(res.Ret===200){
        const arr=res.Data.List||[]
        listState.list=[...listState.list,...arr]
        listState.finished=res.Data?.Paging.IsEnd
    }
}
// 触底加载更多
function onLoadMore(){
    if(listState.finished||listState.loading) return
    listState.page++
    // getChartList()
}

watch(
    ()=>listState.list,
    ()=>{
        emits('update',listState.list.map(item=>item.UniqueCode))
    },
    {
        immediate:true
    }
)

</script>

<template>
    <div class="myETA-insert-wrap">
        <van-cell style="flex-shrink:0" title="分类" :value="activeClassifyName" is-link @click="showClassify=true"/>
        <div class="content-box">
            <div v-if="listState.list.length==0">
                <img class="list-empty-img" src="https://hzstatic.hzinsights.com/static/ETA_mobile/empty_img.png" alt="">
                <p style="text-align:center;color:#999999;font-size:12px">{{listState.finished?'暂无图表':'暂无图表，请先选择分类'}}</p>
            </div>
            
            <ul class="chart-list" v-infinite-scroll="[onLoadMore, { 'distance' : 10 }]">
                <li 
                    :class="['chart-item',{'active':item.HaveOperaAuth}]" 
                    v-for="item in listState.list" 
                    :key="item.ChartInfoId"
                >
                    <div class="van-multi-ellipsis--l2 title">{{item.ChartName}}</div>
                    <img :src="!item.HaveOperaAuth?useNoAuth().noAuthImg:item.ChartImage" alt="">
                    <svg width="29" height="28" viewBox="0 0 29 28" fill="none" xmlns="http://www.w3.org/2000/svg" v-if="item.HaveOperaAuth">
                        <path d="M14.5 28C22.232 28 28.5 21.732 28.5 14C28.5 6.26801 22.232 0 14.5 0C6.76801 0 0.5 6.26801 0.5 14C0.5 21.732 6.76801 28 14.5 28ZM7.5 14.413L8.913 13L12.5 16.586L20.085 9L21.5 10.415L12.5 19.414L7.5 14.413Z" fill="#0052D9"/>
                    </svg>
                </li>
                <li class="chart-item" style="height:0;border:none;margin-bottom:0;padding:0"></li>
                <li class="chart-item" style="height:0;border:none;margin-bottom:0;padding:0"></li>
            </ul>
        </div>
    </div>
    <!-- 选择分类 -->
    <van-popup
        v-model:show="showClassify"
        position="bottom"
        teleport="body"
        :style="{ height: '100%' }"
    >
        <div class="select-classify-wrap">
            <van-search v-model="searchVal" shape="round" placeholder="请输入分类名称" @search="handleRefreshList" @clear="handleRefreshList" />
            <ul class="list">
                <van-radio-group v-model="activeClassify">
                    <van-radio :name="item.MyChartClassifyId" v-for="item in curClassifyList" :key="item.MyChartClassifyId">{{item.MyChartClassifyName}}</van-radio>
                </van-radio-group>
            </ul>
            <div class="btns">
                <van-button @click="showClassify=false">取消</van-button>
                <van-button type="primary" :disabled="activeClassify==''" @click="handleSave">保存</van-button>
            </div>
        </div>
    </van-popup>
</template>

<style lang="scss" scoped>
.myETA-insert-wrap{
    height: 100%;
    display: flex;
    flex-direction: column;
    .content-box{
        overflow: hidden;
        flex: 1;
        padding: var(--van-padding-sm);
        min-height: 300PX;
    }
    .chart-list{
        height: 100%;
        overflow-y: auto;
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
        .chart-item{
            width: 48%;
            background: #FFFFFF;
            border: 3px solid $border-color;
            box-shadow: 0px 0px 12px rgba(0, 0, 0, 0.03);
            border-radius: 4px;
            margin-bottom: 30px;
            overflow: hidden;
            padding: 14px;
            box-sizing: border-box;
            position: relative;
            max-height: 336px;
            .title{
                font-size: 28px;
                min-height: 60px;
            }
            img{
                width: 100%;
            }
        }
        .active{
            border-color: $theme-color;
            svg{
                width: 28px;
                height: 28px;
                position: absolute;
                right: 22px;
                bottom: 22px;
            }
        }
    }
}

.select-classify-wrap{
    height: 100%;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    .list{
        overflow-y: auto;
        flex: 1;
        padding: var(--van-padding-sm) 0 var(--van-padding-sm) var(--van-padding-sm);
        :deep(.van-radio__label){
            flex: 1;
            padding: 32px 0;
            border-bottom: 1px solid $border-color;
        }
    }
    .btns{
        text-align: center;
        padding: var(--van-padding-sm);
        display: flex;
        justify-content: space-around;
        .van-button{
            width: 315px;
        }
    }
}

@media screen and (min-width:$media-width){
    .myETA-insert-wrap{
        .chart-list{
            max-height: 100%;
            height: auto;
            .chart-item{
                width: 260px;
                border-width: 1px;
                border-radius: 2px;
                margin-bottom: 15px;
                padding: 7px;
                max-height: 220px;
                .title{
                    font-size: 14px;
                    min-height: 30px;
                }
            }
            .active{
                border-width: 1px;
                svg{
                    width: 14px;
                    height: 14px;
                    right: 11px;
                    bottom: 11px;
                }
            }
        }
    }
    .select-classify-wrap{
        .list{
            :deep(.van-radio__label){
                padding: 16px 0;
            }
        }
    }
}
</style>