<template>
	<view class="com-donate-key" v-if="info">
		<view class="bg"></view>
		<view class="current">您当前有{{info.user.integral}}个爱心币可捐</view>
		<view class="press-key">
			<view class="sign left" @click="onChangeQty(-1)">-</view>
			<view class="text">{{qty}}</view>
			<view class="sign right" @click="onChangeQty(1)">+</view>
		</view>
		<view class="number-key">
			<view class="left">
				<view class="number">
					<block v-for="(it,index) in nums">
						<view class="key" @click="onDonate(it)">{{it}}</view>
					</block> 
				</view>
				<view class="number1">
					<view class="key" @click="onDonate(0)">0</view>
					<view class="all" @click="onDonate(999)">全部捐出</view>
				</view>
			</view>
			<view class="right">
				<view class="delete" @click="onDonate(-1)">删除</view>
				<view class="enter" @click="onSave">确认捐款</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name:"DonateKeyForm",
		emits:['onSuccess'],
		props:{
			info:{
				type:Object,
				default:()=>{
					return null;
				}
			},
			storeId:{
				type:Number,
				default:()=>{
					return 0;
				}
			}
		},
		data() {
			return {
				qty:0,
				nums:[1,2,3,4,5,6,7,8,9],
			};
		},
		methods:{
			
			onSave(e){
				let _this=this;
				let par={
					'donateId':_this.info.info.id,
					'userId':_this.info.user.id,
					'qty':_this.qty,
					'storeId':_this.storeId,
				}
				this.$http.request('donate/create', par, "", "", true).then(res => {
					
					if(res.code=='200'){
						uni.showToast({
							title:"感谢您的爱心捐赠",
							icon:"success"
						})
						_this.$emit("onSuccess")
					}  
				}).catch(err => {})
			},
			
			onDonate(qty){
				let _this=this; 
				if(qty==999){
					//捐出所有
					_this.qty=_this.info.user.integral;
					return;
				} 
				if(qty==-1){
					_this.qty=0;
					return ;
				}
				// console.log("start qty:",_this.qty);
				_this.qty+=""+qty;
				_this.qty=parseInt(_this.qty);
				
				// console.log(_this.qty);
				// console.log("_this.info.user.integral",_this.info.user.integral);
				if(_this.qty*1>=_this.info.user.integral*1){
					// console.log("A");
					_this.qty=_this.info.user.integral;
				} 
				// console.log("_this.qty",_this.qty);
			},
			//修改金额
			onChangeQty(qty){
				let _this=this; 
				if(qty<1){ 
					if(_this.qty<=0){ 
						_this.qty=0;
					}else{ 
						_this.qty+=qty;
					}
				}else{ 
					_this.qty+=qty;
				} 
				if(_this.qty>_this.info.user.integral){ 
					_this.qty=_this.info.user.integral;
				}
			},
			
		}
	}
</script>

<style lang="scss">
@import './DonateKeyForm.scss'
</style>