<template>
  <div>
    <Modal v-model="showModal" v-drag width="800" class="modal-footer-none full-screen">
      <div slot="header">{{modalTitle}}</div>
      <div>
        <Form id="index-right-form" :label-width="80" inline onsubmit="return false">
          <label class="label-sign"></label>
          <Form-item class="search-item" label="仪器名称:">
            <Input v-model="formObj.equipName" @on-enter="_formSearch" placeholder="请输入仪器名称" clearable/>
          </Form-item>
          <Form-item class="search-btn">
            <Button @click="_formSearch" type="primary">搜索</Button>
          </Form-item>
        </Form>
          <btn-list :msg="btn" @on-result-change="_btnClick" class="contHide" style="margin-bottom: 10px;"/>
        <PTVXETable ref="pageTable" :tableHeight="tableHeight"
                    @on-result-change="_tableResultChange" :getPage="getPage" :icon-msg="iconMsg">
          <vxe-table-column
            :field="item.key"
            :title="item.title"
            :min-width="item.width?item.width:200"
            :fixed="item.fixed?item.fixed:undefined"
            v-for="item in pageColumns" :key="item.key">
            <template slot-scope="scope">
              <span v-if="item.key==='defaulted'">{{scope.row[item.key]?'是':'否'}}</span>
              <span v-else>{{scope.row[item.key]}}</span>
            </template>
          </vxe-table-column>
        </PTVXETable>
      </div>
      <div class="viewFileImg">
        <vue-gallery-slideshow  :images="imgViewList" :index="imgViewIndex"
                               @close="imgViewIndex = null"></vue-gallery-slideshow>
      </div>
    </Modal>
    <IndexManageEdit ref="indexEdit" @on-result-change="_page"></IndexManageEdit>
  </div>
</template>

<script>
import { soilAptitude, soilTest } from '../../../api'
import global from '../../../api/config'
import IndexManageEdit from './IndexManageEdit'

export default {
  components: { IndexManageEdit },
  data() {
    return {
      id: '',
      modalTitle: '',
      showModal: false,
      btn: [{ type: 'success', id: 'ZBC', name: '添加指标' }],
      tableHeight: document.documentElement.clientHeight - 180,
      imgViewList: [],
      imgViewIndex: null,
      viewUri: '/soil/v1/sample_photo/view',
      pageColumns: [
        { title: '仪器名称', key: 'equipName', width: 120 },
        { title: '仪器编号', key: 'equipNum', width: 120 },
        { title: '规格型号', key: 'spec', width: 120 },
        { title: '仪器品牌', key: 'brand', width: 120 },
        { title: '实验室编号', key: 'labNum', width: 120 }
      ],
      getPage: {},
      iconMsg: [{ type: 'md-eye', id: '', name: '预览' }],
      selectIds: [],
      formObj: {
        name: ''
      },
      // 资质信息
      aptitudeItemInfo: {}
    }
  },
  methods: {
    _open(data) {
      console.log(data)
      this.aptitudeItemInfo = data
      this.formObj = this.$resetFields(this.formObj)
      this.id = data.id
      this.modalTitle = data.name + ' 查看采集数据'
      this.showModal = true
      this._page()
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      this.formObj.expId = this.id
      const result = await soilTest.collectFilePage(
        this.$serializeForm(this.formObj)
      )
      if (result) {
        console.log(result)
        this.getPage = result
        this.$refs.pageTable._hideLoading()
      }
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          break
        case 'selectIds':
          this.selectIds = data
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
        case 'changeSize':
          this._page()
          break
      }
    },
    _iconClick(res, data) {
      switch (res) {
        case '编辑':
          this._editModal(true, data)
          break
        case '预览':
          this._viewFile(data)
          break
      }
    },
    _viewFile(data) {
      // this.$pdfView(data.objectKey)
      const imgViewList = []
      // imgViewList.push(global.baseURL + this.downloadFileUrlFile + data.id)
      this.imgViewList = imgViewList
      imgViewList.push(
        global.baseURL +
          this.viewUri +
          '?id=' +
          data.id +
          '&objectKey=' +
          data.objectKey
      )
      console.log(
        global.baseURL +
          this.viewUri +
          '?id=' +
          data.id +
          '&objectKey=' +
          data.objectKey
      )
      this.imgViewIndex = 0
    },
    _editModal(edit, data) {
      if (edit) {
        this._getById(data)
      } else {
        this.$refs.indexEdit._open(this.id, '')
      }
    },
    _getById: async function(data) {
      const result = await soilTest.itemGetById(data.id)
      console.log(result)
      if (result) {
        this.$refs.indexEdit._open('', result)
      }
    },
    _btnClick(msg) {
      switch (msg) {
        case '添加指标':
          this._editModal(false)
          break
        case '删除':
          this._deleteByIds()
          break
      }
    },
    _deleteByIds() {
      if (this.selectIds.length === 0) {
        // this.$msgTip('warning')
        this.$message.warning('请至少选择一条记录')
      } else {
        this.$Modal.confirm({
          title: '提示',
          content: '确定删除 ' + this.selectIds.length + ' 条记录？',
          onOk: () => {
            this._delOk(this.selectIds)
          }
        })
      }
    },
    _delOk: async function(ids) {
      const result = await soilAptitude.expItemDeleteByIds(ids)
      if (result) {
        this.$message.success('删除成功')
        this._page()
      }
    }
  }
}
</script>
<style scoped>
.viewFileImg >>> .vgs__container {
  height: 100% !important;
}
</style>
