<template>
  <div>
    <div>
      <!--内容-->
      <Row>
        <!--查询-->
        <Col span="24" style="margin-bottom: -10px">
        <Form :label-width="70" inline onsubmit="return false">
          <label class="label-sign"></label>
          <Form-item label="文件名称:">
            <Input v-model="fileName" @on-enter="_pageChange(1)" placeholder="请输入文件名称" style="width:200px" clearable></Input>
          </Form-item>
          <Form-item class="search-btn">
            <Button @click="_search" type="primary">搜索</Button>
          </Form-item>
        </Form>
        </Col>
        <Col span="24">
        <FilesListView ref="fileModal" @on-result-change="_fileData"></FilesListView>
        </Col>
      </Row>
    </div>
  </div>
</template>
<script>
import global from '../../../api/config'
import FilesListView from './FilesListView'

export default {
  components: {
    FilesListView
  },
  data() {
    return {
      selectIds: [],
      getPage: {},
      pageParams: {
        page: 1,
        rows: 20
      },
      downloadFileUrl: '',
      fileName: '',
      // id
      salesId: '',
      contractId: '',
      sampleId: '',
      subcontractorId: '',
      // 用于区分是哪种附件（合同、委托、样品）
      idKey: ''
    }
  },
  methods: {
    _open(data, key) {
      this.fileName = ''
      switch (key) {
        case 'contractId':
          this.salesId = ''
          this.contractId = data.id
          this.sampleId = ''
          this.subcontractorId = ''
          this.idKey = 'contractId'
          break
      }
      this.$emit('on-result-change')
      this.downloadFileUrl = data.downloadFileUrl
      this.$refs.fileModal._fileViewData(data, this.idKey)
    },
    _openFile(pageData) {
      this.getPage = pageData
      this.$refs.fileModal._open(pageData, '300')
    },
    _page(uri) {
      this.$store.dispatch(uri, this._searchParams()).then(() => {
        this.$emit('on-result-change', 'file-page', '')
      })
    },
    _pageChange(page) {
      this.pageParams.page = page
      this.$emit('on-result-change')
    },
    _pageSizeChange(rows) {
      this.pageParams.rows = rows
      this.$emit('on-result-change')
    },
    _search() {
      this.$emit('on-result-change')
    },
    _searchParams() {
      const data = {
        page: this.pageParams.page,
        rows: this.pageParams.rows
      }
      if (this.fileName) {
        Object.assign(data, { fileName: this.fileName })
      }
      // 委托id
      if (this.contractId) {
        Object.assign(data, { contractId: this.contractId })
      }
      return data
    },
    // 下载
    _download(id) {
      window.open(global.baseURL + this.downloadFileUrl + id, '_blank')
    },
    _fileData(msg, data) {
      switch (msg) {
        case 'down':
          this._download(data)
          break
        case 'page':
          this._pageChange(data)
          break
        case 'rows':
          this._pageSizeChange(data)
          break
      }
    }
  }
}
</script>
