<template>
	<view class="doctor-index-content">
		<view class="tab">
			<view class="item" @click="changeTab(1)" :class="tabIndex===1?'active':''">
				<text>待诊断</text>
			</view>
			<view class="item" @click="changeTab(2)" :class="tabIndex===2?'active':''">
				<text>已诊断</text>
			</view>		
		</view>
		<view class="search">
			<view class="inp">
				<text class="zhcx-iconfont zhcx-icon-sousuo"></text>
				<input type="text" v-model="keyword"  placeholder="请输入关键字"/>			
			</view>
			<view class="searchBt">
				<button type="primary" @click="search">搜索</button>
			</view>
			<view class="filterBt"  @click="filterHandle">
				<text>筛选</text>
				<text class="zhcx-iconfont " :class="showMorefilter?'zhcx-icon-xiangshang':'zhcx-icon-xiangxia'"></text>
			</view>		
		</view>
		<view class="more-filter" v-if="showMorefilter">
			<view class="illnessTypes">
			   <picker @change="changeIllnessType" :value="illnessTypeIndex" :range="illnessTypes">
					<view class="uni-input">{{illnessTypeIndex>-1?illnessTypes[illnessTypeIndex]:"请选择病症类型"}}</view>
				</picker>
			</view>		
		</view>		
		<view class="tab-cont">
			<view class="item-container">
				<roll-load
					:total="total"
					:size="pageSize"
					@lower="lower"	
					@upper="upper"
					ref="roll-load"
					:topBt="topBt"
					styles="height:calc(100vh - 200px)">	
					<template v-slot:cont>
						<block></block>
						<view class="item" v-for="(item,index) in items" 
							 :key="index" @click="showdetail(item.id)" > 
							<view class="avatar">
								<image src="../../../static/images/order/mine_location.png" mode="widthFix" class="pic"></image>
							</view>
							<view class="item-info">
								<view class="user">
									<text class="name">{{item.name}}</text>	
									<view class="gender" :class="item.gender==='1'?'woman':'man'">
										<text>{{item.gender==='1'?'女':"男"}}</text>
									</view>	
									<text class="age">{{item.age}}</text>	
								</view>
								<view class="illnessType">
									<text>{{item.illnessType}}</text>
								</view>
								<view class="time">
									<text>就诊时间：</text>
									<text>{{item.time}}</text>
								</view>
							</view>
						</view>
					</template>		
					<template v-slot:toTop>
						<text>顶部</text>
					</template>							
				</roll-load>			
			</view>		
		</view>
	</view>
</template>

<script>	
	import {formatTime} from '@/libs';
	import {deepClone} from '@/libs/index.js';
	
	import RollLoad from '@/components/RollLoad/index.vue';
	import {diagnosisList,ReceivedOrder} from '@/api/doctor';
	import { getIlltype } from '@/api/user.js'
	export default{
		name:"doctor-index",
		components:{
			RollLoad
		},	
		data() {
			return {
				showMorefilter:false,
				illnessTypeIndex:-1,
				illnessTypes:[],			
				keyword: '',
				tabIndex:1,
				items:[],
				detailModal:false,
				pageSize:10,
				pageNumber:1,
				total:0,
				topBt:{
					show:true,
					style:'top:80%'
				}			
			}
		},			
		created() {
			let accountInfo=uni.getStorageSync('accountInfo');
			this.userType=accountInfo.userType;
			if(accountInfo.userType===1){
				this.init();
			}
		},
		methods:{
			init(){
				this.getList()
				this.setIllnessType();
			},				
			changeTab(index){
				this.tabIndex=index;
				this.illnessTypeIndex=-1;
				this.getList()
			},
			search(){
				this.pageNumber=1;
				this.getList();
			},
			setIllnessType(){
				getIlltype().then((result) => {
					let illnessTypes=result.data;
					illnessTypes=illnessTypes.map((item)=>{
						return item.typename
					})
					illnessTypes.unshift('全部病症类型');
					this.illnessTypes = illnessTypes;
				})			
			},	
			filterHandle(){
				this.showMorefilter=!this.showMorefilter;
			},			
			getList(){
					let status=(this.tabIndex)-1;
					let keyword=this.keyword;
					// let startTime=formatTime(this.time[0]);
					// let endTime=formatTime(this.time[1]); 
					let page= this.pageNumber;   
					let limit=this.pageSize;   
					let illnessTypeIndex=this.illnessTypeIndex;
					let illnessType=this.illnessTypes[illnessTypeIndex];
					if(illnessTypeIndex<1){
						illnessType="";
					}				
					this.$nextTick(()=>{
						this.$refs['roll-load'].init();  
					})	
					const maxAge = 60;
					const minAge = 20;
					var dictionaries={};									  
					diagnosisList({
						status,keyword,page,limit,illnessType
					}).then((res)=>{
						let age="";
						let items=res.data.list.map((item)=>{
							if(dictionaries.hasOwnProperty(item.patientName)){
								age=dictionaries[item.patientName]
							}else{
								age=dictionaries[item.patientName]=Math.floor(Math.random()*(maxAge-minAge+1)+minAge);
							}	
							return{
								id:item.seekId,
								name:item.patientName,
								time:item.seekTime,
								gender:item.patientSex,
								illnessType:item.illnessType,
								age	
							}
						})
						let total=res.data.totalCount;
						this.total=total;
						this.items=items;
					})
			},
			showdetail(id){
				let status=this.tabIndex;
				uni.navigateTo({
					url:`/views/detail/index?type=diagnose&user=doctor&id=${id}&status=${status}`,
					animationType: 'pop-in',
					animationDuration: 200				
				})
			},
			getItem(){
				let status=(this.tabIndex)-1;
				let keyword=this.keyword;
				let page= this.pageNumber;   
				let limit=this.pageSize;         
				return new Promise((resolve,reject)=>{
					diagnosisList({
						status,keyword,page,limit
					}).then((res)=>{
						let total=res.data.totalCount;
						let items=res.data.list.map((item)=>{
							let site=[];
							return{
								id:item.seekId,
								name:item.patientName,
								time:item.seekTime,
								desc:item.diseaseDesc,
								tags:site,
							}
						})
						this.total=total;
						resolve(items)
					}).catch((err)=>{reject(err);})				
				})
			}, 
			async lower(e){
				let items=deepClone(this.items);
				if(e.status){
					let data=await this.getItem();
					this.items=items.concat(data);
				}				
				console.log('滚动到底部------：'+JSON.stringify(e));
			},
			upper(e){
				//console.log('滚动到顶部:-----'+JSON.stringify(e));
			},	
			changeIllnessType({detail}){
				this.illnessTypeIndex=detail.value;
				this.search();
			}				
		}
	}
</script>

<style lang="scss" scoped>
	@import url('./index.css')
</style>
