<template>
  <div>
    <Modal v-model="showEditModal" :mask-closable="false" :width="1000">
      <p slot="header">
        选择仪器
      </p>
      <!--内容-->
      <div>
        <Row>
          <Col span="24" />
          <Form id="equipInfo-search-form" inline onsubmit="return false" :label-width="90">
            <Form-item class="width-23" label="仪器名称:" style="margin-bottom: 0">
              <Input name="name" placeholder="请输入仪器名称" @on-enter="_search" /></Input>
            </Form-item>
            <Form-item class="marleft-90">
              <Button type="primary" @click="_search">
                搜索
              </Button>
            </Form-item>
          </Form>
          </Col>
          <!-- 表格 -->
          <Col span="24" style="margin-bottom: 10px" />
          <element-table
            ref="pageTable"
            :page-columns="pageColumns"
            :table-height="tableHeight"
            :get-page="getPage"
            :opt-col-width="80"
            @on-result-change="_tableResultChange"
          >
            <el-table-column
              v-for="item in pageColumns"
              :key="item.key"
              show-overflow-tooltip
              sortable
              :prop="item.key"
              :label="item.title"
              :min-width="item.width"
              :fixed="item.fixed?item.fixed:undefined"
            >
              <template slot-scope="scope">
                <div v-if="item.detail">
                  <a @click.stop="_detailModal(scope.row.id)">{{ scope.row[item.key] }}</a>
                </div>
                <div v-else-if="item.date">
                  {{ scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd'):'' }}
                </div>
                <div v-else-if="item.status">
                  <span v-if="scope.row[item.key] ===0" style="color: #F8BB2C;">闲置</span>
                  <span v-else-if="scope.row[item.key] ===1" style="color: #00a0e9;">在用</span>
                  <span v-else-if="scope.row[item.key] ===2" style="color: #6FBA2C;">保养中</span>
                  <span v-else-if="scope.row[item.key] ===3" style="color: gray;">维修中</span>
                  <span v-else-if="scope.row[item.key] ===4" style="color: red;">已报废</span>
                </div>
                <div v-else>
                  {{ scope.row[item.key] }}
                </div>
              </template>
            </el-table-column>
          </element-table>
          </Col>
        </Row>
      </div>
      <div slot="footer">
        <Button type="ghost" style="margin-left: 8px" @click="_cancel">
          取消
        </Button>
        <Button type="primary" @click="_ok">
          提交
        </Button>
      </div>
    </Modal>
  </div>
</template>
<script>
export default {
  data() {
    return {
      keyword: '',
      showEditModal: false,
      currentRowObj: {},
      getPage: {},
      tableHeight: 300,
      pageParams: {
        rows: 20
      },
      pageColumns: [
        { title: '实验室编号', key: 'labNum', sortable: true, width: 160 },
        {
          title: '仪器状态',
          key: 'status',
          width: 100,
          status: true
        },
        {
          title: '供应商名称',
          key: 'supplierName',
          sortable: true,
          width: 160
        },
        { title: '仪器类别', key: 'className', sortable: true, width: 160 },
        { title: '仪器名称', key: 'name', sortable: true, width: 160 },
        { title: '规格型号', key: 'spec', sortable: true, width: 160 },
        { title: '仪器品牌', key: 'brand', sortable: true, width: 160 },
        { title: '出厂编号', key: 'equipNum', sortable: true, width: 160 },
        { title: '证书编号', key: 'ceNum', sortable: true, width: 160 },
        {
          title: '使用科别',
          key: 'departmentName',
          sortable: true,
          width: 160
        },
        { title: '使用地点', key: 'locationName', sortable: true, width: 160 },
        { title: '检定周期（月）', key: 'period', sortable: true, width: 160 },
        {
          title: '购买日期',
          key: 'buyDate',
          width: 160,
          date: true
        },
        {
          title: '有效日期',
          key: 'effectDate',
          width: 160,
          date: true
        }
      ]
    }
  },
  methods: {
    _open() {
      this.showEditModal = true
      this._clearPage()
      this._page()
      this._clearCurrentRow() // 清除选中的仪器
    },
    _clearPage() {
      $('#equipInfo-search-form')
        .find('input')
        .each(function() {
          $(this).val('')
        })
    },
    _page() {
      // this.$refs.pageTable._page('equipInfo-search-form', 'LmsEquipInfo/page')
    },
    _search() {
      this.$refs.pageTable._pageChange(1)
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          break
        case 'select':
          this.currentRowObj = data
          break
        case 'dbSelect':
          this._dbRowChange(data)
          break
        default:
          this._page()
      }
    },
    _cancel() {
      this.showEditModal = false
    },
    _ok() {
      this.$emit('on-result-change', this.currentRowObj)
      this.showEditModal = false
    },
    _clearCurrentRow() {
      $('input[name="name"]').val('')
    },
    _dbRowChange(data, index) {
      // 双击
      this.currentRowObj = data
      this._ok()
    }
  }
}
</script>
