<template>
  <el-dialog v-model="dialogVisible" title="资质管理" width="600px" append-to-body @close="handleClose">
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="资质类型" prop="qualificationType">
            <el-select v-model="formData.qualificationType" placeholder="请选择" style="width: 100%">
              <el-option label="营业执照" :value="1" />
              <el-option label="资质证书" :value="2" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="资质编号" prop="qualificationNo">
            <el-input v-model="formData.qualificationNo" placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="签发机构">
            <el-input v-model="formData.authority" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="截止日期">
            <el-date-picker v-model="formData.deadline" type="date" placeholder="请选择日期" value-format="YYYY-MM-DD" style="width: 100%" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="附件">
            <el-upload class="upload-demo" action="#" :auto-upload="false">
              <el-button size="small">上传</el-button>
              <template #tip>
                <div class="el-upload__tip">支持jpg/png/xlsx等文件</div>
              </template>
            </el-upload>
          </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 } from 'vue';
import type { PartnerQualificationForm } from '@/api/partner/qualification/types';

interface Props {
  visible: boolean;
  formData: PartnerQualificationForm;
}

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

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

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

const rules = {
  qualificationType: [{ required: true, message: '资质类型不能为空', trigger: 'change' }],
  qualificationNo: [{ required: true, message: '资质编号不能为空', trigger: 'blur' }]
};

const formRef = ref();

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

const handleSubmit = () => {
  formRef.value?.validate((valid: boolean) => {
    if (valid) {
      emit('submit', props.formData);
    }
  });
};
</script>
