<!-- 公共资料库 -->
<template>
  <div>
    <template v-if="!addOrUpdateVisible && !detailVisible">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="queryPage()">
        <el-form-item label="名称">
          <el-input v-model="dataForm.fileName" placeholder="文件名称" clearable/>
        </el-form-item>
        <el-form-item label="文件类别">
          <el-select
            v-model="dataForm.fileType"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="item in optionsFileType"
              :key="item.code"
              :label="item.value"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="queryPage()">查询</el-button>
          <el-button v-if="isAuth('doc:ctl:save')" type="primary" @click="addOrUpdateHandle(0)">上传文件</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        style="width: 100%;">
        <el-table-column
          label="序号"
          type="index"
          width="50"
          align="center">
        </el-table-column>
        <el-table-column
          prop="fileName"
          header-align="center"
          align="center"
          min-width="140"
          :show-tooltip-when-overflow="true"
          label="文件名称">
        </el-table-column>
        <el-table-column
          prop="fileType"
          header-align="center"
          align="center"
          min-width="80"
          :show-tooltip-when-overflow="true"
          :formatter="formatFileType"
          label="文件类别">
        </el-table-column>
        <el-table-column
          prop="fileDescribe"
          header-align="center"
          align="center"
          width="180"
          :show-overflow-tooltip="true"
          label="文件描述">
        </el-table-column>
        <el-table-column
          prop="creator"
          header-align="center"
          align="center"
          width="100"
          :show-overflow-tooltip="true"
          label="上传人">
        </el-table-column>
        <el-table-column
          prop="uploadTime"
          header-align="center"
          align="center"
          min-width="140"
          :show-overflow-tooltip="true"
          label="上传时间">
        </el-table-column>
        <el-table-column
          prop="notes"
          header-align="center"
          align="center"
          width="180"
          :show-overflow-tooltip="true"
          label="备注">
        </el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          min-width="200"
          label="电子文件">
          <template slot-scope="scope">
            <div v-for="(item, index) in scope.row.attachList" :key="index + '_fujian'" style="display: inline">
              <span v-if="index > 0">,</span>
              <a :key="item.fileName + index" type="primary" href="#" @click="previewFile(item.fileName, item.url)">{{ item.fileName }}</a>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="150"
          label="操作">
          <template slot-scope="scope">
            <el-button v-if="isAuth('doc:ctl:info')" type="text" size="small" @click="detailHandle(scope.row.docId)">查看</el-button>
            <el-button v-if="isAuth('doc:ctl:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.docId)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </template>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"  @onChose="onChose"></add-or-update>
    <detail v-if="detailVisible" ref="detail"  @onChose="onChose"/>
    <!-- 文件预览 -->
    <preview-component v-if="previewVisible" ref="preview"/>
  </div>
</template>

<script>
  import AddOrUpdate from './doc-add-or-update'
  import Detail from './doc-detail'
  import { downloadUrl } from '@/api/file'
  import PreviewComponent from '../common/preview-component'
  export default {
    name: 'notice',
    components: {
      PreviewComponent,
      AddOrUpdate,
      Detail
    },
    data () {
      return {
        addOrUpdateVisible: false,
        detailVisible: false,
        previewVisible: false,
        dataForm: {
          fileName: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        downloadUrl: downloadUrl,
        dataListLoading: false,
        dataListSelections: [],
        optionsFileType: [
          {
            code: '1', value: '标准文件'
          },
          {
            code: '2', value: '公司文档'
          },
          {
            code: '3', value: '工艺文件'
          },
          {
            code: '4', value: '标准表格'
          },
          {
            code: '5', value: '生产记录'
          }
        ]
      }
    },
    created () {
      this.getDataList()
    },
    methods: {
      // 查询
      queryPage () {
        this.pageIndex = 1
        this.getDataList()
      },
      // 获取数据列表
      getDataList () {
        this.addOrUpdateVisible = false
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/file-service/document-ctl/list'),
          method: 'get',
          params: this.$http.adornParams({
            ...this.dataForm,
            'current': this.pageIndex,
            'size': this.pageSize
          })
        }).then(({data}) => {
          if (data && data.code === '200') {
            this.dataList = data.data.records
            this.totalPage = Number(data.data.total)
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 详情
      detailHandle (id) {
        this.detailVisible = true
        this.$nextTick(() => {
          this.$refs.detail.init(id)
        })
      },
      onChose () {
        this.addOrUpdateVisible = false
        this.detailVisible = false
      },
      // 预览
      previewFile (fileName, url) {
        this.previewVisible = true
        this.$nextTick(() => {
          this.$refs.preview.init(fileName, url)
        })
      },
      // 转换属性“文件类别”
      formatFileType (row) {
        if (this.optionsFileType && row.fileType) {
          const item1 = this.optionsFileType.find((item) => item.code === row.fileType.toString())
          return item1 ? item1.value : ''
        }
      }
    }
  }
</script>

<style scoped>

</style>
