<template>
  <div>
    <Modal v-model="showEquipModal" v-drag width="900" class="modal-footer-none zIndex-1100">
      <p slot="header">
        选择仪器设备
      </p>
      <div>
        <Row>
          <!--查询-->
          <Form id="equip-form" inline onsubmit="return false" :label-width="70">
            <label class="label-sign" />
            <Form-item label="仪器名称:">
              <Input
                name="name"
                placeholder="请输入仪器名称"
                style="width: 200px"
                clearable
                @on-enter="_search"
              />
            </Form-item>
            <Form-item class="search-btn">
              <Button type="primary" @click="_search">
                搜索
              </Button>
            </Form-item>
          </Form>
          <!-- 表格 -->
          <Col span="24">
          <element-table
            ref="pageTable"
            :table-height="tableHeight"
            :get-page="getPage"
            show-check-box
            select-data
            @on-result-change="_tableResultChange"
          >
            <el-table-column
              v-for="item in pageColumns"
              :key="item.key"
              show-overflow-tooltip
              :prop="item.key"
              :align="item.align"
              :label="item.title"
              :min-width="item.width"
              :fixed="item.fixed?item.fixed:undefined"
            >
              <template slot-scope="scope">
                <span>{{ scope.row[item.key] }}</span>
              </template>
            </el-table-column>
          </element-table>
          </Col>
        </Row>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </Modal>
  </div>
</template>
<script>
/**
 * 操作日志-委托
 */
import { equipInfo, drugItem } from '../../../../api'

export default {
  components: {},
  data() {
    return {
      showEquipModal: false,
      getPage: {},
      pageColumns: [
        { title: '仪器编号', key: 'labNum', align: 'center', width: 150 },
        { title: '仪器名称', key: 'name', align: 'center', width: 150 },
        { title: '规格型号', key: 'spec', align: 'center', width: 150 },
        { title: '采集类型', key: 'acquisitionType', align: 'center' },
        { title: '采集地址', key: 'acquisitionAddress', align: 'center' },
        { title: '仪器IP', key: 'ipAddress', align: 'center' },
        { title: '仪器品牌', key: 'brand', align: 'center' }
      ],
      footerList: [
        { id: '', type: '', name: '关闭' },
        { id: '', type: 'primary', name: '确认' }
      ],
      itemIds: [],
      selectData: []
    }
  },
  computed: {
    tableHeight: function() {
      return this.$tableHeight('tableModal')
    }
  },
  methods: {
    _open(ids) {
      this.showEquipModal = true
      this.itemIds = ids
      this._hideLoading()
      this.$refs.pageTable.templateRadio = ''
      this._clearPage()
      this._page()
    },
    _clearPage() {
      $('#equip-location-form')
        .find('input')
        .each(function() {
          $(this).val('')
        })
    },
    _page: async function() {
      const data = this.$serialize('equip-location-form')
      Object.assign(data, this.formObj)
      Object.assign(data, this.$refs.pageTable._searchParams())
      const result = await equipInfo.page(data)
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _searchParams(formId) {
      return this.$refs.pageTable._searchParams(formId)
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'selectData':
          this.selectData = data
          break
        default:
          this._page()
      }
    },
    _footerResult(msg) {
      switch (msg) {
        case '关闭':
          this.showEquipModal = false
          break
        case '确认':
          this._ok()
          break
      }
    },
    _ok() {
      const data = this.selectData
      if (data.length === 0) {
        this.$Message.warning('请先选择一条数据!')
        this.$refs.footerModal._hideLoading()
      } else {
        const equipData = []
        for (let i = 0; i < data.length; i++) {
          equipData.push({
            brand: data[i].brand,
            equipId: data[i].id,
            equipName: data[i].name,
            labNum: data[i].name,
            equipNum: data[i].labNum,
            acquisitionCommand: data[i].acquisitionCommand,
            spec: data[i].spec,
            collectionType: data[i].acquisitionType,
            ipAddress: data[i].ipAddress,
            collectionAddress: data[i].acquisitionAddress
          })
        }
        const equipDTO = {
          equipList: equipData,
          itemIds: this.itemIds
        }
        this._save(equipDTO)
      }
    },
    async _save(equipData) {
      console.log(equipData)
      const result = await drugItem.bathAddEquip(equipData)
      if (result) {
        this.$emit('on-result-change')
        this.$Message.success('选择仪器成功!')
        this.showEquipModal = false
      } else {
        this._hideLoading()
      }
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _search() {
      this.$refs.pageTable._pageChange(1)
    }
  }
}
</script>
