<template>
	<mobile-frame>
		<view class="main">
			<view class="one">
				<view class="one_1">
					<text>¥</text><text>{{money}}</text>
				</view>
				<view class="one_2" @tap="toCash">
					<button type="default" size="mini">提现</button>
				</view>
			</view>
			<view class="two">
				<tabs :tabs="tabs" @tabsChange="tabsChange">
					<view class="tabsList">
						<scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage" @scroll="toScroll">
							<view class="list-scroll-view">
								<view class="list" v-for="(item, index) in list" :key="index">
									<view v-if="tabs.active=='0'" class="other" @tap="toDetail(item,'0')">
										<view class="other_1">
											<view class="source textOver">{{item.zhSource}}</view>
											<view class="time">{{item.time}}</view>
										</view>
										<view class="other_2">
											<view class="money">+{{item.money}}</view>
											<text class="iconfont icon-jiantouyou"></text>
										</view>
									</view>
									<view v-if="tabs.active=='1'" class="other" @tap="toDetail(item,'1')">
										<view class="other_1">
											<view class="source textOver">{{item.apply_reason}}</view>
											<view class="time">{{item.apply_time}}</view>
										</view>
										<view class="other_2">
											<view class="money">-{{item.money}}</view>
											<text class="iconfont icon-jiantouyou"></text>
										</view>
									</view>
								</view>
								<view class="is_bottom" v-if="is_bottom">
									<text>{{config.bottom_title}}</text>
								</view>
							</view>
						</scroll-view>
					</view>
				</tabs>
			</view>
		</view>
		<uni-popup ref="popup" background-color="#fff" type="bottom">
			<view class="popup">
				<uni-forms ref="form" :rules="rules" :model="form" label-width="auto">
					<uni-forms-item label="姓名" name="name">
						<uni-easyinput disabled v-model="form.name" placeholder="请输入姓名" />
					</uni-forms-item>
					<uni-forms-item label="提现金额" name="money">
						<uni-easyinput type="digit" v-model="form.money" @input="toMoney" placeholder="请输入提现金额" />
						<view class="money">
							可提现金额为{{moneyInfo.payTotal||0}},<text @tap="toAll">全部提现</text>
						</view>
					</uni-forms-item>
					<uni-forms-item label="提现理由" name="apply_reason">
						<uni-easyinput type="textarea" v-model="form.apply_reason" placeholder="请输入提现理由" />
					</uni-forms-item>
					<view class="btn">
						<button type="primary" @tap="onSubmit('form')">提现</button>
					</view>
				</uni-forms>
			</view>
		</uni-popup>
	</mobile-frame>
</template>

<script>
	import moment from 'moment';
	import tabs from '@/components/tabs/index.vue';
	export default {
		components: {
			tabs
		},
		data() {
			return {
				// 系统设置
				config: {},
				user: {},
				money: 0,
				tabs: {
					active: '0',
					menu: [ //菜单列表
						{
							title: '收益记录',
							active: '0'
						},
						{
							title: '提现记录',
							active: '1'
						}
					]
				},
				// 提现金额
				moneyInfo: {},
				// 提现
				form: {},
				rules: {
					money: {
						rules: [{
							required: true,
							errorMessage: '请输入提现金额',
						}]
					},
					apply_reason: {
						rules: [{
							required: true,
							errorMessage: '请输入提现理由',
						}]
					},
				},
				list: [],
				total: 0,
				skip: 0,
				limit: 10,
				page: 0,
				// 来源
				sourceList: [],
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0,
			};
		},
		onShow: async function(e) {
			const that = this;
			that.searchConfig();
			await that.searchOther();
			await that.watchLogin();
		},
		onHide: function() {
			const that = this;
			that.clearPage();
		},
		methods: {
			// 查询基本设置
			searchConfig() {
				const that = this;
				uni.getStorage({
					key: 'config',
					success: function(res) {
						if (res.data) that.$set(that, `config`, res.data)
					},
					fail: function(err) {
						console.log(err);
					}
				})
			},
			watchLogin() {
				const that = this;
				uni.getStorage({
					key: 'token',
					success: function(res) {
						let user = that.$jwt(res.data);
						that.$set(that, `user`, user)
						that.search()
					}
				})
			},
			// 查询列表
			async search() {
				const that = this;
				let user = that.user;
				let active = that.tabs.active;
				if (user._id) {
					let info = {
						skip: that.skip,
						limit: that.limit,
					}
					let res;
					if (active == '0') {
						info.inviter = user._id;
						// 收益记录
						res = await that.$api(`/cashBack`, 'GET', {
							...info,
						})
					} else {
						info.customer = user._id;
						// 提现记录
						res = await that.$api(`/cashOut`, 'GET', {
							...info,
						})
					}
					if (res.errcode == '0') {
						let list = [...that.list, ...res.data];
						for (let val of list) {
							let source = that.sourceList.find(i => i.value == val.source)
							if (source) val.zhSource = source.label;
						}
						that.$set(that, `list`, list);
						that.$set(that, `total`, res.total)
					}
					let arr = await that.$api(`/cashBack/computedTotal`, 'GET', {
						customer: user._id
					})
					if (arr.errcode == '0') that.$set(that, `money`, arr.data);
				}
			},
			// 查询其他信息
			async searchOther() {
				const that = this;
				let res;
				res = await that.$api(`/dictData`, 'GET', {
					code: "cashBack_source"
				});
				if (res.errcode == '0') {
					that.$set(that, `sourceList`, res.data)
				}
			},

			// 分页
			toPage(e) {
				const that = this;
				let list = that.list;
				let limit = that.limit;
				if (that.total > list.length) {
					uni.showLoading({
						title: '加载中',
						mask: true
					})
					let page = that.page + 1;
					that.$set(that, `page`, page)
					let skip = page * limit;
					that.$set(that, `skip`, skip)
					that.search();
					uni.hideLoading();
				} else that.$set(that, `is_bottom`, true)
			},
			toScroll(e) {
				const that = this;
				let up = that.scrollTop;
				that.$set(that, `scrollTop`, e.detail.scrollTop);
				let num = Math.sign(up - e.detail.scrollTop);
				if (num == 1) that.$set(that, `is_bottom`, false);
			},
			// 选择选项卡
			tabsChange(e) {
				const that = this;
				that.$set(that.tabs, `active`, e.active)
				that.clearPage();
				that.search()
			},
			// 清空列表
			clearPage() {
				const that = this;
				that.$set(that, `list`, [])
				that.$set(that, `skip`, 0)
				that.$set(that, `limit`, 6)
				that.$set(that, `page`, 0)
			},
			// 提现
			toCash() {
				const that = this;
				let user = that.user;
				that.$set(that.form, `name`, user.name)
				that.$refs.popup.open()
			},
			// 提现金额
			toMoney(value) {
				const that = this;
				let money = that.moneyInfo.payTotal;
				if (parseFloat(value) > parseFloat(money)) {
					uni.showToast({
						title: '输入金额不能超过可提现金额',
						icon: 'none'
					})
				}
			},
			// 全部提现
			toAll() {
				const that = this;
				that.$set(that.form, `money`, that.moneyInfo.payTotal)
			},
			// 提交保存
			async onSubmit(ref) {
				const that = this;
				let money = that.moneyInfo.payTotal;
				that.$refs[ref].validate().then(async params => {
					if (parseFloat(params.money) > parseFloat(money)) {
						uni.showToast({
							title: '输入金额不能超过可提现金额',
							icon: 'none'
						})
					} else {
						params.customer = that.user._id;
						params.apply_time = moment().format('YYYY-MM-DD HH:mm:ss')
						const arr = await that.$api(`/cashOut`, 'POST', params);
						if (arr.errcode == '0') {
							uni.showToast({
								title: `提现申请成功`,
								icon: 'success',
							});
							that.$refs.popup.close();
							that.clearPage();
							that.search()
						} else {
							uni.showToast({
								title: arr.errmsg,
								icon: 'none',
							})
						}
					}
				})
			},
			// 明细
			toDetail(item, type) {
				const that = this;
				that.clearPage();
				uni.navigateTo({
					url: `/pagesMy/assets/info?id=${item._id}&type=${type}`
				})
			},
		},
		onPullDownRefresh: async function() {
			const that = this;
			that.$set(that, `list`, [])
			that.$set(that, `skip`, 0)
			that.$set(that, `limit`, 6)
			that.$set(that, `page`, 0)
			await that.search();
			uni.stopPullDownRefresh();
		}
	}
</script>

<style lang="scss">
	.main {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;

		.one {
			background-color: var(--fFB1Color);
			padding: 5vw 2vw;
			display: flex;
			justify-content: space-between;

			.one_1 {
				color: #fff;
				font-size: 15px;
				font-weight: bold;

				text:last-child {
					padding: 0 0 0 2vw;
					font-size: 25px;
				}
			}

			.one_2 {
				button {
					background: #fff;
					color: #ff0000;
					border-radius: 25px;
					font-size: 14px;
				}
			}

		}

		.two {
			position: relative;
			flex-grow: 1;
			padding: 2vw 0 0 0;

			.tabsList {
				position: relative;
				width: 100vw;
				height: 82vh;

				.list {
					width: 92vw;
					border-bottom: 0.5vw solid var(--f1Color);
					margin: 2vw 2vw 0 2vw;
					padding: 2vw;
					border-radius: 5px;

					.other {
						display: flex;
						justify-content: space-between;
						align-items: center;

						.other_1 {
							width: 60vw;
							font-size: var(--font16Size);
							margin: 0 0 1vw 0;

							.time {
								margin: 1vw 0 0 0;
								color: var(--f85Color);
								font-size: var(--font12Size);
							}
						}

						.other_2 {
							display: flex;
							align-items: center;

							.money {
								font-size: var(--font18Size);
								font-weight: bold;
								margin: 0 1vw;
							}

							.iconfont {
								font-size: 16px;
							}
						}
					}
				}
			}
		}
	}

	.scroll-view {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;

		.list-scroll-view {
			display: flex;
			flex-direction: column;
		}
	}

	.is_bottom {
		text-align: center;

		text {
			padding: 2vw 0;
			display: inline-block;
			color: #858585;
			font-size: 14px;
		}
	}

	.popup {
		min-height: 80vw;
		padding: 2vw;

		.money {
			margin: 1vw 0 0 0;
			font-size: 12px;

			text {
				color: #6A5ACD;
			}
		}

		.btn {
			text-align: center;

			button {
				background-color: var(--fFB1Color);
			}
		}
	}
</style>
