<template>
  <vxe-modal
    resize
    v-model="showModal"
    :title="modalTitle"
    @hide="onHide"
    width="500px"
    show-maximize
    show-footer
    esc-closable
  >
    <el-form ref="formRef" :model="form" :rules="rules" inline label-width="110px">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item class="w-full" label="平台名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入平台名称" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item class="w-full" label="最大分期数" prop="maxStagesNum">
            <el-input v-model="form.maxStagesNum" placeholder="最大分期数" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item class="w-full" label="最低分期金额" prop="minAmount">
            <el-input v-model="form.minAmount" placeholder="最低分期金额" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item class="w-full" label="状态" prop="status">
            <el-radio-group v-model="form.status">
              <el-radio value="enable">启用</el-radio>
              <el-radio value="disable">禁用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <el-button type="default" @click="showModal = false">取消</el-button>
      <el-button :loading="loading" type="primary" @click="submitForm">提交</el-button>
    </template>
  </vxe-modal>
</template>

<script setup name="tenantFormModal">
  import { ref, computed } from 'vue';
  import { ElMessage } from 'element-plus';
  import { savePlatform } from '@/api/platform';

  const emits = defineEmits(['success']);
  const currentTenant = ref(null);
  const showModal = ref(false);
  const formRef = ref(null);

  const isEdit = computed(() => !!currentTenant.value);
  const modalTitle = computed(() => (isEdit.value ? '编辑平台' : '新增平台'));

  const form = ref({
    name: '',
    maxStagesNum: '',
    minAmount: '',
    status: 'enable',
  });
  const rules = ref({
    name: { required: true, message: '请输入平台名称', trigger: 'blur' },
    maxStagesNum: { required: true, message: '请输入最大分期数', trigger: 'blur' },
    minAmount: { required: true, message: '请输入最低分期金额', trigger: 'blur' },
  });

  const loading = ref(false);
  const submitForm = async () => {
    try {
      await formRef.value.validate();
      loading.value = true;

      await savePlatform(form.value);
      emits('success');
      ElMessage({
        type: 'success',
        message: isEdit.value ? '修改成功！' : '添加成功!',
        plain: true,
      });
      showModal.value = false;
    } catch {}
    loading.value = false;
  };
  const onHide = () => {
    form.value = {
      name: '',
      maxStagesNum: '',
      minAmount: '',
      status: 'enable',
    };
    formRef.value.clearValidate();
    currentTenant.value = null;
  };
  const openModal = (role) => {
    role && (form.value = role);
    currentTenant.value = role;
    showModal.value = true;
  };

  defineExpose({
    openModal,
  });
</script>
