<template>
  <el-dialog v-model="drawerVisible" :destroy-on-close="true" size="450px" :title="drawerProps.title">
    <el-form
      ref="ruleFormRef"
      label-width="120px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="formData"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="联系人姓名" prop="name">
        <el-input v-model="formData.name" placeholder="请输入联系人姓名" />
      </el-form-item>
      <el-form-item label="联系人电话" prop="phone">
        <el-input v-model="formData.phone" placeholder="请输入联系人电话" />
      </el-form-item>
      <el-form-item label="联系人微信号" prop="wechat">
        <el-input v-model="formData.wechat" placeholder="请输入联系人微信号" />
      </el-form-item>
      <el-form-item label="联系人职务" prop="position">
        <el-input v-model="formData.position" placeholder="请输入联系人职务" />
      </el-form-item>
      <el-form-item label="重要程度" prop="importanceLevel">
        <el-input-number v-model="formData.importanceLevel" :min="1" :max="5" placeholder="请输入重要程度" />
      </el-form-item>
      <!-- <el-form-item label="是否有效" prop="isValid">
        <el-select v-model="formData.isValid">
          <el-option v-for="(item, index) in isValidType" :key="index" :label="item.label" :value="item.label"></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" type="textarea" placeholder="请输入备注" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="ContactEditModal">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { addContact, updateContact } from "@/api/modules/contact";
import { customerType, isValidType } from "@/utils/dict";

const rules = reactive({
  name: [{ required: true, message: "请输入联系人姓名", trigger: "blur" }],
  phone: [{ required: true, message: "请输入联系人电话", trigger: "blur" }]
});

const formData = ref<any>({
  id: "",
  name: "",
  phone: "",
  wechat: "",
  position: "",
  importanceLevel: 1,
  remark: "",
  customerId: "",
  isValid: true
});

interface DrawerProps {
  title: string;
  isView: boolean;
  row: {
    [key: string]: any;
  };
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});

// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  // 确保 row 对象存在
  if (!drawerProps.value.row) {
    drawerProps.value.row = {};
  }
  formData.value = {
    id: drawerProps.value.row.id || "",
    name: drawerProps.value.row.name || "",
    phone: drawerProps.value.row.phone || "",
    wechat: drawerProps.value.row.wechat || "",
    position: drawerProps.value.row.position || "",
    importanceLevel: drawerProps.value.row.importanceLevel || 1,
    remark: drawerProps.value.row.remark || "",
    customerId: drawerProps.value.row.customerId || ""
  };

  drawerVisible.value = true;
};

// 提交数据
const ruleFormRef = ref<FormInstance>();
const handleSubmit = async () => {
  // 表单验证
  if (!ruleFormRef.value) return;
  const valid = await ruleFormRef.value.validate().catch(() => false);
  if (!valid) return;

  try {
    if (formData.value.id) {
      const res = await updateContact(formData.value);
      console.log(res);
    } else {
      const res = await addContact(formData.value);
      console.log(res);
    }

    ElMessage.success("成功！");

    // 刷新表格数据
    drawerProps.value.getTableList?.();

    // 关闭抽屉
    drawerVisible.value = false;
  } catch (error) {
    console.error("操作失败：", error);
  }
};

defineExpose({
  acceptParams
});
</script>
