<template>
	<view class="box" :style="{paddingTop: top + 'px'}">
		<u-navbar
			title="充值缴费"
			@leftClick="leftClick"
			bgColor="transparent"
			leftIconSize="20px"
			:autoBack="true"
		/>
		<view class="msg">
				账户余额(元)
				<view>{{money}}</view>
				<image src="https://wechat-img-file.oss-cn-beijing.aliyuncs.com/kitchen/recharge_img.png" mode="widthFix" />
		</view>

		<view class="recharge">
				<view class="rechargeTit">充值金额</view>
				<view class="iptbox">
						<view>￥</view>
						<input type="number" :value="amount" @input="headerAmountClick" placeholder="请输入充值金额" placeholder-style="color: #999999;font-size: 24rpx;" />
				</view>
				<view class="btn" @click="headerSubmitClick">
						确认充值
				</view>
		</view>
	
	</view>
	
</template>

<script>
	import { menuButtonInfo, request } from '../../utils/index';
	import { apiArr } from '../../api/kitchen';
	
	export default {
		data() {
			return {
				top: 85,
				money: 0, // 账户余额
				amount: '',
				goodsMsg: {},
			};
		},
		onLoad() {
			const meun = menuButtonInfo();
			this.top = meun.height + meun.top;
			this.init();
		},
		
		methods: {
			// 自定义导航栏 左侧返回按钮
			leftClick() {
				uni.navigateBack({
					delta: 1
				})
			},
			
			async init(){
			 // const res = await request(apiArr.balance, 'POST',{});
			 // console.log('erer', res);
				const res = {
					money: 4
				};
				this.money = res.money
			},
			
			headerAmountClick(e) {
				this.amount = e.detail.value
			},
			
			async headerSubmitClick() {
				console.log('12313', this);
				return;
				const res = request(apiArr.recharge, 'POST', {money: Number(this.amount)}, {silent: false});
				this.goodsMsg = res;
				this.pay(res);
			},
			
			// async pay(res) {
			// 	const res = request(apiArr.recharge, 'POST', {money: Number(this.amount)});
			// }
			
			  pay(res){
			    let that = this
			    util.postUrl2(PayJs.pay,{
			      address: '',
			      goods_id: String(res.order.id),
			      merchant_no: res.merchant_no,
			      out_trade_no: res.order.order_no,
			      quantity: 1,
			      remark: '',
			      subject: '用户充值',
			      term_no: res.term_no,
			      total_amount: String(res.order.money * 100),
			    //   total_amount:String(1),
			      user: String(wx.getStorageSync('userId')),
			      user_id: wx.getStorageSync('openId')
			    },res=>{
			        const {resp_data,msg} = res.data;
			        console.log('1123313', res);
			        wx.hideLoading()
			        if(msg == '成功'){
			          wx.requestPayment({
			            timeStamp:String(resp_data.acc_resp_fields.time_stamp),
			            nonceStr:String(resp_data.acc_resp_fields.nonce_str),
			            package:String(resp_data.acc_resp_fields.package),
			            signType:String(resp_data.acc_resp_fields.sign_type),
			            paySign:String(resp_data.acc_resp_fields.pay_sign),
			            success(res){
			              console.log('1111', res);
			              that.setData({
			                amount: ''
			              });
			              that.queryPay()
			            },
			            fail(fal){
			              console.log(fal);
			            }
			          })
			        }
			    })
			  },
			
			  queryPay(){
			      let that = this
			      const { GoodsMsg } = that.data;
			      console.log('queryPayqueryPayqueryPayqueryPayqueryPay', GoodsMsg);
			      util.postUrl2(PayJs.queryPay,{
			        merchant_no: GoodsMsg.merchant_no,
			        term_no: GoodsMsg.term_no,
			        out_trade_no: GoodsMsg.order.order_no
			      },res=>{
			        console.log('resresresresresresresresres', res);
			        if(res.statusCode == '200'){
			          wx.showToast({
			            title: '支付成功！',
			            success() {
			              setTimeout(() => {
			                that.getBanlance()
			              }, 1500)
			            }
			          })
			        }else{
			          wx.showToast({
			            title: res.msg,
			          })
			        }
			      })
			  },

		},
	}
	
</script>

<style>
	@import url("./index.css");
</style>