<template>
	<view>
		<u-tabs :list="tab" active-color="#c74547" :is-scroll="false" :current="current" @change="change"></u-tabs>
		<view class="list">
			<view class="item" v-for="(item, key) in list" :key="key" @click="detail(item)">
				<view class="order_title">
					<text>广安农机农技智慧服务</text>
					<view class="zhu">
						<text class="theme_bg" v-if="item.state == 0">待支付</text>
						<text style="color: #4CAF50;" v-if="item.state == 1">支付成功,待确认</text>
						<text style="color: #4CAF50;" v-if="item.state == 2">进行中...</text>
						<text style="color: #4CAF50;" v-if="item.state == 3">完成交易</text>
						<text style="color: #4CAF50;" v-if="item.state == -1">已退款</text>
					</view>
				</view>
				<view class="order_item" v-for="(it, index) in item.orderList" :key="index">
					<image :src="ip + it.pic" class="pic" :class="it.goods.state == 1||it.goods.state==null? 'gray' : ''" mode="aspectFill"></image>
					<view class="con">
						<view class="title">{{ it.title }}</view>
						<view class="price">
							<text>¥{{ it.price }}</text>
							<text class="day">/天</text>
						</view>
						<view class="err" v-if="it.goods.state == 1||it.goods.state==null">已下架</view>
					</view>
					<view class="clear"></view>
				</view>
				<view class="hj">
					合计:
					<text class="theme_bg bo pad_left_5">¥ {{ item.totalPrice }}</text>
				</view>
				<view>
					<button class="btn" v-if="item.state == 0" @click.stop="pay(item)">去支付</button>
					<button class="btn cancel" v-if="item.state == 0" @click.stop="cancel(item)">取消订单</button>
				</view>
				<view class="clear"></view>
				<view class="tk" v-if="item.state == -1">抱歉，由于我们无法提供服务，该订单由系统取消，你的款项将原路退回。</view>
			</view>
			<view class="loading"><u-loadmore :status="loadMore ? 'loading' : 'nomore'" /></view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			ip: this.$http.urls.ip,
			tab: [{ name: '所有订单', state: '' }, { name: '待付款', state: 0 }, { name: '进行中', state: 2 }, { name: '已完成', state: 3 }],
			current: 0,
			list: [],
			param: { pageNum: 1, state: '', openId: this.$getUser().openId },
			loadMore: true
		};
	},
	onLoad() {
		this.getData();
	},
	methods: {
		//获取数据
		getData() {
			this.$http.request({
				url: this.$http.urls.order_list,
				data: this.param,
				loading: 'false',
				success: res => {
					this.loadMore = res.data.pages > this.param.pageNum ? true : false;
					res.data.rows.forEach(item => {
						this.list.push(item);
					});
				}
			});
		},
		change(index) {
			this.current = index;
			this.param.state = this.tab[index].state;
			this.refresh();
		},
		detail(item) {
			uni.navigateTo({
				url: 'detail?orderNum=' + item.orderNum
			});
		},
		//取消订单
		cancel(item) {
			uni.showModal({
				title: '提示',
				content: '是否取消该订单?',
				success: res => {
					if (res.confirm) {
						this.$http.request({
							url: this.$http.urls.order_cancel,
							data: { orderNum: item.orderNum },
							success: res => {
								this.list.splice(this.list.indexOf(item), 1);
								uni.showToast({ title: '取消成功' });
							}
						});
					}
				}
			});
		},
		//付款
		pay(item) {
			this.$http.request({
				url: this.$http.urls.pay,
				method: 'POST',
				data: item,
				success: res => {
					//调起微信支付
					wx.requestPayment({
						timeStamp: res.data.data.timeStamp,
						nonceStr: res.data.data.nonceStr,
						package: res.data.data.package,
						signType: res.data.data.signType,
						paySign: res.data.data.paySign,
						success: r => {
							uni.showToast({ title: '支付成功' });
							item.state = 1;
						},
						fail: r => {
							uni.showToast({ title: '取消支付' });
						}
					});
				}
			});
		},
		//刷新数据
		refresh() {
			this.loadMore = true;
			this.param.pageNum = 1;
			this.list = [];
			this.getData();
		}
	},
	//下拉刷新
	onPullDownRefresh() {
		setTimeout(() => {
			uni.stopPullDownRefresh();
			this.refresh();
		}, 1000);
	},
	//上拉加载
	onReachBottom() {
		if (this.loadMore) {
			this.param.pageNum++;
			this.getData();
		}
	}
};
</script>

<style lang="scss">
page {
	background-color: $page;
}
.list {
	padding: 0px 15px 25px 15px;
	.item {
		background-color: white;
		padding: 5px 13px 13px 13px;
		margin-top: 10px;
		border-radius: 5px;
		.hj {
			text-align: right;
			padding-top: 13px;
			.pirce {
				padding-left: 10px;
				color: $theme-color;
			}
		}
		.btn {
			margin-top: 10px;
		}
		.cancel {
			background-color: $dar;
		}
	}
}
</style>
