<template>
	<view class="content">
		
		<view class="tab">
			<view class="item" @click="changeTab(4)" :class="tabIndex===4?'active':''">
				<text>待接单</text>
			</view>		
			<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 class="item" @click="changeTab(3)" :class="tabIndex===3?'active':''">
				<text>已完成</text>
			</view>				
		</view>
		<view class="search">
			<view class="inp">
				<text class="zhcx-iconfont zhcx-icon-sousuo"></text>
				<input class="search-inp" type="text" v-model="keyword"  placeholder="请输入关键字"/>			
			</view>
			<view class="searchBt" @click="search">
				<button type="primary">搜索</button>
			</view>
		</view>
		<view class="tab-cont">
			<view class="item-container">
				<roll-load
					:total="total"
					:size="pageSize"
					@lower="lower"	
					:topBt="topBt"
					ref="roll-load"
					styles="height:calc(100vh - 180rpx)">	
						<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/doctor.jpg" mode="widthFix" class="pic"></image>
							</view>
							<view class="item-info">
								<view class="doctor">
									<view class="lf">
										<text class="name" v-if="tabIndex==4||tabIndex==1">Ai医生</text>
										<text class="name" v-if="item.doctor&&tabIndex!=4">{{item.doctor}}</text>
										<text class="status">线上</text>									
									</view>
									<text class="diagnose-status" v-if="tabIndex==2||tabIndex==3">已就诊</text>
								</view>
								<view class=" jobTitle-box" v-if="tabIndex==4">
									<text class="jobTitle">云影医通</text>
									<text class="department" >人工智能</text>									
								</view>		
								<view class=" jobTitle-box" v-if="tabIndex!=4&&!formateJobTitle(item)">
									<text class="jobTitle" v-if="item.doctorJobTitle">{{item.doctorJobTitle}}</text>
									<text class="department" v-if="item.doctorDepartment">{{item.doctorDepartment}}</text>
								</view>
								<view class="doctorHospital" v-if="item.doctorHospital">{{item.doctorHospital}}</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 RollLoad from '@/components/RollLoad/index.vue';
	import {deepClone,notEmpty} from '@/libs/index.js';
	import {recordList} from '@/api/patient';

	export default{
		name:"doctor-index",
		components:{
			RollLoad
		},		
		data() {
			return {
				keyword: '',
				tabIndex:4,
				items:[],
				detailModal:false,
				pageNumber:1,
				pageSize:15,
				total:0,
				topBt:{
					show:true,
					style:'top:80%'
				}
			}
		},
		created() {
			let accountInfo=uni.getStorageSync('accountInfo');
			this.userType=accountInfo.userType;
			if(accountInfo.userType===2){
				this.init();
			}		
		},
		methods:{
			init(type){
				if(type==="appraise"){
					this.tabIndex=3;
				}
				this.getList();
			},
			changeTab(index){
				this.tabIndex=index;
				this.items=[];
				this.getList();
			},
			search(){
				this.pageNumber=1;
				this.items=[];
				this.getList();
			},
			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; 
				this.$nextTick(()=>{
					this.$refs['roll-load'].init();  
				})
	            recordList({
	                status,keyword,page,limit
	            }).then((res)=>{
					this.total=res.data.totalCount;
					let items=res.data.list.map((item)=>{
						return{
							id:item.seekId,
							name:item.patientName,
							time:item.seekTime,
							desc:item.diseaseDesc,
							tags:this.formateSite(item.aiDiagnosisSite),
							doctor:item.doctorName,
							doctorHospital:item.doctorHospital,
							doctorDepartment:item.doctorDepartment,
							doctorJobTitle:item.doctorJobTitle														
						}
					})
					this.items=items;
	            }).catch(()=>{
					this.items=[]
					uni.showToast({
						icon:'error',
						title:"请求数据失败"
					});
				})	
				
			},
			getItem(){
				let status=(this.tabIndex)-1;
				let keyword=this.keyword;
				let page= this.pageNumber;   
				let limit=this.pageSize;         
				return new Promise((resolve,reject)=>{
					recordList({
						status,keyword,page,limit
					}).then((res)=>{
						let total=res.data.totalCount;
						let items=res.data.list.map((item)=>{
							return{
								id:item.seekId,
								name:item.patientName,
								time:item.seekTime,
								desc:item.diseaseDesc,
								tags:this.formateSite(item.aiDiagnosisSite),
								doctor:item.doctorName,
								doctorHospital:item.doctorHospital,
								doctorDepartment:item.doctorDepartment,
								doctorJobTitle:item.doctorJobTitle								
							}
						})
						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);
				}				
			},		
			showdetail(id){
				let status=this.tabIndex;
				uni.navigateTo({
					url:`/views/detail/index?type=diagnose&user=patient&id=${id}&status=${status}`,
					animationType: 'pop-in',
					animationDuration: 200				
				})
			},
			formateSite(site){
				try{
					if(!site){
						return []
					}else{
						let tags=JSON.parse(site);
						if(Array.isArray(tags)&&site.length>0){
							tags=tags.map((item)=>{
								return item.illnessname;
							})
							return tags;					
						}
						return [];
					}				
				}catch(err){
					return [];
				}
			},
			formateJobTitle(item){	
				return !notEmpty(item.doctorJobTitle)&&!notEmpty(item.doctorDepartment);
			}
		}
	}
</script>

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