<template>
	<view>
		<view class="padding bg-themeRed">
			<view class="text-center padding-top">
				<view class="text-sl padding-bottom-xs">{{user_balance}}</view>
				<view>
					余额
				</view>
			</view>
			<view class="cu-list grid col-3 no-border">
			    <view class="cu-item" v-for="(v,k) in param">
					<view class="money-box" @click="cash(k)">
					  {{v.money}}元
					  <view class="cu-tag badge">
						{{v.type == 1 ? '总共' : '每天'}}
						{{v.num}}次
					  </view>
					</view>
			    </view>
			</view>
		</view>

		<view class="cu-list menu">
			<block v-for="(item,index) in plist" :key="index">
				<view class="cu-item">
					<view class="content padding-tb-sm padding-right-sm">
						<view>
							{{item.remark}}
						</view>
						<view class="text-gray text-sm">{{item.createtime}}</view>
					</view>
					<view class="action">{{item.money}}元</view>
				</view>
			</block>
		</view>
		
		<uni-load-more :status="pstatus"></uni-load-more>
	</view>
</template>

<script>
	import uniLoadMore from "@/components/uni-load-more/uni-load-more.vue";
import { userInfo } from "os";
	var _this;
	export default {
		components: {
			uniLoadMore
		},
		data() {
			return {
				userinfo: {},
				param: [],
				
				pstatus: 'more',
				ppage: 1,
				psize: 20,
				plist: []
			};
		},
		onLoad: function(){ 
			_this = this;
			_this.userinfo = _this.checkLogin("/pages/my/getmoney");
			if (_this.userinfo===false){
				return false;
			}
			_this.myGetmoney();
		},
		onPullDownRefresh: function() {
			_this.ppage = 1;
			_this.pstatus = 'more';
			_this.plist = [];
			_this.getMore();
		},
		onReachBottom: function() {
			if (_this.pstatus !== 'more') {
				return;
			}
			_this.getMore();
		},
		methods: {
			getMore: function() {
				_this.$req.ajax({
					path: "my/getmoneylist",
					data: {
						ppage: _this.ppage,
						psize: _this.psize,
						userid: _this.userinfo.id
					}
				}).then((data) => {
					_this.pstatus = data.pstatus;
					_this.plist = _this.plist.concat(data.plist);
					_this.ppage += 1;
					uni.stopPullDownRefresh();
				}).catch((err) => {
					uni.showModal({
						title: '信息提示',
						content: err,
						showCancel: false
					});
				});
			},
			myGetmoney: function() {
				_this.$req.ajax({
					path: "my/getmoneyparam",
					data: {
						userid: _this.userinfo.id 
					}
				}).then((data) => {
					if (data.getmoney.length === 0) {
						uni.showModal({
							title: '信息提示',
							content: '请联系管理员配置参数',
							showCancel: false,
							success: (res) => {
								uni.navigateBack();
							}  
						});
						return false;
					}
					_this.param = data.getmoney;
					_this.getMore();
				}).catch((err) => {
					uni.showModal({
						title: '信息提示',
						content: err,
						showCancel: false
					});
				});
			},
			cash: function(index) {
				uni.showModal({
					title: '信息提示',
					content: '确定要提现？',
					showCancel: true,
					success: (res) => {
						_this.$req.ajax({
							path: "my/cash",
							data: {
								index: index,
								userid: _this.userinfo.id
							}
						}).then((data) => {
							_this.userinfo = data;
							uni.setStorageSync('userinfo', data);
							_this.ppage = 1;
							_this.pstatus = 'more';
							_this.plist = [];
							_this.getMore();
							uni.showModal({
								title: '提现成功',
								content: '到账略有延迟，如果长时间未到账，请联系管理员',
								showCancel: false
							});
						}).catch((err) => {
							uni.showModal({
								title: '信息提示',
								content: err,
								showCancel: false
							});
						});
					}  
				});
			}
		},
		computed: {
			user_balance() {
				let balance = parseFloat(this.userinfo.balance);
				return balance.toFixed(2);
			}
		}
	}
</script>

<style>
	.money-box{width:80%;height:60rpx;line-height:60rpx;margin:0 auto;border-radius: 5rpx;border:1rpx solid #eee;font-size:40rpx;color:black;}
</style>
