<template>
    <div class="flex flex-col guide-list" style="height: 100vh">
        <div class="p-3 text-16 text-black text-center shrink-0 doc-nav-bar" v-if='showNav()'>
            <span>健康指导</span>
        </div>
        <van-tabs v-model:active='activeTab' class='shrink-0' v-if='tabList.length'
                @change='tabChange'
                shrink>
            <van-tab v-for='item in tabList' :key='item.name'
                :title='item.title' :name='item.name'></van-tab>
        </van-tabs>
        <div class='grow overflow-y-auto' ref='list'>
            <van-pull-refresh v-model='loadingRefresh' @refresh='onRefresh'
                :disabled='isRefreshDisable'
                style="min-height: 100%">
                <van-list
                    v-model:loading='loading'
                    :finished='finished'
                    :finished-text="list.length ? '没有更多了' : ''"
                    :immediate-check='false'
                    @load='onMore'
                >
                    <div class="p-2 flex flex-col gap-y-2.5 card-list">
                        <div class="py-3 px-4 card" v-for='item in list' :key="item.id"
                            @click="toDetail(item)">
                            <div>
                                <span class="label">日期</span>
                                <span>{{ item.visitDate }}</span>
                            </div>
                            <div>
                                <span class="label">筛查单位</span>
                                <span>{{ item.visitUnitName }}</span>
                            </div>
                            <div>
                                <span class="label">筛查科室</span>
                                <span>{{ item.visitOfficeName }}</span>
                            </div>
                            <span class="tag tag-orange" v-show="item.visitWayRules.includes('2')">健康指导</span>
                            <span class="tag tag-green" v-show="item.visitWayRules.includes('3')">健康宣教</span>
                        </div>
                    </div>
                </van-list>
                <div class='text-center shrink-0 empty' v-if='!list.length'>
                    <img src='@/assets/image/doctor/empty.png' alt='' style='width: 1.2rem;'>
                    <p>暂无数据</p>
                </div>
            </van-pull-refresh>
        </div>
    </div>
</template>

<script>
import { queryGuideList } from '@/api/residentWX/guide.js'
import { useStore } from '@/residentWX/store'

export default {
    inject: ['showNav'],
    data() {
        return {
            store: useStore(),
            activeTab: '',
            tabList: [
                { title: '全部', name: '' },
                { title: '健康指导', name: '2' },
                { title: '健康宣教', name: '3' },
            ],
            list: [],
            pagination: {
                total: 0,
                pageIndex: 1,
                pageSize: 8
            },
            loading: false,
            finished: false,
            loadingRefresh: false,
            isRefreshDisable: false
        }
    },
    computed: {
        userInfo() {
            return this.store.userInfo
        },
    },
    created() {
        this.init()
    },
    mounted() {
        const list = this.$refs.list
        list.addEventListener('scroll', () => {
            if (list.scrollTop > 0) {
                this.isRefreshDisable = true
            } else {
                this.isRefreshDisable = false
            }
        })
    },
    methods: {
        init() {
            this.load()
        },
        load(loading = true) {
            const query = {
                residentInfoId: this.userInfo.residentInfoId,
                pageIndex: this.pagination.pageIndex,
                pageSize: this.pagination.pageSize,
                visitWayRulesList: [2,3],
            }
            if (this.activeTab) {
                query.visitWayRulesList = [this.activeTab]
            }
            
            queryGuideList(query, loading).then(res => {
                if (this.pagination.pageIndex === 1) {
                    this.list = []
                } 
                this.list = this.list.concat(res.data.dataList || [])
                this.pagination.total = res.data.total || 0
                this.finished = this.list.length >= this.pagination.total
            }).finally(() => {
                this.loading = false
                this.loadingRefresh = false
            })
        },
        onMore() {
            this.pagination.pageIndex++
            this.load()
        },
        onRefresh() {
            this.pagination.pageIndex = 1
            this.load(false)
        },
        tabChange() {
            this.pagination.pageIndex = 1
            this.load()
        },
        toDetail(record) {
            if (!record) return
            let path = `/residentWX/guide/list/${record.id}`
            this.$router.push({ path })
        }
    }
}
</script>

<style lang="less" scoped>
@import url('../utils/common.less');

:deep(.van-tab__text) {
    font-weight: 500;
}

.guide-list {
    background-color: #f9f9f9;
}
.card-list {
    padding-bottom: .3rem;
}
.card {
    position: relative;
    background-color: #fff;
    border-radius: 12px;
    >div {
        position: relative;
        display: flex;
        margin-bottom: 8px;
        &:last-child {
            margin-bottom: 0;
        }
    }
    .label {
        min-width: 6em;
        color: #8C8C8C;
    }
}
.tag {
    position: absolute;
    top: 8px;
    right: 8px;
    border-radius: 2px;
    padding: 4px 8px;
    font-size: 12px;
}
.tag-orange {
    border: 1px solid #FFD591;
    color: #FA8C16;
    background-color: #FFF7E6;
}
.tag-green {
    border: 1px solid #B7EB8F;
    color: #52C41A;
    background-color: #F6FFED;
}
</style>
