<template>
  <div>
    <Modal v-model="showUploadModal" v-drag :width="800" class="modal-footer-none" :styles="{marginTop:'-80px'}">
      <p slot="header">
        {{ modalTitle }}
      </p>
      <div>
        <!--内容-->
        <Row>
          <!--查询-->
          <Col span="24" style="margin-bottom: -10px">
          <Form inline onsubmit="return false" :label-width="70">
            <label class="label-sign" />
            <Form-item label="文件名称:">
              <Input
                v-model="fileName"
                placeholder="请输入文件名称"
                style="width:200px"
                clearable
                @on-enter="_pageChange(1)"
              />
            </Form-item>
            <Form-item class="search-btn">
              <Button type="primary" @click="_search">
                搜索
              </Button>
            </Form-item>
          </Form>
          </Col>
          <!--列表-->
          <Col span="24">
          <FilesList ref="fileModal" @on-result-change="_fileData" />
          </Col>
        </Row>
      </div>
    </Modal>
    <!--删除提示框-->
    <modalConfirm
      ref="confirmModal"
      :content="modalContent"
      :btn-modal-list="btnModalList"
      @on-result-change="_modalResult"
    />
  </div>
</template>
<script>
import global from '../../api/config'
import { drugFileManage } from '../../api'
import FilesList from './FilesList'
import modalConfirm from './modalConfirm'

export default {
  components: {
    FilesList,
    modalConfirm
  },
  props: {
    // 只能删除自己的标识
    flag: null
  },
  data() {
    return {
      showUploadModal: false,
      modalTitle: '附件',
      fileName: '',
      selectIds: [],
      getPage: {},
      pageParams: {
        page: 1,
        rows: 20
      },
      downloadFileUrl: '',
      // id
      salesId: '',
      contractId: '',
      sampleId: '',
      partId: '',
      subcontractorId: '',
      // 用于区分是哪种附件（合同、委托、样品）
      idKey: '',
      // 弹框的内容
      modalContent: '',
      btnModalList: [{ name: '取消' }, { type: 'primary', name: '确定' }],
      //
      deleteId: []
    }
  },
  methods: {
    // 删除提示框返回
    _modalResult(msg) {
      if (msg === '确定') {
        // 删除一条记录
        if (this.flag === undefined) {
          // 只能删除自己上传的
          this.$emit('on-result-change', 'delete-page', this.deleteId)
        } else {
          // 可以删除全部
          this.$emit('on-result-change', 'delete-page', this.deleteId)
        }
      }
    },
    _open(data, key) {
      this.deleteId = []
      this.fileName = ''
      this.showUploadModal = true
      this.downloadFileUrl = data.downloadFileUrl
      switch (key) {
        case 'contractId':
          this.salesId = ''
          this.contractId = data.id
          this.sampleId = ''
          this.subcontractorId = ''
          this.idKey = 'contractId'
          break
        case 'sampleId':
          this.salesId = ''
          this.contractId = ''
          this.sampleId = data.id
          this.subcontractorId = ''
          this.idKey = 'sampleId'
          break
        case 'subcontractorId':
          this.salesId = ''
          this.contractId = ''
          this.sampleId = ''
          this.subcontractorId = data.id
          this.idKey = 'subcontractorId'
          break
        case 'partId':
          this.partId = data.id
          this.salesId = ''
          this.contractId = ''
          this.sampleId = ''
          this.subcontractorId = ''
          this.idKey = 'partId'
          break
      }
      this.$emit('on-result-change')
      this.$refs.fileModal._setUploadData(data, this.idKey)
    },
    _openFile(pageData) {
      this.getPage = pageData
      this.$refs.fileModal._open(pageData, '300')
    },
    _page: async function() {
      const result = await drugFileManage.pageSampleAnnex(this._searchParams())
      if (result) {
        this.$emit('on-result-change', 'file-page', result)
      }
    },
    _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 })
      }
      // 零部件id
      if (this.partId) {
        Object.assign(data, { partId: this.partId })
      }
      // 样品id
      if (this.sampleId) {
        Object.assign(data, { sampleId: this.sampleId })
      }
      if (this.subcontractorId) {
        Object.assign(data, { subcontractorId: this.subcontractorId })
      }
      return data
    },
    _deleteById(ids) {
      this.deleteId = ids
      this.$Modal.confirm({
        title: '提示',
        content: '确定要删除这 ' + ids.length + ' 个附件?',
        onOk: () => {
          if (this.flag === undefined) {
            // 只能删除自己上传的
            this.$emit('on-result-change', 'delete-page', this.deleteId)
          } else {
            // 可以删除全部
            this.$emit('on-result-change', 'delete-page', this.deleteId)
          }
        }
      })
    },
    // 删除附件成功
    _delPage: async function(uri, data) {
      const result = await drugFileManage.deleteById(data)
      if (result) {
        this.$emit('on-result-change', 'success', '')
        this.$refs.confirmModal._closeModal()
        this.deleteId = []
      }
    },
    // 下载
    _download(id) {
      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>
