<template>
    <el-dialog v-model="visible" title="导入职工信息" width="600" append-to-body>
      <el-alert
        type="warning"
        :closable="false"
        show-icon
        style="margin-bottom: 20px;">
        <template #title>
          <span> 可先导出教职工信息,按照导出信息的模板填入职工数据,再执行导入</span>
        </template>
      </el-alert>

<!--      <div style="text-align: center; margin-bottom: 20px;">-->
<!--        <a href="excel/dictlist.xlsx" rel="external nofollow" download="职工信息字典下载" style="text-decoration: none;">-->
<!--          <el-button type="success" :icon="Download">下载字典文件</el-button>-->
<!--        </a>-->
<!--      </div>-->

      <el-upload
        ref="uploadRef"
        class="upload-demo"
        action="/professional/file/makeImportTeacherInfoSimpleTask"
        :headers="headers"
        :accept="'.xls,.xlsx'"
        :on-success="handleUploadSuccess"
        :on-error="handleAvatarError"
        :limit="1"
        drag>
        <el-icon class="el-icon--upload"><upload-filled /></el-icon>
        <div class="el-upload__text">
          将文件拖到此处，或<em>点击上传</em>
        </div>
        <template #tip>
          <div class="el-upload__tip">
            只能上传 .xls 或 .xlsx 格式的 Excel 文件
          </div>
        </template>
      </el-upload>
    </el-dialog>
</template>

<script setup lang="ts">
  import { ref, computed, nextTick } from 'vue'
  import { ElNotification } from 'element-plus'
  import { Download, UploadFilled } from '@element-plus/icons-vue'
  import { Session } from '/@/utils/storage'

  // 响应式数据
  const visible = ref(false)

  // 计算属性
  const headers = computed(() => {
        return {
      "Authorization": 'Bearer ' + Session.getToken()
        }
  })

  const uploadRef = ref<{ clearFiles?: () => void }>()

  // 方法
  const init = () => {
    visible.value = true
    nextTick(() => {
      uploadRef.value?.clearFiles?.()
    })
  }

  const handleUploadSuccess = () => {
    visible.value = false
    ElNotification({
      title: '成功',
      message: '导入成功',
      type: 'success',
    })
  }

  const handleAvatarError = (err: any) => {
    const result = JSON.parse(err.message)
    if (result.code == "1") {
      ElNotification.error({
        title: '错误',
        message: result.msg,
        duration: 30000
      })
      }
    }

  // 暴露方法给父组件
  defineExpose({
    init
  })
</script>

<style scoped lang="scss">
.upload-demo {
  :deep(.el-upload-dragger) {
    width: 100%;
  }
}
</style>
