<template>
  <div class="p-2">
    <el-dialog v-model="dialog.visible" :title="dialog.title" width="30%" :draggable="draggable">
      <el-form ref="formRef" v-loading="formLoading" :model="formData" :rules="formRules" label-width="100px">
        <el-form-item label="账户名称" prop="accountName">
          <el-input v-model="formData.accountName" placeholder="请输入结算账户名称" />
        </el-form-item>
        <el-form-item label="账户代码" prop="accountCode">
          <el-input v-model="formData.accountCode" placeholder="请输入结算账户代码" />
        </el-form-item>
        <el-form-item label="账户密码" prop="queryPwd">
          <el-input v-model="formData.queryPwd" placeholder="请输入账户查询密码" />
        </el-form-item>
        <el-form-item label="联系电话" prop="telephone">
          <el-input v-model="formData.telephone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="银行卡号" prop="bankNumber">
          <el-input v-model="formData.bankNumber" placeholder="请输入银行卡号" />
        </el-form-item>
        <el-form-item label="账户状态" prop="status">
          <el-radio-group v-model="formData.status">
            <el-radio v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="管理员姓名" prop="adminName">
          <el-input v-model="formData.adminName" placeholder="请输入管理员姓名" />
        </el-form-item>
        <el-form-item label="证件编号" prop="idNumber">
          <el-input v-model="formData.idNumber" placeholder="请输入证件编号" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="formData.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="formLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="dialog.visible = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="PtAccountForm" lang="ts">
import * as PtAccountApi from '@/api/basics/merchant/ptAccount';
import { PtAccountForm } from '@/api/basics/merchant/ptAccount/types';
import { useI18n } from 'vue-i18n';

defineOptions({ name: 'PtAccountForm' });

// 接收父组件传入属性
// const props = defineProps({})

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
// 国际化
const { t } = useI18n();

/** 数据字典 */
const { sys_normal_disable } = toRefs<any>(proxy?.useDict('sys_normal_disable'));
// 对话框
const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});
// 窗体是否可以拖动
const draggable = ref(true);
// 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formLoading = ref(false);
// 表单的类型：create - 新增；update - 修改
const formType = ref('');
// 表单引用
const formRef = ref<ElFormInstance>();
// 表单数据
const formData = ref<PtAccountForm>({
  accountId: undefined,
  accountName: '',
  accountCode: '',
  queryPwd: '',
  telephone: '',
  bankNumber: '',
  status: '',
  adminName: '',
  idNumber: '',
  remark: ''
});
// 表单校验规则
const formRules = reactive({
  accountId: [{ required: true, message: '结算账户Id,主键不能为空', trigger: 'blur' }],
  accountName: [{ required: true, message: '结算账户名称不能为空', trigger: 'blur' }],
  accountCode: [{ required: true, message: '结算账户代码不能为空', trigger: 'blur' }]
  // queryPwd: [{ required: true, message: '账户查询密码不能为空', trigger: 'blur' }],
  // telephone: [{ required: true, message: '联系电话不能为空', trigger: 'blur' }],
  // bankNumber: [{ required: true, message: '银行卡号不能为空', trigger: 'blur' }],
  // status: [{ required: true, message: '账户状态不能为空', trigger: 'change' }],
  // adminName: [{ required: true, message: '管理员姓名不能为空', trigger: 'blur' }],
  // idNumber: [{ required: true, message: '证件编号不能为空', trigger: 'blur' }]
});
/** 当前组件方法 */
/** 窗体打开事件 */
const open = async (type: string, id?: number) => {
  console.log(type, 'open');
  dialog.visible = true;
  dialog.title = t('action.' + type);
  formType.value = type;
  resetForm();
  // 修改时，设置数据
  if (id) {
    formLoading.value = true;
    try {
      const res = await PtAccountApi.getPtAccount(id);
      Object.assign(formData.value, res.data);
    } finally {
      formLoading.value = false;
    }
  }
};
/** 表单重置 */
const resetForm = () => {
  formData.value = {
    accountId: '',
    accountName: '',
    accountCode: '',
    queryPwd: '',
    telephone: '',
    bankNumber: '',
    status: '',
    adminName: '',
    idNumber: '',
    remark: ''
  };
  formRef.value?.resetFields();
};
// 传回给父组件的属性与方法
// 提供 open 方法，用于打开弹窗
defineExpose({ open });
/** 触发父组件的事件 */
/** 定义 success 事件，用于操作成功后的回调 */
const emit = defineEmits(['success']);
/** 提交按钮 */
const submitForm = async () => {
  // 校验表单
  if (!formRef.value) return;
  const valid = await formRef.value?.validate();
  if (!valid) return;
  // 提交请求
  formLoading.value = true;
  try {
    const data = formData.value as unknown as PtAccountForm;
    if (formType.value === 'create') {
      await PtAccountApi.addPtAccount(data);
      proxy?.$modal.msgSuccess('新增成功');
    } else {
      await PtAccountApi.updatePtAccount(data);
      proxy?.$modal.msgSuccess('修改成功');
    }
    dialog.visible = false;
    // 发送操作成功的事件
    emit('success');
  } finally {
    formLoading.value = false;
  }
};

/** 初始化 */
onMounted(() => {});
</script>
