<template>
	<view class="good-list">

		<view class="flex nav-box">
			<view class="tab">
				总计：<text class="font-color-red">{{total_count}}</text>（个）
			</view>
			<view class="line"></view>
			<view class="tab">
				押金：<text class="font-color-red">{{total_pledge_money}}</text>（元）
			</view>
		</view>
		<view class="deposit-item">
			<view class="list flex" v-for="(item,index) in navList[tabCurrentIndex].orderList" @click="open(item)">
				<view class="content">
					<view class="title clamp2">
						{{item.product.keyword}}
					</view>
					<view class="flex type-list">
						<view class="ls">
							余桶：<text class="font-color-red">{{item.remain_num}}</text>
						</view>
						<view class="ls">
							押桶：<text class="font-color-red">{{item.pledge_num}}</text>
						</view>
						<view class="ls">
							借桶：<text class="font-color-red">{{item.borrow_num}}</text>
						</view>
					</view>
				</view>
				<view class="iconfont iconenter">

				</view>
			</view>
			
		</view>
		<uni-load-more :status="navList[tabCurrentIndex].loadingType"></uni-load-more>
		<uni-popup ref="popup" type="bottom">
			<view class="buttomBox">
				<view class="item borde-b" @click="addBarrel">
					押桶
				</view>
				<view class="item borde-b" @click="delBarrel">
					退桶
				</view>
			</view>
			<view class="buttomBox" @click="$refs.popup.close()">
				<view class="item qx">
					取消
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		certificate,
		createPledge,
		delPledge
	} from '@/api/water.js';
	export default {
		data() {
			return {
				tabCurrentIndex: 0,
				navList: [{
					state: 0,
					text: '全部',
					loadingType: 'more',
					orderList: [],
					page: 1, //当前页数
					limit: 10 //每次信息条数
				}],
				// 保存当前选中的桶对象
				actionItem: {},
				total_pledge_money: 0, //押金
				total_count: 0, //总数
			};
		},
		onReachBottom() {
			this.getGoodList();
		},
		onLoad: function(option) {
			this.getGoodList();
		},
		methods: {
			// 押桶
			addBarrel() {
				const that = this;
				uni.showModal({
					title: '押桶',
					placeholderText: '请输入押桶数量',
					editable: true,
					cancelText: '关闭',
					confirmText: '押桶',
					success: res => {
						if (res.confirm) {
							if(res.content.search(/^\d*$/)<0){
								uni.showModal({
									title: '错误',
									content: '请输入数字',
									showCancel: false,
								});
								return
							}
							createPledge({
								id: that.actionItem.id,
								num: res.content,
								pay_type: 'yue'
							}).then(() => {
								that.$refs.popup.close();
								uni.showToast({
									title: '押桶成功'
								})
								that.reloadList();
							}).catch(e => {
								console.log(e);
							});
						}
					},
				});
			},
			// 退桶
			delBarrel() {
				const that = this;
				uni.showModal({
					title: '退桶',
					placeholderText: '请输入退桶数量',
					editable: true,
					cancelText: '关闭',
					confirmText: '退桶',
					success: res => {
						if (res.confirm) {
							if(res.content.search(/^\d*$/)<0){
								uni.showModal({
									title: '错误',
									content: '请输入数字',
									showCancel: false,
								});
								return
							}
							console.log(that.actionItem.pledge_num,res.content);
							if(that.actionItem.pledge_num<+res.content){
								uni.showModal({
									title: '错误',
									content: '退桶数不可大于押桶数',
									showCancel: false,
								});
								return
							}
							delPledge({
								id: that.actionItem.id,
								num: res.content,
							}).then(() => {
								that.$refs.popup.close();
								uni.showToast({
									title: '退桶成功'
								})
								that.reloadList();
							}).catch(e => {
								console.log(e);
							});
						}
					},
				});
			},
			// 重新加载数据
			reloadList() {
				this.navList = [{
					state: 0,
					text: '全部',
					loadingType: 'more',
					orderList: [],
					page: 1, //当前页数
					limit: 10 //每次信息条数
				}];
				this.getGoodList();
			},
			// 打开选择弹窗
			open(item) {
				this.actionItem = item;
				this.$refs.popup.open()
			},
			// 加载数据
			getGoodList(source) {
				//这里是将订单挂载到tab列表下
				let index = this.tabCurrentIndex;
				let navItem = this.navList[index];
				let state = navItem.state;
				console.log(navItem, '数据');
				if (source === 'tabChange' && navItem.loaded === true) {
					//tab切换只有第一次需要加载数据
					return;
				}
				if (navItem.loadingType === 'loading') {
					//防止重复加载
					return;
				}
				if (navItem.loadingType === 'noMore') {
					//防止重复加载
					return;
				}
				// 修改当前对象状态为加载中
				navItem.loadingType = 'loading';
				certificate({
						page: navItem.page,
						limit: navItem.limit
					})
					.then(({
						data
					}) => {
						this.total_pledge_money = data.total_pledge_money;
						this.total_count = data.total_count;
						let arr = data.data.map(e => {
							return e;
						});

						navItem.orderList = navItem.orderList.concat(arr);
						// console.log(navItem.orderList);
						navItem.page++;
						if (navItem.limit == arr.length) {
							//判断是否还有数据， 有改为 more， 没有改为noMore
							navItem.loadingType = 'more';
							return;
						} else {
							//判断是否还有数据， 有改为 more， 没有改为noMore
							navItem.loadingType = 'noMore';
						}
						uni.hideLoading();
						this.$set(navItem, 'loaded', true);
					})
					.catch(e => {
						console.log(e);
					});
			},
		}
	};
</script>


<style lang="scss">
	.good-list {
		padding: 30rpx 0;
		width: 750rpx;

		.nav-box {
			border-radius: 10rpx;
			background-color: #FFF;
			border-bottom: 2px solid $page-color-base;

			.tab {
				width: 50%;
				height: 95rpx;
				text-align: center;
				flex-shrink: 1;
				font-size: $font-lg;
				font-weight: bold;
				line-height: 95rpx;
			}

			.line {
				height: 55rpx;
				width: 3px;
				background-color: $page-color-base;
			}
		}
	}

	.deposit-item {
		padding: 0 30rpx;
		background-color: #FFF;
		line-height: 1;

		.list {
			border-bottom: 2px solid $page-color-base;
			padding: 30rpx 0;

			.content {
				width: 0;
				flex-grow: 1;

				.title {
					font-size: $font-lg;
					font-weight: bold;
				}

				.type-list {
					padding-top: 30rpx;
					font-size: $font-sm;
					color: $font-color-base;

					.ls {
						width: 33%;
					}
				}
			}
		}
	}

	.buttomBox {
		border-radius: 20rpx;
		margin: 0 $page-row-spacing;
		margin-bottom: 30rpx;
		color: $font-color-dark;
		background-color: #FFFFFF;
		overflow: hidden;

		.item {
			line-height: 100rpx;
			height: 100rpx;
			text-align: center;
			font-size: 32rpx;
			color: $base-color;

			&.qx {
				color: $color-red;
			}
		}

		.border_b {
			border-bottom: 1px solid $page-color-light;
		}
	}
</style>
