<template>
  <div>
    <!--内容-->
    <div class="layout-content-padding">
      <div class="layout-content-main">
        <Row>
          <!--查询-->
          <Col span="24" style="margin-top: 10px">
            <Form id="search-form" :label-width="80" inline onsubmit="return false">
              <label class="label-sign"></label>
              <Form-item class="width-23" label="位置:">
                <input name="type" type="hidden" value=0>
                <Input v-model="formObj.location" @on-enter="_formSearch" placeholder="请输入位置" clearable></Input>
              </Form-item>
              <Form-item label="日期:" style="margin-left: -20px;" class="width-23">
                <Date-picker @on-change="_dateChange" :editable="false" type="daterange" split-panels
                             style="width:100%;" placeholder="请选择监测日期"></Date-picker>
              </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="tableHeight"
                        @on-result-change="_tableResultChange" :getPage="getPage" :iconMsg="iconMsg" :optColWidth="120">
              <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 v-if="item.date">
                    {{scope.row.monitorFormat?(scope.row[item.key]?$dateformat(scope.row[item.key],scope.row.monitorFormat):'')
                    :(scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd'):'')}}
                  </div>
                  <div v-else>{{scope.row[item.key]}}</div>
                </template>
              </vxe-table-column>
            </PTVXETable>
          </Col>
        </Row>
      </div>
    </div>
    <!-- 添加、编辑 -->
    <LmsEnvNormalMonitorEdit ref="editModal" @on-result-change="_search"></LmsEnvNormalMonitorEdit>
    <!-- 查看详情 -->
    <!--导入-->
    <!--操作记录-->
    <earlyWarningSet ref="setModal"></earlyWarningSet>
  </div>
</template>
<script>
import { soilAptitude } from '../../../api'
import LmsEnvNormalMonitorEdit from './LmsEnvNormalMonitorEdit.vue'
import earlyWarningSet from './earlyWarningSet'

export default {
  components: {
    LmsEnvNormalMonitorEdit,
    earlyWarningSet
  },
  data() {
    return {
      btn: [
        { type: 'success', id: 'early-warning-add', name: '添加' },
        { type: 'error', id: 'early-warning-deletes', name: '删除' },
        { type: '', id: 'early-warning-set', name: '预警设置' }
      ],
      iconMsg: [
        { type: 'md-create', id: 'early-warning-edit', name: '编辑' },
        { type: 'md-trash', id: 'early-warning-delete', name: '删除' }
      ],
      selectIds: [],
      pageColumns: [
        { title: '位置', key: 'location', detail: true, width: 140 },
        { title: '相对湿度(%)', key: 'humidity', width: 120 },
        { title: '温度(℃)', key: 'temperature', width: 100 },
        { title: '设备编号', key: 'equipNum' },
        { title: '监测日期', key: 'monitorDate', date: true, width: 140 },
        { title: '记录人', key: 'recorderName', width: 120 }
      ],
      dataObj: {
        file: ''
      },
      getPage: {},
      formObj: {
        location: '',
        startDate: '',
        endDate: ''
      }
    }
  },
  mounted() {
    this._page()
  },
  methods: {
    _dateChange(data) {
      this.formObj.startDate = data[0]
      this.formObj.endDate = data[1]
    },
    _import() {
      this.$refs.importModal._open()
    },
    _iconClick(res, data) {
      switch (res) {
        case '编辑':
          this._editModal(true, data.id)
          break
        case '删除':
          this._deleteById(data.id)
          break
        case '操作日志':
          this._normalRecord(data.id)
          break
      }
    },
    _normalRecord(id) {
      this.$refs.recordModal._open(id)
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await soilAptitude.soilEnvMonitorWarning(
        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.envMonitorWarningDelete(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 + ' 条记录？')
      }
    },
    _detailModal(id) {},
    _editModal(edit, id) {
      if (edit) {
        // 编辑
        this._getInfoById(id)
      } else {
        // 添加
        this.$refs.editModal._open()
      }
    },
    _getInfoById: async function(id) {
      const result = await soilAptitude.envMonitorWarningInfo({ id: id })
      console.log(result)
      if (result) {
        this.$refs.editModal._open(result)
      }
    },
    _search() {
      this._page()
    },
    _export() {
      const ids = this.selectIds
      if (ids.length === 0) {
        this._exportAll()
      } else {
        this._exportByIds(ids, '确定导出 ' + ids.length + ' 条记录？')
      }
    },
    _exportAll() {
      this.$Modal.confirm({
        title: '提示',
        content: '确定要导出全部数据？',
        onOk: () => {
          window.open(
            global.baseURL + '/res/v1/env_monitor/exportNormalAll',
            '_blank'
          )
        }
      })
    },
    _exportByIds(ids, content) {
      this.$Modal.confirm({
        title: '提示',
        content: content,
        onOk: () => {
          window.open(
            global.baseURL + '/res/v1/env_monitor/exportNormal?ids=' + ids,
            '_blank'
          )
        }
      })
    },
    _beupload(file) {
      this.dataObj.file = file.name
    },
    _handelsuccess(response, file, fileList) {
      // 上传成功
      if (response.success) {
        this.$Message.success('上传成功！')
      } else {
        this.$Message.warning(response.msg)
      }
      this._search()
    },
    _btnClick(msg) {
      switch (msg) {
        case '添加':
          this._editModal(false)
          break
        case '删除':
          this._deleteSelected()
          break
        case '导入':
          this._import()
          break
        case '导出':
          this._export()
          break
        case '预警设置':
          this._earlyWarning()
          break
      }
    },
    _earlyWarning() {
      this.$refs.setModal._open()
    },
    _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>
