<template>
  <div class="withdraw-popup-warn">
    提现提示：申请提现后自动进入审核流程，审核需要t+7个工作日，节假日顺延，审核成功后自动打款
  </div>
  <el-form :model="formData" label-width="120px" ref="ruleForm" :rules="rules" v-loading="loading" class="pr-4">
    <div class="flex flex-row justify-between">
      <div class="w-full">
        <el-form-item label="可提现金额" prop="name">
          <el-input v-model="props.primary.yue_money" disabled placeholder="请输入商户名称" />
        </el-form-item>
        <el-form-item label="提现金额" prop="tixian_money">
          <div class="items-center w-full">
            <el-input style="width:200px;margin:0 5px;" v-model="formData.tixian_money" placeholder="请输入金额">
              <template #append>元</template>
            </el-input>
            <span v-if="Number(props.primary.yue_money) > 0" class="text-blue-400 cursor-pointer"
              @click="formData.tixian_money = props.primary.yue_money">全部提现</span>
          </div>
        </el-form-item>
        <el-form-item label="提现账户" prop="bank_card_id">
          <el-select class="w-full" v-model="formData.bank_card_id" filterable clearable placeholder="选择提现账户">
            <el-option v-for="item in applyBank" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
      </div>
    </div>
    <div class="flex justify-end">
      <el-button type="primary" @click="submitForm(ruleForm)">{{ $t('system.confirm') }}</el-button>
    </div>
  </el-form>
</template>

<script lang="ts" setup>
import { useCreate } from '@/hook/curd/useCreate';
import { useShow } from '@/hook/curd/useShow';
import type { FormInstance, FormRules } from 'element-plus'
import { jiesuanManageBankAccountListAvailableBankCard, jiesuanManageJiesuanTixian } from '@/api/settlement/index'
const ruleForm = ref<FormInstance>()
import { onMounted, ref } from 'vue';
const props = defineProps({
  primary: Object,
});
const formData = ref({})
const rules = reactive({
  bank_card_id: [{ required: true, message: '请选择提现账户' }],
  tixian_money: [
    {
      required: true,
      message: '请输入金额',
      trigger: 'blur'
    },
    { pattern: /^\d+(\.\d{1,2})?$/, message: '金额最多只能有两位小数', trigger: 'blur' },
    { pattern: /^(?!0\d+)\d+(\.\d+)?$/, message: '请输入有效的金额', trigger: 'blur' },
    {
      pattern: /^500(\.\d{1,2})?$|^([5-9]\d{2}|[1-9]\d{3,})(\.\d{1,2})?$/,
      message: '请输入不低于500的金额',
      // trigger: 'blur'
    }
  ],
});
const loading = ref(false)
const applyBank = ref([])

const submitForm = (formEl: FormInstance | undefined) => {
  console.log(formData.value, 'formData.valueformData.value');
  if (!formEl) return;
  formEl
    .validate(valid => {
      if (valid) {
        jiesuanManageJiesuanTixian(formData.value).then(res => {
          ElMessage.success(res.message)
          emit('close')
          loading.value = false;
        })
      } else {
        loading.value = false;
      }
    })
    .then(() => { });
}

if (props.primary) {
  console.log(props.primary, 'props.primaryprops.primary');
}

const emit = defineEmits(['close']);
onMounted(() => {
  jiesuanManageBankAccountListAvailableBankCard().then(res => {
    applyBank.value = res.data;
    applyBank.value = res.data.map(v => {
      return {
        label: `${v.bank_name}(***********${v.card_no.slice(-4)})`,
        value: v.id,
      };
    });;
  })

});
</script>

<style lang="scss" scoped>
.withdraw-popup-warn {
  padding: 10px;
  color: red;
  margin-bottom: 10px;
  line-height: 1.8;
  padding-left: 30px;
  font-size: 12px;
  background-color: #ffecf4;
}
</style>
