<template>
  <el-row>
    <el-row>
      <el-form :inline="true" :model="searchForm" class="demo-form-inline" style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
        <el-form-item>
          <el-button size="mini" type="danger" @click="goToUpload">上传</el-button>
          <el-button size="mini" type="danger" @click="downloadFile">下载</el-button>
          <el-button size="mini" type="danger" @click="deleteMultiple">删除</el-button>
          <el-button size="mini" type="danger" @click="createNewFolder">新建文件夹</el-button>
          <el-button size="mini" type="danger" @click="move(1)">移动</el-button>
          <el-button size="mini" type="danger" @click="move(2)">复制</el-button>
          <el-button size="mini" type="danger" @click="share">分享</el-button>
        </el-form-item>
        <el-form-item>
          <el-select v-model="searchForm.fileType" placeholder="文件类型" @change="onSelect">
            <el-option label="全部" value="0" />
            <el-option label="图片" value="1001" />
            <el-option label="视频" value="1002" />
            <el-option label="音频" value="1003" />
            <el-option label="文本" value="1004" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input v-model="searchForm.filename" placeholder="" />
        </el-form-item>
        <el-form-item>
          <el-button size="mini" type="warning" @click="search">查询</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <el-row>
      <el-col :md="2">
        <span>当前路径:</span>
      </el-col>
      <el-col :md="22">
        <el-breadcrumb separator-class="el-icon-arrow-right" style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
          <el-breadcrumb-item v-for="(item, index) of parentDirs" :key="index">
            <a href="javascript:void(0)" style="text-decoration-line: none" @click="goTo(item.path)">{{ item.filename }}</a>
          </el-breadcrumb-item>
        </el-breadcrumb>
      </el-col>
    </el-row>
    <el-row>
      <el-table
        ref="multipleTable"
        :data="dataList"
        border
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
        />
        <el-table-column
          prop="filename"
          label="文件名"
          width="400px"
        >
          <template slot-scope="scope">
            <a href="javascript:void(0)" style="text-decoration-line: none" @click="handleFile(scope.row)">
              <el-image :src="scope.row.icon" min-width="16" height="16" />
              <el-tooltip
                :content="scope.row.filename"
                raw-content
                placement="top-start"
              >
                <span v-if="scope.row.filename.length <= 60">
                  {{ scope.row.filename }}
                </span>
                <span v-else>
                  {{ scope.row.filename.substr(0, 55) + "..." }}
                </span>
              </el-tooltip>
            </a>
          </template>
        </el-table-column>
        <el-table-column
          prop="owner"
          label="修改时间"
        />
        <el-table-column
          prop="size"
          label="大小"
        />
        <el-table-column
          prop="fileType"
          label="文件类型"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.fileType === 1000" :type="'warning'" disable-transitions>
              目录
            </el-tag>
            <el-tag v-else-if="scope.row.fileType === 1001" :type="'warning'" disable-transitions>
              图片
            </el-tag>
            <el-tag v-else-if="scope.row.fileType === 1002" :type="'success'" disable-transitions>
              视频
            </el-tag>
            <el-tag v-else-if="scope.row.fileType === 1003" :type="'danger'" disable-transitions>
              音频
            </el-tag>
            <el-tag v-else-if="scope.row.fileType === 1004" :type="'danger'" disable-transitions>
              文本
            </el-tag>
            <el-tag v-else :type="'danger'" disable-transitions>
              其他
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="getDetail(scope.row.fileId)"
            >详情</el-button>
            <el-button
              size="mini"
              @click="cache(scope.row)"
            >分享</el-button>
            <el-button
              size="mini"
              @click="cache(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-row>
      <el-pagination
        background
        :small="screenWidth <= 768"
        layout="prev, pager, next"
        :page-size="pageSize"
        :current-page="currentPage"
        :total="totalSize"
        @current-change="handleCurrentChange"
        @prev-click="handleCurrentChange"
        @next-click="handleCurrentChange"
      />
    </el-row>

    <!--创建新目录弹窗-->
    <el-dialog
      title="创建新目录"
      append-to-body
      :visible.sync="createFolderDialog"
      width="30%"
      center
    >
      <el-form ref="form" :model="createFolderForm">
        <el-form-item label="目录名">
          <el-input
            v-model="createFolderForm.folderName"
            placeholder="新目录名"
            style="width: 70%; padding-right: 2px"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="onCreateFolder"
          >创建</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!--文件预览弹窗-->
    <el-dialog
      title="预览文件"
      append-to-body
      :visible.sync="previewFileDialog"
      width="70%"
      center
    >
      <div v-if="fileDetail !== null">
        <div v-if="fileDetail.fileType === 1001">
          <el-image :src="fileDetail.originalUrl" />
        </div>
        <div v-else-if="fileDetail.fileType === 1002">
          <video-preview-player :video-prop.sync="videoProp" />
        </div>
        <div v-else-if="fileDetail.fileType === 1003">
          <span>音频文件</span>
        </div>
        <div v-else-if="fileDetail.fileType === 1004">
          <span>文本文件</span>
        </div>
        <div v-else>
          <span>文件没有预览</span>
        </div>
      </div>
    </el-dialog>
    <!--移动/复制弹窗-->
    <el-dialog
      title="移动文件"
      append-to-body
      :visible.sync="moveDialog"
      width="50%"
      center
    >
      <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>
            <el-button style="float: right; padding: 3px 0" type="text" @click="onMove">确定</el-button>
          </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-dialog>
  </el-row>
</template>

<script>
import VideoPreviewPlayer from 'components/VideoPreviewPlayer'
import {
  getFileList,
  deleteDiskFile,
  addDiskFolder,
  getFileDetail,
  getFileUrl,
  getFolderTree,
  moveDiskFile
} from '@/api/disk'

export default {
  name: 'FileList',
  components: { VideoPreviewPlayer },
  data() {
    return {
      // 屏幕宽度, 为了控制分页条的大小
      screenWidth: document.body.clientWidth,
      currentPage: 1,
      pageSize: 10,
      totalSize: 0,
      dataList: [],
      searchForm: {
        page: 1,
        path: '/',
        fileType: '0',
        filename: null
      },
      currentDir: '/',
      multipleSelection: [],
      parentDirs: [],
      createFolderDialog: false,
      createFolderForm: {
        parent: null,
        folderName: null
      },
      previewFileDialog: false,
      fileDetail: null,
      moveDialog: false,
      treeNode: [],
      defaultProps: {
        children: 'children',
        label: 'label',
        value: 'value'
      },
      moveForm: {
        type: 1,
        pid: null,
        fileIds: []
      }
    }
  },
  created() {
    const path = this.$route.query.path
    if (path !== undefined && path !== null) {
      this.searchForm.path = path
    }

    document.title = '文件列表'
    this.getData(this.searchForm)
  },
  methods: {
    handleCurrentChange(pageNumber) {
      this.currentPage = pageNumber
      this.searchForm.page = this.currentPage
      this.getData(this.searchForm)
      // 回到顶部
      scrollTo(0, 0)
    },
    getData(searchForm) {
      this.dataList = []
      getFileList(searchForm).then(resp => {
        if (resp.code === 0) {
          this.dataList = resp.data.fileList.list
          this.totalSize = resp.data.fileList.totalSize
          this.parentDirs = resp.data.pathList
          this.currentDir = this.parentDirs[this.parentDirs.length - 1].path
        } else {
          this.$notify({
            title: '提示',
            message: resp.msg,
            type: 'warning',
            duration: 3000
          })
        }
      }).catch(error => {
        this.$notify({
          title: '提示',
          message: error.message,
          type: 'error',
          duration: 3000
        })
      })
    },
    getDetail(fileId) {
      getFileDetail(fileId).then(resp => {
        if (resp.code === 0) {
          console.log(resp.data)
        }
      })
    },
    cache(row) {
    },
    onSelect() {
      this.currentPage = 1
      this.searchForm.page = this.currentPage
      this.getData(this.searchForm)
    },
    search() {
      this.currentPage = 1
      this.searchForm.page = this.currentPage
      this.getData(this.searchForm)
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    goToUpload() {
      const path = '/disk/upload'
      if (this.$route.path === path) {
        this.$router.go(0)
        return
      }
      this.$router.push(path)
    },
    downloadFile() {
      console.log('下载文件')
    },
    deleteMultiple() {
      if (this.multipleSelection.length === 0) {
        this.$notify({
          message: '至少应选中一行',
          type: 'warning',
          duration: 3000
        })
        return
      }

      var fileIds = []
      for (const item of this.multipleSelection) {
        fileIds.push(item.fileId)
      }

      this.$confirm('确定要删除文件?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteDiskFile(fileIds).then(resp => {
          if (resp.code === 0) {
            this.$refs.multipleTable.clearSelection()
            this.$notify({
              message: resp.msg,
              type: 'warning',
              duration: 3000
            })
          } else {
            this.$notify({
              title: '提示',
              message: resp.msg,
              type: 'warning',
              duration: 3000
            })
          }
        }).catch(error => {
          this.$notify({
            title: '提示',
            message: error.message,
            type: 'error',
            duration: 3000
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    handleFile(item) {
      console.log(item)
      const fileType = item.fileType
      if (fileType === 1000) {
        const filename = item.filename
        const namePath = this.parentDirs[this.parentDirs.length - 1]
        let currentPath
        if (namePath.filename === '/') {
          currentPath = namePath.path + filename
        } else {
          currentPath = namePath.path + '/' + filename
        }
        this.searchForm.path = currentPath
        this.getData(this.searchForm)
      } else {
        const fileId = item.fileId
        this.previewFile(fileType, fileId)
      }
    },
    goTo(path) {
      this.searchForm.page = 1
      this.searchForm.path = path
      this.getData(this.searchForm)
    },
    previewFile(fileType, fileId) {
      getFileUrl(fileType, fileId).then(resp => {
        if (resp.code === 0) {
          this.fileDetail = resp.data
          this.previewFileDialog = true
        }
      }).catch(error => {
        this.$notify({
          title: '请求错误',
          message: error.message,
          type: 'error',
          duration: 3000
        })
      })
    },
    createNewFolder() {
      this.createFolderDialog = true
    },
    onCreateFolder() {
      this.createFolderForm.parent = this.currentDir
      addDiskFolder(this.createFolderForm).then(resp => {
        if (resp.code === 0) {
          this.$router.go(0)
        } else {
          this.$notify({
            message: resp.msg,
            type: 'warning',
            duration: 3000
          })
        }
      })
    },
    move(type) {
      if (this.multipleSelection.length === 0) {
        this.$notify({
          message: '至少应选中一行',
          type: 'warning',
          duration: 3000
        })
        return
      }

      this.moveForm.type = type
      for (const item of this.multipleSelection) {
        this.moveForm.fileIds.push(item.fileId)
      }

      getFolderTree().then(resp => {
        if (resp.code === 0) {
          this.treeNode = resp.data
          this.moveDialog = true
        } else {
          this.$notify({
            title: '提示',
            message: resp.msg,
            type: 'error',
            duration: 3000
          })
        }
      }).catch(error => {
        this.$notify({
          title: '提示',
          message: error.message,
          type: 'warning',
          duration: 3000
        })
      })
    },
    handleNodeClick(data) {
      this.moveForm.pid = data.fileId
    },
    onMove() {
      moveDiskFile(this.moveForm).then(resp => {
        if (resp.code === 0) {
          this.moveDialog = false
          this.$refs.multipleTable.clearSelection()
        }
      })
    },
    share() {
      this.$notify({
        title: '提示',
        message: '分享文件接口未实现',
        type: 'info',
        duration: 3000
      })
    }
  }
}
</script>

<style>
</style>
