<template>
  <div>
    <Modal v-model="showUploadModal" :width="800" class="modal-footer-none zIndex-1200">
      <p slot="header">{{modalTitle}}</p>
      <div>
        <!--内容-->
        <Row>
          <!--查询-->
          <Col span="24" style="margin-bottom: -10px">
          <Form id="search-form-upload" :label-width="70" inline onsubmit="return false">
            <label class="label-sign"></label>
            <Form-item label="文件名称:" class="search-item">
              <Input v-model="fileName" @on-enter="_pageChange(1)" name="fileName" placeholder="请输入文件名称" clearable/>
              <input name="contractId" type="hidden">
              <input name="originalId" type="hidden">
              <input name="salesId" type="hidden">
            </Form-item>
            <Form-item class="search-btn">
              <Button @click="_search" type="primary">搜索</Button>
            </Form-item>
          </Form>
          </Col>
          <!--列表-->
          <Col span="24">
         <FilesList ref="fileModal" @on-result-change="_fileData"></FilesList>
          </Col>
        </Row>
      </div>
    </Modal>

  </div>
</template>
<script>
import global from '../../../api/config'
import FilesList from './FilesList'
import UploadFile from './UploadFile'

export default {
  components: {
    FilesList,
    UploadFile
  },
  data() {
    return {
      showUploadModal: false,
      modalTitle: '附件',
      fileName: '',
      selectIds: [],
      uri: '',
      getPage: {},
      pageParams: {
        rows: 20
      },
      downloadFileUrl: ''
    }
  },
  methods: {
    _open(data, key, title) {
      console.log('data', data)
      this.fileName = ''
      this.showUploadModal = true
      this.downloadFileUrl = data.downloadFileUrl
      this.modalTitle = title
      this.url = data.pageUrl
      $('input[name=' + key + ']').val(data.id)
      this._page()
      this.$emit('on-result-change')
      this.$refs.fileModal._setUploadData(data)
    },
    _openFile(pageData) {
      this.getPage = pageData
      this.$refs.fileModal._open(pageData, '300')
    },

    _delPage(url, data) {
      this.$store.dispatch(url, data).then(() => {
        this.$emit('on-result-change', 'success', '')
      })
    },
    _page() {
      this.$store
        .dispatch('FoodOriginalDataFile/page', 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 = this.$serialize('search-form-upload')
      return this.$extend(data, this.pageParams)
    },
    _deleteById(id) {
      // 删除一条记录
      this.$emit('on-result-change', 'delete-page', id)
    },
    // 下载
    _download(id) {
      this.$Modal.confirm({
        title: '提示',
        content: '确定要下载？',
        onOk: () => {
          window.open(global.baseURL + this.downloadFileUrl + id, '_blank')
        }
      })
    },
    _fileData(msg, data) {
      switch (msg) {
        case 'delete':
          this._deleteById(data)
          break
        case 'down':
          this._download(data)
          break
        case 'page':
          this._pageChange(data)
          break
        case 'rows':
          this._pageSizeChange(data)
          break
        case 'refresh':
          this._search()
          break
      }
    }
  }
}
</script>
