<!-- 经验履历编辑弹窗 -->
<script name="ResumeDialog" lang="ts" setup>
import { ElMessage } from 'element-plus'
import type { IStaffResumeInfo } from '../person-regitster'
import { addResumeRec, updateResumeRec } from '@/api/resource/register'

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

const emit = defineEmits(['recordSaved'])

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

const staffResume = ref<IStaffResumeInfo>({
  id: '',
  staffId: '',
  resumeName: '',
  details: '',
  resumeDate: '',
})

const staffResumeRules = ref({
  resumeName: [{ required: true, message: '履历名称不能为空', trigger: 'blur' }],
  details: [{ required: true, message: '详细介绍不能为空', trigger: 'blur' }],
  resumeDate: [{ required: true, message: '时间不能为空', trigger: 'blur' }],
}) // 表单验证规则

const showDialog = ref(false)

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

  if (show === false) {
    staffResume.value = {
      id: '',
      staffId: '',
      resumeName: '',
      details: '',
      resumeDate: '',
    }
  }
}

const initRecordData = (record: IStaffResumeInfo) => {
  staffResume.value = { ...record }
}

const saveStaffResume = async () => {
  await resumeFormRef.value.validate((valid: boolean) => {
    if (valid === true) {
      if (staffResume.value.id === '') {
        staffResume.value.staffId = props.staffId
        addResumeRec(staffResume.value).then((res) => {
          if (res.code === 200) {
            ElMessage.success('经验履历保存成功')
            emit('recordSaved')
            showRecordDialog(false)
          }
          else {
            ElMessage.success(`经验履历保存失败：${res.message}`)
          }
        })
      }
      else {
        updateResumeRec(staffResume.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="resumeFormRef" :model="staffResume" :rules="staffResumeRules" label-position="right" label-width="110px" border stripe>
        <el-row :gutter="24">
          <el-col :span="18" :offset="3">
            <el-form-item label="履历名称" prop="resumeName">
              <el-input v-model="staffResume.resumeName" placeholder="请输入履历名称" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24">
          <el-col :span="18" :offset="3">
            <el-form-item label="详细介绍" prop="details">
              <el-input v-model="staffResume.details" placeholder="请输入详细介绍" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24">
          <el-col :span="18" :offset="3">
            <el-form-item label="时间" prop="resumeDate">
              <el-date-picker v-model="staffResume.resumeDate" placeholder="请选择时间" format="YYYY-MM-DD" value-format="YYYY-MM-DD" type="date" style="width: 100%;" />
            </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="saveStaffResume">
          保存
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
