<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>
		<load-refresh ref="loadRefresh" :isRefresh="true" :isPaging="false" refreshType="loader" refreshTime="2000"
		 heightReduce="10" backgroundCover="#fff" :pageNo="pageindex" :totalPageNo="totalPage" @loadMore="loadMore" @refresh="refresh">
			<view slot="content-list">
				<view class="margin-top-sm margin-lr-sm padding-lr-xs" v-for="(item,index) in ordersList" @click="goinfoorder"
				 :data-id="item.id" :key="index" style="border-radius: 16rpx;box-shadow:0px 0px 10px rgba(0,0,0,.2);">
					<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-if="item.order_status==5">已取消</text>
							<text v-else-if="item.order_status==6">已超时</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" @click.stop="payorder(item.id,item.payment_amount,item.product_type)"
							 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 class="cu-bar bg-white tabbar" v-if="item.order_status==1||item.order_status==2" style="width: 100%;display: flex;justify-content: flex-end;">
						<u-button shape="circle" size="mini" @click="cancelOrder(item,index)" :ripple="true">取消订单</u-button>
					</view>
				</view>
				<u-empty text="暂无数据" mode="order" :show="show" margin-top="250"></u-empty>
			</view>
		</load-refresh>
		<!-- <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>
		<!-- <view class="cu-tabbar-height"></view>
		<view class="cu-tabbar-height"></view> -->
		<u-keyboard default="" ref="uKeyboard" mode="number" :mask="true" :mask-close-able="false" :dot-enabled="false"
		 v-model="showpay" :safe-area-inset-bottom="true" :tooltip="false" @change="onChange" @backspace="onBackspace">
			<view>
				<view class="u-text-center u-padding-20 money">
					<text>{{payment_amount/100}}</text>
					<text class="u-font-20 u-padding-left-10">元</text>
					<view class="u-padding-10 close" data-flag="false" @tap="showPop(false)">
						<u-icon name="close" color="#333333" size="28"></u-icon>
					</view>
				</view>
				<view class="u-flex u-row-center">
					<u-message-input mode="box" :maxlength="6" :dot-fill="true" v-model="password" :disabled-keyboard="true" @finish="finish"></u-message-input>
				</view>
				<view class="u-text-center u-padding-top-10 u-padding-bottom-20 tips">请输入密码</view>
			</view>
		</u-keyboard>
	</view>
</template>

<script>
	import loadRefresh from '@/components/load-refresh/load-refresh'
	import IMService from "../../../common/goeasyimutil.js"
	export default {
		components: {
			loadRefresh
		},
		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();
				}
			}
			this.ordersList = []
			this.pageindex = 1
			this.getordersList()
		},
		mounted() {

		},
		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: "",
				totalPage: "",
				showpay: false,
				payment_amount: "",
				password: "",
				payid: "",
				paytype: ""
			}
		},
		// onReachBottom() {
		// 	this.getordersList()
		// },
		// onPullDownRefresh() {
		// 	this.pageindex = 1
		// 	this.ordersList = []
		// 	this.getordersList()
		// 	uni.stopPullDownRefresh()
		// },
		methods: {
			payorder(id, price, type) {
				this.showpay = true
				this.payment_amount = price
				this.payid = id
				this.paytype = type
			},
			showPop(flag = true) {
				this.password = '';
				this.showpay = flag;
			},
			onChange(val) {
				if (this.password.length < 6) {
					this.password += val;
				}

				if (this.password.length >= 6) {
					this.paymoney()
					return false
				}
			},
			paymoney: async function() {
				uni.showLoading({
					title: '支付中'
				})
				let res = await this.$request.post("/api/v1/order/orderPay", {
					order_id: this.payid,
					pay_password: this.password
				})
				if (res.status == 0) {
					uni.hideLoading();
					this.showpay = false;
					this.password = ""
					uni.showToast({
						icon: 'success',
						title: '支付成功',
						duration: 1000
					})
					setTimeout(() => {
						uni.navigateTo({
							url: "../../order/order?type=" + this.paytype
						})
					}, 1000)
				} else {
					uni.hideLoading();
					this.password = ""
					uni.showModal({
						title: "提示",
						content: res.message,
						confirmText: "确定",
						showCancel: false,
						success: (rr) => {
							if (rr.confirm) {
								this.showpay = false;
							}
						}
					})
				}
			},
			onBackspace(e) {
				if (this.password.length > 0) {
					this.password = this.password.substring(0, this.password.length - 1);
				}
			},
			cancelOrder: async function(item, index) {
				uni.showModal({
					title: "提示",
					content: "确定要取消订单吗？",
					success: (res) => {
						if (res.confirm) {
							uni.requestSubscribeMessage({
								tmplIds: ['bjZ-r79i8sl7zEtdttAtcCPZLkCgFSYHTNWnNVd1cG0'],
								success: (res) => {
									this.confirmorder(item, index)
								},
								fail: (err) => {
									this.confirmorder(item, index)
								}
							})
						}
					}
				})
			},
			confirmorder: async function(item, index) {
				let res = await this.$request.post("/api/v1/order/orderCancel", {
					order_id: item.id
				})
				if (res.status == 0) {
					uni.showToast({
						title: "取消成功",
						icon: "none"
					})
					this.ordersList.splice(index, 1)
				} else {
					uni.showModal({
						title: "提示",
						content: res.message,
						showCancel: false,
						success: (res) => {}
					})
				}
			},
			loadMore() {
				this.getordersList()
			},
			refresh() {
				this.pageindex = 1
				this.ordersList = []
				this.getordersList()
			},
			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
				}, false)
				if (res.status == 0) {
					this.$refs.loadRefresh.loadOver()
					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++
						this.totalPage = res.data.last_page
					}
				}
				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;
	}

	.money {
		font-size: 80rpx;
		color: #fa3534;
		position: relative;

		.close {
			position: absolute;
			top: 20rpx;
			right: 20rpx;
			line-height: 28rpx;
			font-size: 28rpx;
		}
	}

	.tips {
		color: $u-tips-color;
	}

	.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>
