<!-- 科研项目编辑弹窗 -->
<script name="ProjectDialog" lang="ts" setup>
import { ElLoading, ElMessage } from 'element-plus'
import type { IStaffProjectInfo } from '../person-regitster'
import { UploadFile } from '@/api/file'
import { addProjectRec, updateProjectRec } from '@/api/resource/register'

const props = defineProps({
  staffId: { type: String, default: '' },
})

const emit = defineEmits(['recordSaved'])

const title = ref<string>('')
const projectFormRef = ref()

const staffProject = ref<IStaffProjectInfo>({
  id: '',
  staffId: '',
  projectDate: '',
  projectName: '',
  remark: '',
  file: '',
})

const staffProjectRules = ref({
  projectDate: [{ required: true, message: '项目时间不能为空', trigger: 'blur' }],
  projectName: [{ required: true, message: '项目名称不能为空', trigger: 'blur' }],
}) // 表单验证规则

const showDialog = ref(false)

// 逻辑
const showRecordDialog = (show: boolean, tableTitle = '') => {
  showDialog.value = show
  title.value = tableTitle

  if (show === false) {
    staffProject.value = {
      id: '',
      staffId: '',
      projectDate: '',
      projectName: '',
      remark: '',
      file: '',
    }
  }
}

const initRecordData = (record: IStaffProjectInfo) => {
  staffProject.value = { ...record }
}

const uploadProjectFile: any = (file: any) => {
  const fd = new FormData()
  fd.append('multipartFile', file.file)
  const loading = ElLoading.service({
    lock: true,
    background: 'rgba(255, 255, 255, 0.8)',
  })
  UploadFile(fd).then((res) => {
    if (res.code === 200) {
      ElMessage.success('附件上传成功')
      staffProject.value.file = res.data[0]
      loading.close()
    }
  }).catch(() => {
    loading.close()
    ElMessage.error('附件上传失败')
  })
}

const saveStaffProject = async () => {
  await projectFormRef.value.validate((valid: boolean) => {
    if (valid === true) {
      if (staffProject.value.id === '') {
        staffProject.value.staffId = props.staffId
        addProjectRec(staffProject.value).then((res) => {
          if (res.code === 200) {
            ElMessage.success('科研项目保存成功')
            emit('recordSaved')
            showRecordDialog(false)
          }
          else {
            ElMessage.success(`科研项目保存失败：${res.message}`)
          }
        })
      }
      else {
        updateProjectRec(staffProject.value).then((res) => {
          if (res.code === 200) {
            ElMessage.success('科研项目保存成功')
            emit('recordSaved')
            showRecordDialog(false)
          }
          else {
            ElMessage.success(`科研项目保存失败：${res.message}`)
          }
        })
      }
    }
  })
}

defineExpose({
  showRecordDialog,
  initRecordData,
})
</script>

<template>
  <el-dialog v-model="showDialog" :title="`${title}科研项目`" :append-to-body="true" :close-on-click-modal="false">
    <detail-block title="">
      <el-form ref="projectFormRef" :model="staffProject" :rules="staffProjectRules" label-position="right" label-width="110px" border stripe>
        <el-row :gutter="24">
          <el-col :span="18" :offset="3">
            <el-form-item label="时间" prop="projectDate">
              <el-date-picker v-model="staffProject.projectDate" placeholder="请选择项目时间" format="YYYY-MM-DD" value-format="YYYY-MM-DD" type="date" style="width: 100%;" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24">
          <el-col :span="18" :offset="3">
            <el-form-item label="名称" prop="projectName">
              <el-input v-model="staffProject.projectName" placeholder="请输入项目名称" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24">
          <el-col :span="18" :offset="3">
            <el-form-item label="备注">
              <el-input v-model="staffProject.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24">
          <el-col :span="18" :offset="3">
            <el-form-item label="附件">
              <el-input v-model="staffProject.file" type="hidden" />

              <el-upload
                :show-file-list="false"
                :http-request="uploadProjectFile"
                style="margin-right: 20px; display: inline-flex;"
              >
                <el-button type="primary">
                  <template v-if="staffProject.file !== ''">
                    替换
                  </template>
                  <template v-else>
                    上传
                  </template>
                </el-button>
              </el-upload>

              <span>{{ staffProject.file }}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </detail-block>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="showRecordDialog(false)">取消</el-button>
        <el-button type="primary" @click="saveStaffProject">
          保存
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
