<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" :icon-msg="iconMsg" :getPage="getPage">
          <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>
    </Modal>
    <IndexManageEdit ref="indexEdit" @on-result-change="_page"></IndexManageEdit>
  </div>
</template>

<script>
import { soilTest } from '../../../api'
import IndexManageEdit from './IndexManageEdit'

export default {
  components: { IndexManageEdit },
  data() {
    return {
      id: '',
      modalTitle: '',
      showModal: false,
      btn: [],
      tableHeight: document.documentElement.clientHeight - 180,
      pageColumns: [
        { title: '仪器名称', key: 'equipName', width: 120 },
        { title: '仪器编号', key: 'equipNum', width: 120 },
        { title: '仪器品牌', key: 'brand', width: 120 },
        { title: '采集类型', key: 'collectionType', width: 120 },
        { title: '采集地址', key: 'collectionAddress', width: 120 },
        { title: '采集命令', key: 'acquisitionCommand', width: 120 }
      ],
      getPage: {},
      iconMsg: [{ type: 'md-trash', 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.equipPage(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._deleteByIds(data.id)
          break
      }
    },
    _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(id) {
      this.$Modal.confirm({
        title: '提示',
        content: '确定删除这条记录？',
        onOk: () => {
          this._delOk(id)
        }
      })
    },
    _delOk: async function(id) {
      const result = await soilTest.deleteEquip(id)
      if (result) {
        this.$Message.success('删除成功')
        this._page()
      }
    }
  }
}
</script>
