<!-- 晋升情况编辑弹窗 -->
<script name="PromoteDialog" lang="ts" setup>
import { ElMessage } from 'element-plus'
import type { IStaffPromoteInfo } from '../person-regitster'
import { addPromoteRec, updatePromoteRec } from '@/api/resource/register'

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

const emit = defineEmits(['recordSaved'])

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

const staffPromote = ref<IStaffPromoteInfo>({
  id: '',
  staffId: '',
  promoteExperience: '',
  company: '',
  promoteDate: '',
})

const staffPromoteRules = ref({
  promoteExperience: [{ required: true, message: '晋升经历不能为空', trigger: 'blur' }],
  company: [{ required: true, message: '晋升单位不能为空', trigger: 'blur' }],
  promoteDate: [{ required: true, message: '时间不能为空', trigger: 'blur' }],
}) // 表单验证规则

const showDialog = ref(false)

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

  if (show === false) {
    staffPromote.value = {
      id: '',
      staffId: '',
      promoteExperience: '',
      company: '',
      promoteDate: '',
    }
  }
}

const initRecordData = (record: IStaffPromoteInfo) => {
  staffPromote.value = { ...record }
}

const saveStaffPromote = async () => {
  await promoteFormRef.value.validate((valid: boolean) => {
    if (valid === true) {
      if (staffPromote.value.id === '') {
        staffPromote.value.staffId = props.staffId
        addPromoteRec(staffPromote.value).then((res) => {
          if (res.code === 200) {
            ElMessage.success('晋升情况保存成功')
            emit('recordSaved')
            showRecordDialog(false)
          }
          else {
            ElMessage.success(`晋升情况保存失败：${res.message}`)
          }
        })
      }
      else {
        updatePromoteRec(staffPromote.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="promoteFormRef" :model="staffPromote" :rules="staffPromoteRules" label-position="right" label-width="110px" border stripe>
        <el-row :gutter="24">
          <el-col :span="18" :offset="3">
            <el-form-item label="晋升经历" prop="promoteExperience">
              <el-input v-model="staffPromote.promoteExperience" placeholder="请输入晋升经历" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24">
          <el-col :span="18" :offset="3">
            <el-form-item label="晋升单位" prop="company">
              <el-input v-model="staffPromote.company" placeholder="请输入晋升单位" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24">
          <el-col :span="18" :offset="3">
            <el-form-item label="时间" prop="promoteDate">
              <el-date-picker v-model="staffPromote.promoteDate" 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="saveStaffPromote">
          保存
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
