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

const router=useRouter()

const currentLang = localStorage.getItem('chartETALange')||'CN'

const keyword=ref('')
const listState = reactive({
    list:[],
    page:1,
    pageSize:20,
    finished:false,
    loading:false
})
async function getList(){
    const res=await apiChart.ETAChartListByES({
        CurrentIndex:listState.page,
        PageSize:listState.pageSize,
        Keyword:keyword.value,
        IsShowMe:false
    })
    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:'/chartETA/chartdetail',
        query:{
            id:item.ChartInfoId,
            chartType:item.ChartType,
            chartClassifyId:0,
            keyword:keyword.value,
            DateType:item.DateType,
            StartDate:(item.DateType===5||item.DateType===6)?item.StartDate:'',
            EndDate:(item.DateType===5||item.DateType===6)?item.EndDate:'',
            Calendar:item.Calendar?item.Calendar:'公历',
            SeasonStartDate:item.SeasonStartDate,
            SeasonEndDate:item.SeasonEndDate,
            StartYear:item.StartYear
        }
    })
}

</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-ellipsis name">{{currentLang==='EN'?(item.ChartNameEn||item.ChartName):item.ChartName}}</div>
                    <img class="img" :src="!item.HaveOperaAuth?useNoAuth().noAuthImg:item.ChartImage" alt="">
                    <div class="time">
                        <span>{{moment(item.CreateTime).format('YYYY-MM-DD')}}</span>
                        <span>{{item.SysUserRealName}}</span>
                    </div>
                </li>
                <li class="item" style="height:0;padding:0;border:none"></li>
                <li class="item" style="height:0;padding:0;border:none"></li>
                <li class="item" style="height:0;padding:0;border:none"></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;
    justify-content: space-between;
    .item{
        width: 326px;
        padding: 14px;
        background: #FFFFFF;
        border: 1px solid $border-color;
        box-shadow: 0px 0px 12px rgba(0, 0, 0, 0.03);
        border-radius: 12px;
        box-sizing: border-box;
        margin-bottom: 30px;
        .img{
            margin: 14px 0;
            width: 100%;
            height: 220px;
        }
        .time{
            display: flex;
            justify-content: space-between;
            color: $font-grey_999;
            font-size: 28px;
        }
    }
}
@media screen and (min-width:$media-width){
    .search-box{
        top: 60px;
    }
    .list-wrap{
        padding: 20px 30px;
        justify-content: center;
        .item{
            width: 260px;
            padding: 14px;
            border-radius: 6px;
            margin-left: 8px;
            margin-right: 8px;
            margin-bottom: 15px;
            .img{
                margin: 14px 0;
                width: 100%;
                height: 200px;
            }
            .time{
                font-size: 14px;
                justify-content: flex-start;
                span{
                    margin-right: 20px;
                }
            }
        }
        
    }
}
</style>
