<template>
	<page-meta :page-style="themeColor"></page-meta>
	<view class="balance">
		<!-- #ifdef MP -->
		<view class="custom-navbar" :style="{
				'padding-top': menuButtonBounding.top + 'px',
				'height': menuButtonBounding.height + 'px'
			}"
		>
			<view class="navbar-wrap">
				<text class="iconfont icon-back_light back" @click="$util.redirectTo('/pages/member/index')"></text>
				<view class="navbar-title">
					我的消费券
				</view>
			</view>
		</view>
		<!-- #endif -->
		
		<view class="head-wrap" :style="{ background: 'url(' + $util.img('public/uniapp/balance/balance_bg.png') + ') no-repeat right bottom/ auto 340rpx, linear-gradient(314deg, #FE7849 0%, #FF1959 100%)' }">
			<view class="balance price-font">{{ (parseFloat(balanceInfo.balance) + parseFloat(balanceInfo.balance_money)).toFixed(2) }}</view>
			<view class="title">账户消费券（元）</view>
			<view class="flex-box">
				<view class="flex-item">
					<view class="num price-font">{{  balanceInfo.balance_money|moneyFormat }}</view>
					<view class="font-size-tag">现金消费券（元）</view>
				</view>
<!--				<view class="flex-item">-->
<!--					<view class="num price-font">{{ balanceInfo.balance|moneyFormat }}</view>-->
<!--					<view class="font-size-tag">储值消费券（元）</view>-->
<!--				</view>-->
			</view>
		</view>

		<view class="menu-wrap">
			<view class="menu-item" @click="toBalanceDetail">
				<view class="icon">
					<text class="iconfont icon-yuemingxi"></text>
				</view>
				<text class="title">消费券明细</text>
				<text class="iconfont icon-right"></text>
			</view>
			<view class="menu-item" @click="toOrderList" v-if="addonIsExist.memberrecharge && memberrechargeConfig && memberrechargeConfig.is_use">
				<view class="icon">
					<text class="iconfont icon-chongzhijilu"></text>
				</view>
				<text class="title">充值记录</text>
				<text class="iconfont icon-right"></text>
			</view>
		</view>
		
		<view class="action">
			<view @click="toList" class="recharge-withdraw " v-if="addonIsExist.memberrecharge && memberrechargeConfig && memberrechargeConfig.is_use">
				{{ $lang('recharge') }}
			</view>
			<view class="withdraw  " v-if="addonIsExist.memberwithdraw && withdrawConfig && withdrawConfig.is_use" @click="toWithdrawal">
				{{ $lang('withdrawal') }}
			</view>
      <view class="withdraw  "  @click="toGoufang">
        转赠
      </view>
		</view>
		<loading-cover ref="loadingCover"></loading-cover>
	</view>
</template>

<script>
export default {
	data() {
		return {
			balanceInfo: {
				balance: 0,
				balance_money: 0
			},
			withdrawConfig: null,
			memberrechargeConfig: null,
			menuButtonBounding: {} // 小程序胶囊属性
		};
	},
	async onShow() {
		this.getWithdrawConfig();
		this.getMemberrechargeConfig();
		if (!uni.getStorageSync('token')) {
			setTimeout(() => {
				this.$refs.login.open('/pages_tool/member/balance');
			});
		} else {
			this.getUserInfo();
		}
	},
	onLoad() {
		// #ifdef MP
		this.menuButtonBounding = uni.getMenuButtonBoundingClientRect();
		// #endif
	},
	methods: {
    toGoufang() {
      this.$util.redirectTo('/pages_tool/member/balance_transfer');
    },
		toWithdrawal() {
			this.$util.redirectTo('/pages_tool/member/apply_withdrawal');
		},
		toOrderList() {
			this.$util.redirectTo('/pages_tool/recharge/order_list');
		},
		toBalanceDetail() {
			this.$util.redirectTo('/pages_tool/member/balance_detail');
		},
		toList() {
			this.$util.redirectTo('/pages_tool/recharge/list');
		},
		//获取消费券信息
		getUserInfo() {
			this.$api.sendRequest({
				url: '/api/memberaccount/info',
				data: {
					account_type: 'balance,balance_money'
				},
				success: res => {
					if (res.data) {
						this.balanceInfo = res.data;
					} else {
						this.$util.showToast({
							title: res.message
						});
					}
					if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
				},
				fail: res => {
					mescroll.endErr();
					if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
				}
			});
		},
		/**
		 * 获取消费券提现配置
		 */
		getWithdrawConfig() {
			this.$api.sendRequest({
				url: '/api/memberwithdraw/config',
				success: res => {
					if (res.code >= 0 && res.data) {
						this.withdrawConfig = res.data;
					}
				}
			});
		},
		/**
		 * 获取充值提现配置
		 */
		getMemberrechargeConfig() {
			this.$api.sendRequest({
				url: '/memberrecharge/api/memberrecharge/config',
				success: res => {
					if (res.code >= 0 && res.data) {
						this.memberrechargeConfig = res.data;
					}
				}
			});
		}
	},
	onBackPress(options) {
		if (options.from === 'navigateBack') {
			return false;
		}
		this.$util.redirectTo('/pages/member/index', {}, 'reLaunch');
		return true;
	},
	watch: {
		storeToken: function(nVal, oVal) {
			if (nVal) {
				this.getUserInfo();
			}
		}
	},
	computed: {
		storeToken() {
			return this.$store.state.token;
		}
	}
};
</script>

<style lang="scss">
@import './public/css/balance.scss';
</style>
