<template>
  <el-dialog v-model="dialogVisible" title="银行账户管理" width="850px" append-to-body @close="handleClose">
    <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="开户名称" prop="account">
            <el-input v-model="form.account" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="财务登记号">
            <el-input v-model="form.registrationNumber" placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="开户银行名称" prop="bankId">
            <el-select v-model="form.bankId" placeholder="请选择" class="w-full" filterable @change="handleBankChange">
              <el-option v-for="bank in bankList" :key="bank.id" :label="`${bank.bnId} , ${bank.bnName}`" :value="bank.id" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="银行账号">
            <el-input v-model="form.bankNumber" placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="开户银行所在地">
            <el-input v-model="form.bankLocation" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="银行联行号">
            <el-input v-model="form.bankInterbankNumber" placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="电话">
            <el-input v-model="form.phone" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="地址">
            <el-input v-model="form.address" placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="handleSubmit">确认</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, computed, watch } from 'vue';
import type { PartnerBankForm } from '@/api/partner/bank/types';
import { listBank } from '@/api/company/bank';
import { BankVO } from '@/api/company/bank/types';
const bankList = ref<BankVO[]>([]);
interface Props {
  visible: boolean;
  formData: PartnerBankForm;
}

interface Emits {
  (e: 'update:visible', value: boolean): void;
  (e: 'update:formData', value: PartnerBankForm): void;
  (e: 'submit', value: PartnerBankForm): void;
}

const props = defineProps<Props>();
const emit = defineEmits<Emits>();

const dialogVisible = computed({
  get: () => props.visible,
  set: (value) => emit('update:visible', value)
});

const rules = {
  account: [{ required: true, message: '开户名称不能为空', trigger: 'blur' }]
};

const formRef = ref();
const form = ref<PartnerBankForm>({ ...props.formData });

watch(
  () => props.formData,
  (newVal) => {
    form.value = { ...newVal };
  },
  { deep: true }
);

const handleBankChange = (bankId: string | number) => {
  const bank = bankList.value.find((b) => b.id === bankId);
  form.value.accountBankName = bank?.bnName || '';
  form.value.bankLocation = bank?.bnAddr || '';
};

const handleClose = () => {
  emit('update:visible', false);
  formRef.value?.resetFields();
};

const handleSubmit = () => {
  formRef.value?.validate((valid: boolean) => {
    if (valid) {
      emit('submit', form.value);
    }
  });
};
// 获取银行列表
const getBankList = async () => {
  try {
    const res = await listBank({ pageNum: 1, pageSize: 9999, isShow: '0' });
    bankList.value = res.rows || [];
  } catch (error) {
    console.error('获取银行列表失败', error);
  }
};
onMounted(() => {
  getBankList();
});
</script>
