<template>
	<view class="pages">
		<view class="topBar" :style="{'padding-top':systemBarHeight.statusBarHeight+'px','height':systemBarHeight.navigationBarHeight+'px'}">
			<!-- <u-icon @click="Utils.goBack()"  name="arrow-left" color="#000000" size="20"></u-icon> -->
			<view class="tabTitle" :style="{'line-height':systemBarHeight.navigationBarHeight+'px'}">我的订单</view>
		</view>
		<view v-show="false">{{userms}}</view>
		<view :style="{'padding-top':(systemBarHeight.statusBarHeight+systemBarHeight.navigationBarHeight)+'px'}">
			<view class="empty" v-show="userms && empty == true">
			<view :style="{'text-align':'center'}">
				<image class="empty_icon" src="/static/imgs/wudingdanzhanweitu.png"></image>
			</view>
			<view  class="empty_text">——您还未下单，下单品尝一下吧——</view>
			<button class="empty_button" @click="goHome">去品尝</button>
			</view>
			<view class="empty_notdl" v-show="!userms">
			<view :style="{'text-align':'center'}">
				<image class="empty_icon" src="/static/imgs/weidengluzhanweitu.png"></image>
			</view>
			<view  class="empty_text">——您还未登录，请先登录——</view>
			<button class="empty_button" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">去登录</button>
			</view>
			<view class="orders" :style="{'padding-bottom':orderBottom+'rpx'}">
			<view class="order_item" v-for="item in list" :key="item.id" @click="openInfo(item)">
				<view class="order_header">
					<view>
						<view class="order_adrass">{{ item.shop.name }}</view>
						<view class="order_time">{{ item.createdAt }}</view>
					</view>
					<view class="order_status">
						<text>{{ getStateText(item.state) }}</text>
						<image class="xiaojiantou" src="@/static/imgs/xiaojiantou.png"/>
					</view>
				</view>
				<div class="order_content">
					<view class="order_content_box1">
						<view class="goods_item" v-for="(good,index) in item.orderDetails" :key="good.id">
							<image class="goods_img" v-if="jsonParse(good.goods.pics).thumbnailApplet" :src="jsonParse(good.goods.pics).thumbnailApplet"></image>
							<image v-else class="goods_img" :src="jsonParse(good.goods.pics).thumbnail"></image>

							<view class="goods_text">
								<view class="goods_title">{{ good.goodsName }}</view>
								<view class="goods_spce">
									<view>
										<span v-for="(rule, index) in jsonParse(good.specRuleDetail)" :key="rule.specId">
											{{ rule.ruleName }}
											<span v-if="index!=jsonParse(good.specRuleDetail).length-1">/</span>
										</span>
									</view>
									<view class="numbox">
										×{{ good.num }}
										<a class="btntk" v-if="checkStatus(item.state,[12,13,14,15])&&(index+1)===item.orderDetails.length" @click.stop="refundInfo(item)" type="primary">退款详情</a>
									</view>
								</view>
							</view>
							<view v-if="index==item.orderDetails.length-1 && (item.state == 2 || item.state == 3 || item.state == 4 || item.state == 5)" class="qr_code">
								<image class="qr_code_img" src="/static/imgs/icon-barcode.png"></image>
								<view class="qr_text">点击二维码取单</view>
							</view>
						</view>
					</view>
				</div>
				<view class="order_footer" :style="{'margin-top':checkStatus(item.state,[12,13,14,15])?'16rpx':'39rpx'}">
					<view class="total">
						共{{ orderDetailsSize(item.orderDetails) }}件商品 实付<text class="price">￥{{ Utils.isInteger(item.paidAmount) }}</text>
					</view>
					<a class="btn" @click.stop="PayNow(item)" v-if="item.state == 1" type="primary">立即支付</a>
					<a class="btn" v-else @click.stop="oneMoreOrder(item)" type="primary">再来一单</a>
				</view>
			</view>
			</view>
		</view>

		<show-toast ref="toast"/>
		<taBar select="1"></taBar>
	</view>
</template>

<script>
import order from '@/request/order'
import Menu from '@/request/menu';
import Utils from '@/utils/utils'
import User from '@/request/user'
import { $EventBus } from "@/utils/EventBus";

export default {
	onLoad() {
		if(!this.userms){
			User.wxLoginAndGetOpenid(true).then(loginInfo=>{
				this.loginInfo = loginInfo
			})
		}
	},
	name: 'order',
	data() {
		return {
			empty: false,
			list: [],
			loginInfo:"",
			Utils,
			orderBottom:0
		}
	},
	onShow() {
		this.getList()
	},
	mounted() {
		this.orderBottom = uni.getStorageSync('orderBottom')
		// this.getList()
	},	
	computed: {
		userms() {
			return this.$store.getters.Authorization;
		},
		systemBarHeight(){
    	    return  this.$store.state.user.systemBarHeight
    	},
	},
	methods: {
		refundInfo(item) {
			const { id } = item
			uni.setStorageSync('orderId', id);
			let url = '/orderSubPackage/pages/refundInfo/index'
			uni.navigateTo({ url })
		},
		checkStatus(status,list){
			if(list.includes(Number(status))){
				return true
			}else{
				return false
			}
		},
		getStateText(state) {
			state = '' + state;
			switch (state) {
				case '1':
					return '待付款'
				case '2':
					return '待制作'
				case '3':
					return '制作中'
				case '4':
					return '待取餐'
				case '5':
					return '取餐中'
				case '6':
				case '7':
					return '已完成'
				case '8':
				case '9':
				case '10':
				case '11':
					return '已取消'
				case '12':
					return '退款中'
				case '13':
					return '退款失败'
				case '14':
					return '部分退款'
				case '15':
					return '已退款'
				default:
					return '未知状态'
			}
		},
		async oneMoreOrder(item) {
			// 清空购物车
			uni.removeStorageSync('shopCarInfo');
			$EventBus.$emit('updateCar');
			//
			const numObj = {}
			const { id, shopId, orderDetails } = item;
			orderDetails.forEach(item => {
				orderDetails[item.skuId] = Number(item.num)
			})
			const { data } = await order.moreOrder({ orderId: id, shopId });
			if(!data || !data.data){
				this.showToast({ title: "该订单中的商品已经售罄了" });
				return;
			}
			
			let list = new Array();
			data.data.forEach(item => {
				const skuId = item.skus[0].skuId
				const nextData = { ...item, skuId, num: orderDetails[skuId], flag: true, sku: item.skus[0] }
				if (nextData.sku.state == 1) {
					list.push(nextData);
				}
			});
			if(!list || list.length<=0) {
				this.showToast({ title: "该订单中的商品已经售罄了", icon: 'error' });
				return;
			}
			list.forEach(nextData => {
				Utils.getallNum(nextData);
			})
			uni.switchTab({ url: '/pages/menu/menu' });
		},
		async getList() {
			if (!this.userms) return
			const { data } = await order.getMyOrder();
			if (data) {
				this.list = data.rows;
				if(this.list && this.list.length>0) {
					this.empty = false;
				} else {
					this.empty = true;
				}
			} else {
				this.showToast({ title: '请登录！', icon: 'error' })
			}
		},
		async PayNow({ id }) {
			const orderInfo = await order.payOrder({ orderId: id })
			const sendData = {
				orderId:id,
				...orderInfo.data.data
			}
			if (orderInfo) {
				if (orderInfo && orderInfo.data.code == 200) {
					await Menu.requestPayment(sendData);
					this.getList()
				}
			}
		},
		goHome() {
			uni.switchTab({ url: '/pages/menu/menu' });
		},
		// 手机号授权登录
		getPhoneNumber(e) {
			if (e.detail.errMsg == 'getPhoneNumber:ok') {
				e.loginInfo = this.loginInfo
				User.getPhoneNumber(e, this.getList);
			} else if (e.detail.errMsg == "getPhoneNumber:fail user deny") {
				this.showToast({ title: '已拒绝手机号授权', icon: 'error' })
			}
		},
		openInfo(data) {
			const { id } = data
			uni.setStorageSync('orderId', id);
			let url = '/orderSubPackage/pages/orderInfo/index'
			uni.navigateTo({ url })
		},
		jsonParse(json) {
			const data = json || {}
			return JSON.parse(data)
		},
		orderDetailsSize(orderDetails) {
			let size = 0;
			orderDetails.forEach(item => {
				const { num } = item;
				size += Number(num)
			});
			return size
		}


	}
}
</script>

<style lang="scss" scoped>
.pages {

	.topBar{
		position: fixed;
		width: 100%;
		background: white;
		display: flex;
		justify-content: flex-start;
		align-items: center;
		z-index: 9999;
		padding-left: 34rpx;
		// .arrow-left {
		// 	margin-left: 20rpx;
		// }
		.tabTitle{
			font-size: 32rpx;
			font-family: PingFangSC-Medium, PingFang SC;
			font-weight: 600;
			text-align: center;
			width: 92%;
			color: #333333
		}
	}
}
.empty {
	 position: fixed;
	 /* 居中对齐begin */
	 left: 50%;
	 /* 兼容老版本的方法 */
	 -webkit-transform: translateX(-50%);
	 transform: translate(-50%);
	 margin-top: 311rpx;
	
	.empty_icon {
		 width: 324rpx;
		 height: 236rpx;
		 
	}
	.empty_text {
		width: 500rpx;

		margin-top: 32.73rpx;
		text-align: center;

		font-size: 24rpx;
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: 500;
		color: #999999;
	}
	.empty_button {
		margin-top: 34.29rpx;
		width: 167rpx;
		height: 64rpx;
		border-radius: 2rpx;
		font-size: 24rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		background: #0050F6;
		font-weight: 400;
		color: #FFFFFF;
		line-height: 64rpx;
	}
	.empty_button::after{
		border-radius: 2rpx;
		border: 0rpx;
	}
}
.empty_notdl{
	 position: fixed;
	 /* 居中对齐begin */
	 left: 50%;
	 /* 兼容老版本的方法 */
	 -webkit-transform: translateX(-50%);
	 transform: translate(-50%);
	 margin-top: 352rpx;
	.empty_icon {
		 width: 324rpx;
		 height: 236rpx;
	}
	.empty_text {
		margin-top: 32rpx;
		text-align: center;

		font-size: 24rpx;
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: 500;
		color: #999999;
	}
	.empty_button {
		margin-top: 36rpx;
		width: 167rpx;
		height: 64rpx;
		border-radius: 2rpx;
		font-size: 24rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		background: #0050F6;
		font-weight: 400;
		color: #FFFFFF;
		line-height: 64rpx;
	}
	.empty_button::after{
		border-radius: 2rpx;
		border: 0rpx;
	}
}

.orders {
	padding: 1rpx;

	.order_item {
		width: 686rpx;
		background: #FFFFFF;
		margin: 32rpx auto 0;
		box-sizing: border-box;
		padding: 25rpx 32rpx 32rpx 32rpx;

		.order_header {
			display: flex;
			justify-content: space-between;

			.order_adrass {
				font-size: 28rpx;
				font-family: PingFangSC-Medium, PingFang SC;
				font-weight: 500;
				color: #333333;
				line-height: 40rpx;
			}

			.order_time {
				font-size: 24rpx;
				font-family: ArialMT;
				margin-top: 3rpx;
				line-height: 28rpx;
				color: #999999;
			}

			.order_status {
				margin-top: 7rpx;
				font-size: 24rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #999999;
				display: flex;
				align-items: baseline;
				justify-content: flex-start;
				line-height: 34rpx;
				.xiaojiantou {
					width: 12rpx;
					height: 16rpx;
					margin-left: 5rpx;
				}
			}

		}

		.order_content {
			display: flex;
			justify-content: space-between;
			align-items: center;

			.order_content_box1{
				flex: 1;
			}

			.goods_item {
				margin-top: 41rpx;
				display: flex;
				// align-items: center;

				.qr_code {
					text-align: center;
					margin-right: 10rpx;
					margin-left: 38rpx;

					.qr_code_img {
						vertical-align: bottom;
						width: 76rpx;
						height: 76rpx;
					}

					.qr_text {
						line-height: 28rpx;
						margin-top: 10rpx;
						font-size: 20rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #999999;
					}
				}

				.goods_img {
					height: 96rpx;
					width: 96rpx;
					background-color: #eee;
				}

				.goods_text {
					margin-left: 22rpx;
					flex: 1;

					.goods_title {
						font-size: 28rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #666666;
						margin-top: 10rpx;
						line-height: 40rpx;
					}

					.goods_spce {
						margin-top: 2.62rpx;
						font-size: 24rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						line-height: 34rpx;
						color: #999999;
						display: flex;
						justify-content: space-between;
						align-items: flex-start;
						.numbox{
							display: flex;
							.btntk {
								box-sizing: border-box;
								width: 167rpx;
								height: 64rpx;
								line-height: 64rpx;			
								border-radius: 2rpx;
								font-family: PingFangSC-Regular, PingFang SC;	
								border: 2rpx solid #0050F6;
								font-size: 24rpx;
								margin: 0;
								margin-left: 17rpx;						
								color: #0050F6;
								font-weight: 400;
								text-align: center;
							}
						}

					}
				}
			}
		}

		.order_footer {
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-top: 39rpx;

			.total {
				font-size: 28rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #666666;

				.price {
					font-size: 28rpx;
					font-family: Futura-Medium, Futura;
					font-weight: 500;
					color: #666666;
					letter-spacing: 1rpx;
				}
			}

			.btn {
				width: 167rpx;
				height: 64rpx;
				line-height: 64rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				background: #0050F6;
				font-size: 24rpx;
				margin: 0;
				color: #fff;
				font-weight: 400;
				text-align: center;
			}
		}
	}

}
</style>