<template>
  <div>
    <el-container>
      <el-header style="padding: 0px;display:flex;justify-content:space-between;align-items: center">
        <div style="display: inline">
          <el-button
            v-show="level>0"
            :loading="tableLoading"
            type="primary"
            style="margin-left: 5px;display: inline"
            size="mini"
            icon="el-icon-arrow-left"
            @click="back"
          >
            返回
          </el-button>
          <el-upload
            v-show="level>0"
            :http-request="dataUpload"
            :before-upload="beforeUpload"
            class="el-upload-button"
            show-file-list="false"
            action="/sysFile/upload"
            ref="upload"
          >
            <el-button v-loading.fullscreen.lock="fullscreenLoading" slot="trigger" size="mini" type="primary" >
              上传文件
            </el-button>
          </el-upload>
        </div>
      </el-header>
      <el-main style="padding-left: 0px;padding-top: 0px">
        <div>
          <el-table
            v-loading="tableLoading"
            :data="emps"
            height="550"
            border
            stripe
            size="mini"
            style="width: 100%;"
            @selection-change="handleSelectionChange">
            <el-table-column
              type="selection"
              header-align="center"/>
            <el-table-column
              :show-overflow-tooltip="true"
              header-align="center"
              width="250"
              label="文件名">
              <template slot-scope="scope" >
                <el-button
                  v-if="scope.row.fileType === '文件夹'"
                  icon="el-icon-arrow-right"
                  type="warning"
                  style="padding: 3px 4px 3px 4px;margin: 2px"
                  size="mini"
                  @click="open(scope.row)">{{ scope.row.fileName }}
                </el-button>
                <span v-else-if="scope.row.fileType === '文件'">{{ scope.row.fileName }}</span>
              </template>
            </el-table-column>
            <el-table-column
              :show-overflow-tooltip="true"
              prop="filePath"
              header-align="center"
              label="文件路径"/>
            <el-table-column
              sortable
              prop="fileType"
              header-align="center"
              width="180"
              label="文件类型"/>
            <el-table-column
              sortable
              prop="fileTime"
              header-align="center"
              width="180"
              label="文件修改时间"/>
            <el-table-column
              fixed="right"
              header-align="center"
              width="210"
              label="操作">
              <template slot-scope="scope">
                <el-button
                  v-show="scope.row.fileType === '文件'||scope.row.filePath.indexOf('logs')>-1 && scope.row.fileName != '日志文件'||scope.row.filePath.indexOf('systemFile')>-1 && scope.row.fileName != '系统运维资料'"
                  type="success"
                  style="padding: 3px 4px 3px 4px;margin: 2px"
                  size="mini"
                  icon="el-icon-download"
                  @click="downFile(scope.row)">下载
                </el-button>
                <el-button
                  type="danger"
                  style="padding: 3px 4px 3px 4px;margin: 2px"
                  size="mini"
                  icon="el-icon-delete"
                  @click="deleteEmp(scope.row)">删除
                </el-button>
                <el-button
                  v-show="scope.row.fileType === '文件'"
                  type="primary"
                  style="padding: 3px 4px 3px 4px;margin: 2px"
                  size="mini"
                  icon="el-icon-view"
                  @click="showFileContent(scope.row)">预览
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <div style="display: flex;justify-content: space-between;margin: 2px">
            <el-button
              v-if="false"
              :disabled="multipleSelection.length==0"
              type="danger"
              size="mini"
              @click="deleteManyEmps">批量删除
            </el-button>
            <el-button
              :disabled="multipleSelection.length==0"
              type="success"
              size="mini"
              @click="downloadManyEmps">批量下载
            </el-button>
          </div>
        </div>
      </el-main>
    </el-container>
    <el-form :model="emp" style="margin: 0px;padding: 0px;" label-width="100px" >
      <div style="text-align: left">
        <el-dialog
          :close-on-click-modal="false"
          :visible.sync="dialogVisibleInfo"
          :title="emp.dailogTitleName+'文件内容'"
          style="padding: 0px;"
          label-width="1px"
          width="1000px">
          <el-input v-model="emp.fileContent" :rows="20" :readonly="true" type="textarea" style="width: 100%"/>
          <span slot="footer" class="dialog-footer">
            <el-button size="mini" @click="cancelShowFileContent">关 闭</el-button>
          </span>
        </el-dialog>
      </div>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      emps: [],
      emp: {
        fileContent: '',
        dailogTitleName: ''
      },
      multipleSelection: [],
      depTextColor: '#c0c4cc',
      tableLoading: false,
      totalCount: -1,
      nowpath: '',
      fullscreenLoading: false,
      level: 0,
      dialogVisibleInfo: false
    }
  },
  mounted: function() {
    this.loadEmps()
  },
  methods: {
    beforeUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 1024 // 这里做文件大小限制
      if (!isLt2M) {
        this.$message({
          message: '上传文件大小不能超过1020M!',
          type: 'warning'
        })
      }
      return isLt2M
    },
    dataUpload(item) {
      this.fullscreenLoading = true
      const formData = new FormData()
      formData.append('path', this.nowpath)
      formData.append('file', item.file)
      this.$axios.post('/sysFile/upload', formData).then((response) => {
        this.$refs.upload.clearFiles()
        this.fullscreenLoading = false
        if (response && response.code === 0) {
          this.loadEmps()
          this.$message.success(response.data.msg)
        } else {
          this.$message.error(response.data.msg)
        }
      })
    },
    back() {
      var path = this.$fpath
      var _this = this
      // this.level为1，二级目录返回一级目录，path为空
      if (_this.level === 1) {
        _this.nowpath = ''
      } else {
        _this.nowpath = _this.nowpath.substring(0, _this.nowpath.lastIndexOf(path.sep))
      }
      _this.tableLoading = true
      this.$axios.get('/sysFile/getFileList',{params:{"path":_this.nowpath}}).then((resp) => {
        _this.tableLoading = false
        if (resp && resp.code === 0) {
          _this.level = _this.level - 1
          var data = resp.data
          _this.emps = data
        }
      })
    },
    downFile(row) {
      var _this = this
      var fn = row.fileName
      if (_this.level === 0) {
        fn = ''
      }

        _this.tableLoading = true
        const formData = new FormData()
        formData.append('path', row.filePath)
        formData.append('name', fn)
        formData.append('type', row.fileType)

        if(row.fileType === '文件夹'){
          this.$axios({ // 用axios发送post请求
            method: 'post',
            url: '/sysFile/downLoadFolder', // 请求地址
            data: formData, // 参数
            responseType: 'blob', // 表明返回服务器返回的数据类型
            headers: {
              'Content-Type': 'application/json'
            }
          }).then(res => { // 处理返回的文件流
            _this.tableLoading = false
            if (resp && resp.code === 0) {
              _this.$message.error('下载失败！')
            }
          })
        }else{
          this.$axios({ // 用axios发送post请求
            method: 'post',
            url: '/sysFile/downLoadFile', // 请求地址
            data: formData, // 参数
            responseType: 'blob', // 表明返回服务器返回的数据类型
            headers: {
              'Content-Type': 'application/json'
            }
          }).then(res => { // 处理返回的文件流
            _this.tableLoading = false
            if (resp && resp.code === 0) {
              _this.$message.error('下载失败！')
            }
          })
        }
    },
    open(row) {

      var path = this.$fpath
      var _this = this

      // this.level为0，一级目录跳转2级目录，不要拼接row.filePath+row.fileName
      if (_this.level === 0) {
        _this.nowpath = decodeURI(row.filePath)
      } else {
        _this.nowpath = path.join(row.filePath, row.fileName)
      }
      _this.tableLoading = true
      this.$axios.get('/sysFile/getFileList',{params:{"path":_this.nowpath}}).then((resp) => {
        _this.tableLoading = false
        if (resp && resp.code === 0) {
          _this.level = _this.level + 1
          var data = resp.data
          _this.emps = data
        }
      })
    },
    loadEmps() {
      var _this = this
      _this.tableLoading = true
      // _this.nowpath = 'D:/F_data/new'
      // const formData = new FormData()
      // formData.append('path', _this.nowpath)
      this.$axios.get('/sysFile/getFileList',{params:{"path":_this.nowpath}}).then((resp) => {
        _this.tableLoading = false
        if (resp && resp.code === 0) {
          var data = resp.data
          _this.emps = data
        }
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    deleteManyEmps() {
      this.$confirm('此操作将删除[' + this.multipleSelection.length + ']条数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var names = ''
        var pathfile = ''
        for (var i = 0; i < this.multipleSelection.length; i++) {
          names += this.multipleSelection[i].fileName + ','
        }
        if (names.length > 0) {
          pathfile = this.multipleSelection[0].filePath
          this.doDelete(names, pathfile)
        }
      }).catch(() => {
      })
    },
    downloadManyEmps() {
      var wjj = 0
      var wj = 0
      for (var i = 0; i < this.multipleSelection.length; i++) {
        if (this.multipleSelection[i].fileType === '文件') {
          wj++
        } else {
          wjj++
        }
      }
      this.$confirm('此操作选择[' + this.multipleSelection.length + ']条数据，其中包含[' + wjj + ']个文件夹，暂不支持下载文件夹，将下载[' + wj + ']条数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'success'
      }).then(() => {
        for (var i = 0; i < this.multipleSelection.length; i++) {
          this.downFile(this.multipleSelection[i])
        }
      }).catch(() => {
      })
    },
    deleteEmp(row) {
      this.$confirm('此操作将永久删除[' + row.fileName + '], 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doDelete(row.fileName, row.filePath)
      }).catch(() => {
      })
    },
    doDelete(name, path) {
      var _this = this
      _this.tableLoading = true
      const formData = new FormData()
      formData.append('path', path)
      formData.append('name', name)
      this.$axios.post('/sysFile/deleteFile',formData).then((resp) => {
        _this.tableLoading = false
        if (resp && resp.code === 0) {
          var data = resp.data
          _this.emps = data
        }
      })
    },
    showFileContent(row) {
      // 获取后台流文件内容
      var _this = this
      _this.tableLoading = true
      const path = row.filePath
      const name = row.fileName
      this.$axios.get('/sysFile/getFileContent',{params:{"path":path,"name":name}}).then(resp => {
        _this.tableLoading = false
        if (resp && resp.code === 0) {
          this.emp.fileContent = resp.data.fileContent
        }
      })

      // 将文件内容显示到弹出框中
      this.emp.dailogTitleName = row.fileName
      this.dialogVisibleInfo = true
    },
    cancelShowFileContent() {
      this.dialogVisibleInfo = false
      this.emptyEmpData()
    },
    emptyEmpData() {
      this.emp = {
        fileContent: ''
      }
    }
  }
}
</script>
<style>
    .el-dialog__body {
        padding-top: 0px;
        padding-bottom: 0px;
    }

    .slide-fade-enter-active {
        transition: all .8s ease;
    }

    .slide-fade-leave-active {
        transition: all .8s cubic-bezier(1.0, 0.5, 0.8, 1.0);
    }

    .slide-fade-enter, .slide-fade-leave-to {
        transform: translateX(10px);
        opacity: 0;
    }
    .el-upload-button {
      display: inline-block;
    }
</style>
