<template>
  <el-dialog :modelValue="show" width="40%" @close="handleClose">
    <el-upload
      drag
      :action="action"
      :data="data"
      :accept="fileAccept"
      multiple
      :headers="{ Authorization: globalStore.token }"
      :show-file-list="false"
      :before-upload="handleBeforeUpload"
      :on-success="handleAvatarSuccess"
    >
      <div class="flx-center-column">
        <div class="upload-title">拖拽文件到这里或点击上传简历</div>
        <p>简历建议使用 PDF 文件，也支持DOC、DOCX、JPG、PNG 格式</p>
        <span>文件大小不超过20M</span>
        <div class="btn">上传简历</div>
      </div>
    </el-upload>
  </el-dialog>
</template>

<script setup lang="ts">
import type { UploadProps } from 'element-plus'
import { useGlobalStore } from '@/stores/modules/global'

const globalStore = useGlobalStore()

const props = defineProps({
  show: {
    type: Boolean,
    default: false
  },
  action: {
    type: String,
    default: ''
  },
  data: {
    type: Object,
    default: () => {}
  },
  fileAccept: {
    type: String,
    default: '.pdf,.doc,.docx,.jpg,.png'
  },
  message: {
    type: String,
    default: ''
  }
})

const emits = defineEmits(['update:show', 'init'])

const handleBeforeUpload = (file: any) => {
  const isLt20M = file.size / 1024 / 1024 < 20
  if (!isLt20M) {
    ElMessage.warning('文件大小超过20MB限制')
  }
  return isLt20M
}

const handleAvatarSuccess: UploadProps['onSuccess'] = res => {
  if (res.code === 200) {
    ElNotification.success({
      title: props.message,
      message: '上传成功'
    })
    emits('init')
    handleClose()
  } else {
    ElMessage.error(res.msg)
  }
}

const handleClose = () => {
  emits('update:show', false)
}
</script>

<style lang="scss" scoped>
.upload-title {
  height: 18px;
  font-size: 18px;
  font-weight: 400;
  color: #222222;
  line-height: 18px;
}
p {
  height: 18px;
  font-size: 16px;
  font-weight: 400;
  color: #999999;
  line-height: 18px;
  margin-top: 50px;
}

span {
  height: 18px;
  font-size: 16px;
  font-weight: 400;
  color: #ff3333;
  line-height: 18px;
  margin-top: 16px;
}
.btn {
  width: 152px;
  height: 34px;
  background: linear-gradient(90deg, #177cfa 0%, #177cfa 100%);
  border-radius: 4px;
  font-size: 18px;
  font-weight: 400;
  color: #ffffff;
  line-height: 34px;
  text-align: center;
  margin: 50px 0;
}
</style>
