<template>
	<view class="main bg-white">
		<u-dropdown>
			<u-dropdown-item v-model="value1" :title="options1[value1].label" @change="typechange" :options="options1"></u-dropdown-item>
			<u-dropdown-item v-model="value2" :title="options2[value2].label" @change="paychange" :options="options2"></u-dropdown-item>
			<u-dropdown-item v-model="value3" :title="options3[value3].label" @change="timechange" :options="options3"></u-dropdown-item>
		</u-dropdown>
		<view class="margin-lr-sm bg-white">
			<view class="margin-top-sm" v-for="(item,index) in ordersList" @click="goinfoorder" :data-id="item.id" :key="index"
			 style="border-radius: 16rpx;box-shadow: 0 0 50rpx 0 rgba(0, 0, 0, 0.1);">
				<view class="flex justify-between align-center bg-white" style="padding: 28rpx;border-radius: 16rpx;">
					<view class="jinxing">
						<text style="display: inline-block;width: 12rpx;height: 12rpx;border-radius: 50%;background-color:#EEAA3F;margin-right: 8rpx;"></text>
						<text v-if="item.order_status==3">进行中</text>
						<text v-else-if="item.order_status==1">未支付</text>
						<text v-else-if="item.order_status==2">待接单</text>
						<text v-else-if="item.order_status==4">已完成</text>
						<text v-else>已取消</text>
					</view>
					<text class="phonezi" v-if="item.product_type==1">电话咨询</text>
					<text class="phonezi" v-else-if="item.product_type==2">图文咨询</text>
				</view>
				<view class="" style="background: #FBFBFB;padding: 28rpx; border-radius: 16rpx;">
					<view class="nametaile flex align-center">
						<u-avatar :src="item.docter.avatar" mode="circle"></u-avatar>
						<text class="margin-left-sm">{{item.docter.name}}</text>
						<text class="text_style" v-for="(itm,idx) in item.docter.label_texts" :key="index">{{itm.label_name}}</text>
					</view>
					<view class="textstyle margin-top-xs flex align-center justify-between">
						<text style="color: #333333;">科室：{{item.docter.office.name||'暂无'}} {{item.docter.qualification.name||'暂无'}}</text>
						<text v-if="item.order_status==4&&item.product_type==2" class="text-blue" @click.stop="gosuggest(item)">意见单</text>
						<text v-else-if="item.order_status==1" class="text-blue">去支付</text>
						<text v-else class="text-blue">查看详情</text>
					</view>
					<view class="textstyle margin-top-xs flex align-center justify-between">
						<text style="color: #333333;">编号：{{item.order_sn}}</text>
						<text v-if="item.order_status==4&&item.is_evaluate!=1" @click.stop="evaluate(item,index)" style="width: 130rpx; text-align: right;"
						 class="text-blue">评价</text>
						<text v-else-if="item.order_status==1" class="text-blue">取消</text>
						<text v-else-if="item.order_status==4&&item.is_evaluate==1" class="text-blue" @click.stop="gotoeva">查看评价</text>
					</view>
					<view class="textstyle margin-top-xs">
						<text style="color: #333333;">下单时间：{{item.created_at}}</text>
					</view>
				</view>
			</view>
		</view>
		<u-popup v-model="showpinjia" mode="center" border-radius="14">
			<view class="padding-xl flex flex-direction align-center">
				<view class="text-xl text-bold text-black text-center">
					填写评价
				</view>
				<view class="margin-tb-sm">
					<u-rate :count="count" size="48" active-color="#EEAA3F" v-model="ratevalue"></u-rate>
				</view>
				<textarea v-model="content" class="textareastyle"></textarea>
				<view class="flex align-center justify-between margin-tb" style="width: 100%;">
					<button class="cancel" @click="concelpop">取消</button>
					<button class="determine" @click="determinepop">确定</button>
				</view>
			</view>
		</u-popup>
		<u-empty text="暂无数据" mode="order" :show="show" margin-top="250"></u-empty>
		<view class="cu-tabbar-height"></view>
		<view class="cu-tabbar-height"></view>
	</view>
</template>

<script>
	import IMService from "../../../common/goeasyimutil.js"
	export default {
		onLoad(options) {

		},
		onShow() {
			console.log(uni.getStorageSync('user').flag, typeof uni.getStorageSync('user').flag)
			if (getApp().globalData.imService == null) {
				let user = uni.getStorageSync('user')
				getApp().globalData.imService = new IMService();
				let loginResult = getApp().globalData.imService.login({
					flag: user.flag,
					nickname: user.nickname,
					avatar: user.avatar
				});
				if (loginResult) {
					//连接IM
					getApp().globalData.imService.connectIM();
				}
			}
		},
		mounted() {
			this.getordersList()
		},
		data() {
			return {
				value1: 0, //全部类型
				value2: 0, //全部状态
				value3: 1, //时间正序
				options1: [{
						value: 0,
						label: "全部类型"
					},
					{
						value: 1,
						label: "电话咨询"
					},
					{
						value: 2,
						label: "图文咨询"
					},
				],
				options2: [{
						value: 0,
						label: "全部状态"
					},
					{
						value: 1,
						label: "未支付"
					},
					{
						value: 2,
						label: "待接单"
					},
					{
						value: 3,
						label: "进行中"
					},
					{
						value: 4,
						label: "已完成"
					},
					{
						value: 5,
						label: "已取消"
					},
				],
				options3: [{
						value: 0,
						label: "时间正序"
					},
					{
						value: 1,
						label: "时间倒序"
					},
				],
				ordersList: [],
				pageindex: 1,
				show: false,
				showpinjia: false,
				ratevalue: "",
				count: 5,
				content: "",
				currentorder: "",
				evaluateindex: ""
			}
		},
		onReachBottom() {
			this.getordersList()
		},
		onPullDownRefresh() {
			this.pageindex = 1
			this.ordersList = []
			this.getordersList()
			uni.stopPullDownRefresh()
		},
		methods: {
			evaluate(item, index) {
				this.showpinjia = true
				this.currentorder = item
				this.evaluateindex = index
			},
			determinepop: async function() {
				let res = await this.$request.post("/api/v1/evaluate/sumbitEvaluate", {
					order_id: this.currentorder.id,
					docter_id: this.currentorder.docter_id,
					score: this.ratevalue,
					content: this.content
				})
				console.log(res)
				if (res.status == 0) {
					uni.showToast({
						title: "评价成功",
						icon: "none",
						duration: 500
					})
					this.ordersList[this.evaluateindex].is_evaluate = 1
					setTimeout(() => {
						this.showpinjia = false
					}, 500)
					this.$forceUpdate()
				}
			},
			concelpop() {
				this.content = ""
				this.showpinjia = false
			},
			typechange(value) {
				this.value1 = value
				this.pageindex = 1
				this.ordersList = []
				this.getordersList()
			},
			paychange(value) {
				this.value2 = value
				this.pageindex = 1
				this.ordersList = []
				this.getordersList()
			},
			timechange(value) {
				this.value3 = value
				this.pageindex = 1
				this.ordersList = []
				this.getordersList()
			},
			getordersList: async function() {
				let res = await this.$request.post("/api/v1/order/orderList", {
					page: this.pageindex,
					list_type: this.value1,
					product_type: this.value1,
					order_status: this.value2,
					time_sort: this.value3
				})
				if (res.status == 0) {
					if (this.pageindex > res.data.last_page) {
						uni.showToast({
							title: "没有更多了",
							icon: "none"
						})
					} else {
						this.ordersList = this.ordersList.concat(res.data.data)
						this.ordersList.forEach((item, index) => {
							if (item.docter == null) {
								this.ordersList.splice(index, 1)
							}
						})
						this.ordersList.forEach((item, index) => {
							if (item.product_type == 6) {
								this.ordersList.splice(index, 1)
							}
						})
						this.ordersList.forEach((item, index) => {
							if (item.product_type == 5) {
								this.ordersList.splice(index, 1)
							}
						})
						this.ordersList.forEach((item, index) => {
							if (item.product_type != 1 && item.product_type != 2) {
								this.ordersList.splice(index, 1)
							}
						})
						this.ordersList.forEach((item, index) => {
							if (item.product_type == 3) {
								this.ordersList.splice(index, 1)
							}
						})
						console.log(this.ordersList)
						this.pageindex++
					}
				}
				if (this.ordersList.length == 0) {
					this.show = true
				} else {
					this.show = false
				}
			},
			pinjia() {

			},
			goinfoorder(e) {
				uni.navigateTo({
					url: "consultingInfo?id=" + e.currentTarget.dataset.id
				})
			},
			gosuggest(item) {
				uni.navigateTo({
					url: "opinionInfo?id=" + item.suggest.id
				})
			},
			gotoeva() {
				uni.navigateTo({
					url: "../me_evaluate"
				})
			}
		}
	};
</script>

<style lang="scss">
	page {
		background-color: #fff;
	}

	.cancel {
		width: 240rpx;
		height: 78rpx;
		border-radius: 16rpx;
		border: 2rpx solid #0B73B9;
		color: #0B73B9;
		font-size: 32rpx;
	}

	.determine {
		width: 240rpx;
		height: 78rpx;
		background: #0B73B9;
		border-radius: 16rpx;
		color: #fff;
		font-size: 32rpx;
	}

	.textareastyle {
		width: 560rpx;
		height: 269rpx;
		padding: 25rpx;
		border: 1px solid #C0C0C0;
		border-radius: 16rpx;
		font-size: 28rpx;
	}

	.main {}

	.text_style {
		font-weight: 400;
		margin-left: 15rpx;
		background-color: #E5F5FF;
		color: #0B73B9;
		width: 100rpx;
		height: 28rpx;
		font-size: 20rpx;
		border-radius: 10rpx;
		text-align: center;
		align-items: center;
	}

	.nametaile {
		font-size: 30rpx;
		font-weight: 500;
		color: #333333;
	}

	.textstyle {
		font-size: 26rpx;
		color: #666666;
		font-weight: 400;
	}

	.phonezi {
		width: 160rpx;
		height: 54rpx;
		background: #F6F6F6;
		border-radius: 27rpx;
		font-size: 28rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #666666;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.jinxing {
		height: 30rpx;
		font-size: 30rpx;
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: 500;
		color: #EEAA3F;
		display: flex;
		align-items: center;
	}
</style>
