<template>
	<view>
		<u-modal :mask-close-able="true" confirm-text="确定提现" @confirm="confirm" v-model="passwordShow" title="提示">
			<view class="slot-content" style="padding: 20rpx;">
				<u-input v-model="withdrawPassword" placeholder="请填写提现密码" type="password" />
			</view>
		</u-modal>
		
		<view class="bg-img flex justify-center align-center" style="background-image: url('https://upload-file-data.obs.cn-south-1.myhuaweicloud.com/97d63ec49f544a33a6a8cc3c0b64b17a-songRankBgImg.png');height: 302upx;">
			<view class="text-white" >
				<text class="text-lg">提现金额：</text>
				<text style="font-size: 60rpx;">￥{{total}}</text>
			</view>
		</view>
		<view v-if="list" class="card" v-for="(item,index) in list" :key="index">
			<view class="left">
				<view>
					活动名称：
					<text class="text-red">{{item.activityName}}</text>
				</view>
				<view>
					可用热力：
					<text class="text-red">{{item.usableHotValue}}</text>
				</view>
				<view>
					可提余额：
					<text class="text-red">￥{{item.usableCash}}</text>
				</view>
			</view>
			<view class="right">
				<u-number-box  v-model="withdrawList[index].totalCash" :positive-integer="false"  :index="index" :min="0" :max="max(item.usableCash)"  :input-width="100" :input-height="60"></u-number-box>
				<text  class="padding-left-sm text-red">元</text>
			</view>
		</view>
		<view class="" style="height: 140rpx;"></view>
		<view 
			class="footer-fixed flex align-center justify-end padding bg-white"
			style="padding: 30rpx;;border-top: 1rpx solid #e5e5e5;z-index: 9;">
			<button class="cu-btn round text-white theme-bg-color" style="width: 180upx;height: 80upx;"
				@click="withdraw">提现</button>
		</view>
	</view>
</template>

<script>
	import md5Libs from "uview-ui/libs/function/md5";
	export default {
		computed:{
			max(){
				return data=>{
					if (this.$u.test.isEmpty(data)) {
						return 0
					}
					return parseInt(data)
				}
			},
			total:{
				get(){
					let total=0
					this.withdrawList.forEach(item=>{
						total+=item.totalCash
					})
					return total
				}
			}
		},
		data() {
			return {
				userId:'',
				list:[],
				withdrawList:[],
				withdrawPassword:'',
				passwordShow:false
			}
		},
		onLoad(options) {
			this.userId=options.userId
			if (!this.userId) {
				this.$u.toast('用户未登录')
				return
			}
			this.fetchList()
		},
		methods: {
			fetchList(){
				let params={
					userId:this.userId,
					size:500
				}
				this.$u.api.user.statisticalList(params).then(res=>{
					this.list=res.records
					this.withdrawList=[]
					this.list.forEach(item=>{
						let obj={
							id:item.id,
							userId:item.userId,
							activityId:item.activityId,
							totalCash:0,
						}
						this.withdrawList.push(obj)
					})
				})
			},
			withdraw(){
				this.passwordShow=true
			},
			confirm(){
				if (this.$u.test.isEmpty(this.withdrawPassword)) {
					this.$u.toast("请填写提现密码")
					return
				}
				if (this.total<=0) {
					this.$u.toast("请输入正确的兑换金额")
					return
				}
				this.$u.api.user.withdraw(this.withdrawList,md5Libs.md5(this.withdrawPassword)).then(res=>{
					if (res.code==400) {
						this.$dialog.showModal(res.msg,false)
					}else{
						this.$dialog.showModal(res,false).then(res=>{
							uni.navigateBack({
								delta:1
							})
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.card{
		margin: 15rpx;
		padding: 15rpx;
		border-radius: 12rpx;
		background-color: #FFFFFF;
		display: flex;
		justify-content: space-between;
		.left{
			display: flex;
			flex-direction: column;
			
			view{
				padding: 7rpx 0;
			}
		}
		
		.right{
			display: flex;
			justify-content: center;
			align-items: center;
		}
	}
</style>
