<script setup name="MyETASearchList">
import {ref,reactive} from 'vue'
import {apiMyETAChartSearch} from '@/api/myETA'
import { showToast } from 'vant'
import moment from 'moment'
import { useRouter } from 'vue-router'
import { useNoAuth } from '@/hooks/useNoAuth'

const router=useRouter()

const keyword=ref('')
const listState = reactive({
    list:[],
    page:1,
    pageSize:20,
    finished:false,
    loading:false
})
async function getList(){
    const res=await apiMyETAChartSearch({
        CurrentIndex:listState.page,
        PageSize:listState.pageSize,
        Keyword:keyword.value
    })
    if(res.Ret===200){
        listState.loading=false
        if(!res.Data){
            listState.finished=true
            return
        }
        
        listState.finished=res.Data.Paging.IsEnd
        const arr=res.Data.List||[]
        listState.list=[...listState.list,...arr]
    }
}
function onLoad(){
    listState.page++
    getList()
}

function handleSearch(){
    if(!keyword.value){
        showToast('请输入关键词')
        return
    }
    listState.page=1
    listState.list=[]
    listState.finished=false
    getList()
}

function goDetail(item){
    if(!item.HaveOperaAuth) return showToast(useNoAuth().chart)
    router.push({
        path:"/myETA/chartdetail",
        query:{
            code:item.UniqueCode,
            iscommon:true
        }
    })
}

</script>

<template>
    <div class="chart-search-list-page">
        <div class="search-box">
            <van-search 
                shape="round"
                placeholder="请输入图表名称"
                v-model="keyword"
                @search="handleSearch"
            />
        </div>
        <img v-if="listState.list.length==0&&listState.finished&&keyword" class="list-empty-img" src="https://hzstatic.hzinsights.com/static/ETA_mobile/empty_img.png" alt="">
        <van-list
            v-model:loading="listState.loading"
            :finished="listState.finished"
            :finished-text="listState.list.length>0?'没有更多了':'暂无图表'"
            :immediate-check="false"
            @load="onLoad"
        >
            <ul class="list-wrap">
                <li class="item" v-for="item in listState.list" :key="item.ChartInfoId" @click="goDetail(item)">
                    <div class="van-multi-ellipsis--l2 name">{{item.ChartName}}</div>
                    <img class="img" :src="!item.HaveOperaAuth?useNoAuth().noAuthImg:item.ChartImage" alt="">   
                    <div class="time">
                        <span>{{item.CreateTime.slice(0,10)}}</span>
                    </div>
                </li>
            </ul>
        </van-list>
    </div>
</template>

<style lang="scss" scoped>
.search-box{
    position: sticky;
    top: 0;
    background-color: #fff;
    z-index: 99;
}
.list-wrap{
    padding: $page-padding;
    display: flex;
    flex-wrap: wrap;
    box-sizing: border-box;
    justify-content: space-between;
    .item{
        box-sizing: border-box;
        width: 326px;
        padding: 10px 14px;
        border: 1px solid $border-color;
        margin-bottom: 20px;
        border-radius: 12px;
        .name{
            min-height: 70px;
        }
        .img{
            width: 100%;
            height: 220px;
            display: block;
            margin: 10px 0;
        }
        .time{
            font-size: 28px;
            color: $font-grey_999;
            position: relative;     
        }
    }
}
@media screen and (min-width:$media-width){
    .search-box{
        top: 60px;
    }
    .list-wrap{
        padding: var(--van-search-padding);
        justify-content: flex-start;
        .item{
            width: 326px;
            padding: 5px 7px;
            margin-bottom: 10px;
            border-radius: 6px;
            margin-left: 5px;
            margin-right: 5px;
            .name{
                min-height: 35px;
            }
            .img{
                height: 220px;
                margin: 5px 0;
            }
            .time{
                font-size: 14px;
            }
        }
        
    }
}
</style>