<template>
  <a-drawer v-model:visible="vdata.isShow" title="账户操作" width="30%" :maskClosable="false" @close="vdata.isShow = false">
    <a-form ref="infoFormModel" :label-col="{span: 6}" :wrapper-col="{span: 15}">
      <a-form-item label="账号余额 ：" name="balanceAmount">
        <a-input v-model:value="vdata.balanceAmount" class="form-item-content" :disabled="true">
          <template #suffix>元</template>
        </a-input>
        <a-button :loading="vdata.confirmLoading1" size="small" style="margin-left: 20px;" type="primary" @click="queryBalanceFunc"><save-outlined />查询</a-button>
      </a-form-item>


      <a-form-item label="提现 ：" name="cashoutAmount">
        <a-input v-model:value="vdata.cashoutAmount" class="form-item-content">
          <template #suffix>元</template>
        </a-input>
        <a-button :loading="vdata.confirmLoading2" size="small" style="margin-left: 20px;" type="primary" @click="cashoutFunc"><send-outlined />提现</a-button>
      </a-form-item>
    </a-form>
  </a-drawer>
</template>
<script setup lang="ts">
import { $divisionReceiverBalance, $divisionReceiverCashout, req } from '@/api/manage'
import { reactive, ref, getCurrentInstance } from 'vue'

const { $infoBox } = getCurrentInstance()!.appContext.config.globalProperties


const vdata : any = reactive({

    confirmLoading1: false, // 显示确定按钮loading图标
    confirmLoading2: false, // 显示确定按钮loading图标
    isShow: false, // 是否显示弹层/抽屉
    recordId: null, // 更新对象ID

    balanceAmount: null, // 账户余额
    cashoutAmount: null, // 提现余额

})

function queryBalanceFunc(){

  vdata.confirmLoading1 = true
  $divisionReceiverBalance(vdata.recordId).then(res => {
    vdata.balanceAmount = ( ( res.balanceAmount || 0 ) / 100).toFixed(2)
  }).finally(() => {
    vdata.confirmLoading1 = false
  })

}


function cashoutFunc(){

  if(!vdata.cashoutAmount || vdata.cashoutAmount <= 0){
    return $infoBox.message.error('请填入金额')
  }

  $infoBox.confirmDangerPromise('确认提现？').then(() => {
    vdata.confirmLoading2 = true
    $divisionReceiverCashout(vdata.recordId, vdata.cashoutAmount).then(res => {
      if(res.state == 1){
        return $infoBox.message.success('申请成功')
      }

      return $infoBox.message.error('错误信息： ' + res.errMsg)

    }).finally(() => {
      vdata.confirmLoading2 = false
    })
  })

}

function show(recordId){
    vdata.balanceAmount = null
    vdata.cashoutAmount = null
    vdata.recordId = recordId
    vdata.isShow = true
}

defineExpose({ show })
</script>
<style>


.form-item-content{
  width: 60%
}

</style>