<template>
  <el-dialog v-model="dialogVisible" title="合同管理" width="700px" append-to-body @close="handleClose">
    <el-form ref="formRef" :model="form" :rules="rules" label-width="130px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="合同编号" prop="contractNo">
            <el-input v-model="form.contractNo" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="合作项目名称" prop="cooperationId">
            <el-select v-model="form.cooperationId" placeholder="请选择" class="w-full" filterable @change="handleCooperationChange">
              <el-option
                v-for="project in projectInfoList"
                :key="project.id"
                :label="`${project.projectNo} , ${project.projectName}`"
                :value="project.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="合作开始时间">
            <el-date-picker v-model="form.startTime" type="date" placeholder="请选择日期" value-format="YYYY-MM-DD" style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="合作结束时间">
            <el-date-picker v-model="form.endTime" type="date" placeholder="请选择日期" value-format="YYYY-MM-DD" style="width: 100%" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="合作费用">
            <el-input v-model="form.fee" placeholder="请输入" type="number" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="合同类型">
            <el-select v-model="form.contractType" placeholder="请选择" style="width: 100%">
              <el-option label="战略合作" :value="1" />
              <el-option label="项目合作" :value="2" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="结算周期">
            <el-input v-model="form.settlementCycle" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="销售目标（万）">
            <el-input v-model="form.salesTarget" placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="合作区域">
            <el-input v-model="form.area" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系人">
            <el-input v-model="form.contacts" placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="联系电话">
            <el-input v-model="form.phone" placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="附件">
            <el-upload class="upload-demo" action="#" :auto-upload="false">
              <el-button size="small">上传</el-button>
              <template #tip>
                <div class="el-upload__tip">支持jpg/png/xlsx等文件</div>
              </template>
            </el-upload>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="重要条款">
            <el-input v-model="form.importantTerms" type="textarea" :rows="3" placeholder="请输入" />
          </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 { PartnerContractForm } from '@/api/partner/contract/types';
import { listProjectInfo } from '@/api/project/projectInfo';
interface Props {
  visible: boolean;
  formData: PartnerContractForm;
}

const projectInfoList = ref<any[]>([]);
interface Emits {
  (e: 'update:visible', value: boolean): void;
  (e: 'update:formData', value: PartnerContractForm): void;
  (e: 'submit', value: PartnerContractForm): void;
}

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

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

const rules = {
  contractNo: [{ required: true, message: '合同编号不能为空', trigger: 'blur' }]
};

const formRef = ref();
const form = ref<PartnerContractForm>({
  ...props.formData,
  cooperationId: props.formData.cooperationId || null
});

const handleClose = () => {
  emit('update:visible', false);
  formRef.value?.resetFields();
};

/** 查询项目信息列表 */
const getProjectList = async () => {
  const res = await listProjectInfo();
  projectInfoList.value = res.rows;
};

const handleCooperationChange = (cooperationId: string | number) => {
  const cooperative = projectInfoList.value.find((b) => b.id === cooperationId);
  form.value.cooperativeName = cooperative?.projectName || '';
};

const handleSubmit = () => {
  formRef.value?.validate((valid: boolean) => {
    if (valid) {
      emit('submit', form.value);
    }
  });
};
onMounted(() => {
  getProjectList();
});
</script>
