<template>
  <div>
      <!--内容-->
    <Modal v-model="showModal" title="预警设置" width="1000" class="modal-footer-none">
      <div>
        <Row>
          <!--查询-->
          <Col span="24">
            <Form id="search-form-edit" :label-width="50" inline onsubmit="return false">
              <label class="label-sign"></label>
              <Form-item label="名称:">
                <Input @on-enter="_formSearch" v-model="formObj.name" placeholder="请输入名称" style="width:200px"
                       clearable></Input>
              </Form-item>
              <Form-item style="margin-left: -50px">
                <Button @click="_formSearch" type="primary">搜索</Button>
              </Form-item>
            </Form>
          </Col>
          <!--操作-->
          <Col span="24">
            <btn-list :msg="btn" @on-result-change="_btnClick" class="contHide"></btn-list>
          </Col>
          <!-- 表格 -->
          <Col span="24">
            <PTVXETable ref="pageTable" :tableHeight="400"
                        @on-result-change="_tableResultChange" :getPage="getPage" :iconMsg="iconMsg" :optColWidth="100">
              <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"
                sortable>
                <template slot-scope="scope">
                  <div>{{scope.row[item.key]}}</div>
                </template>
              </vxe-table-column>
            </PTVXETable>
          </Col>
        </Row>
      </div>
    </Modal>
    <earlySetEdit ref="setModal" @on-result-change="_page"></earlySetEdit>
    <!-- 未关联耗材 -->
  </div>
</template>
<script>
import { soilAptitude } from '../../../api'
import earlySetEdit from './earlySetEdit'
export default {
  components: { earlySetEdit },
  data() {
    return {
      btn: [
        { type: 'success', id: 'early-warning-set-add', name: '添加' },
        { type: 'error', id: 'early-warning-set-deletes', name: '移除' }
      ],
      supplierId: '',
      selectIds: [],
      relChemicalPage: {},
      pageColumns: [
        { title: '科室', key: 'groupName', ellipsis: true },
        { title: '预警项目', key: 'warningType', ellipsis: true },
        { title: '预警上限', key: 'upperLimit', ellipsis: true },
        { title: '预警下限', key: 'lowerLimit', ellipsis: true },
        { title: '手机号', key: 'tel', ellipsis: true },
        { title: '邮箱', key: 'email', ellipsis: true },
        { title: '备注', key: 'remark', ellipsis: true }
      ],
      iconMsg: [
        { type: 'md-create', id: 'early-warning-set-edit', name: '编辑' },
        {
          type: 'md-remove-circle',
          id: 'early-warning-set-delete',
          name: '删除'
        }
      ],
      showModal: false,
      relSupplierData: {
        // 保存要提交的信息
        chemicalId: '',
        supplierId: '',
        price: ''
      },
      getPage: {},
      formObj: {
        name: '',
        supplierId: ''
      }
    }
  },
  methods: {
    _iconClick(res, data) {
      switch (res) {
        case '编辑':
          this._openSet(true, data.id)
          break
        case '删除':
          this._deleteById(data.id)
          break
      }
    },
    _open(id) {
      this.showModal = true
      this.formObj = this.$resetFields(this.formObj)
      this._page()
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await soilAptitude.envMonitorWarningPage(
        this.$serializeForm(this.formObj)
      )
      if (result) {
        this.$refs.pageTable._hideLoading()
        this.getPage = result
      }
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _deleteByIds(ids, content) {
      this.$Modal.confirm({
        title: '提示',
        content: content || '确定删除该记录？',
        onOk: () => {
          this._delete(ids.join(','))
        }
      })
    },
    _delete: async function(ids) {
      const result = await soilAptitude.deleteEnvMonitorWarning(ids)
      if (result) {
        this._formSearch()
        this.$Message.success('删除成功！')
      }
    },
    _deleteById(id) {
      // 删除一条记录
      this._deleteByIds([id])
    },
    _deleteSelected() {
      // 批量删除
      const ids = this.selectIds
      if (ids.length === 0) {
        this.$Message.warning('请选择一条或多条数据！')
      } else {
        this._deleteByIds(ids, '确定移除 ' + ids.length + ' 条记录？')
      }
    },
    _search() {
      this._page()
    },
    _btnClick(msg) {
      switch (msg) {
        case '添加':
          this._openSet(false)
          break
        case '移除':
          this._deleteSelected()
          break
      }
    },
    _openSet(data, id) {
      if (data) {
        this._getInfoById(id)
      } else {
        this.$refs.setModal._open()
      }
    },
    _getInfoById: async function(id) {
      const result = await soilAptitude.infoEnvMonitorWarning({ id: id })
      console.log(result)
      if (result) {
        this.$refs.setModal._open(result)
      }
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'selectIds':
          this.selectIds = data
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
        case 'changeSize':
          this._page()
          break
      }
    }
  }
}
</script>
