<template>
	<view class="box">
		<view class="box-head flexs">
			<!-- 修改 in 2 改成 in 3 标题名调用titleName方法 -->
			<view class="box-head-item center" @click="changeIndex(index)" v-for="(item, index) in 3" :class="{ active: index == i }" :key="index">{{ titleName(index) }}</view>
		</view>
		<!-- 修改 -->
		<view class="box_ul">
			<!-- 待提货 已回列表 -->
			<template v-if="i < 2">
				<view class="main" v-for="(item, index) in boxList" :key="index" @click="change(item)">
					<image class="main-icon" v-if="i == 0" :src="item.flag ? '/static/image/publice/xuanzhong1@2x.png' : '/static/image/me/weixuanzhong@2x.png'" mode=""></image>
					<view class="main-img"><image :src="item.goods_image" mode="aspectFill"></image></view>
					<view class="main-main">
						<view class="main-name">
							<!-- 修改 -->
							<text>{{ item.goods_name }}</text>
						</view>
						<view class="main-price">
							<view class="kh-price">
								开盒价：
								<text>{{ item.box_coin_price }}金币</text>
							</view>
							<view class="xs-price">
								销售价：
								<text>{{ item.goods_coin_price }}金币</text>
							</view>
							<view class="tk-price">
								退款价：
								<text>{{ item.goods_hui_price }}金币</text>
							</view>
						</view>
					</view>
					<view class="main_time flex">
						<!-- 修改 -->
						<view>时间：{{ item.time }}</view>
						<!-- 新增 -->
						<view v-if="i == 0" class="item-btn center" @click.stop="donationOpen(item)">一键转赠</view>
					</view>
				</view>
			</template>
			<!-- 新增 转赠列表 -->
			<template v-else>
				<view class="main flexs" v-for="(item, index) in donationList" :key="'donation-' + index" @click="item.flag = !item.flag">
					<view class="main-img"><image :src="item.zzimage" mode="aspectFill"></image></view>
					<view class="main-main">
						<view class="main-name">
							<!-- 修改 -->
							<text>{{ item.boxgoods }}</text>
						</view>
						<view class="main-price flex">
							<view class="main_time">时间：{{ item.zhuanztime }}</view>
							<text style="text-align: right;">已转赠</text>
						</view>
						<view class="main_time">受赠人：{{ item.szuser }}</view>
					</view>
				</view>
			</template>
		</view>
		<view class="box_footer flex" v-if="i == 0">
			<view class="box_footer_all flexs">
				<image @click="changeAll" :src="isAll ? '/static/image/publice/xuanzhong1@2x.png' : '/static/image/me/weixuanzhong@2x.png'" mode=""></image>
				<text>全选({{ totalNumber }})</text>
			</view>
			<view class="box_footer_ul flexs">
				<view class="btn-box-item center" v-for="(items, index) in 2" :key="index" @click="apply(index)">{{ index == 0 ? '一键退款' : '申请发货' }}</view>
			</view>
		</view>
		<!-- 新增 转赠窗口 -->
		<uni-popup ref="donation">
			<view class="donation">
				<view class="title">请输入您转赠人的手机号</view>
				<view class="input-container"><input v-model="donationPhone" type="number" placeholder="请输入转赠人注册手机号" /></view>
				<view class="tip">注：一旦转赠成功不可退货，请谨慎操作</view>
				<view class="btn-box">
					<view class="btn-box-item center" @click="$refs.donation.close()">取消</view>
					<view class="btn-box-item center" @click="donation()">确定</view>
				</view>
			</view>
		</uni-popup>
		<recycle ref="recycle"></recycle>
	</view>
</template>

<script>
import recycle from '@/components/recycle.vue';
export default {
	components: {
		recycle
	},
	data() {
		return {
			i: 0, //
			page: 1,
			isAll: false, //
			pages: null, //
			totalNumber: 0, //总数量
			boxList: [], //盒子列表
			donationList: [], //转赠列表
			recordId: null, //转赠商品id
			donationPhone: null //转赠人手机号
		};
	},
	watch: {
		boxList: {
			handler(val) {
				let totalNumber = 0;

				this.isAll = val.every(item => item.flag);
				if (val.length == 0) this.isAll = false;
				val.forEach(item => {
					if (item.flag) {
						totalNumber++;
					}
				});
				this.totalNumber = totalNumber;
			},
			deep: true //true 深度监听
		}
	},
	methods: {
		// 新增 标题名称
		titleName(index) {
			switch (index) {
				case 0:
					return '待提货';
				case 1:
					return '已退款';
				case 2:
					return '已转赠';
			}
		},
		//申请发货
		apply(index) {
			if (!this.boxList.some(item => item.flag)) return uni.showToast({ title: '请勾选要' + (index == 0 ? '退款' : '发货') + '的商品', icon: 'none' });
			let ids = [];
			this.boxList.forEach(item => {
				if (item.flag) {
					ids.push(item.record_id);
				}
			});
			if (index == 0) {
				uni.showModal({
					cancelText: '取消',
					confirmText: '确认',
					title: '一键退款',
					content: '是否确认一键退款？',
					success: res => {
						if (res.confirm) {
							this.$api.exchange({ record_ids: ids.join(',') }).then(res => {
								if (res.code === 1) {
									uni.showToast({ title: res.msg });
									this.page = 1;
									this.getMyBox();
									this.$refs['recycle'].open(res.data);
								}
							});
						}
					}
				});
			} else {
				uni.navigateTo({ url: '/pages/me/applyFaHuo?id=' + ids.join(',') });
			}
		},
		//全选
		changeAll() {
			this.isAll = !this.isAll;
			this.boxList.forEach(item => {
				item.flag = this.isAll;
			});
		},
		// 选择和取消
		change(item) {
			item.flag = !item.flag;
		},
		changeIndex(index) {
			this.i = index;
			this.page = 1;
			this.boxList = [];
			this.getMyBox();
		},
		//获取我的盒柜
		getMyBox() {
			// 新增 获取已转赠
			if (this.i == 2) {
				this.$api.echargezs({ msg: '数据加载中' }).then(res => {
					this.donationList = res.data;
				});
				return;
			}
			this.$api.myBox({ status: this.i + 1, page: this.page, msg: '数据加载中' }).then(res => {
				if (res.code === 1) {
					res.data.data.forEach(item => {
						item.flag = false;
					});
					this.pages = res.data.last_page;
					this.boxList = this.page == 1 ? res.data.data : [...this.boxList, ...res.data.data];
				}
			});
		},
		// 新增 打开转赠窗口
		donationOpen(item) {
			this.recordId = item.record_id;
			this.$refs.donation.open();
		},
		// 新增 转赠提交
		donation() {
			if (!this.checkPhone()) {
				return;
			}
			this.$api.echargez({ record_ids: this.recordId, mobile: this.donationPhone }).then(res => {
				if (res.code === 1) {
					//转赠成功
					const index = this.boxList.findIndex(item => item.record_id == this.recordId);
					this.boxList.splice(index, 1);
					this.$refs.donation.close();
					uni.showToast({
						icon: 'success',
						title: '转赠成功'
					});
				}
			});
		},
		/* 新增 检查手机号 */
		checkPhone() {
			if (!/^1[345789]\d{9}$/.test(this.donationPhone)) {
				uni.showToast({
					title: '请输入正确的手机号码',
					icon: 'none'
				});
				return false;
			}
			return true;
		}
	},
	onShow() {
		this.getMyBox();
	},
	onReachBottom() {
		if (this.i == 2) {
			return;
		}
		if (this.page < this.pages) {
			this.page++;
			this.getMyBox();
		}
	}
};
</script>

<style lang="scss">
.main-icon {
	height: 25px;
	width: 25px;
	float: left;
	position: relative;
	top: 42px;
}
.box-head {
	position: sticky;
	top: 88rpx;
	/* #ifndef H5 */
	top: 0;
	/* #endif */
	left: 0;
	background: #fafafa;
	z-index: 2021;
	width: 100%;
	.box-head-item {
		flex: 1;
		font-size: 30rpx;
		height: 90rpx;
		font-weight: bold;
	}
	.active {
		border-radius: 100rpx;
		color: #fe3b3b;
	}
}
.box_ul {
	padding: 0 30rpx 98rpx 30rpx;
	.main {
		padding: 15rpx;
		margin-bottom: 20rpx;
		background: #ffffff;
		border-radius: 20rpx;
		.main-img {
			width: 30%;
			float: left;
			image {
				width: 200rpx;
				height: 200rpx;
				border-radius: 10rpx;
			}
			margin-right: 20rpx;
		}
		.main-main {
			flex: 1;
		}
		//修改
		.main-name {
			font-size: 28rpx;
			display: flex;
			overflow: hidden;
			text {
				display: -webkit-box;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 2;
				flex: 1;
				font-weight: bold;
			}
		}
		.main-price {
			text {
				color: #fe3b3b;
				font-size: 28rpx;
				font-weight: 600;
			}
			image {
				width: 44rpx;
				height: 44rpx;
			}
			margin: 15rpx 0;
		}
		.main-price .tk-price,
		.kh-price,
		.xs-price {
			display: block;
			margin: 10rpx 0;
			uni-text {
				float: right;
			}
		}
		.main-price .tk-price {
			uni-text {
				color: #000000;
			}
		}
		.main_time {
			color: #999999;
			font-size: 22rpx;
			align-items: center;
			margin-top: 20rpx;
			uni-text {
				line-height: 70rpx;
			}
			uni-image {
				width: 48rpx;
				height: 48rpx;
				float: right;
			}
			.item-btn {
				background-image: linear-gradient(0deg, #ff97c5 0%, #fe3b3b 100%);
				color: #ffffff;
				border-radius: 19px;
				font-size: 28rpx;
				margin-left: auto;
				width: 200rpx;
				height: 60rpx;
				float: right;
			}
		}
	}
}
.box_footer {
	height: 98rpx;
	z-index: 20;
	position: fixed;
	bottom: 92rpx;
	/* #ifndef H5 */
	bottom: 0;
	/* #endif */
	width: 100%;
	padding: 0 30rpx;
	background: #ffffff;
	.box_footer_all {
		image {
			width: 44rpx;
			height: 44rpx;
			margin-right: 10rpx;
		}
		text {
			color: #000000;
			font-size: 28rpx;
		}
	}
	.btn-box-item {
		color: #ffffff;
		width: 200rpx;
		height: 70rpx;
		border-radius: 39rpx;
		font-size: 30rpx;
		margin-left: 30rpx;
		&:first-child {
			// background-image: -moz-linear-gradient( 60deg, rgb(255,200,222) 0%, rgb(255,103,164) 100%);
			// background-image: -webkit-linear-gradient( 60deg, rgb(255,200,222) 0%, rgb(255,103,164) 100%);
			// background-image: -ms-linear-gradient( 60deg, rgb(255,200,222) 0%, rgb(255,103,164) 100%);
			background-image: linear-gradient(0deg, #b0f7b9 0%, #42f16b 100%);
		}
		&:last-child {
			background-image: linear-gradient(0deg, #ff97c5 0%, #fe3b3b 100%);
			// background-image: -moz-linear-gradient( 0deg, rgb(137,247,254) 0%, rgb(102,166,255) 100%);
			// background-image: -webkit-linear-gradient( 0deg, rgb(137,247,254) 0%, rgb(102,166,255) 100%);
			// background-image: -ms-linear-gradient( 0deg, rgb(137,247,254) 0%, rgb(102,166,255) 100%);
		}
	}
}
// 新增 转赠窗口样式
.donation {
	background-color: #fff;
	padding: 30rpx 40rpx;
	border-radius: 10px;
	.title {
		font-size: 16px;
		font-weight: bold;
		text-align: center;
		margin-bottom: 30rpx;
	}
	.input-container {
		background-color: #f3f3f3;
		padding: 16rpx 20rpx;
		border-radius: 6rpx;
		margin-bottom: 20rpx;
		input {
			font-size: 30rpx;
		}
	}
	.tip {
		font-size: 12px;
		color: red;
		text-align: center;
		margin-bottom: 30rpx;
	}
	.btn-box {
		display: flex;
		gap: 30rpx;
		view {
			color: #ffffff;
			flex: 1;
			width: 100%;
			height: 70rpx;
			border-radius: 39rpx;
			font-size: 30rpx;
			&:first-child {
				background: -webkit-linear-gradient(60deg, #ffc8de 0%, #ff67a4 100%);
			}
			&:last-child {
				background: -webkit-linear-gradient(0deg, #89f7fe 0%, #66a6ff 100%);
			}
		}
	}
}
</style>
