<template>
  <div class="contacts">
    <div class="toolbar">
      <el-button type="primary" @click="handleAdd">新增</el-button>
    </div>
    <el-table :data="contactsList" border>
      <el-table-column prop="name" label="联系人姓名" />
      <el-table-column prop="phone" label="联系电话" />
      <el-table-column prop="email" label="电子邮箱" />
      <el-table-column prop="contactType" label="联系人类型" />
      <el-table-column label="操作" width="150">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button link type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="600px">
      <el-form :model="formData" label-width="120px">
        <el-form-item label="联系人姓名">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="formData.phone" />
        </el-form-item>
        <el-form-item label="电子邮箱">
          <el-input v-model="formData.email" />
        </el-form-item>
        <el-form-item label="联系人类型">
          <el-select v-model="formData.contactType" placeholder="请选择">
            <el-option label="主要联系人" :value="1" />
            <el-option label="次要联系人" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="formData.remark" type="textarea" :rows="3" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { getCurrentPartnerInfo, getPartnerContactsList, addPartnerContacts, updatePartnerContacts, deletePartnerContacts } from '@/api/partner';

const contactsList = ref([]);
const loading = ref(false);
const dialogVisible = ref(false);
const dialogTitle = ref('');
const formData = ref({
  id: null,
  partnerId: null,
  name: '',
  phone: '',
  email: '',
  contactType: null,
  remark: ''
});

const fetchContactsList = async () => {
  loading.value = true;
  try {
    const partnerResponse = await getCurrentPartnerInfo();
    if (partnerResponse.code === 200 && partnerResponse.data) {
      const partnerId = partnerResponse.data.id;
      formData.value.partnerId = partnerId;
      const response = await getPartnerContactsList(partnerId);
      if (response.code === 200) {
        contactsList.value = response.rows || response.data || [];
      }
    }
  } catch (error) {
    console.error('获取联系人列表失败', error);
    ElMessage.error('获取联系人列表失败');
  } finally {
    loading.value = false;
  }
};

const handleAdd = () => {
  dialogTitle.value = '新增联系人';
  formData.value = {
    id: null,
    partnerId: formData.value.partnerId,
    name: '',
    phone: '',
    email: '',
    contactType: null,
    remark: ''
  };
  dialogVisible.value = true;
};

const handleEdit = (row: any) => {
  dialogTitle.value = '编辑联系人';
  formData.value = { ...row };
  dialogVisible.value = true;
};

const handleDelete = (row: any) => {
  ElMessageBox.confirm('确定要删除该联系人吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      const response = await deletePartnerContacts([row.id]);
      if (response.code === 200) {
        ElMessage.success('删除成功');
        fetchContactsList();
      } else {
        ElMessage.error(response.msg || '删除失败');
      }
    } catch (error) {
      console.error('删除失败', error);
      ElMessage.error('删除失败');
    }
  });
};

const handleSubmit = async () => {
  try {
    const apiCall = formData.value.id ? updatePartnerContacts : addPartnerContacts;
    const response = await apiCall(formData.value);
    if (response.code === 200) {
      ElMessage.success(formData.value.id ? '修改成功' : '新增成功');
      dialogVisible.value = false;
      fetchContactsList();
    } else {
      ElMessage.error(response.msg || '操作失败');
    }
  } catch (error) {
    console.error('操作失败', error);
    ElMessage.error('操作失败');
  }
};

onMounted(() => {
  fetchContactsList();
});
</script>

<style scoped lang="scss">
.contacts {
  padding: 20px;
}

.toolbar {
  margin-bottom: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
