<!-- 业务能力编辑弹窗 -->
<script name="AbilityDialog" lang="ts" setup>
import { ElLoading, ElMessage } from 'element-plus'
import type { IStaffAbilityInfo } from '../person-regitster'
import { UploadFile } from '@/api/file'
import { addAbilityRec, updateAbilityRec } from '@/api/resource/register'

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

const emit = defineEmits(['recordSaved'])

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

const staffAbility = ref<IStaffAbilityInfo>({
  id: '',
  staffId: '',
  obtainDate: '',
  abilityName: '',
  publication: '',
  remark: '',
  file: '',
})

const staffAbilityRules = ref({
  obtainDate: [{ required: true, message: '时间不能为空', trigger: 'blur' }],
  abilityName: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
  publication: [{ required: true, message: '获奖情况不能为空', trigger: 'blur' }],
}) // 表单验证规则

const showDialog = ref(false)

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

  if (show === false) {
    staffAbility.value = {
      id: '',
      staffId: '',
      obtainDate: '',
      abilityName: '',
      publication: '',
      remark: '',
      file: '',
    }
  }
}

const initRecordData = (record: IStaffAbilityInfo) => {
  staffAbility.value = { ...record }
}

const uploadAbilityFile: 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('附件上传成功')
      staffAbility.value.file = res.data[0]
      loading.close()
    }
  }).catch(() => {
    loading.close()
    ElMessage.error('附件上传失败')
  })
}

const saveStaffAbility = async () => {
  await abilityFormRef.value.validate((valid: boolean) => {
    if (valid === true) {
      if (staffAbility.value.id === '') {
        staffAbility.value.staffId = props.staffId
        addAbilityRec(staffAbility.value).then((res) => {
          if (res.code === 200) {
            ElMessage.success('业务能力保存成功')
            emit('recordSaved')
            showRecordDialog(false)
          }
          else {
            ElMessage.success(`业务能力保存失败：${res.message}`)
          }
        })
      }
      else {
        updateAbilityRec(staffAbility.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="abilityFormRef" :model="staffAbility" :rules="staffAbilityRules" label-position="right" label-width="200px" border stripe>
        <el-row :gutter="24">
          <el-col :span="18" :offset="3">
            <el-form-item label="时间" prop="obtainDate">
              <el-date-picker v-model="staffAbility.obtainDate" 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="abilityName">
              <el-input v-model="staffAbility.abilityName" placeholder="请输入名称" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24">
          <el-col :span="18" :offset="3">
            <el-form-item label="发表刊物（期号）/会议名称（获奖情况）" prop="publication">
              <el-input v-model="staffAbility.publication" 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="staffAbility.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="staffAbility.file" type="hidden" />

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

              <span>{{ staffAbility.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="saveStaffAbility">
          保存
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
