<template>
  <el-row class="movie-list">
    <el-col :md="8" style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
      <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
        <el-card class="box-card" :body-style="{ paddingTop: '0px' }">
          <div slot="header" class="clearfix">
            <span>选择目录</span>
          </div>
          <div class="item">
            <el-tree
              :accordion="true"
              :data="treeNode"
              :props="defaultProps"
              highlight-current
              @node-click="handleNodeClick"
            >
              <span slot-scope="{ node, data }">
                <span :class="data.icon">{{ node.label }}</span>
              </span>
            </el-tree>
          </div>
        </el-card>
      </el-row>
    </el-col>
    <el-col :md="16" style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
      <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
              <span>存储节点: <span style="color: blue">{{ ossUrl }}</span></span>
            </el-row>
            <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
              <span>上传文件到目录: <span style="color: red">{{ currentDir }}</span></span>
            </el-row>
          </div>
          <div class="text item">
            <uploader
              class="uploader-example"
              :options="options"
              :auto-start="true"
              @file-added="onFileAdded"
              @file-success="onFileSuccess"
              @file-progress="onFileProgress"
              @file-error="onFileError"
            >
              <uploader-unsupport />
              <uploader-drop>
                <p>拖动文件到此处或</p>
                <uploader-btn :attrs="attrs">选择文件</uploader-btn>
              </uploader-drop>
              <uploader-list />
            </uploader>
          </div>
        </el-card>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { getServerInfo } from '@/api/content'
import { addDiskFile, getFolderTree } from '@/api/disk'

export default {
  name: 'UploadFile',
  data() {
    return {
      // ****************************************************************************************************************
      options: {
        target: process.env.VUE_APP_OSS_URL,
        chunkSize: 1024 * 1024 * 1024 * 10, // 10GiB
        fileParameterName: 'file',
        testChunks: false,
        query: (file, chunk) => {
          return {
            channelId: 1
          }
        },
        headers: {
          Authorization: ''
        },
        withCredentials: true
      },
      attrs: {
        accept: '*'
      },
      // ****************************************************************************************************************
      ossUrl: null,
      // ****************************************************************************************************************
      treeNode: [],
      defaultProps: {
        children: 'children',
        label: 'label',
        value: 'value'
      },
      currentDir: '/',
      // ****************************************************************************************************************
      pid: 0,
      form: {
        fileUrl: null,
        imageUrl: null
      }
    }
  },
  created() {
    document.title = '上传文件'
    getServerInfo(1).then(resp => {
      if (resp.code === 0) {
        const resData = resp.data
        this.ossUrl = resData.ossUrl
        this.options.target = resData.ossUrl
        this.options.chunkSize = resData.maxSize
        this.options.headers.Authorization = 'Bearer ' + resData.token
      } else {
        this.$notify({
          title: '提示',
          message: '获取 OSS 服务器地址失败, 暂时无法上传文件',
          type: 'error',
          duration: 3000
        })
      }
    }).catch(error => {
      this.$notify({
        title: '提示',
        message: error.message,
        type: 'warning',
        duration: 3000
      })
    })

    getFolderTree().then(resp => {
      if (resp.code === 0) {
        this.treeNode = resp.data
      } else {
        this.$notify({
          title: '提示',
          message: resp.msg,
          type: 'error',
          duration: 3000
        })
      }
    }).catch(error => {
      this.$notify({
        title: '提示',
        message: error.message,
        type: 'warning',
        duration: 3000
      })
    })
  },
  mounted() {
  },
  methods: {
    // ****************************************************************************************************************
    onFileAdded(file) {
      if (file.file.size > 1024 * 1024 * 1024 * 5) {
        file.cancel()
        this.$notify({
          title: '提示',
          message: '视频文件应小于 5GiB',
          type: 'warning',
          duration: 3000
        })
      }
    },
    onFileProgress(rootFile, file, chunk) {
    },
    onFileSuccess(rootFile, file, response, chunk) {
      const resp = JSON.parse(response)
      if (resp.code === 0) {
        this.form.fileUrl = resp.data.url

        const jsonData = {}
        jsonData.ossUrl = this.ossUrl
        jsonData.uploadId = resp.data.uploadId
        jsonData.pid = this.pid
        this.addDiskFile(jsonData)
      } else {
        this.$notify({
          title: '提示',
          message: resp.msg,
          type: 'warning',
          duration: 3000
        })
      }
    },
    onFileError(rootFile, file, response, chunk) {
      const resp = JSON.parse(response)
      this.$notify({
        message: resp.msg,
        type: 'warning',
        duration: 3000
      })
    },
    // ****************************************************************************************************************
    handleNodeClick(data) {
      this.pid = data.fileId
      this.currentDir = data.label
    },
    addDiskFile(jsonData) {
      addDiskFile(jsonData).then(resp => {
        if (resp.code !== 0) {
          this.$notify({
            message: resp.msg,
            type: 'warning',
            duration: 3000
          })
        }
      })
    }
  }
}
</script>

<style>
.uploader-example {
  width: 500px;
  padding: 15px;
  margin: 40px auto 0;
  font-size: 12px;
  box-shadow: 0 0 10px rgba(0, 0, 0, .4);
}
.uploader-example .uploader-btn {
  margin-right: 4px;
}
.uploader-example .uploader-list {
  max-height: 440px;
  overflow: auto;
  overflow-x: hidden;
  overflow-y: auto;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 320px;
  height: 240px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 320px;
  height: 240px;
  display: block;
}
</style>
