<template>
	<view>
		<view class="w100 h-340">
			<scroll-view scroll-x="true" class="rows rowsm" style="max-width: 750rpx;white-space: nowrap;">
				<view class="rows rowsm h-80 w100 rowsa bgff">
					<view class="rows rowsl rowsm rowsc" v-for="(item,index) in list" :key="index" @click="onSels(index,item.id)"
					:class="ids==item.id?'fw-b fs-32':'fs-30'">
						<text>{{item.name}}</text>
						<view class="w-88 h-8 mt-10 bg br-10" v-if="ids==item.id"></view>
					</view>
				</view>
			</scroll-view>
			<scroll-view scroll-y="true" style="height: calc(100vh - 120rpx);">
				<view class="w100 rows rowsm rowsc rowsl mt-20" v-if="orders.length>0">
					<view class="w-710 p-all-20 bgff br-20 mb-20 rows rowsl" v-for="(item,index) in orders" :key="index">
						<view class="w100 rows rowsb h-80 rowsm mb-20" style="border-bottom: 1rpx solid #f5f5f5;">
							<text class="fs-26">订单时间：{{item.createtime}}</text>
							<text class="fs-26 col" v-if="item.status==-1">已取消</text>
							<text class="fs-26 col" v-if="item.status==0">待付款</text>
							<text class="fs-26 col" v-if="item.status==1">待发货</text>
							<text class="fs-26 col" v-if="item.status==2">待收货</text>
							<text class="fs-26 col" v-if="item.status==3">待评价</text>
							<text class="fs-26 col" v-if="item.status==4">已完成</text>
						</view>
						<scroll-view scroll-y="true" style="max-height: 350rpx;">
							<view class="rows mb-20" v-for="(itm,indexs) in item.shop" :key="indexs">
								<image class="w-180 h-180 br-20 mr-20" :src="$tools.oss(itm.image)" mode="aspectFill"></image>
								<view class="rows rowsl rowsb w100 h-180">
									<view class="fw-b col333 fs-28">{{itm.shop_name}}</view>
									<view class="rows rowsm">
										<text class="col666 fs-28">x{{itm.num}}</text>
									</view>
									<text class="fs-20 fw-b" style="color: #F8222A;">￥{{itm.price}}</text>
								</view>
							</view>
						</scroll-view>
						<view class="w100 h-80 rows rowsb rowsm" style="border-top: 1rpx dashed #f5f5f5;">
							<view class="rows rowsm">
								<text class="fs-24 col333">合计:￥{{item.total_amount}}</text>
							</view>
							<view class="rows rowsm">
								<view class="w-130 h-58 rows rowsc rowsm bg br-100 fs-28 colfff mr-20" @click="open(item.order_number)" v-if="item.status==0">
									去支付
								</view>
								<view class="w-160 h-58 rows rowsc rowsm bg br-100 fs-28 colfff" @click="cancelOrder(item.id)" v-if="item.status==0">
									取消订单
								</view>
								<view class="w-160 h-58 rows rowsc rowsm bg br-100 fs-28 ml-20 colfff" @click="deliveryOrder(item.id)"
								v-if="item.status==2">
									确认收货
								</view>
								<view class="w-160 h-58 rows rowsc rowsm colfff bg br-100 fs-28 ml-20" 
								@click="$tools.goNext(`/pageOne/mes/orderDetail?id=${item.id}`)">
									查看详情
								</view>
							</view>
						</view>
					</view>
				</view>
				<view class="w100 rows rowsc h-600 rowsm" v-else>
					<image class="w-436 h-366" src="/static/other/5.png" mode=""></image>
				</view>
			</scroll-view>
		</view>
		<uni-popup ref="popup" type="bottom" :is-mask-click="false" :safe-area="false">
			<view class="w100 p-all-24 bgff">
				<text class="fs-30 fw-b">选择支付方式</text>
				<view class="w100 rows rowsm rowsl">
					<view class="w-710 bgff br-20 p-all-24">
						<view class="rows rowsl">
							<view class="w100 rows rowsm rowsb mt-20" v-for="(item,index) in payList" :key="index" @click="selpay(item)">
								<view class="rows rowsm">
									<image class="w-72 h-72 mr-20" :src="item.icon" mode=""></image>
									<text class="fs-28 col333">{{item.name}}</text>
								</view>
								<image class="w-36 h-36 mr-15" :src="item.status?'/static/images/03_qou.png':'/static/images/03_w.png'" mode=""></image>
							</view>
						</view>
					</view>
				</view>
				<view class="w100 rows rowsm rowsa">
					<view class="w-300 h-80 rowsm rows rowsc col br-100 fs-28" style="border: 2rpx solid #F8222A;" @click="close">
						取消
					</view>
					<view class="w-300 h-80 bg rowsm rows rowsc colfff br-100 fs-28" @click="create">
						确定
					</view>
				</view>
				<view class="w100 h-30"></view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list:[
					{id:66,name:'全部'},
					{id:0,name:'待付款'},
					{id:1,name:'待发货'},
					{id:2,name:'待收货'},
					// {id:3,name:'待评价'},
					{id:4,name:'已完成'},
				],
				selects:0,
				orders:[],
				ids:'',
				payList:[
					{id:1,name:'微信支付',status:true,icon:'/static/images/icon_7wx.png'},
					{id:2,name:'余额支付',status:false,icon:'/static/images/044vef.png'},
				],
				order_number:'',
				payId:1
			}
		},
		onShow() {
			this.orderList()
		},
		onLoad(e) {
			this.ids = e.id
		},
		methods: {
			onSels(index,id){
				this.selects = index
				this.ids = id
				this.orderList()
			},
			selpay(item){
				this.payId = item.id
				this.payList.forEach(item => { 
					item.status = false
				})
				item.status = true
			},
			orderList(){
				this.$tools.axiosFromToken("POST","order/orderList",{
					type:this.ids
				}).then(res => {
					this.orders = res.data
				})
			},
			// 取消订单
			cancelOrder(id){
				let _this = this
				uni.showModal({
					title: '提示',
					content: '确定要取消订单吗？',
					success: function (res) {
						if (res.confirm) {
							_this.$tools.axiosFromToken("POST","order/cancelOrder",{id}).then(res => {
								_this.$tools.showtt(res.msg)
								if(res.code!=1) return
								_this.orderList()
							})
						} 
					}
				})
			},
			// 确认收货
			deliveryOrder(id){
				let _this = this
				uni.showModal({
					title: '提示',
					content: '确定要确认收货吗？',
					success: function (res) {
						if (res.confirm) {
							_this.$tools.axiosFromToken("POST","order/deliveryOrder",{id}).then(res => {
								_this.$tools.showtt(res.msg)
								if(res.code!=1) return
								_this.orderList()
							})
						} 
					}
				})
			},
			open(order_number){
				this.order_number = order_number
				this.$refs['popup'].open()
			},
			close() {
				this.$refs.popup.close()
			},
			create(){
				this.$refs.popup.close()
				let _this = this
				uni.showModal({
					title: '提示',
					content: '确定要支付吗？',
					success: function (res) {
						if (res.confirm) {
							_this.$tools.axiosFromToken("POST","pay/shopPay",{order_number:_this.order_number,pay_type:_this.payId}).then(res => {
								if(res.code==1){
									if(_this.payId==1){
										uni.requestPayment({
											timeStamp: res.data.info.timeStamp,
											nonceStr: res.data.info.nonceStr,
											package: res.data.info.package,
											signType: res.data.info.signType,
											paySign: res.data.info.paySign,
											success(res) {
												_this.$tools.showtt('支付成功')
												_this.orderList()
											},
											fail(res) {
												_this.$tools.showtt('支付失败')
											}
										})
									}else {
										_this.$tools.showtt(res.msg)
										if (res.code!=1)  return
										_this.orderList()
									}
								}else{
									_this.$tools.showtt(res.msg)
								}
							})
						} 
					}
				})
			},
		}
	}
</script>

<style>
	page{
		background-color: #F7F6FA;
	}
</style>
