<template>
  <el-dialog v-model="visible" title="新建跟进记录" width="600px" append-to-body>
    <el-form :model="form" :rules="rules" ref="formRef" label-width="100px">
      <el-form-item label="拜访方式" prop="callTypeCode">
        <el-select v-model="form.callTypeCode" placeholder="请选择">
          <el-option label="电话拜访" value="phone" />
          <el-option label="上门拜访" value="visit" />
          <el-option label="邮件" value="email" />
        </el-select>
      </el-form-item>
      <el-form-item label="拜访日期" prop="callDate">
        <el-date-picker v-model="form.callDate" type="date" value-format="YYYY-MM-DD" style="width:100%" />
      </el-form-item>
      <el-form-item label="跟进内容" prop="followUpCondition">
        <el-input v-model="form.followUpCondition" type="textarea" :rows="4" placeholder="请输入跟进内容" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, computed } from 'vue';

const props = defineProps({
  modelValue: Boolean,
  projectId: [String, Number],
  customName: String
});

const emit = defineEmits(['update:modelValue', 'success']);

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

const form = ref({
  callTypeCode: 'visit',
  callDate: new Date().toISOString().split('T')[0],
  followUpCondition: ''
});

const rules = {
  callTypeCode: [{ required: true, message: '拜访方式不能为空', trigger: 'change' }],
  followUpCondition: [{ required: true, message: '跟进内容不能为空', trigger: 'blur' }]
};

const formRef = ref(null);

const handleSubmit = async () => {
  if (!formRef.value) return;
  await formRef.value.validate(async (valid) => {
    if (valid) {
      // 调用保存 API
      emit('success');
      visible.value = false;
    }
  });
};
</script>

<style scoped>
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}
</style>
