<template>
  <div class="app-container filePreview">
    <div class="page-container filePreviewPage">
      <div class="page-form-title-box">
        <el-form :model="queryParams" class="form-box" ref="queryForm" :inline="true">
          <el-form-item label="" prop="category" label-width="60px">
            <el-select v-model="queryParams.category" clearable placeholder="请选择分类" style="width: 200px">
              <el-option
                v-for="item in optionList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="fileName">
            <el-input
              maxLength="30"
              v-model="queryParams.fileName"
              placeholder="请输入文件名"
              clearable
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item label="" prop="dateRange">
            <el-date-picker
              :clearable="false"
              v-model="dateRange"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="上报时间"
              end-placeholder="上报时间"
            ></el-date-picker>
          </el-form-item>
          <p class="page-inquire-common-style-button" @click="handleQuery">查询</p>
          <p class="page-reset-common-style-button" @click="resetQuery">重置</p>
        </el-form>
      </div>
      <div class="page-content-box">
        <el-table class="table-box" v-loading="loading" border :data="tableList" ref="multipleTable">
          <el-table-column label="文件名" align="center" prop="fileName" show-overflow-tooltip/>
          <el-table-column label="类型" align="center" prop="fileType" show-overflow-tooltip width="130"/>
          <el-table-column label="尺寸（MB）" align="center" prop="fileSize" show-overflow-tooltip width="130"/>
          <el-table-column label="文件地址" align="center" prop="fileUrl" show-overflow-tooltip width="470">
            <template slot-scope="scope">
              <p class="clickCopy" @click="clickCopy(scope.row.fileUrl,'文件地址')">{{scope.row.fileUrl}}</p>
            </template>
          </el-table-column>
          <el-table-column label="上传时间" align="center" prop="createTime" show-overflow-tooltip width="200">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime,"{y}-{m}-{d} {h}:{i}") }}</span>
            </template>
          </el-table-column>
          <el-table-column label="上传人" align="center" prop="userName" show-overflow-tooltip width="170"/>
          <el-table-column label="操作" align="center" prop="deptName" width="170" v-if="tableButtonType">
            <template slot-scope="scope">
              <div class="table-button-box">
                <p class="table-button-null"></p>
                <p class="table-button-p" @click="tableButton(scope.row)"
                   v-hasPermiRouter="['system:files:detail']">预览</p>
                <p class="table-button-null"></p>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <pagination :page-sizes="[20, 30, 40, 50]"
                    v-show="total>0"
                    :total="total"
                    :page.sync="queryParams.page"
                    :limit.sync="queryParams.pageSize"
                    @pagination="getList"
        />
      </div>
    </div>
    <!--单张图片预览-->
    <fullScreenView :fullScreenViewProps="fullScreenViewProps" ref="fullScreenView"></fullScreenView>
    <!--视频预览-->
    <lookVideoDialog ref="lookVideoDialog"></lookVideoDialog>
    <!--文件预览-->
    <fullScreenFileLook ref="fullScreenFileLook"></fullScreenFileLook>
  </div>
</template>

<script>
  import { getFileList,getFileCategory } from "@/api/systemManagement/index";
  import fullScreenView from "@/components/fullScreenView/fullScreenView.vue";
  import lookVideoDialog from '@/components/lookVideoDialog/lookVideoDialog.vue'
  import fullScreenFileLook from "@/components/fullScreenFileLook/fullScreenFileLook.vue";

  export default {
    name: 'filePreview',
    components: {
      fullScreenView,
      lookVideoDialog,
      fullScreenFileLook
    },
    data(){
      return{
        tableButtonType:this.hasPermiDom(['system:files:detail']),
        loading:false,
        optionList:[],
        queryParams:{
          page:1,
          pageSize:20,
          category:"",
          fileName:"",
        },
        dateRange:[],
        tableList:[],
        total:0,
        propsLookDocumentData:{},
        lookDocumentType:false,
        fullScreenViewProps:[],
      }
    },
    created(){

    },
    mounted(){
      this.getFileCategory();
      this.getList();
    },
    methods:{
      handleQuery(){
        this.$set(this.queryParams,'page',1);
        this.getList();
      },
      resetQuery(){
        this.$set(this,'dateRange',[])
        this.$set(this,'queryParams',{
          page:1,
          pageSize:20,
          category:"",
          fileName:"",
        });
        this.getList();
      },
      //获取数据列表
      getList(){
        this.$set(this,'loading',true);
        let obj = JSON.parse(JSON.stringify(this.queryParams))
        if(this.dateRange[0]){
          obj.startTime = this.dateRange[0]+'T00:00:00'
          obj.endTime = this.dateRange[1]+'T23:59:59'
        }else{
          obj.startTime = "";
          obj.endTime = "";
        }
        getFileList(obj).then(response => {
          this.$set(this,'loading',false);
          this.$set(this,'tableList',response.data.records);
          this.$set(this,'total',response.data.total);
        });
      },
      //预览
      tableButton(item){
        //判断类型
        if(item.fileName&&item.fileUrl){
          let type = '';
          if(item.fileUrl.split('.')[1] == 'docx'){
            type = 'docx'
          }else if(item.fileUrl.split('.')[1] == 'xlsx'){
            type = 'excel'
          }else if(item.fileUrl.split('.')[1] == 'pdf'){
            type = 'pdf'
          }else if(item.fileUrl.split('.')[1] == 'png' || item.fileUrl.split('.')[1] == 'jpg' || item.fileUrl.split('.')[1] == 'gif'){
            type = 'img'
          }else if(item.fileType.indexOf("video") !== -1){
            type = 'mp4'
          }
          //整合地址
          let url = localStorage.getItem('fileBrowseEnvironment')+item.fileUrl;
          // 判断图片还是文件
          if(type == 'img'){
            this.$set(this,'fullScreenViewProps',[url]);
            this.$refs['fullScreenView'].initialize();
          }else if(type == 'docx' || type == 'excel' || type == 'pdf'){
            this.$refs['fullScreenFileLook'].initialize(item.fileName,url,type);
          }else if(type == 'mp4'){
            this.$refs.lookVideoDialog.lookVideoDialogOpen(1,url,'预览');
          }else{
            this.msgError('该类型文件暂无法预览')
          }
        }else{
          this.msgError('数据异常无法预览')
        }
      },
      lookDocumentButton(type,url){
        if(type==1){
          this.$set(this,'propsLookDocumentData',{
            title:"预览",
            url:url
          });
          this.$set(this,'lookDocumentType',true);
        }else{
          this.$set(this,'lookDocumentType',false);
        }
      },
      //获取文件分类列表
      getFileCategory(){
        getFileCategory().then(response => {
          this.$set(this,'optionList',response.data);
        });
      },
    },
  }
</script>

<style scoped lang="scss">
  .filePreview{
    .filePreviewPage{

    }
  }
</style>
