<template>
	<view class="content">
		<view class="top-fixed">
			 <u-tabs
			  :scrollable="false"
			  :list="tabs"  
			  lineColor="#C39A58"
			 @change="inquiryTypeChange">
			 </u-tabs>
		</view>
		<mescroll-body top="88rpx" bottom="0" ref="mescrollRef" @init="mescrollInit"    :down="downOption" :up="upOption" @down="downCallback" @up="upCallback">
		<view class="order-list">
			<view class="order-item"  v-for="(item) in dataList" @click="navTo('./inquiryOrderDetails?orderId='+item.orderId)">
				<view class="order-top"  >
					<view class="left" v-if="item.doctorId!=null">
						<image class="head" mode="aspectFill" :src="item.avatar"></image>
						<view class="name">
							{{item.doctorName}}
						</view>
						<view class="dept">
							{{item.deptName}}
						</view>
					</view>
					<view class="left" v-if="item.inquiryType==2">
						<view class="title" v-if="item.status==1">
							支付后为您安排医生接诊
						</view>
						<view class="title" v-if="item.status==2">
							正在为您安排医生接诊
						</view>
						<view class="title" v-if="item.status==-1">
							订单已取消
						</view>
						<view class="title" v-if="item.status==-2">
							订单已退款
						</view>
						<view class="title" v-if="item.status==-3">
							医生已拒单
						</view>
					</view>
					<view class="status red" v-if="item.status==1">
						{{$getDictLabelName(orderStatusOptions,item.status)}}
					</view>
					<view class="status red" v-if="item.status==2">
						{{$getDictLabelName(orderStatusOptions,item.status)}}
					</view>
					<view class="status green" v-if="item.status==3">
						{{$getDictLabelName(orderStatusOptions,item.status)}}
					</view>
					<view class="status green" v-if="item.status==4">
						{{$getDictLabelName(orderStatusOptions,item.status)}}
					</view>
					<view class="status gray" v-if="item.status<0">
						{{$getDictLabelName(orderStatusOptions,item.status)}}
					</view>
				</view>
				<view class="order-cont">
					<view class="order-type"  >
						订单类型:
						<text>{{$getDictLabelName(inquiryTypeOptions,item.inquiryType)}}</text>
						<text>-{{$getDictLabelName(orderTypeOptions,item.orderType)}}</text>
					</view>
					<view class="order-desc">病情描述:{{item.title}}</view>
					<view class="order-time-box">
						<view class="order-time">
							{{item.createTime}}
						</view>
						<view class="right">
							<view class="btn" v-if="item.status==1" @click.stop="pay(item)">去支付</view>
							<view class="btn" v-if="item.status==2" @click.stop="cancel(item)">取消订单</view>
							<view class="btn" v-if="item.status==4&&item.isPing==0" @click.stop="ping(item)">去评价</view>
							<view class="btn" v-if="item.status==3" @click.stop="toIM(item)">去咨询</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		</mescroll-body>
	</view>
</template>

<script>
	import store from "@/store";
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	import {cancel,getMyInquiryOrderList} from '@/api/inquiryOrder.js'
	import {getDictByKey} from '@/api/common.js'
	export default {
		mixins: [MescrollMixin], // 使用mixin
		data() {
			return {
				orderStatusOptions:[],
				orderTypeOptions:[],
				inquiryTypeOptions:[],
				tabs:[
					{
						id:2,
						name:'快速咨询'
					},
					{
						id:1,
						name:'专家咨询'
					},
					{
						id:3,
						name:'开药咨询'
					}
					 
				],
				inquiryType:2,
				mescroll:null,
				downOption: {   //下拉刷新
				 	use:true,
					auto: false // 不自动加载 (mixin已处理第一个tab触发downCallback)
				},
				upOption: {
					onScroll:false,
					use: true, // 是否启用上拉加载; 默认true
					page: {
						pae: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 10 // 每页数据的数量,默认10
					},
					noMoreSize: 10, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					textNoMore:"已经到底了",
					empty: {
						icon:'https://cos.his.cdwjyyh.com/fs/20240423/cf4a86b913a04341bb44e34bb4d37aa2.png',
						tip: '暂无数据'
					}
				},
				dataList: []
			}
		},
		onLoad() {
			this.getDictByKey("sys_inquiry_order_type");
			this.getDictByKey("sys_inquiry_type");
			this.getDictByKey("sys_inquiry_status");
			var that=this;
			uni.$on('refreshInquiryOrder', () => {
				that.mescroll.resetUpScroll()
			})
			uni.$emit('refreshIM')
		},
		onShow() {
		},
		methods: {
			cancel(item){
				var that=this;
				uni.showModal({
					title:"提示",
					content:"确认取消订单吗?",
					showCancel:true,
					cancelText:'取消',
					confirmText:'确定',
					success:res=>{
						if(res.confirm){
							// 用户点击确定
							var data={orderId:item.orderId}
							cancel(data).then(
								res => {
									if(res.code==200){
										this.mescroll.resetUpScroll()
										uni.showToast({
										 	icon:'success',
										 	title: "操作成功",
										});
									}else{
										uni.showToast({
											icon:'none',
											title: res.msg,
										});
									}
								},
								rej => {}
							);
														
						}else{
							// 否则点击了取消  
						}
					}
				})
			},
			ping(item){
				uni.navigateTo({
					url: "/pages_order/pingOrder?orderId="+item.orderId
				})
			},
			pay(item){
				uni.navigateTo({
					url: "/pages_order/inquiryPay?orderId="+item.orderId
				})
			},
			toIM(item){
				var that=this;
				var id="C2CD-"+item.doctorId;
				store.commit("timStore/setType","startInquiry");
				store.commit("timStore/setOrderType",item.orderType);
				store.commit("timStore/setImType", 1);
				store.commit("timStore/setFollowId", 0);
				store.commit("timStore/setOrderId",item.orderId);
				store.commit("timStore/setConversationID", id);
				//uni.$TUIKit.TUIChatServer.updateStore(conversationId)
				uni.$TUIKit.TUIConversationServer.setMessageRead(id);
				uni.$TUIKit.TUIConversationServer.getConversationProfile(id)
				  .then((res) => {
						//通知 TUIChat 关闭当前会话
						const { conversation } = res.data;
						store.commit("timStore/setConversation", conversation);
						let url = "/pages/TUIKit/TUIPages/TUIChat/index";
						const { nick: name } = conversation.userProfile;
						url = `${url}?conversationName=${
							conversation.userProfile.nick?.nick ||
							conversation.userProfile.userID
						}&orderId=`+item.orderId;
						uni.redirectTo({ url });
				})
				.catch((err) => {
				    console.warn("获取 group profile 异常 = ", err);
				});
			},
			getDictByKey(key){
				var data={key:key}
				getDictByKey(data).then(
					res => {
						if(res.code==200){
							if(key=="sys_inquiry_order_type"){
								this.orderTypeOptions=res.data;
							}
							if(key=="sys_inquiry_type"){
								this.inquiryTypeOptions=res.data;
							}
							if(key=="sys_inquiry_status"){
								this.orderStatusOptions=res.data;
							}
						}
					},
					err => {
					}
				);
				
			},
			inquiryTypeChange(item){
				this.inquiryType=item.id
				console.log(item)
				this.mescroll.resetUpScroll()
			},
			navTo(url){
				uni.navigateTo({
					url: url
				})
			},
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},
			/*下拉刷新的回调 */
			downCallback() {
				this.mescroll.resetUpScroll()
			},
			/*上拉加载的回调*/
			upCallback(page) {   
				//联网加载数据
				var that = this;
				var data = {
					inquiryType:this.inquiryType,
					pageNum: page.num,
					pageSize: page.size
				};
				getMyInquiryOrderList(data).then(res => {
					if(res.code==200){
						if (page.num == 1) {
							that.dataList = res.data.list; 
							
						} else {
							that.dataList = that.dataList.concat(res.data.list);
							 
						}
						that.mescroll.endBySize(res.data.list.length, res.data.total);
						
					}else{
						uni.showToast({
							icon:'none',
							title: "请求失败",
						});
						that.dataList = null;
						that.mescroll.endErr();
					}
				});
			},
			
		}
	}
</script>

<style lang="scss">
	page{
		
		height: 100%;
		background: #f6f6f6;
	}
</style>
<style scoped lang="scss">
	.content{
		height: 100%;
		.top-fixed{
			width: 100%;
			position: fixed;
			top: 0;
			left: 0;
			z-index: 10;
			height: 88upx;
			background-color: #fff;
		}
		.order-list{
			width: 100%;
			padding: 15rpx;
			display: flex;
			flex-direction: column;
			align-items: flex-start;
			justify-content: flex-start;
			.order-item{
				margin-bottom: 15rpx;
				width: 100%;
				padding: 15rpx;
				box-shadow: 0px 0px 5px 2px rgba(0,0,0,0.05);
				background-color: #fff;
				border-radius: 15rpx;
				display: flex;
				flex-direction: column;
				align-items: flex-start;
				justify-content: flex-start;
				.order-top{
					display: flex;
					align-items: center;
					justify-content: space-between;
					width: 100%;
					padding-bottom: 15rpx;
					border-bottom: 1px solid #efefef;
					.left{
						display: flex;
						align-items: center;
						justify-content: flex-start;
						image{
							width:80rpx;
							height:80rpx;
							border-radius: 50%;
						}
						.title{
							font-size: 32upx;
							font-family: PingFang SC;
							font-weight: bold;
							color: #111111;
						}
						.name{
							margin-left: 20rpx;
							font-size: 32upx;
							font-family: PingFang SC;
							font-weight: bold;
							color: #111111;
						}
						.dept{
							margin-left: 15rpx;
							font-size: 28upx;
							font-family: PingFang SC;
							color: #9a9a9c;
						}
						 
					}
					.status{
						font-size: 28upx;
						font-family: PingFang SC;
					}
					.red{
						color: #db5053;
					}
					.green{
						color: #C39A58;
					}
					.gray{
						color: #9c9c9c;
					}
				}
				.order-cont{
					width: 100%;
					margin-top: 15rpx;
					.order-type{
						font-size: 32upx;
						font-family: PingFang SC;
						// font-weight: bold;
						color: #111111;
					}
					.order-desc{
						margin-top: 15rpx;
						font-size: 28upx;
						font-family: PingFang SC;
						// font-weight: bold;
						color: #111111;
					}
					.order-time-box{
						width: 100%;
						margin-top: 15rpx;
						display: flex;
						align-items: center;
						justify-content: space-between;
						.order-time{
							font-size: 32upx;
							font-family: PingFang SC;
							color: #9a9a9c;
						}
						.right{
							.btn{
								padding: 10rpx 30rpx;
								border: 1rpx solid #C39A58;
								color: #C39A58;
								font-size: 28rpx;
								border-radius: 30rpx;
							}
						}
					}
				}
				
			}
		} 
		
	}
	 
</style>
