<template>
    <nav-bar :navbar-data="navbarData"></nav-bar>
    <view class="more-doctor" :style="{ paddingTop: height +'px', height: '100vh', boxSizing: 'border-box' }">
        <!-- <view class="more-doctor-search">
            <van-search
                placeholder="搜索医生" 
                bind:search="onSearch"
                background="#f5f4f4"
            />
        </view> -->
        <van-dropdown-menu class="dropdown-menu">
            <van-dropdown-item :title="locDesc?locDesc:'全部科室'" ref="LocList">
                <van-tree-select :items="locList" :main-active-index="mainActiveIndex" :active-id="locID" @click-nav="onClickNav" @click-item="onClickItem" />
            </van-dropdown-item>
        </van-dropdown-menu>
        <scroll-view class="more-doctor-content" :style="{ height: 'calc(100vh - '+ height +'px + 100rpx)'}" scroll-y="true" @scrolltolower="scrolltolower" :scroll-into-view="scrollSta" scroll-with-animation>
            <view v-for="(item,index) in docList" :key="index">
                <doctorInfo :doctorInfo="item" path="../../pages/doctorInfo/doctorInfo" />
            </view>
            <van-divider contentPosition="center">{{ scrollShow ? '数据已加载完成' : '资源加载中。。。' }}</van-divider>
        </scroll-view>
    </view>
</template>
<script>
import $http from "@/utils/https"
import Util from "@/utils/utils"
import navBar from '@/components/navBar/index'
import VanDropdownMenu from '@/components/dropdown-menu/index'
import VanDropdownItem from '@/components/dropdown-item/index'
import VanTreeSelect from '@/components/tree-select/index.vue'
import VanDivider from '@/components/divider/index'
import doctorInfo from '../components/doctorInfo/doctorInfo'
const app = getApp()
export default {
    components: {
        'nav-bar': navBar,
        'van-tree-select': VanTreeSelect,
        'van-dropdown-menu': VanDropdownMenu,
        'van-dropdown-item': VanDropdownItem,
        'van-divider': VanDivider,
        'doctorInfo': doctorInfo
    },
    data() {
        return {
            height: app.globalData.height,
            navbarData: {
                isGoHome: true,
                isGoBack: true,
                title: "选择医生"
            },
            userData: null,
            docList: [],
            searchDocList: [],
            currentPage: 0,
            selectVal: "",
            scrollSta: "",
            scrollShow: true,
            unShowDocNum: 0,
            locID: '',
            mainActiveIndex: 0,
            locDesc: "",
            hisHospID: null,
            locList: [],
            beforeLocID: ''
        }
    },
    onLoad(options) {
        const userData = this.$cache.get('userData')
        if (userData) {
            this.userData = userData
            this.hisHospID = userData.hisHospID
        }

        app.globalData.writeObj.fileList = [];
        app.globalData.writeObj.disesNote = "";
        app.globalData.writeObj.schTimeKey = "";
        app.globalData.writeObj.schDateKey = "";
        app.globalData.patObj = {};
        this.getLocList();
        if (options && options.locID) {
            this.setData({
                locID: options.locID,
                locDesc: options.locDesc
            }, () => {
                this.getRecommendedDoctor();
            })
        } else {
            this.getRecommendedDoctor();
        }
    },
    onShow: function () {
        app.globalData.writeObj.fileList = [];
        app.globalData.writeObj.disesNote = "";
        app.globalData.writeObj.schTimeKey = "";
        app.globalData.writeObj.schDateKey = "";
        //this.getRecommendedDoctor();
    },
    methods: {
        onClickNav({
            detail = {}
        }) {
            this.setData({
                mainActiveIndex: detail.index || 0
            });
        },

        onConfirm() {
            this.refs.LocList.toggle();
        },

        ///变更科室
        onClickItem(e) {
            console.log(e)
            this.setData({
                locID: detail.id,
                locDesc: detail.text
            }, () => {
                this.getRecommendedDoctor();
                this.onConfirm();
            });

        },
        ///调接口获取科室列表
        getLocList() {
            let data = {
                params: [{
                    hospCode: this.hisHospID,
                    imageFlag: "Y",
                    videoFlag: "Y"
                }]
            }
            $http.post("urlS", this, {
                namespace: "Y",
                code: "03100037",
                data: data,
                success: function (res) {
                    console.log('科室列表',res)
                    if (+res.errorCode === 0) {
                        var allLoc = [{
                            id: '',
                            text: "全部科室",
                            code: "全部科室",
                            children: [{
                                id: '',
                                text: "全部科室",
                                code: "全部科室"
                            }]
                        }]
                        console.log(allLoc.concat(res.result.data))
                        this.setData({
                            locList: allLoc.concat(res.result.data)
                        });
                    } else {
                        this.setData({
                            locList: []
                        });
                    }
                }
            });
        },

        // 获取推荐医生数据
        getRecommendedDoctor: Util.debounce(function () {
            let userData = this.$cache.get('userData');
            let data = {
                params: [{
                    patID: userData.hisPatID || "",
                    locID: this.locID || "",
                    patNo: userData.hisPatNo || "",
                    hospCode: userData.hisHospID || "",
                    docDesc: this.selectVal ? this.selectVal : ''
                }],
                pagination: [{
                    pageSize: 10,
                    currentPage: this.beforeLocID == this.locID ? (this.currentPage + 1) : 1
                }]
            }
            uni.showLoading({
                title: '数据加载中...',
                mask: true
            })
            $http.post("urlS", this, {
                code: "03100042",
                data: data,
                namespace: "Y",
                success: function (res) {
                    this.setData({
                        scrollShow: true
                    }, () => {
                        if (+res.errorCode === 0) {
                            if (this.beforeLocID == this.locID) {
                                let recommendedDoctor = res.result.data;
                                this.setData({
                                    docList: this.docList.concat(recommendedDoctor),
                                    currentPage: this.currentPage + 1,
                                    unShowDocNum: res.result.surplusDocNum,
                                    beforeLocID: this.locID
                                })
                            } else {
                                this.setData({
                                    docList: res.result.data,
                                    currentPage: 1,
                                    unShowDocNum: res.result.surplusDocNum,
                                    beforeLocID: this.locID
                                })
                            }
                        } else {
                            this.setData({
                                docList: []
                            })
                        }
                    })
                    uni.hideLoading();
                }
            });
        }),

        onSearch(event) {
            this.setData({
                selectVal: event.detail,
                docList: [],
                currentPage: 1
            }, () => {
                this.getRecommendedDoctor()
            })
        },

        scrolltolower() {
            if(this.unShowDocNum <= 0) return
            this.setData({
                scrollSta: 'd' + (this.docList.length - 1),
                scrollShow: false
            }, () => {
                this.getRecommendedDoctor();
            })
        },
    }
}
</script>