<template>
  <div class="contract">
    <div class="toolbar">
      <el-button type="primary" @click="handleAdd">新增</el-button>
    </div>
    <el-table :data="contractList" border>
      <el-table-column prop="contractNo" label="合同编号" />
      <el-table-column prop="cooperativeName" label="合作项目名称" />
      <el-table-column prop="startTime" label="合作开始时间" />
      <el-table-column prop="endTime" 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="140px">
        <el-form-item label="合同编号">
          <el-input v-model="formData.contractNo" />
        </el-form-item>
        <el-form-item label="合作项目名称">
          <el-input v-model="formData.cooperativeName" />
        </el-form-item>
        <el-form-item label="合作开始时间">
          <el-date-picker v-model="formData.startTime" type="date" placeholder="选择日期" style="width: 100%" />
        </el-form-item>
        <el-form-item label="合作结束时间">
          <el-date-picker v-model="formData.endTime" type="date" placeholder="选择日期" style="width: 100%" />
        </el-form-item>
        <el-form-item label="合作包类">
          <el-input v-model="formData.categories" />
        </el-form-item>
        <el-form-item label="联系人">
          <el-input v-model="formData.contacts" />
        </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.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, getPartnerContractList, addPartnerContract, updatePartnerContract, deletePartnerContract } from '@/api/partner';

const contractList = ref([]);
const loading = ref(false);
const dialogVisible = ref(false);
const dialogTitle = ref('');
const formData = ref({
  id: null,
  partnerId: null,
  contractNo: '',
  cooperativeName: '',
  startTime: null,
  endTime: null,
  categories: '',
  contacts: '',
  phone: '',
  remark: ''
});

const fetchContractList = 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 getPartnerContractList(partnerId);
      if (response.code === 200) {
        contractList.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,
    contractNo: '',
    cooperativeName: '',
    startTime: null,
    endTime: null,
    categories: '',
    contacts: '',
    phone: '',
    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 deletePartnerContract([row.id]);
      if (response.code === 200) {
        ElMessage.success('删除成功');
        fetchContractList();
      } else {
        ElMessage.error(response.msg || '删除失败');
      }
    } catch (error) {
      console.error('删除失败', error);
      ElMessage.error('删除失败');
    }
  });
};

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

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

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

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