<template>
  <div>
    <Card style="height: 95%">
      <Row style="margin: 0px 0;" type="flex">
        <Col span="11" order="1">
          <!--高级检索-->
          <bc-senior ref="senior" common senior keyValue2 clear :pageName="$options.name" :fieldList="fieldList"
                     :commonList="commonList" @seniorFind="reloadData" :item.sync="searchForm"></bc-senior>
        </Col>
        <Col span="13" order="2" style="text-align: right">
          <div style="width: 55px;float: right;margin-top: 10px">
            <bc-custom-sort ref="customSort" :sortParams.sync="sortParams" :searchForm="searchForm"
                            @reload-data="reloadData"></bc-custom-sort>
          </div>
          <div style="width: 65px;float: right;margin-top: 10px">
            <bc-table-ex :dataColumns.sync='dataColumns' :columnsShow.sync="columnsShow" firstColumn="selection"
                         :pageKey="this.$options.name"
                         layout ex></bc-table-ex>
          </div>
          <div style="width: 60px;float: right;margin-top: 10px">
            <!--<bc-export-excel :dataColumns="columnsShow" :selectedData="selectedData" :tableData="tableData"
                             :excelName="'attachmentExcel'"
                             :searchForm="searchForm" @export-data="exportData"
                             :getData="this.getList"></bc-export-excel>-->
          </div>
          <div style="width: 90px;float: right">
            <!--<Button @click="showUpload" type="primary">批量导入</Button>
            <bc-upload :show.sync="uploadAttachment" :import-url="importUrl" ></bc-upload>-->
          </div>
          <div style="width: 90px;float: right">
            <Button @click="attachmentAdd" type="primary">添加</Button>
          </div>
        </Col>
      </Row>
      <Table ref="tables" searchable search-place="top" :data="tableData" :columns="columnsShow" size="small"
             :loading="loading" @on-sort-change="sortChange" @on-selection-change="selectedChange"
             highlight-row @on-current-change="onCurrentChange" style="min-height: 670px;margin-top: 10px;" border>
        <template slot="actions" slot-scope="{ row }">
          <Button type="primary" size="small" @click="toPreview(row)" ghost style="margin-right: 5px">预览</Button>
          <Button type="primary" size="small" @click="downFile($event,row)" ghost style="margin-right: 5px">下载</Button>
          <Poptip confirm transfer size="small"
                  placement="bottom"
                  width="180"
                  title="你确定要删除吗?"
                  @on-ok="attachmentDelete(row)">
            <Button type="primary" size="small" ghost>删除</Button>
          </Poptip>
        </template>
      </Table>
      <bcPage ref="attachment-list-page" @reload-data="reloadData" :dataCount="dataCount" size="small"
              :pageSize.sync="pageSize"></bcPage>
      <attachmentEdit ref="attachmentEdit" :show.sync="showModal" :value="modalParams"
                      @reload-data="reloadData"></attachmentEdit>
    </Card>
  </div>
</template>

<script>
  import { getAttachmentPage, getAttachmentList, deleteAttachment } from '../../../../api/base/attachment-api'
  import attachmentEdit from './attachment-modal.vue'
  import {jumpPrintPageFdfs, downFileFdfs} from '../../../bc-libs/print-util'
  let $this
  export default {
    name: 'attachmentPage',
    components: {
      attachmentEdit,
    },
    data () {
      return {
        importUrl: '',
        uploadAttachment: false,
        searchForm: {},
        showModal: false,
        modalParams: {},
        nowAttachment: {},
        dataCount: 0,
        pageSize: 10,
        columnsShow: [],
        dataColumns: [
          { title: '模块名称', key: 'moduleName', sortable: true, show: true },
          { title: '业务名称', key: 'busName', sortable: true, show: true },
          { title: '原文件名', key: 'oldName', sortable: true, show: true },
          { title: '新文件名', key: 'newName', sortable: true, show: true },
          { title: '文件类型', key: 'fileType', sortable: true, show: true },
          {
            title: '储存类型',
            key: 'saveType',
            render: (h, params) => h('span', params.row.saveType === 'distributed' ? '分布式' : '本地'),
            sortable: true,
            show: true
          },
          { title: '后缀', key: 'suffix', sortable: true, show: true },
          { title: '文件大小', key: 'fileSize', sortable: true, show: true },
          /*{ title: '组别', key: 'groupName', sortable: true, show: true },
          { title: '路径', key: 'path', sortable: true, show: true },
          { title: '全路径', key: 'fullPath', sortable: true, show: true },*/
          {
            title: '能否下载', key: 'canDown',
            render: (h, params) => h('span', params.row.canDown === 1 ? '是' : '否'), sortable: true, show: true
          },
          { title: '上传时间', key: 'uploadTime', sortable: true, show: true },
          { title: '来源地址', key: 'sourceAddress', sortable: true, show: true },
          /* { title: '状态  1--正常 0--无效', key: 'state', sortable: true, show: true },*/
          { title: '备注', key: 'remark', sortable: true, show: true },
          /*{ title: '是否删除 0-否 1-是', key: 'deleteTag', sortable: true, show: true },
          { title: '创建人', key: 'createBy', sortable: true, show: true },
          { title: '创建人姓名', key: 'createByName', sortable: true, show: true },
          { title: '创建时间', key: 'createTime', sortable: true, show: true },
          { title: '修改人', key: 'modifyBy', sortable: true, show: true },
          { title: '修改人姓名', key: 'modifyByName', sortable: true, show: true },
          { title: '修改时间', key: 'modifyTime', sortable: true, show: true },*/
          { title: '操作', key: 'action', width: 200, align: 'center', fixed: 'right', slot: 'actions', show: true }
        ],
        tableData: [],
        selectedData: [],
        loading: false,
        sortParams: [], //[{title: 姓名, orderStr: 降序, sortKey: name, order: -}]
        getList: getAttachmentList,
        commonList: [
          { id: 1, label: '本地存储', findItem: [{ field: 'saveType', judge: 'eq', value: 'local' }] },
          { id: 2, label: '分布式存储', findItem: [{ field: 'saveType', judge: 'eq', value: 'distributed' }] },
          /*{id: 2, label: '常用固化查询条件2', findItem: [
                  {field: 'fieldName1', judge: 'eq', value: 'fieldValue2'},
                  {field: 'fieldName2', judge: 'eq', value: 'fieldValue2'},
                  ]},*/

        ],//固化常用列表
        fieldList: [//用户可以自行选择查询的字段列表及配置
          /*{
            label: '姓名',
            field: 'name',
            flags: ['=', 'like', 'isNull', 'isNotNull'],
            fieldType: 'input'
          },s
          {
            label: '身份证',
            field: 'idCard',
            flags: ['=', 'like', 'isNull', 'isNotNull'],
            fieldType: 'input'
          },*/
        ]
      }
    },
    methods: {
      toPreview (row) {
        jumpPrintPageFdfs(row.id);
      },
      downFile (e, row) {
        e.stopPropagation()
        downFileFdfs(row.id);
      },
      showUpload () {
        this.uploadAttachment = true
      },
      attachmentView (row) {
        this.showModal = true
        this.modalParams = { title: '查看', flag: 'view', bean: row }
      },
      attachmentAdd () {
        this.showModal = true
        this.modalParams = { title: '添加', flag: 'add', bean: null }
      },
      attachmentEdit (e, row) {
        e.stopPropagation()
        this.showModal = true
        this.modalParams = { title: '修改', flag: 'edit', bean: row }
      },
      attachmentDelete (row) {
        deleteAttachment(row.id).then(data => {
          this.$Message.success('操作成功！')
          this.tableData.splice(row._index, 1)
        }).catch(err => {
          this.$Message.error('操作失败！')
          console.log(err)
        })
      },
      selectedChange (selection) {
        this.selectedData = selection
      },
      sortChange (column) {
        this.$refs.customSort.sortChange(column)
      },
      exportData (params) {
        this.$refs.tables.exportCsv(params)
      },
      reloadData (pageSize, pageNo) {
        //表格加载中效果
        this.loading = true
        //处理表单查询参数及分页
        if (!pageNo) {
          this.dataCount = 0
        }
        if (!pageSize) {
          pageSize = this.pageSize
        }
        let page_params = { 'pageSize': pageSize, 'pageNo': pageNo }
        let params = Object.assign({}, page_params, this.searchForm) // 这样不会改变原对象

        getAttachmentPage(params).then(res => {
          if (res.data.result.totalSize) {
            this.dataCount = parseInt(res.data.result.totalSize)
          }
          if (res.data.result.datas && res.data.result.datas.length > 0) {
            this.tableData = res.data.result.datas
            this.tableData[0]._highlight = true
            this.nowAttachment = this.tableData[0]
          } else {
            this.tableData = []
          }
          this.loading = false
        }).catch(err => {
          this.tableData = []
          this.$Message.error('查询失败！')
          console.log(err)
        })
      },
      onCurrentChange (currentRow, oldCurrentRow) {
        this.nowAttachment = currentRow
      },
    },
    mounted () {
      this.reloadData()
      $this = this
    }
  }
</script>

<style>

</style>
