<template>
  <el-row>
    <el-row>
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item>
          <el-select v-model="searchForm.fileType" placeholder="文件类型" @change="search">
            <el-option label="全部" value="0" />
            <el-option label="图片" value="1" />
          </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-form :inline="true" class="demo-form-inline" style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
        <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">移动</el-button>
        <el-button size="mini" type="danger" @click="move">复制</el-button>
        <el-button size="mini" type="danger" @click="share">分享</el-button>
      </el-form>
      <el-breadcrumb separator-class="el-icon-arrow-right" style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/1' }">图片</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/1/2' }">照片</el-breadcrumb-item>
        <el-breadcrumb-item v-text="this.$router.currentRoute.name"></el-breadcrumb-item>
      </el-breadcrumb>
      <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="scope.row.filename" style="text-decoration-line: none" target="_blank">
              <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-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="cache(scope.row)"
            >详情</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="newFolderDialog"
      width="30%"
      center
    >
      <el-form ref="form" :model="newFolderForm">
        <el-form-item label="目录名">
          <el-input
            v-model="newFolderForm.newFolderName"
            placeholder="新目录名"
            style="width: 70%; padding-right: 2px"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click.native="loginBtn"
          >创建</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </el-row>
</template>

<script>
import { getFileList, deleteDiskFile } from '@/api/disk'

export default {
  name: 'FileList',
  data() {
    return {
      // 屏幕宽度, 为了控制分页条的大小
      screenWidth: document.body.clientWidth,
      currentPage: 1,
      pageSize: 10,
      totalSize: 0,
      dataList: [],
      searchForm: {
        page: 1,
        fileType: '0',
        filename: null
      },
      multipleSelection: [],
      parentDirs: ['图片', '照片', '2023'],
      newFolderDialog: false,
      newFolderForm: {
        newFolderName: null
      }
    }
  },
  created() {
    document.title = '文件列表'
    this.getData()
  },
  methods: {
    handleCurrentChange(pageNumber) {
      this.currentPage = pageNumber
      this.searchForm.page = this.currentPage
      this.getData()
      // 回到顶部
      scrollTo(0, 0)
    },
    getData() {
      this.dataList = []
      getFileList(this.searchForm).then(resp => {
        if (resp.code === 0) {
          this.dataList = resp.data.list
          this.totalSize = resp.data.totalSize
        } else {
          this.$notify({
            title: '提示',
            message: resp.msg,
            type: 'warning',
            duration: 3000
          })
        }
      }).catch(error => {
        this.$notify({
          title: '提示',
          message: error.message,
          type: 'error',
          duration: 3000
        })
      })
    },
    cache(row) {
    },
    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: '已取消'
        })
      })
    },
    createNewFolder() {
      this.newFolderDialog = true
    },
    move() {
      console.log('移动/复制文件')
    },
    share() {
      console.log('分享文件')
    }
  }
}
</script>

<style>
</style>
