<script setup lang="ts">
import { Upload } from '@element-plus/icons-vue'
import { useFileDialog } from '@vueuse/core'
import { ElMessage } from 'element-plus'

import { uploadContestant } from '../api'

const emit = defineEmits<{
  (e: 'update'): void
}>()

// 批量导入
const { files, open, reset } = useFileDialog()
function handleImport() {
  open({
    accept: '.csv,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel',
    multiple: false
  })
}
watchEffect(() => {
  if (!files.value?.length) return
  const [file] = files.value
  uploadContestant({ file }).then(() => {
    ElMessage({ message: '导入成功', type: 'success' })
    emit('update')
  })
  reset()
})
</script>

<template>
  <el-dialog title="批量导入" :close-on-click-modal="false" width="400px">
    <div class="box">
      <el-button type="primary" round :icon="Upload" @click="handleImport">本地上传</el-button>
      <p>
        <a
          href="https://webapp-pub.ezijing.com/project/saas-lab/%E5%8F%82%E8%B5%9B%E9%80%89%E6%89%8B%E6%A8%A1%E6%9D%BF.xlsx"
          download
          >下载模板</a
        >
      </p>
    </div>
  </el-dialog>
</template>

<style lang="scss" scoped>
.box {
  padding: 20px 0;
  display: flex;
  align-items: center;
  justify-content: center;
  .el-button {
    width: 220px;
  }
  p {
    color: #999;
    margin-left: 20px;
  }
}
</style>
