<template>
	<view class="app" v-if="order">
		<view class="time" style="" v-if="order.orderStatus=='WAIT_PAY'">
			剩余付款时间：
			<u-count-down style='' :timestamp="timestamp">
			</u-count-down>
		</view>
		<view class="OrderStatus" v-if="order.orderStatus=='REFUNDED'||order.orderStatus=='CANCEL'">
			订单已关闭
		</view>
		<!-- <custom-nav nav='To' Url="/pagesB/CommodityOrder/CommodityOrder" title='商品订单'></custom-nav> -->
		<view class="content">
			<view class="name">
				{{order.orderTime}}
				<image src="../../static/huijiantou.png" mode=""></image>
			</view>
			<view class="details">
				<view class="detailslist">
					<image :src="baseurl+order.personPictureUrl" mode=""
						v-if="order.orderType =='HEALTH_CONSULTATION'&&order.personPictureUrl"></image>
					<image src="@/static/docHead.png" mode=""
						v-if="order.orderType =='HEALTH_CONSULTATION'&&!order.personPictureUrl"></image>
					<image :src="baseurl+order.attributePitureUrl" mode=""
						v-if="order.orderType !='HEALTH_CONSULTATION'"></image>
					<view class="model">
						<view class="top">
							<span v-if="order.orderType =='HEALTH_CONSULTATION'">健康咨询</span>
							<span v-else>{{order.goodsName}}</span>
							<span v-if="order.orderType=='DIRECT_BUY'">￥{{order.goodsPrice}}</span>
						</view>
						<view class="bottom" v-if="order.orderType !='HEALTH_CONSULTATION'">
							<span class="box">型号：{{order.goodsAttributeName}}</span>
							<span class="box">X{{order.goodsCount}}</span>
						</view>
						<view class="bottom" v-if="order.orderType !='HEALTH_CONSULTATION'&&order.discountPrice>0">
							<span class="box" style='width:40%'>优惠金额：</span>
							<span class="box">-￥{{order.discountPrice}}</span>
						</view>
						<view class="refund"
							v-if="order.orderStatus != 'WAIT_PAY'&& order.orderStatus!='CANCEL'&&order.orderStatus!='WAIT_REFUND'&&order.orderStatus!='REFUNDED'&&order.orderStatus!='RETURNED_GOODS'&&order.orderStatus!='WAIT_RETURNED_GOODS'">
							<text @tap='gorefundType' v-if="order.orderType=='INTEGRAL_EXCHANGE'">取消兑换</text>
							<text @tap='gorefundType' v-if="order.orderType=='DIRECT_BUY'">申请退款</text>
							<text @tap='gorefundType'
								v-if="order.orderType=='HEALTH_CONSULTATION'&&order.orderStatus=='WAIT_RECEIVED_GOODS'">取消咨询</text>
						</view>
						<view class="refund" style="border: 1rpx solid #969394;" v-if="order.orderStatus=='WAIT_REFUND'"
							@tap='goAftersalesdetails(order)'>
							退款中
						</view>
						<view class="refund" style="border: 1rpx solid #969394;" v-if="order.orderStatus=='REFUNDED'">
							退款成功
						</view>
						<view class="refund" style="border: 1rpx solid #969394;"
							v-if="order.orderStatus=='WAIT_RETURNED_GOODS'">
							待退货
						</view>
						<view class="refund" style="border: 1rpx solid #969394;" v-if="order.orderStatus=='CANCEL'">
							已取消
						</view>
					</view>
				</view>
				<!-- <view class="payinfo" style="bottom:90rpx">
					<text class="pay" style="color: #969394;">优惠金额</text>
					<text class="pay" style="float: right;	color: #969394;">-￥{{order.discountPrice}}</text>
				</view> -->
				<view class="payinfo">
					<text class="pay">实付款</text>
					<text class="price"
						v-if="order.orderType=='DIRECT_BUY'||order.orderType =='HEALTH_CONSULTATION'">￥{{order.totalPrice}}</text>
					<text class="price"
						v-if="order.orderType=='INTEGRAL_EXCHANGE'">{{order.integralExchangeSill}}健康豆</text>
				</view>
			</view>
		</view>
		<view class="info">
			<span>收货人：<text>{{order.receiver}}</text></span>
			<span>联系电话：<text>{{order.phone}}</text></span>
			<span v-if="order.orderType =='HEALTH_CONSULTATION'">咨询医生：<text>{{order.hospitalPersonName}}</text></span>
			<span v-if="order.orderType =='HEALTH_CONSULTATION'">咨询时间：<text>{{order.healthAppointDate}}</text></span>
			<span v-if="order.orderType !='HEALTH_CONSULTATION'">收货地址：<text>{{order.receiveAddress}}</text></span>
			<span>订单编号：<text>{{order.goOrderNo}}</text></span>
			<span>下单时间：<text>{{order.orderTime}}</text></span>
			<span
				v-if="order.orderStatus=='REFUNDED'&&order.orderType=='DIRECT_BUY'">退款时间：<text>{{order.updateTime}}</text></span>
			<span
				v-if="order.orderStatus=='REFUNDED'&&order.orderType=='INTEGRAL_EXCHANGE'">退款时间：<text>{{order.appleTime}}</text></span>
		</view>
		<view class="buy" v-if="order.orderStatus == 'WAIT_PAY'" @tap='pay'>
			<view class="pay">
				去支付
			</view>
		</view>
		<view class="buy" v-if="order.orderStatus == 'EVALUATED'" @tap='golookrate'>
			<view class="pay" style="background-color: #26A888;">
				查看评价
			</view>
		</view>
		<view class="buy" v-if="order.orderStatus == 'RECEIVED_GOODS'" @tap='rate'>
			<view class="pay" style="background-color: #26A888;">
				去评价
			</view>
		</view>
		<view class="buy" v-if="order.orderStatus == 'WAIT_RECEIVED_GOODS'&&order.orderType !='HEALTH_CONSULTATION'">
			<view class="logistics" style="" @tap='gologistics'>查看物流
			</view>
			<view class="pay" style="background-color: #26A888;" @tap='Receipt'>
				确认收货
			</view>
		</view>
		<view class="buy" v-if="order.orderStatus == 'WAIT_RECEIVED_GOODS'&&order.orderType =='HEALTH_CONSULTATION'">
			<view class="pay" style="background-color: #26A888;" @tap='Receipt'>
				咨询完成
			</view>
		</view>
		<u-toast ref="uToast" />
		<!-- //收货 -->
		<view class="frame">
			<u-popup v-model="show" mode="bottom" length="45%" border-radius="30" :closeable='true'>
				<view class="payment">
					<span v-if="order.orderType =='HEALTH_CONSULTATION'">确认咨询完成了吗</span>
					<span v-else>确认收到货了吗</span>
				</view>
				<view class="chat">
					<view class="image" style="height:182rpx">
						<image :src="baseurl+order.attributePitureUrl" mode=""
							v-if="order.orderType !='HEALTH_CONSULTATION'"></image>
						<view class="blackground" v-if="order.orderType !='HEALTH_CONSULTATION'">共1件</view>
					</view>
					<view class="word" v-if="order.orderType =='HEALTH_CONSULTATION'">为了保证您的售后权益，请确认健康咨询无误后再确认完成
					</view>
					<view class="word" v-else>为了保证您的售后权益，请收到商品确认无误后再确认收货</view>
				</view>
				<view class="submits" @tap='Receipts'>确定</view>
			</u-popup>
		</view>
	</view>
</template>

<script>
	import {
		appletGoodsOrderPay
	} from '@/api/pagesB/confirmOrder/index.js'
	import {
		confirmReceipt,
		ssgoodsOrder,
	} from '@/api/pagesB/CommodityOrder/index.js'
	import {
		handCloseOrder
	} from '@/api/pagesB/ServiceDetails/ServiceDetails.js'
	import baseurl from '@/api/baseurl.js'
	export default {
		data() {
			return {
				baseurl: '',
				order: null,
				show: false, //收货
				timestamp: 0,
				timecount: undefined,
				Timers: null,
				orderTypeList: {
					goodsOrderId: null,
					patientId: '',
					openid: null,
					// 健康咨询
					orderType: '',
					orderStatus: '',
					
					
					orderTypeList: [
			
					],
					// orderStatus: '',
					pageSize: 15,
					pageNum: 1,
					goodsName: '',
				},
			}
		},
		onUnload() {
			clearInterval(this.Timers); //清除该函数
		},
		watch: { //监听
			timecount() {
				if (this.timecount <= 0) {
					clearInterval(this.Timers); //清除该函数
					handCloseOrder().then(res => {
						this.order.orderStatus = 'CANCEL'
					})
				}
			},
		},
		methods: {
			//查看评价
			golookrate() {
				uni.navigateTo({
					url: `/pagesB/lookrate/lookrate?item=${JSON.stringify(this.order)}`
				})
			},
			//收货
			Receipts() {
				confirmReceipt(this.order.goOrderNo).then(res => {
					if (res.code == 200) {
						this.$refs.uToast.show({
							title: '完成订单成功',
							type: 'success',
							duration: '1000'
						})
						this.show = false
						uni.setStorageSync("Refresh", 'Refresh')
						this.goodsOrderinfo()
						setTimeout(e => {
							uni.navigateTo({
								url: `/pagesB/goodsorderRate/goodsorderRate?item=${JSON.stringify(this.order)}&delta=${2}`
							})
						}, 1000)
					} else {
						this.$refs.uToast.show({
							title: '完成订单失败',
							type: 'error',
							duration: '1000'
						})
						this.show = false
					}
				})
			},
			//收货
			Receipt() {
				this.show = true
			},
			//看物流信息
			gologistics() {
				uni.navigateTo({
					url: `/pagesB/logistics/logistics?item=${JSON.stringify(this.order)}`
				})
			},
			//评价
			rate() {
				uni.navigateTo({
					url: `/pagesB/goodsorderRate/goodsorderRate?item=${JSON.stringify(this.order)}&delta=${2}`
				})
			},
			//支付
			pay() {
				var that = this
				let paydata = this.order
				paydata.openid = this.orderTypeList.openid
				paydata.payType = "WECHAT_PAY"
				paydata.paymentPrice = this.order.totalPrice
				paydata.orderChannel = 'WECHAT_APPLET'
				paydata.orderNo = this.order.goOrderNo
				appletGoodsOrderPay(paydata).then(response => {
					if (response.code == 200) {
						uni.requestPayment({
							timeStamp: response.data.timeStamp,
							nonceStr: response.data.nonceStr,
							package: response.data.prepayId,
							signType: response.data.signType,
							paySign: response.data.paySign,
							success: function(res) {
								uni.setStorageSync("Refresh", 'Refresh')
								that.goodsOrderinfo()
								that.$refs.uToast.show({
									title: '支付成功',
									type: 'success',
									duration: 1500,
									url: `/pagesB/paysuccess/paysuccess?delta=${2}`
								})
							},
							fail: function(err) {
								that.$refs.uToast.show({
									title: '取消支付',
									type: 'error',
									duration: 1500
								})
							}
						});
					} else {
						that.$refs.uToast.show({
							title: response.msg,
							type: 'error',
							duration: 1500
						})
					}
				})
			},
			//退款页面
			gorefundType() {
				uni.navigateTo({
					url: `/pagesB/refundType/refundType?order=${JSON.stringify(this.order)}`
				})
			},
			goodsOrderinfo() {
				ssgoodsOrder(this.orderTypeList).then(
					res => {
						this.order = res.rows[0]
						var ordertimes = this.order.orderTime.replaceAll(/\-/gi, "/")
						var time = new Date(ordertimes).getTime() / 1000 + (60 * 60 * 2)
						var times = new Date().getTime() / 1000
						this.timestamp = time - times
						if (this.timestamp >= 0) {
							this.timecount = this.timestamp
							this.Timers = setInterval(() => {
								this.timecount--;
							}, 1000)
						}
					})
			},
			//跳转售后详情
			goAftersalesdetails(item) {
				uni.navigateTo({
					url: `/pagesB/Aftersalesdetails/Aftersalesdetails?goodsOrderId=${this.order.goodsOrderId}`
				})
			},
		},
		onShow() {
			this.goodsOrderinfo()
		},
		onLoad(options) {
			//2健康咨询  1商品
			if(options.delta=='2'){
				this.orderTypeList={
					goodsOrderId: null,
					patientId: '',
					openid: null,
					// 健康咨询
					orderType: 'HEALTH_CONSULTATION',
					orderStatus: 'WAIT_PAY',
					
					
					// orderTypeList: [
					// 	'INTEGRAL_EXCHANGE',//商城 
					// 	'DIRECT_BUY',
					// ],
					// orderStatus: '',
					pageSize: 15,
					pageNum: 1,
					goodsName: '',
				}
				
				
			}else if(options.delta=='1'){
				this.orderTypeList={
					goodsOrderId: null,
					patientId: '',
					openid: null,
					// 健康咨询
					// orderType: 'HEALTH_CONSULTATION',
					// orderStatus: 'WAIT_PAY',
					
					
					orderTypeList: [
						'INTEGRAL_EXCHANGE',//商城 
						'DIRECT_BUY',
					],
					// orderStatus: '',
					pageSize: 15,
					pageNum: 1,
					goodsName: '',
				}
				
			}
			this.baseurl = baseurl
			this.orderTypeList.goodsOrderId = options.goodsOrderId
			let that = this
			const value = uni.getStorageSync('patientId');
			const value2 = uni.getStorageSync('openid');
			if (value && value2) {
				that.orderTypeList.patientId = value
				that.orderTypeList.openid = value2
			}
		}
	}
</script>
<style lang="scss">
	@import "./orderDetails.scss";
</style>
