<template>
  <div>
    <a-modal
      v-model:visible="vdata.visible"
      title="提现"
      :confirm-loading="confirmLoading"
      :closable="false"
      @ok="handleOk"
      @cancel="handleCancel"
    >
      <!-- <a-row>
        <a-col :sm="24">
          <a-descriptions>
            <a-descriptions-item label="ifCode">
              <a-tag color="purple">
                {{ vdata.cashoutData.ifCode }}
              </a-tag>
            </a-descriptions-item>
          </a-descriptions>
        </a-col>
        <a-col :sm="24">
          <a-descriptions>
            <a-descriptions-item label="appId">
              <a-tag color="purple">
                {{ vdata.cashoutData.appId }}
              </a-tag>
            </a-descriptions-item>
          </a-descriptions>
        </a-col>
        <a-col :sm="24">
          <a-descriptions>
            <a-descriptions-item label="可提现金额">
              <a-tag color="green">
                {{ vdata.cashoutData.amount/100 }}
              </a-tag>
            </a-descriptions-item>
          </a-descriptions>
        </a-col>
      </a-row> -->

      <a-form ref="cashoutInfo" :rules="rules" :model="vdata.cashoutData">
        <a-form-item label="提现金额" name="amount">
          <a-input-number v-model:value="vdata.cashoutData.amount" placeholder="请输入提现金额" :precision="2" style="width:100%" />
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>
<script setup lang="tsx" >
import { ENT_MCH_CHANNEL_ACCOUNT, req } from '@/api/manage'
import { ref, reactive, getCurrentInstance, defineProps, defineExpose } from 'vue'

// 定义函数
const props = defineProps({
  callbackFunc: { type: Function, default: () => () => ({}) }
})

// 导入全局函数
const { $infoBox, $access } = getCurrentInstance()!.appContext.config.globalProperties

// cashoutInfo组件
const cashoutInfo = ref()

let currentBalance = 0
let confirmLoading= false

const vdata : any = reactive({
  visible: false,
  cashoutData: null,  // 提现请求对象
})

const rules = {
  amount: [{ required: true, message: '请输入金额', trigger: 'blur',type:'number' },
  {
      validator: (rule, value) => {
          if (value < 0.01 || value >  currentBalance) {
            return Promise.reject('提现金额应大于0并且不能大于当前余额')
          }else{
            return Promise.resolve()
          }
      }
  }
  ]
}

function show (ifCode, balance, appId) {
  if (cashoutInfo.value !== undefined) {
    cashoutInfo.value.resetFields()
  }

  vdata.cashoutData = { ifCode, appId }
  currentBalance = balance

  vdata.visible = true
}

function handleOk (e) {
    cashoutInfo.value.validate().then( (valid) => {
    // 提现接口
    req.add(ENT_MCH_CHANNEL_ACCOUNT, { pageSize: -1, appId: vdata.cashoutData.appId, ifCode: vdata.cashoutData.ifCode, amount: vdata.cashoutData.amount }).then(res => {
      if (res) {
        if (res.state == 2) {
          $infoBox.message.success('提现成功')
        } else if (res.state == 3) {
          $infoBox.message.error('提现失败：' + res.failInfo)
        } else {
          $infoBox.message.success('已发起提现')
        }
      }
      
      vdata.visible = false
      props.callbackFunc() // 刷新列表
    })
  })
}
function handleCancel (e) {
    vdata.visible = false
}

defineExpose({ show })

</script>

<style scoped lang="less">

</style>
