<!-- 人员登记详情 -->
<script name="RegisterDetail" lang="ts" setup>
import RegisterBasic from './component/basic.vue'
import RegisterEducation from './component/education.vue'
import RegisterAuthorize from './component/authorize.vue'
import RegisterResume from './component/resume.vue'
import RegisterVerifier from './component/verifier.vue'
import RegisterTrainning from './component/trainning.vue'

// 定义变量
const route = useRoute()
const router = useRouter()
const title = ref<string>('')
const type = ref<string>('')

const radioItems = ref([
  { name: '基本信息', value: 'register-basic' },
  { name: '教育科研', value: 'register-education' },
  { name: '授权信息', value: 'register-authorize' },
  { name: '人员履历', value: 'register-resume' },
  { name: '检定员信息', value: 'register-verifier' },
  { name: '训练情况', value: 'register-trainning' },
])
const current = ref<string>('')
const currentLabel = ref<string>('')

const staffId = ref<string>('')

// 子组件的引用
const refRegisterBasic = ref()
const refRegisterEducation = ref()
const refRegisterAuthorize = ref()
const refRegisterResume = ref()
const refRegisterVerifier = ref()

// 逻辑
const printToWord = () => {

}

const printToPDF = () => {

}

// 关闭
const resetForm = () => {
  sessionStorage.removeItem('staffBasicInfo')
  router.go(-1)
}

// 保存
const saveForm = () => {
  switch (current.value) {
    case 'register-basic':
      refRegisterBasic.value.saveBasicForm()
      break

    default:
      break
  }
}

// 弹窗初始化
const initDialog = (params: any) => {
  type.value = params.type

  current.value = radioItems.value[0].value
  currentLabel.value = radioItems.value[0].name

  switch (params.type) {
    case 'create' :
      title.value = '人员登记（新增）'
      break
    case 'update':
      title.value = '人员登记（编辑）'
      staffId.value = params.id

      // 调用子组件的方法
      refRegisterBasic.value.getStaffBasicById(staffId.value)
      break
    case 'detail':
      title.value = '人员登记（详情）'
      staffId.value = params.id

      // 调用子组件的方法
      refRegisterBasic.value.getStaffBasicById(staffId.value)

      break
    default:
      title.value = ''
      break
  }
}

const getStaffId = (id: string) => {
  staffId.value = id
}

const radioChangeHandler = (newVal: string | number | boolean) => {
  const radioTarget = radioItems.value.filter(item => item.name === newVal)
  if (radioTarget.length > 0) {
    currentLabel.value = radioTarget[0].name
    current.value = radioTarget[0].value
  }
  else {
    currentLabel.value = radioItems.value[0].name
    current.value = radioItems.value[0].value
  }
}

const eduFileChangeHandler = (filename: string) => {
  refRegisterBasic.value.setEducationFile(filename)
}

onMounted(() => {
  initDialog(route.query)
})
</script>

<template>
  <app-container style="overflow: hidden;">
    <detail-page :title="`${title}`">
      <template #btns>
        <template v-if="type === 'detail'">
          <el-button type="primary" @click="printToWord()">
            导出Word
          </el-button>
          <el-button type="primary" @click="printToPDF()">
            导出PDF
          </el-button>
        </template>
        <el-button v-if="type !== 'detail' && current === 'register-basic'" type="primary" @click="saveForm()">
          保存
        </el-button>
        <el-button type="info" @click="resetForm()">
          关闭
        </el-button>
      </template>

      <el-radio-group v-model="currentLabel" style="margin-bottom: 20px;" @change="radioChangeHandler">
        <el-radio-button v-for="item in radioItems" :key="item.value" :label="item.name" :disabled="staffId === ''" />
      </el-radio-group>

      <register-basic
        v-show="current === 'register-basic'" ref="refRegisterBasic"
        :operation="type"
        @after-add-basic="getStaffId" @return-to-list="resetForm"
      />
      <register-education
        v-show="current === 'register-education'" ref="refRegisterEducation"
        :operation="type"
        :staff-id="staffId"
        @education-file-changed="eduFileChangeHandler"
      />
      <register-authorize
        v-show="current === 'register-authorize'" ref="refRegisterAuthorize"
        :operation="type"
        :staff-id="staffId"
      />
      <register-resume
        v-show="current === 'register-resume'" ref="refRegisterResume"
        :operation="type"
        :staff-id="staffId"
      />
      <register-verifier
        v-show="current === 'register-verifier'" ref="refRegisterVerifier"
        :operation="type"
        :staff-id="staffId"
      />
      <register-trainning v-show="current === 'register-trainning'" ref="register-education-form" :staff-id="staffId" />
    </detail-page>
  </app-container>
</template>
