<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="name">
            <el-input v-model="formData.name" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电话">
            <el-input v-model="formData.phone" 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.email" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系人类型">
            <el-select v-model="formData.contactType" placeholder="请选择" style="width: 100%">
              <el-option label="主要联系人" :value="1" />
              <el-option label="次要联系人" :value="2" />
            </el-select>
          </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 { PartnerContactsForm } from '@/api/partner/contacts/types';

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

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

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

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

const rules = {
  name: [{ 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>
