<template>
	<view class="main">
		<view class="tips">
			<text class="icon">&#xe694;</text>
			<text>账户余额保障中</text>
		</view>
		<view class="mcard">
			<view class="money">{{ item.money }}</view>
			<view class="desc">账户余额（元）</view>
			<view class="flex">
				<view class="f">
					<button class="btn" style="background-color: #607d8b" @click="go('/pages/user/money/cash_out')">
						<text class="icon">&#xe612;</text>
						<text>提现</text>
					</button>
				</view>
				<view class="f">
					<button class="btn" @click="go('/pages/user/money/add')">
						<text class="icon">&#xe7c4;</text>
						<text>充值</text>
					</button>
				</view>
			</view>
		</view>
		<view class="tab">
			<u-tabs :list="type" @click="click($event, 'type')"></u-tabs>
		</view>
		<view class="list">
			<view class="item" v-for="(item, index) in list" :key="index">
				<view class="title">{{ item.title }}</view>
				<view class="desc">
					<text v-if="item.state == 0 && item.type == 0" style="color: #f44336">未支付</text>
					<text v-if="item.state == 1 && item.type == 0" style="color: #4caf50">充值成功</text>
					<text v-if="item.state == 1 && item.type == 1" style="color: #4caf50">兼职支出</text>
					<text v-if="item.state == 0 && item.type == 2" style="color: #f44336">提现审核中...</text>
					<text v-if="item.state == 1 && item.type == 2" style="color: #4caf50">提现成功</text>
					<text v-if="item.state == 1 && item.type == 3" style="color: #4caf50">兼职收入</text>
					<text v-if="item.state == 2 && item.type == 2" style="color: #f44336" @click="popup(item.msg)">
						<text class="icon" style="padding-right: 5px">&#xe610;</text>
						<text>提现失败，退回余额</text>
					</text>
					<text v-if="item.state == 1 && item.type == 4" style="color: #4caf50">退款成功</text>
					<text>{{ item.createTime }}</text>
				</view>
				<view class="price">
					<text v-if="item.type == 0 || item.type == 3 || item.type == 4" style="color: orangered">+{{ item.money }}元</text>
					<text v-if="item.type == 1" style="color: darkgray">-{{ item.money }}元</text>
					<text v-if="item.type == 2 && item.state == 0" style="color: darkgray">-{{ item.money }}元</text>
					<text v-if="item.type == 2 && item.state == 1" style="color: darkgray">-{{ item.money }}元</text>
					<text v-if="item.type == 2 && item.state == 2" style="color: #f44336">+{{ item.money }}元</text>
				</view>
			</view>
			<view class="loading" v-if="loadMore"><u-loadmore :status="loadMore ? 'loading' : 'nomore'" /></view>
			<u-empty v-if="!loadMore && list.length == 0"></u-empty>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			item: {},
			current: 0,
			type: [
				{ name: '全部', value: '' },
				{ name: '充值', value: 0 },
				{ name: '支出', value: 1 },
				{ name: '提现', value: 2 },
				{ name: '收入', value: 3 },
				{ name: '退款', value: 4 }
			],
			list: [],
			param: { pageNum: 1, pageSize: 10 },
			loadMore: true
		};
	},
	onLoad(e) {
		this.getInfo();
		this.getData();
		uni.$on('payMoney', (res) => {
			this.getInfo();
			this.refresh();
		});
	},
	methods: {
		getInfo() {
			this.http.request({
				url: '/app/user/info',
				success: (res) => {
					this.item = res.data.data;
					uni.setStorageSync('money', res.data.data.money || 0);
				}
			});
		},
		getData() {
			this.http.request({
				url: '/app/pay/list',
				data: this.param,
				loading: 'false',
				success: (res) => {
					this.loadMore = res.data.pages > this.param.pageNum ? true : false;
					this.list.push(...res.data.rows);
				}
			});
		},
		click(e) {
			this.param.type = e.value;
			this.refresh();
		},
		popup(msg) {
			uni.showModal({ title: '提示', content: msg, showCancel: false });
		},
		go(url) {
			uni.navigateTo({ url: url });
		},
		//刷新数据
		refresh() {
			this.loadMore = true;
			this.param.pageNum = 1;
			this.list = [];
			this.getData();
		}
	},
	//下拉刷新
	onPullDownRefresh() {
		setTimeout(() => {
			this.refresh();
			this.getInfo();
			uni.stopPullDownRefresh();
		}, 1000);
	},
	//上拉加载
	onReachBottom() {
		if (this.loadMore) {
			this.param.pageNum++;
			this.getData();
		}
	}
};
</script>

<style lang="scss">
.main {
	padding-top: 5px;
}
.tips {
	border-radius: 5px;
	margin-bottom: 8px;
}
.list {
	background-color: white;
	border-radius: 8px;
	padding: 15px 15px 15px 15px;
	.item {
		padding: 15px 0px 15px 0px;
		border-bottom: 1px solid $line;
		padding-bottom: 10px;
		&:first-child {
			padding-top: 0px;
		}
		&:last-child {
			border: 0px;
			padding-bottom: 5px;
		}
		.title {
			width: 70%;
		}
		.desc {
			font-size: 14px;
			padding-top: 5px;
			color: $font-c;
			text {
				padding-right: 15px;
			}
		}
		.price {
			float: right;
			margin-top: -48px;
		}
	}
}
</style>
