<!-- 检定员证书编辑弹窗 -->
<script name="VerifierDialog" lang="ts" setup>
import { ElLoading, ElMessage } from 'element-plus'
import type { IStaffVerifierInfo } from '../person-regitster'
import { UploadFile } from '@/api/file'
import { addVerifierRec, updateVerifierRec } from '@/api/resource/register'

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

const emit = defineEmits(['recordSaved'])

const verifierFormRef = ref()

const title = ref<string>('')

const staffVerifier = ref<IStaffVerifierInfo>({
  id: '',
  staffId: '',
  certNo: '',
  certName: '',
  grantDate: '',
  effectiveDate: '',
  grantCompany: '',
  file: '',
})

const staffVerifierRules = ref({
  certNo: [{ required: true, message: '证书号不能为空', trigger: 'blur' }],
  certName: [{ required: true, message: '证书名称不能为空', trigger: 'blur' }],
  grantDate: [{ required: true, message: '发放日期不能为空', trigger: 'blur' }],
  effectiveDate: [{ required: true, message: '有效日期不能为空', trigger: 'blur' }],
  grantCompany: [{ required: true, message: '发放单位不能为空', trigger: 'blur' }],
}) // 表单验证规则

const showDialog = ref(false)

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

  if (show === false) {
    staffVerifier.value = {
      id: '',
      staffId: '',
      certNo: '',
      certName: '',
      grantDate: '',
      effectiveDate: '',
      grantCompany: '',
      file: '',
    }
  }
}

const initRecordData = (record: IStaffVerifierInfo) => {
  staffVerifier.value = { ...record }
}

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

const saveStaffVerifier = async () => {
  await verifierFormRef.value.validate((valid: boolean) => {
    if (valid === true) {
      if (staffVerifier.value.id === '') {
        staffVerifier.value.staffId = props.staffId
        addVerifierRec(staffVerifier.value).then((res) => {
          if (res.code === 200) {
            ElMessage.success('检定员证书保存成功')
            emit('recordSaved')
            showRecordDialog(false)
          }
          else {
            ElMessage.success(`检定员证书保存失败：${res.message}`)
          }
        })
      }
      else {
        updateVerifierRec(staffVerifier.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="verifierFormRef" :model="staffVerifier" :rules="staffVerifierRules" label-position="right" label-width="110px" border stripe>
        <el-row :gutter="24">
          <el-col :span="18" :offset="3">
            <el-form-item label="证书号" prop="certNo">
              <el-input v-model="staffVerifier.certNo" placeholder="请输入证书号" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24">
          <el-col :span="18" :offset="3">
            <el-form-item label="证书名称" prop="certName">
              <el-input v-model="staffVerifier.certName" placeholder="请输入证书名称" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24">
          <el-col :span="18" :offset="3">
            <el-form-item label="发放日期" prop="grantDate">
              <el-date-picker v-model="staffVerifier.grantDate" 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="effectiveDate">
              <el-date-picker v-model="staffVerifier.effectiveDate" 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="grantCompany">
              <el-input v-model="staffVerifier.grantCompany" 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="staffVerifier.file" type="hidden" />

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

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