<template>
	<view class="content">
		<view class="nav-wrap flex">
			<view class="item" v-for="(nav,inden) in navList" :class="{'action': navCurrent == inden}"
				@click="changeNav(inden)">
				{{nav.tit}}
			</view>
		</view>
		<view class="flex nav-list">
			<view class="item" v-for="(item,index) in navList[navCurrent].list" :class="{'action': current == index}"
				@click="changeNavT(index)">
				{{item.tit}}
			</view>
		</view>
		<swiper disable-touch :style="{'height': maxHeight}" class="scroll-wrap" :current="current">
			<swiper-item v-for="navItem in navList[navCurrent].list">
				<scroll-view scroll-y="true" :style="{'height': maxHeight}" @scrolltolower="getList">
					<view class="order-wrap " v-for="orderItem in navItem.list">
						<view class="flex m-info">
							<image src="../../static/icon/gs-av.png" mode="" class="card-img"></image>
							<text style=" display: inline-block;line-height: 55rpx;" v-if="orderItem.to_user && (orderItem.to_user.phone || orderItem.to_user.nickname)">{{orderItem.to_user.phone || orderItem.to_user.nickname}}</text>
						</view>
						<view class="price">
							¥{{orderItem.total_price}}
						</view>
						
						<view class="num">
							数量:{{orderItem.amount}}
						</view>
						<view class="num">
							单价:¥{{(orderItem.price*1).toFixed(2)}}
						</view>
						<view class="num">
							{{ getTime(orderItem.add_time)}}
						</view>
						<view class="status" :style="{color: showColor(orderItem.status)}">
							{{orderItem.status == 0 ? '挂售中': (orderItem.status == 1? '待支付': (orderItem.status == 2? '待审核': orderItem.status == 3 ? '已完成': (orderItem.status == 4? '已取消': '未通过')))}}
						</view>
						<view class="flex btn-list">
							<view class="order-btn" v-if="orderItem.status == 0 && navCurrent == 1" @click="qxOrder(orderItem)">
								取消交易
							</view>
							<view class="order-btn" v-if="orderItem.status == 1 && navCurrent == 1" @click="upPz(orderItem)">
								上传凭证
							</view>
							<view class="order-btn" v-if="orderItem.status == 2 && navCurrent == 1" @click="upPz(orderItem)">
								修改凭证
							</view>
							<view class="order-btn" v-if="orderItem.status == 2 || orderItem.status == 3" @click="lookimg(orderItem)">
								查看凭证
							</view>
							<view class="order-btn" v-if="orderItem.status == 2 && navCurrent == 0" @click="auth(orderItem,-1)">
								拒绝
							</view>
							<view class="order-btn" v-if="orderItem.status == 2 && navCurrent == 0" @click="auth(orderItem,1)">
								通过
							</view>
							<view class="order-btn order-btn-hui" v-if="orderItem.status == 4">
								已取消
							</view>
							<view class="order-btn order-btn-hui" v-if="orderItem.status == -1" @click="lookimg(orderItem)">
								查看凭证
							</view>
							<view class="order-btn order-btn-hui" v-if="orderItem.status == -1">
								未通过
							</view>
						</view>
					</view>
					<empty v-if="navItem.loaded && navItem.list == 0"></empty>
					<uni-load-more :status="navItem.loadingType" v-else></uni-load-more>
				</scroll-view>
			</swiper-item>
		</swiper>
		<uni-popup ref="popup" type="center">
			<view class="pz-wrap">
				<view class="tit">
					上传凭证
				</view>
				<image v-if="!pz" src="../../static/img/add.png" mode="" @click="upimg"></image>
				<image v-else :src="pz" mode="" @click="upimg"></image>
				<view class="btn" @click="goUp">
					上传
				</view>
			</view>
		</uni-popup>
	</view>

</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	
	import empty from '@/components/empty.vue'
	
	import {
		getGsList,
		qxGs,
		upEvaluation,
		auth,
		cancelBuy
	} from '@/api/zero.js'
	import { upload } from '@/api/order.js';
	export default {
		components: {
			empty
		},
		data() {
			return {
				pzing: false,
				orderId: '',
				pz: '',
				maxHeight: '',
				navCurrent: 0, // 0-》买单 1-》卖单
				current: 0, //二层次序
				navList: [{
						tit: '我的抢单',
						list: [
							{
								tit: '全部',
								status: -2,
								list: [],
								loadingType: 'more',
								page: 1,
								limit: 10,
								loaded: false
							},
							// {
							// 	tit: '抢单中',
							// 	status: 0,
							// 	list: [],
							// 	loadingType: 'more',
							// 	page: 1,
							// 	limit: 10,
							// 	loaded: false
							// },
							{
								tit: '待支付',
								status: 1,
								list: [],
								loadingType: 'more',
								page: 1,
								limit: 10,
								loaded: false
							},
							{
								tit: '待审核',
								status: 2,
								list: [],
								loadingType: 'more',
								page: 1,
								limit: 10,
								loaded: false
							},
							{
								tit: '已完成',
								status: 3,
								list: [],
								loadingType: 'more',
								page: 1,
								limit: 10,
								loaded: false
							}
						]
					},
					{
						tit: '我的买入',
						list: [{
								tit: '全部',
								status: -2,
								list: [],
								loadingType: 'more',
								page: 1,
								limit: 10,
								loaded: false
							},
							{
								tit: '抢单中',
								status: 0,
								list: [],
								loadingType: 'more',
								page: 1,
								limit: 10,
								loaded: false
							},
							{
								tit: '待支付',
								status: 1,
								list: [],
								loadingType: 'more',
								page: 1,
								limit: 10,
								loaded: false
							},
							{
								tit: '待审核',
								status: 2,
								list: [],
								loadingType: 'more',
								page: 1,
								limit: 10,
								loaded: false
							}
						]
					}
				]
			}
		},
		computed: {
			...mapState('user',['userInfo'])
		},
		onLoad() {
			
		},
		onShow() {
			this.getList('reload')
		},
		onReachBottom() {

		},
		onReady() {
			var that = this;
			uni.getSystemInfo({
				success: resu => {
					const query = uni.createSelectorQuery();
					query.select('.scroll-wrap').boundingClientRect();
					query.exec(function(res) {
						that.maxHeight = resu.windowHeight - res[0].top + 'px';
					});
				},
				fail: res => {}
			});
		},
		methods: {
			showColor(status) {
				if(status == -1 || status == 4) {
					return '#aaa'
				}else {
					return '#FF5570'
				}
			},
			auth(item,type) {
				let that =this
				uni.showModal({
					title: '提示',
					content: type == 1?'是否确认收款完成?': '是否确认当前提交的凭证没有通过审核',
					complete(e) {
						if(e.confirm) {
							auth({
								id: item.id,
								auth: type
							}).then(res => {
								uni.showToast({
									title:'审核成功',
									duration:2000
								});
								that.getList('reload')
							})
						}
					}
					
				})
			},
			// 取消交易
			qxOrder(item) {
				let that = this
				uni.showModal({
					title: '提示',
					content: '是否确认取消挂售?',
					complete(e) {
						if(e.confirm) {
							cancelBuy({
								id: item.id
							}).then(res => {
								uni.showToast({
									title:'取消成功',
									duration:2000
								});
								that.getList('reload')
							})
						}
					}
					
				})
			},
			upPz(item) {
				// if(item.up_files) {
				// 	this.pz = item.up_files
				// }
				// this.orderId = item.id
				// this.$refs.popup.open()
				uni.navigateTo({
					url: '/pages/zero/pay?id=' + item.id
				})
			},
			//上传凭证
			goUp() {
				if(this.pzing) {
					return
				}
				if(!this.pz) {
					return this.$api.msg('请上传凭证')
				}
				this.pzing = true
				upEvaluation({
					id: this.orderId,
					up_files: this.pz
				}).then(res => {
					uni.showToast({
						title:'上传成功',
						duration:2000
					});
					this.$refs.popup.close()
					this.orderId = ''
					this.getList('reload')
					this.pzing = false
				}).catch(err => {
					this.pzing = false
				})
			},
			upimg() {
				upload({
					file: ''
				})
					.then(e => {
						console.log(e,'e')
					this.pz = e[0].url
					console.log(this.pz)
					})
					.catch(e => {});
			},
			lookimg(item) {
				let arr = item.up_files.split(',')
				uni.previewImage({
					current: 0,
					loop: false,
					urls: arr,
					indicator: 'default'
				});
			},
			getTime(time) {
				const num =13 - (time+'').length;
				let l = 1;//倍数
				for (let i = 0; i < num; i++) {
					l+='0';
				}
				// 重新解析为数字
				l = parseInt(l)
				const date = new Date(parseInt(time) * l);
				const year = date.getFullYear();
				const mon = date.getMonth() + 1;
				const day = date.getDate();
				const hours = date.getHours();
				const minu = date.getMinutes();
				const sec = date.getSeconds();
				return year + '-' + mon + '-' + day + ' ' + hours + ':' + minu + ':' + sec;
			},
			// 切换nav
			changeNav(index) {
				if (index === this.navCurrent) {
					return
				}
				console.log('切换');
				this.navCurrent = index
				this.getList('tab')
			},
			changeNavT(index) {
				if (index === this.current) {
					return
				}
				console.log('切换');
				this.current = index
				this.getList('reload')

			},
			getList(type) {
				let that = this
				let item = that.navList[that.navCurrent].list[that.current]
				if(type == 'reload') {
					item.page = 1
					item.list = []
					item.loadingType = 'more'
					item.loaded = false
				}
				if (item.loadingType == 'loading' || item.loadingType == 'noMore') {
					return
				}
				if (type == 'tab' && item.loaded) {
					return
				}
				item.loadingType = 'loading'
				let qdata = {
					page: item.page,
					limit: item.limit,
					status: item.status,
					type: 1,
					order: 'id desc',
					out: 0,
				}
				if(that.navCurrent == 1) {
					qdata.to_uid = that.userInfo.uid
				}else {
					qdata.uid = that.userInfo.uid
				}
				getGsList(qdata).then(res => {
					let arr = res.data.result.list
					item.list = item.list.concat(arr)
					if (item.limit == arr.length) {
						item.loadingType = 'more'
						item.page++
					} else {
						item.loadingType = 'noMore'
					}
					item.loaded = true
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.nav-wrap {

		padding: 28rpx 30rpx 20rpx;
		background-color: #fff;

		.item {
			width: 336rpx;
			height: 81rpx;
			border-radius: 10rpx;
			border: 1px solid #EC5A54;
			color: #EC5A54;
			line-height: 80rpx;
			text-align: center;
		}

		.action {
			background-color: #EC5A54;
			color: #fff;
		}

	}

	.nav-list {
		justify-content: space-around;
		background-color: #fff;
		font-weight: 500;
		color: #333333;

		.item {
			padding: 28rpx 20rpx 16rpx;

		}

		.action {
			color: #EC5A54;
			border-bottom: 2px solid red;
		}
	}

	.scroll-wrap {
		padding-top: 20rpx;
	}

	.order-wrap {
		width: 686rpx;
		// height: 297rpx;
		background: #FFFFFF;
		box-shadow: 0rpx 0rpx 20rpx 0rpx rgba(50, 50, 52, 0.06);
		border-radius: 20rpx;
		margin: 0 auto 20rpx;
		padding: 33rpx 40rpx;
		position: relative;
		.m-info {
			justify-content: flex-start;

			image {
				width: 55rpx;
				height: 55rpx;
				margin-right: 13rpx;
			}

			text {
				font-size: 28rpx;
				font-weight: bold;
				color: #333333;
			}
		}

		.price {
			font-size: 40rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #FF4C4C;
			padding: 40rpx 0 30rpx;
		}

		.num {
			font-size: 26rpx;
			font-weight: 500;
			color: #888785;
		}
		.status {
			position: absolute;
			right: 10rpx;
			top: 10rpx;
			font-size: 26rpx;
		}
		.btn-list {
			border-top: 1px solid #eee;
			margin-top: 20rpx;
			padding-top: 20rpx;
			justify-content: flex-end;
		}
		.order-btn {
			width: 148rpx;
			height: 62rpx;
			border: 2px solid #FF5570;
			border-radius: 30rpx;
			line-height: 62rpx;
			text-align: center;
			font-size: 28rpx;
			font-weight: bold;
			color: #FF4C4C;
			margin-left: 20rpx;
		}
		.order-btn-hui {
			color: #aaa;
			border: 2px solid #aaa;
		}
	}
	.pz-wrap {
		width: 550rpx;
		// height: 500rpx;
		background-color: #fff;
		border-radius: 20rpx;
		text-align: center;
		font-size: 32rpx;
		padding:40rpx;
		image {
			width: 250rpx;
			height: 250rpx;
		}
		.tit {
			font-weight: bold;
			padding: 20rpx 0;
		}
		.btn {
			background-color: $base-color;
			padding:0 20rpx;
			height: 65rpx;
			border-radius: 25rpx;
			width:250rpx;
			color: #fff;
			line-height: 60rpx;
			margin:20rpx auto 0; 
		}
	}
</style>