<template>
  <div class="bank">
    <div class="toolbar">
      <el-button type="primary" @click="handleAdd">新增</el-button>
    </div>
    <el-table :data="bankList" border>
      <el-table-column prop="accountBankName" label="开户银行名称" />
      <el-table-column prop="account" label="开户名称" />
      <el-table-column prop="bankNumber" label="银行账号" />
      <el-table-column prop="bankLocation" label="开户银行所在地" />
      <el-table-column label="操作" width="150">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button link type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="600px">
      <el-form :model="formData" label-width="140px">
        <el-form-item label="开户银行名称">
          <el-input v-model="formData.accountBankName" />
        </el-form-item>
        <el-form-item label="开户名称">
          <el-input v-model="formData.account" />
        </el-form-item>
        <el-form-item label="银行账号">
          <el-input v-model="formData.bankNumber" />
        </el-form-item>
        <el-form-item label="开户银行所在地">
          <el-input v-model="formData.bankLocation" />
        </el-form-item>
        <el-form-item label="财务登记号">
          <el-input v-model="formData.registrationNumber" />
        </el-form-item>
        <el-form-item label="银行联行号">
          <el-input v-model="formData.bankInterbankNumber" />
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="formData.phone" />
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="formData.address" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="formData.remark" type="textarea" :rows="3" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { getCurrentPartnerInfo, getPartnerBankList, addPartnerBank, updatePartnerBank, deletePartnerBank } from '@/api/partner';

const bankList = ref([]);
const loading = ref(false);
const dialogVisible = ref(false);
const dialogTitle = ref('');
const formData = ref({
  id: null,
  partnerId: null,
  accountBankName: '',
  account: '',
  bankNumber: '',
  bankLocation: '',
  registrationNumber: '',
  bankInterbankNumber: '',
  phone: '',
  address: '',
  remark: ''
});

const fetchBankList = async () => {
  loading.value = true;
  try {
    const partnerResponse = await getCurrentPartnerInfo();
    if (partnerResponse.code === 200 && partnerResponse.data) {
      const partnerId = partnerResponse.data.id;
      formData.value.partnerId = partnerId;
      const response = await getPartnerBankList(partnerId);
      if (response.code === 200) {
        bankList.value = response.rows || response.data || [];
      }
    }
  } catch (error) {
    console.error('获取银行列表失败', error);
    ElMessage.error('获取银行列表失败');
  } finally {
    loading.value = false;
  }
};

const handleAdd = () => {
  dialogTitle.value = '新增银行账户';
  formData.value = {
    id: null,
    partnerId: formData.value.partnerId,
    accountBankName: '',
    account: '',
    bankNumber: '',
    bankLocation: '',
    registrationNumber: '',
    bankInterbankNumber: '',
    phone: '',
    address: '',
    remark: ''
  };
  dialogVisible.value = true;
};

const handleEdit = (row: any) => {
  dialogTitle.value = '编辑银行账户';
  formData.value = { ...row };
  dialogVisible.value = true;
};

const handleDelete = (row: any) => {
  ElMessageBox.confirm('确定要删除该银行账户吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      const response = await deletePartnerBank([row.id]);
      if (response.code === 200) {
        ElMessage.success('删除成功');
        fetchBankList();
      } else {
        ElMessage.error(response.msg || '删除失败');
      }
    } catch (error) {
      console.error('删除失败', error);
      ElMessage.error('删除失败');
    }
  });
};

const handleSubmit = async () => {
  try {
    const apiCall = formData.value.id ? updatePartnerBank : addPartnerBank;
    const response = await apiCall(formData.value);
    if (response.code === 200) {
      ElMessage.success(formData.value.id ? '修改成功' : '新增成功');
      dialogVisible.value = false;
      fetchBankList();
    } else {
      ElMessage.error(response.msg || '操作失败');
    }
  } catch (error) {
    console.error('操作失败', error);
    ElMessage.error('操作失败');
  }
};

onMounted(() => {
  fetchBankList();
});
</script>

<style scoped lang="scss">
.bank {
  padding: 20px;
}

.toolbar {
  margin-bottom: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
