<template>
	<view class="main bg-white">
		<u-dropdown>
			<u-dropdown-item v-model="value1" :title="textcontent" @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" @click="gotoinfo(item)" v-for="(item,index) in ordersList" :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-if="item.order_status==5">已取消</text>
						<text v-else-if="item.order_status==6">已超时</text>
						<text v-else-if="item.order_status==7">已预约</text>
					</view>
					<text class="phonezi" v-if="item.product_type==3">门诊预约</text>
					<text class="phonezi" v-else-if="item.product_type==4">计免预约</text>
					<text class="phonezi" v-else>儿保预约</text>
				</view>
				<view class="" style="background: #FBFBFB;padding: 28rpx; border-radius: 16rpx;">
					<view class="nametaile flex align-center">
						<!-- <u-avatar v-if="item.product_type==3" :src="item.docter.avatar" mode="circle"></u-avatar> -->
						<!-- <u-avatar v-else :src="item.organization.docter[0].avatar" mode="circle"></u-avatar> -->
						<text v-if="item.product_type==3" class="">预约地址：{{item.organization.address}}</text>
						<text v-else class="">预约地址：{{item.organization.address}}</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;" v-if="item.product_type==3">科室：{{item.docter.office.name||'暂无'}}
							{{item.docter.qualification.name||'暂无'}}</text>
					</view> -->
					<view class="textstyle margin-top-xs flex align-center justify-between" v-if="item.product_type == 3">
						<text style="color: #333333;">预约医生：{{item.docter.name}}</text>
					</view>
					<view class="textstyle margin-top-xs flex align-center justify-between" v-else-if="item.product_type == 4">
						<text style="color: #333333;">预约疫苗：{{item.order_vaccine.vaccine_name}}</text>
					</view>
					<view class="textstyle margin-top-xs flex align-center" v-else-if="item.product_type == 5">
						<text style="color: #333333;">儿保项目：</text>
						<view class="">
							<view class="" v-for="(itm,idx) in item.order_nurse" :key="idx">
								{{itm.nurse_name}}
							</view>
						</view>
					</view>
					<view class="textstyle margin-top-xs">
						<text style="color: #333333;">预约时间：{{format(item.order_patient.appoint_start_time*1000)+'至'+format(item.order_patient.appoint_end_time*1000)}}</text>
					</view>
					<view class="cu-bar bg-white tabbar" v-if="item.order_status==1||item.order_status==2||item.order_status==7" 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>
			</view>
		</view>
		<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>
	export default {
		onLoad(options) {
			if (this.value1 == 0) {
				this.textcontent = "全部预约"
			}
		},
		mounted() {
			this.getordersList()
			console.log(this.value1)
		},
		data() {
			return {
				value1: 0, //全部类型
				value2: 0, //全部状态
				value3: 1, //时间正序
				options1: [{
						value: 0,
						label: "全部预约"
					},
					{
						value: 3,
						label: "门诊预约"
					},
					{
						value: 4,
						label: "计免预约"
					},
					{
						value: 5,
						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,
				textcontent: ""
			}
		},
		onPullDownRefresh() {
			this.pageindex = 1
			this.ordersList = []
			this.getordersList()
			uni.stopPullDownRefresh()
		},
		methods: {
			cancelOrder(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) => {}
					})
				}
			},
			gotoinfo(item) {
				uni.navigateTo({
					url: "appointmentInfo?id=" + item.id
				})
			},
			format(t) {
				let time = new Date(t);
				let str = time.getMonth() < 9 ? ('0' + (time.getMonth() + 1)) : (time.getMonth() + 1);
				str += '-';
				str += time.getDate() < 10 ? ('0' + time.getDate()) : time.getDate();
				str += ' ';
				str += time.getHours();
				str += ':';
				str += time.getMinutes() < 10 ? ('0' + time.getMinutes()) : time.getMinutes();
				return str;
			},
			typechange(value) {
				this.value1 = value
				if (value == 3) {
					this.textcontent = "门诊预约"
				} else if (value == 4) {
					this.textcontent = "计免预约"
				} else if (value == 5) {
					this.textcontent = "儿保预约"
				} else {
					this.textcontent = "全部预约"
				}
				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: 2,
					product_type: this.value1,
					order_status: this.value2,
					time_sort: this.value3
				})
				console.log(this.value1)
				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.pageindex++
					}
				}
				console.log(this.ordersList)
				if (this.ordersList.length == 0) {
					this.show = true
				} else {
					this.show = false
				}
			}
		}
	};
</script>

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

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