<template>
    <div class='h-full flex flex-col visit'>
        <div class="p-3 text-black text-center shrink-0 doc-nav-bar">
            <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 card-list' ref='list'>
            <van-pull-refresh v-model='loadingRefresh' @refresh='onRefresh'
                              :disabled='isRefreshDisable'>
                <van-list
                    v-model:loading='loading'
                    :finished='finished'
                    :finished-text="list.length ? '没有更多了' : ''"
                    :immediate-check='false'
                    @load='onMore'
                >
                    <div v-for='item in list' :key='item.id' class='p-4 mt-3 card' @click.stop='toDetail(item)'>
                        <div class='flex items-center'>
                            <div class='detail-left'>
                                慢病类型
                            </div>
                            <div class='detail-right'>
                                {{ item.diseaseTypeName }}
                            </div>
                        </div>
                        <div class='mt-3 flex items-center'>
                            <div class='detail-left'>
                                随访日期
                            </div>
                            <div class='detail-right'>
                                {{ item.serveDate }}
                            </div>
                        </div>
                        <div class='mt-3 flex items-center'>
                            <div class='detail-left'>
                                下次随访日期
                            </div>
                            <div class='detail-right'>
                                {{ item.nextVisitDate || '-' }}
                            </div>
                        </div>
                        <div class='mt-3 flex items-center'>
                            <div class='detail-left'>
                                随访医生
                            </div>
                            <div class='detail-right'>
                                {{ item.serveDoctorName }}
                            </div>
                        </div>
                        <div class='mt-3 flex items-center'>
                            <div class='detail-left'>
                                随访方式
                            </div>
                            <div class='detail-right'>
                                {{ item.serveTypeName }}
                            </div>
                        </div>
                        <div class='mt-3 flex items-center'>
                            <div class='detail-left'>
                                随访机构
                            </div>
                            <div class='detail-right'>
                                {{ item.serveUnitName }}
                            </div>
                        </div>

                        <van-divider class='mt-3' />

                        <span class="tag tag-red" v-show="item.serveType == '1'">高危筛查</span>
                        <span class="tag tag-blue" v-show="item.serveType == '2'">专病筛查</span>
                    </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 class='p-3 floor' v-if='activeTab'>
            <van-button round block type='primary' class='card-bt' @click='addBtn'>新增通用随访</van-button>
        </div>
    </div>
</template>

<script>
import {  deleteVisit } from '@/api/doctor/generalFU.js'
import { showConfirmDialog, showToast } from 'vant'
import { useStore } from '@/residentWX/store'
import { queryVisitList } from '@/api/residentWX/visit'

export default {
    data() {
        return {
            store: useStore(),
            activeTab: undefined,
            list: [],
            pagination: {
                total: 0,
                pageIndex: 1,
                pageSize: 4
            },
            loading: false,
            finished: false,
            loadingRefresh: false,
            isRefreshDisable: false
        }
    },
    computed: {
        baseInfo() {
            return this.store.userInfo
        },
        chronicTagsArray() {
            let chronicTagsArray = this.baseInfo.chronicTagsArray || ''
            return chronicTagsArray.split(',')
        },
        tabList() {
            const list = [
                { title: '全部', name: undefined, code: undefined },
                { title: '高血压', name: 1, code: '1' },
                { title: '糖尿病', name: 2, code: '2' },
                { title: '冠心病', name: 3, code: '4' },
                { title: '脑卒中', name: 4, code: '8' },
                { title: '慢性阻塞性疾病', name: 5, code: '16' },
                { title: '慢性肾病', name: 6, code: '32' },
                { title: '血脂异常', name: 7, code: '64' }
            ]
            let res = list.filter(e => this.chronicTagsArray.includes(e.code)) || []
             res.unshift( { title: '全部', name: '', code: '' })
            return res
        }
    },
    mounted() {
        const list = this.$refs.list
        list.addEventListener('scroll', () => {
            if (list.scrollTop > 0) {
                this.isRefreshDisable = true
            } else {
                this.isRefreshDisable = false
            }
        })
    },
    methods: {
        load(loading = true) {
            debugger
            const query = {
                residentInfoId: this.baseInfo.residentInfoId,
                diseaseType: this.activeTab,
                pageIndex: this.pagination.pageIndex,
                pageSize: this.pagination.pageSize
            }
            queryVisitList(query, loading).then(res => {
                debugger
                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()
        },
        init() {
            if (this.chronicTagsArray.length) {
                const item = this.tabList.find(e => e.code === this.chronicTagsArray[0]) || {}
                this.activeTab = item.name
            }
        },
        tabChange() {
            this.list = []
            this.pagination.pageIndex = 1
            this.load()
        },
        onRefresh() {
            this.list = []
            this.pagination.pageIndex = 1
            this.load(false)
        },
        addBtn() {
            if (this.baseInfo.chronicStatus === 9) {
                showToast('该居民已标记为死亡，无法新增随访')
                return
            }
            let par = {
                residentInfoId: this.baseInfo.residentInfoId,
                diseaseType: this.activeTab
            }
            this.$router.push({
                path: `/doctor/followUp/generalFU/add`,
                query: par
            })
        },
        toDetail(item) {
            let par = {
                relationId: item.relationId
            }
            this.$router.push({
                path: `/doctor/followUp/generalFU/detail`,
                query: par
            })
        },
        editBtn(item) {
            let par = {
                id: item.relationId,
                residentInfoId: this.baseInfo.residentInfoId
            }
            this.$router.push({
                path: `/doctor/followUp/generalFU/add`,
                query: par
            })
        },
        delBtn(item) {
            showConfirmDialog({
                title: '提示',
                message: '确认删除该随访记录吗？\n确认后将无法查看该随访信息'
            }).then(() => {
                // on confirm
                // console.log(item.id)
                deleteVisit({ id: item.id }).then(res => {
                    this.load()
                })
            })
                .catch(() => {
                    // on cancel
                })
        }
    }
}
</script>

<style lang='less' scoped>
.card-list {
    padding-bottom: 76px;
}

.card {
    position: relative;
    background-color: #fff;

    .detail-left {
        width: 8em;
        color: #8C8C8C;
        flex-shrink: 0;
    }

    .card-bt {
        font-size: 14px;
        color: #607FF0;
        background-color: #F0F3FF;
        border: 0;
        height: 26px;
    }

    .float-bt {
        position: absolute;
        top: 10px;
        right: 0;
        background-color: var(--van-primary-color);
        color: #fff;
        border-top-left-radius: 40px;
        border-bottom-left-radius: 40px;
        height: 26px;
        line-height: 26px;
    }
}

.floor {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
}

.tag {
    position: absolute;
    top: 8px;
    right: 8px;
    border-radius: 2px;
    padding: 4px 8px;
    font-size: 12px;
}
.tag-red {
    border: 1px solid #FFA39E;
    color: #F5222D;
    background-color: #FFF1F0;
}
.tag-blue {
    border: 1px solid #ADC6FF;
    color: #2F54EB;
    background-color: #F0F5FF;
}
</style>
