<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="order flex">
			<view class="" @click="navto('/pages/zero/order')">
				<image src="../../static/icon/order1.png" mode=""></image>挂售订单
			</view>
			<view class="" @click="navto('/pages/zero/orders')">
				<image src="../../static/icon/order1.png" mode="">买入订单
			</view>
		</view>
		<swiper :style="{'height':maxHeight}" class="scroll-wrap" :current="navCurrent">
			<swiper-item v-for="(item,index) in navList" >
				<scroll-view scroll-y="true" :style="{'height':maxHeight}">
					<view class="order-wrap " v-for="orderItem in item.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="navCurrent == 0 && orderItem.user">{{orderItem.user.nickname}}</text>
							<text style=" display: inline-block;line-height: 55rpx;" v-if="navCurrent == 1 && orderItem.to_user">{{orderItem.to_user.nickname}}</text>
						</view>
						<view class="price">
							￥{{orderItem.total_price}}
						</view>
						<view class="num">
							数量:{{orderItem.amount}}
						</view>
						<view class="num">
							单价:{{orderItem.price}}
						</view>
						<view class="num">
							{{ getTime(orderItem.add_time)}}
						</view>
						<view class="order-btn"  @click="gobuy(orderItem)">
							{{navCurrent == 0? '立即购买':'立即抢单'}}
						</view>
					</view>
					<empty v-if="item.list.length==0 && item.loaded"></empty>
					<uni-load-more v-else :status="item.loadingType"></uni-load-more>
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>

</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';

	import empty from '@/components/empty.vue'

	import {
		getGsList,
		qxGs,
		upEvaluation,
		auth,
		buyGs,
		goBuy
	} 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: [{
					status: 0,
						tit: '我要买',
						list: [],
						out: 1,
						loadingType: 'more',
						page: 1,
						limit: 10,
						loaded: false
					},
					{
						out: 0,
						tit: '我要卖',
						list: [],
						loadingType: 'more',
						page: 1,
						limit: 10,
						status: 0,
						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: {
			navto(url) {
				uni.navigateTo({
					url,
					fail() {
						uni.switchTab({
							url
						})
					}
				})
			},
			gobuy(item) {
				if(this.buying) {
					return
				}
				this.buying = true
				if(this.navCurrent == 0) {
					buyGs({
						id: item.id
					}).then(res => {
						uni.showToast({
							title:'购买成功',
							duration:2000
						});
						this.buying = false
						this.getList('reload')
					}).catch( err => {
						this.buying = false
					})
				}else {
					goBuy({
						id: item.id
					}).then(res => {
						uni.showToast({
							title:'提交成功',
							duration:2000
						});
						this.buying = false
						this.getList('reload')
					}).catch( err => {
						this.buying = false
					})
				}
				
			},
			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) {
							qxGs({
								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 > 9 ? mon : ('0' + mun)) + '-' + (day > 9 ? day : ('0' + day))  + ' ' + (hours > 9 ? hours : ('0' + hours)) + ':' + (minu > 9 ? minu : ('0' + minu)) + ':' + (sec > 9 ? sec : ('0' + 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]
				if (type == 'reload' || type == 'tab') {
					item.page = 1
					item.list = []
					item.loadingType = 'more'
					item.loaded = false
				}
				if (item.loadingType == 'loading' || item.loadingType == 'noMore') {
					return
				}
				item.loadingType = 'loading'
				let qdata = {
					page: item.page,
					limit: item.limit,
					status: item.status,
					order: 'id desc',
					out: item.out
				}
				getGsList(qdata).then(res => {
					let arr = res.data.result.list
					item.list = item.list.concat(arr)
					console.log(item.list)
					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;
			position: absolute;
			right: 40rpx;
			bottom: 33rpx;
		}

		.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;
		}
	}
	.order {
		background-color: #fff;
		padding:30rpx;
		
		image {
			width: 30rpx;
			height: 33rpx;
			vertical-align: middle;
			margin-right: 10rpx;
		}
	}
</style>