<template>
  <div class="app-container">
    <div class="filter-container">

      <el-input v-model="search.keyWord" placeholder="关键字" style="width: 200px;" class="filter-item" />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="searchData">
        搜索
      </el-button>

      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-plus"
        @click="handleCreateAdd">
        上传文件
      </el-button>
    </div>
    <el-table
      :data="tableData"
      v-loading="listLoading"
      style="width: 100%;margin-bottom: 20px;"
      border
      default-expand-all>
      <el-table-column label="上传人" align="center" prop="userName" />
      <el-table-column label="文件名" align="center" prop="srcFileName" :show-overflow-tooltip="true" />
      <el-table-column label="文件描述" align="center" prop="fileDescription" :show-overflow-tooltip="true"/>
      <el-table-column label="文件类型" align="center" :show-overflow-tooltip="true">
        <template slot-scope="{row}">
          <span v-if="row.fileKind == 0">
            设计
          </span>
          <span v-else-if="row.fileKind ==1">
            公文
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="left" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-link :href="row.downloadUrl" target="_blank" type="primary">
            下载
          </el-link>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="listData.total>0" :total="listData.total" :page.sync="listData.page" :limit.sync="listData.pageSize" @pagination="getFileList" />

    <el-dialog :close-on-click-modal="false" :title="dialogTitle" :visible.sync="dialogVisible" width="600px">
      <el-form label-width="130px" label-position="right">
        <el-form-item label="文件描述">
          <el-input v-model="fileDescription" style="width: 240px"></el-input>
        </el-form-item>
        <el-form-item label="文件类型" prop="fileKind">
        <el-select v-model="fileKind" placeholder="请选择" style="width: 240px">
          <el-option
            v-for="item in fileKindOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
        <el-form-item label="上传文件">
          <plupload
            v-if="showUpload"
            :file-list="files"
            :limit="1"
            :description="fileDescription"
            :file-kind = 'fileKind'
            biz-type="trainModule"
            accept-files=".cdr,.ai,.ps,.eps,.zip,.rar,.xml,.txt,.pdf,.docx,.doc,.xlsx,.xls,.ppt,.pptx,.mp4,.mp3,.gif,.png,.jpg,.jpeg,.bmp"
            @onChange="handleFileChange"
          />
        </el-form-item>
      </el-form>
<!--      <div slot="footer" class="dialog-footer">-->
<!--        <el-button @click="dialogVisible = false">取 消</el-button>-->
<!--        <el-button type="primary" @click="dialogStatus==='1'?addBtn():editBtn()">确定上传</el-button>-->
<!--      </div>-->
    </el-dialog>
    <cancledialog @tableRefresh="tableRefresh" ref="confirmDialog"></cancledialog>
  </div>
</template>

<script>
import { fileList, addConfig, getConfigInfo, updateConfig } from '@/api/baseInfo/fileManagement.js'
import Cancledialog from '@/components/Dialog/Cancledialog.vue'
import plupload from '@/components/plupload/plupload.vue'

const form = {
  configKey: undefined,
  configName: undefined,
  configValue1: undefined,
  configValue2: undefined,
  configValue3: undefined,
  configType: 0,
  configModule: undefined,
  remark: undefined
}

export default {
  name: 'fileManagement',
  components: {
    Cancledialog, plupload
  },
  data() {
    return {
      dialogStatus: 1, // 1增加，2修改
      dialogTitle: '',
      dialogVisible: false,

      innerVisible: false,

      search: {
        keyWord: ''
      },
      form: Object.assign({}, form),

      confirmdialog: false,
      listLoading: false,
      tableData: [],

      listData: {
        total: 0,
        page: 1,
        pageSize: 10
      },

      fileKindOptions: [
        {
          value: 0,
          label: '设计'
        },
        {
          value: 1,
          label: '公文'
        }
      ],

      fileKind: 0,
      fileDescription: '',
      showUpload: true,
      files: []

    }
  },
  mounted() {
    this.getFileList()
  },
  methods: {
    // 获取列表
    getFileList() {
      const _that = this
      _that.listLoading = true
      const params = _that.search
      var page = {
        pageIndex: _that.listData.page,
        pageSize: _that.listData.pageSize
      }
      const data = {
        'params': JSON.stringify(params),
        'page': JSON.stringify(page)
      }
      // console.log(data);
      fileList(data).then(response => {
        console.log(response)
        const data = response.data
        // //  组合表单数据
        _that.tableData = data
        // console.log(_that.tableData)
        _that.listData.total = response.total
        _that.listLoading = false
      }).catch(err => {
        _that.$message.error(err)
        _that.listLoading = false
      })
    },

    // 增加上传文件
    handleCreateAdd() {
      const _that = this
      _that.form = Object.assign({}, form)
      _that.dialogTitle = '上传文件'
      _that.dialogStatus = '1'
      _that.dialogVisible = true
      _that.files = []
      _that.fileKind = 0
      _that.fileDescription = ''

      _that.showUpload = false
      _that.$nextTick(() => (_that.showUpload = true))
    },

    // 刷新变革
    tableRefresh() {
      // 刷新变革
      this.getFileList()
    },

    // 搜索
    searchData() {
      this.listData.page = 1
      this.getFileList()
    },

    handleFileChange(fileList) {
      var _that = this
      console.log('handleFileChange', fileList)
      _that.dialogVisible = false
      setTimeout(() => {
        _that.tableRefresh()
      }, 1000)
    }
  }
}
</script>

<style lang="scss" scoped>
  .filter-container {
    margin-bottom: 15px;
  }
  .tableItemInput{
    width: 30%;
  }
</style>
