<script setup>
import {reactive, ref,watch} from 'vue'
import apiSheetChart from '@/api/sheet'
import { vInfiniteScroll } from '@vueuse/components'
import { showToast } from 'vant'
import { useNoAuth } from '@/hooks/useNoAuth'
import ChildSheet from './ChildSheet.vue'


const emits=defineEmits(['update','insert'])

const searchVal=ref('')
const onlyMe=ref(false)
const listState=reactive({
    page:1,
    pageSize:20,
    list:[],
    finished:false,
    loading:false
})
async function getSheetList(){
    const params={
        Keyword:searchVal.value,
        CurrentIndex:listState.page,
        PageSize:listState.pageSize,
        Source:5,
        IsShowMe:onlyMe.value
    }
    listState.loading=true
    let res=await apiSheetChart.sheetList(params)
    listState.loading=false
    if(res.Ret===200){
        const arr=res.Data.List||[]
        listState.list=[...listState.list,...arr]
        listState.finished=res.Data?.Paging.IsEnd
    }
}
getSheetList()
// 触底加载更多
function onLoadMore(){
    if(listState.finished||listState.loading) return
    listState.page++
    getSheetList()
}
function handleRefreshList(){
    listState.list=[]
    listState.page=1
    listState.finished=false
    getSheetList()
}

const selectChartList=ref([])
const showChildSheet=ref(false)
const activeSheetData=ref(null)
function handleSelect(item){
    if(!item.HaveOperaAuth) return showToast(useNoAuth().sheet)
    activeSheetData.value=item
    showChildSheet.value=true    
}

function handleInsert(e){
    emits('update',e)
    emits('insert')
}
</script>

<template>
    <div class="sheet-insert-wrap">
        <div class="sticky-box">
            <van-search v-model="searchVal" shape="round" placeholder="请输入表格名称" @search="handleRefreshList" @clear="handleRefreshList" />
            <van-checkbox v-model="onlyMe" @change="handleRefreshList">只看我的</van-checkbox>
        </div>
        
        <div class="content-box">
            <div v-if="listState.list.length==0&&listState.finished">
                <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">暂无表格</p>
            </div>
            
            <ul class="chart-list" v-infinite-scroll="[onLoadMore, { 'distance' : 10 }]">
                <li 
                    :class="['chart-item',selectChartList.includes(item.UniqueCode)&&'active']" 
                    v-for="item in listState.list" 
                    :key="item.UniqueCode"
                    @click="handleSelect(item)"
                >
                    <div class="van-multi-ellipsis--l2 title">{{item.ExcelName}}</div>
                    <img :src="!item.HaveOperaAuth?useNoAuth().noAuthImg:item.ExcelImage" alt="">
                </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>
    <!-- 子平衡表 -->
    <ChildSheet v-model:show="showChildSheet" :sheetData="activeSheetData" @insert="handleInsert"/>
  
</template>

<style lang="scss" scoped>
.sheet-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;
        align-content: flex-start;
        .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;
            .title{
                font-size: 28px;
                min-height: 60px;
            }
            img{
                width: 100%;
                height: 220px;
                object-fit: contain;
            }
        }
        .active{
            border-color: $theme-color;
            svg{
                width: 28px;
                height: 28px;
                position: absolute;
                right: 22px;
                bottom: 22px;
            }
        }
    }
}
.sticky-box{
    flex-shrink: 0;
    background-color: #fff;
    .van-tabs{
        border-bottom: 1px solid $border-color;
    }
    .van-checkbox{
        margin: var(--van-padding-sm);
    }
}
@media screen and (min-width:$media-width){
    .sheet-insert-wrap{
        .chart-list{
            height: auto;
            max-height: 100%;
            .chart-item{
                width: 260px;
                border-width: 1px;
                border-radius: 2px;
                margin-bottom: 15px;
                padding: 7px;
                .title{
                    font-size: 14px;
                    min-height: 30px;
                }
                img{
                    height: 150px;
                }
            }
            .active{
                svg{
                    width: 14px;
                    height: 14px;
                    right: 11px;
                    bottom: 11px;
                }
            }
        }
    }
}
</style>