<template>
  <div>
    <Row>
      <!--查询-->
      <Col span="24" />
      <Form
        v-show="searchOpen"
        inline
        onsubmit="return false"
        :label-width="90"
        class="search-form"
      >
        <label class="label-sign" />
        <label class="label-sign" />
        <Form-item class="width-23" label="位置:">
          <Input v-model="formObj.location" name="location" placeholder="请输入位置" clearable @on-enter="_formSearch" /></Input>
          <input v-model="formObj.type" name="type" value="1" type="hidden">
        </Form-item>
        <Form-item label="日期:" style="margin-left: -20px;">
          <Date-picker
            v-model="formObj.startDate"
            name="startDate"
            type="date"
            placeholder="开始日期"
            format="yyyy-MM-dd"
            style="width:125px;"
          />
          <label class="line-color">——</label>
          <Date-picker
            v-model="formObj.endDate"
            name="endDate"
            type="date"
            placeholder="结束日期"
            format="yyyy-MM-dd"
            style="width:125px;"
          />
        </Form-item>
        <Form-item class="search-btn">
          <Button type="primary" @click="_formSearch">
            搜索
          </Button>
        </Form-item>
      </Form>
      </Col>
      <!--按钮-->
      <Col span="24" />
      <!--      <btn-list :msg="btn" :open="searchOpen" show-search-btn="true" @on-result-change="_btnClick" />-->
      <el-button type="success" style="margin-left: 5px;margin-bottom: 5px" @click="_add">
        添加
      </el-button>
      <el-button type="success" style="margin-left: 5px;margin-bottom: 5px" @click="_delete">
        删除
      </el-button>
      </Col>
      <!--table-->
      <Col span="24" />
      <element-table
        id="testBasisTable"
        ref="pageTable"
        :page-columns="pageColumns"
        :table-height="tableHeight"
        :get-page="getPage"
        :opt-col-width="140"
        :icon-msg="iconMsg"
        show-check-box
        @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"
          :width="item.width"
          :min-width="200"
          :fixed="item.fixed?item.fixed:undefined"
        >
          <template slot-scope="scope">
            <div v-if="item.dateTime">
              {{ scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd HH:MM:ss'):'' }}
            </div>
            <div v-else-if="item.date">
              {{ scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd'):'' }}
            </div>
            <div v-else>
              {{ scope.row[item.key] }}
            </div>
          </template>
        </el-table-column>
      </element-table>
      </Col>
    </Row>
    <!--添加编辑-->
    <LmsEnvNormalMonitorEdit ref="editModal" @on-result-change="_page" />

    <EnvItemRecord ref="recordModal" @on-result-change="_page" />
  </div>
</template>
<script>
import { envMonitor } from '../../../api'
import EnvItemRecord from '../lms-env-microbe-monitor/env-monitor-record/EnvItemRecord'
import LmsEnvNormalMonitorEdit from './LmsEnvNormalMonitorEdit.vue'

export default {
  components: { LmsEnvNormalMonitorEdit, EnvItemRecord },
  data() {
    return {
      searchOpen: true,
      formObj: {
        location: '',
        startDate: '',
        endDate: '',
        type: '2'
      },
      selectIds: [],
      btn: [
        { type: 'success', id: '', name: '添加' },
        { type: 'error', id: '', name: '删除' }
      ],
      pageColumns: [
        { title: '位置', key: 'location', width: 200, detail: true },
        { title: '相对湿度(%)', key: 'humidity', width: 150 },
        { title: '温度(℃)', key: 'temperature', width: 150 },
        { title: '监测日期', key: 'monitorDate', sortable: true, date: true },
        { title: '记录人', key: 'recorderName', width: 120 }
      ],
      getPage: {},
      iconMsg: [
        { type: 'pt-a-edit', id: '', name: '编辑' },
        { type: 'pt-a-clock', id: '', name: '操作日志' },
        { type: 'pt-a-trash', id: '', name: '删除' }
      ]
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('search')
      } else {
        return this.$tableHeight('noSearch')
      }
    }
  },
  mounted() {
    this._page()
  },
  methods: {
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      console.log(this.formObj)
      const result = await envMonitor.page(this.$serializeForm(this.formObj))
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _searchParams(formId) {
      return this.$refs.pageTable._searchParams(formId)
    },
    _btnClick(msg, data) {
      switch (msg) {
        case '添加':
          this.$refs.editModal._open()
          break
        case '删除':
          this._deleteByIds()
          break
        case '导出':
          if (this.getPage.records.length === 0) {
            this.$Message.warning('暂无数据，不可导出！')
          } else {
            this._export()
          }
          break
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
        case 'selectIds':
          this.selectIds = data
          break
      }
    },
    _iconClick(res, data) {
      switch (res) {
        case '删除':
          this._deleteById(data.id)
          break
        case '编辑':
          this._editModal(data.id)
          break
        case '操作日志':
          this._recordModal(data.id)
          break
      }
    },
    // 删除
    _deleteById(id) {
      this.$Modal.confirm({
        title: '提示',
        content: '确定删除该数据？',
        onOk: () => {
          this._deleteOk(id)
        }
      })
    },
    _deleteByIds() {
      // 批量删除
      const ids = this.selectIds
      if (ids.length === 0) {
        this.$Message.warning('请选择一条或多条数据！')
      } else {
        this.$Modal.confirm({
          title: '提示',
          content: '确定删除 ' + ids.length + ' 条记录？',
          onOk: () => {
            this._deleteOk(ids)
          }
        })
      }
    },
    _deleteOk: async function(id) {
      const result = await envMonitor.deleteById(id)
      if (result) {
        this.$Message.success('删除成功')
        this._page()
      }
    },

    _recordModal(id) {
      this.$refs.recordModal._open(id)
    },
    _delete() {
      if (this.selectIds.length === 0) {
        this.$Message.warning('请选择要删除的数据！')
        return false
      }
      this._deleteById(this.selectIds)
    },
    _editModal: async function(id) {
      const result = await envMonitor.getById(id)
      if (result) {
        this.$refs.editModal._open(result)
      }
    },
    _export: async function() {
      const result = await envMonitor.page(this._searchParamsTemp())
      // 导出全部数据
      if (!result) {
        return
      }
      this.$Modal.confirm({
        title: '提示',
        content: '确定导出全部数据（最多导出5000条）？',
        onOk: () => {
          this.$exportExcel(
            'testBasisTable',
            '检测依据',
            this.pageColumns,
            result.records
          )
        }
      })
    },
    // 参数
    _searchParamsTemp: function() {
      const data = this.$serializeForm(this.formObj)
      return this.$extend(data, { page: 1, rows: 5000 })
    },
    _add() {
      this.$refs.editModal._open()
    }
  }
}
</script>
