<template>
	<view>
		<h-dark :title="t('recharge.title')"></h-dark>
		<view class="flex items-center bg-white px-[50rpx] py-[20rpx] mt-[12rpx]">
			<u-icon size="80" :name="method.logo"></u-icon>
			<view class="ml-[16rpx]">{{method.name}}</view>
		</view>
		<view class="bg-white mt-[20rpx] p-[40rpx]">
			<view class="mb-[15rpx] mt-[30rpx]">
				<view class="mb-[16rpx] text-muted">{{t('recharge.bankName')}}</view>
				<view class="bg-[#f6f7fb] rounded p-[20rpx]">
					<view class="flex break-all items-center justify-between ">
						<view>{{method.bank_name}}</view>
						<view @click.stop="copyText(method.bank_name)"><u-icon size="26"
								name="/static/images/common/copy_1.png"></u-icon></view>
					</view>
				</view>
			</view>
			<view class="mb-[15rpx] mt-[30rpx]">
				<view class="mb-[16rpx] text-muted">{{t('recharge.bankUsername')}}</view>
				<view class="bg-[#f6f7fb] rounded p-[20rpx]">
					<view class="flex break-all items-center justify-between ">
						<view>{{method.bank_username}}</view>
						<view @click.stop="copyText(method.bank_username)"><u-icon size="26"
								name="/static/images/common/copy_1.png"></u-icon></view>
					</view>
				</view>
			</view>
			<view class="mb-[15rpx] mt-[30rpx]">
				<view class="mb-[16rpx] text-muted">{{t('recharge.bankAccount')}}</view>
				<view class="bg-[#f6f7fb] rounded p-[20rpx]">
					<view class="flex break-all items-center justify-between ">
						<view>{{method.account}}</view>
						<view @click.stop="copyText(method.account)"><u-icon size="26"
								name="/static/images/common/copy_1.png"></u-icon></view>
					</view>
				</view>
			</view>
		</view>
		<view class="bg-white mt-[20rpx] px-[40rpx] py-[30rpx]">
			<view class="text-muted mb-[16rpx]">
				{{t('recharge.rechargeMoney')}}
			</view>
			<view class="font-bold text-lg mb-[50rpx]">
				{{getCurrency()}}{{formData.money}}
				<text class="text-muted" v-if="method.rate!=1">(≈
					{{formatMoney(formData.money,method.symbol,method.rate,method.precision)}})</text>
			</view>
			<view class="text-muted mb-[16rpx]">
				{{t('recharge.voucher')}}
			</view>
			<view class="">
				<view class="bg-[#f6f7fb] rounded w-full flex justify-center">
					<u-upload class="" ref="uploader" :custom-btn="true" :deletable="false" :auto-upload="false"
						max-count="1" :max-size="20*1024*1024" v-model="formData.voucher">
						<template v-slot:addBtn>
							<view class="flex items-center justify-center slot-btn w-[180rpx] h-[180rpx] "
								hover-class="slot-btn__hover" hover-stay-time="150">
								<u-icon name="photo" size="80" class="text-muted"></u-icon>
							</view>
						</template>
					</u-upload>
				</view>
			</view>
		</view>
		<view class=" px-[40rpx] my-[80rpx]  pb-[50rpx]">
			<u-button type="primary" @click="handleConfirm"> {{t('common.submit')}} </u-button>
		</view>
	</view>
</template>

<script lang="ts" setup>
	import { ref, reactive } from 'vue'
	import { onLoad } from '@dcloudio/uni-app'
	import { rechargeMethodDetail, recharge } from '@/api/finance'
	import { uploadImage } from '@/api/app'
	import { useUserStore } from '@/stores/user'
	import { formatDate, formatMoney, getCurrency } from '@/utils/util'
	import { useCopy } from '@/hooks/useCopy'
	import { t } from '@/utils/util'

	uni.setNavigationBarTitle({ title: t('recharge.title') })
	const { copy } = useCopy()
	const userStore = useUserStore()
	const uploader = ref();
	const method = ref<any>({})
	const formData = reactive({
		id: 0,
		money: '',
		voucher: '',
		date: formatDate(Math.round(Date.now() / 1000))
	})

	const copyText = async (text : string) => {
		copy(text);
		uni.$u.toast(t('common.copySuccess'))
	}


	const handleConfirm = async () => {
		if (uploader.value.lists.length <= 0) return uni.$u.toast(t('recharge.voucherEmpty'))
	const res : any = await uploadImage(uploader.value.lists[0].url, userStore.temToken!)
	formData.voucher = res.url;
	await recharge(formData)
	uni.redirectTo({
		url: '/pages/common/successful?type=0'
	})
	}

	const getData = async (id : number) => {
		method.value = await rechargeMethodDetail({ id })
	}
	onLoad((options : any) => {
		formData.id = options.id
		formData.money = options.money
		getData(formData.id)
	})
</script>

<style lang="scss">
	.shadow-inner {
		border-radius: 10px;
		padding: 5px 10px;
		box-shadow: inset 0px 1px 4px 0px rgb(0 0 0 / 0.3);
	}
</style>