<template>
  <div>
    <div class="mb-3 ml-5">
      <el-alert title="注：结算金额会打入您预留的银行卡中，请仔细核对账户信息" type="error" :closable="false" effect="dark" />
    </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="owner_name">
            <el-input v-model="formData.owner_name" placeholder="请输入商户名称" />
          </el-form-item>
          <el-form-item label="收款人" prop="name_of_payee">
            <el-input v-model="formData.name_of_payee" placeholder="请输入收款人" />
          </el-form-item>
          <el-form-item label="银行卡号" prop="card_no">
            <el-input v-model="formData.card_no" clearable size="large" placeholder="请输入银行卡号" />
          </el-form-item>
          <el-form-item label="开户银行" prop="bank_name">
            <el-select class="w-full" v-model="formData.bank_name" filterable clearable placeholder="选择开户银行">
              <el-option v-for="item in bankList" :key="item.id" :label="item.name" :value="item.name" />
            </el-select>
          </el-form-item>
          <el-form-item label="开户支行" prop="sub_bank_name">
            <el-input v-model="formData.sub_bank_name" clearable size="large" placeholder="请输入开户支行" />
          </el-form-item>
          <el-form-item label="收款人手机号" prop="phone_of_payee">
            <el-input v-model="formData.phone_of_payee" clearable size="large" placeholder="请输入收款人手机号" />
          </el-form-item>
          <el-form-item label="所属商务" prop="business_id">
            <el-select v-model="formData.business_id" filterable clearable placeholder="选择所属商务">
              <el-option v-for="item in businessList" :key="item.id" :label="item.username" :value="item.id" />
            </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>
  </div>
</template>

<script lang="ts" setup>
import type { FormInstance, FormRules } from 'element-plus'
import { jiesuanManageBankAccountListBank, jiesuanManageBankAccountListShangwu, jiesuanManageBankAccountAddCompanyCard } 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_name: [{ required: true, message: '请选择开户银行' }],
  owner_name: [
    {
      required: true,
      message: '请输入商户名称',
      trigger: 'blur'
    }
  ],
  name_of_payee: [
    {
      required: true,
      message: '请输入收款人',
      trigger: 'blur'
    }
  ],
  card_no: [
    {
      required: true,
      message: '请输入银行卡号',
      trigger: 'blur'
    },
    { max: 64, message: '至多输入64个字符', trigger: 'blur' }
  ],
  phone_of_payee: [
    {
      required: true,
      message: '请输入收款人手机号',
      trigger: 'blur'
    },
    { pattern: /^1[3456789]\d{9}$/, message: '号码格式不正确', trigger: 'blur' }
  ],
  business_id: [
    { required: true, message: '请选择所属商务' }
  ],
  sub_bank_name: [
    {
      required: true,
      message: '请输入开户支行',
      trigger: 'blur'
    }
  ]
});
const loading = ref(false)
const businessList = ref([])
const bankList = ref([])

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

if (props.primary) {
  formData.value = JSON.parse(JSON.stringify(props.primary))
}

const emit = defineEmits(['close']);
onMounted(() => {
  init()
});
</script>
