<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-form
          :inline="true"
          class="demo-form-inline"
          label-position="right"
          label-width="80px"
          label-suffix=":"
        >
          <el-form-item label="设备编码">
            <el-input
              v-model="query.device_code"
              size="mini"
              clearable
              placeholder="设备编码"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>

          <el-form-item label="是否异常">
            <el-select
              v-model="query.is_fault"
              clearable
              size="mini"
              placeholder="请选择"
              class="filter-item"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in dict.IS_OR_NOT"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="设备区域">
            <el-select
              v-model="query.region_code"
              clearable
              size="mini"
              placeholder="请选择"
              class="filter-item"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in dict.DEVICE_REGION_TYPE"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="设备车间">
            <el-select
              v-model="query.plant_code"
              clearable
              size="mini"
              placeholder="请选择"
              class="filter-item"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in dict.product_area"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <rrOperation />
        </el-form>
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="tableData"
        size="mini"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column prop="device_code" label="设备编码" show-overflow-tooltip width="150px" />
        <el-table-column prop="device_name" label="设备名称 " show-overflow-tooltip width="300px" />
        <el-table-column prop="is_fault" label="状态 " show-overflow-tooltip width="100px">
          <template slot-scope="scope">
            <el-button v-if="scope.row.is_fault==='1'" type="danger" circle />
            <el-button v-if="scope.row.is_fault==='0'" type="success" circle />
          </template>
        </el-table-column>
        <el-table-column prop="plant_code" label="所属车间 " show-overflow-tooltip width="100px" :formatter="formatType1" />
        <el-table-column prop="region_code" label="所属区域 " show-overflow-tooltip width="100px" :formatter="formatType2" />
        <el-table-column prop="fault_type" label="异常类型 " show-overflow-tooltip width="100px" :formatter="formatType3" />
        <el-table-column prop="fault_info" label="故障信息" show-overflow-tooltip width="250px" />
        <el-table-column prop="solve_mode" label="解决方式" show-overflow-tooltip width="250px" />
      </el-table>
      <!--分页组件-->
<!--      <pagination />-->
    </div>
  </div>
</template>

<script>
import crudFaultdevice from '@/views/wms/basedata/master/faultdevice/faultdevice'
import CRUD, { crud, form, header, presenter } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = {
}
export default {
  name: 'Devicecharge',
  dicts: ['DEVICE_ERROR_TYPE', 'IS_OR_NOT', 'DEVICE_REGION_TYPE', 'product_area'],
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '设备故障监控',
      url: 'api/faultdevice/deviceCharge',
      optShow: {
        add: false,
        reset: true
      },
      idField: 'device_id',
      sort: 'device_id,desc',
      crudMethod: { ...crudFaultdevice }
    })
  },
  data() {
    return {
      permission: {},
      tableData: [],
      rules: {
      }
    }
  },
  mounted() {
    this.init()
  },
  beforeDestroy() {
    // js提供的clearInterval方法用来清除定时器
    console.log('定时器销毁')
    clearInterval(this.timer)
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    init: function() {
      this.initStageData()
    },
    initStageData() {
      this.timer = setInterval(() => {
        console.log('定时器启动')
        this.initStatus()
      }, 2000)
    },
    initStatus() {
      const data = this.crud.query
      crudFaultdevice.deviceCharge(data).then(res => {
        // this.crud.data = res.content
        this.tableData = res.content
      })
    },
    formatType1(row) {
      return this.dict.label.product_area[row.plant_code]
    },
    formatType2(row) {
      return this.dict.label.DEVICE_REGION_TYPE[row.region_code]
    },
    formatType3(row) {
      return this.dict.label.DEVICE_ERROR_TYPE[row.fault_type]
    }
  }
}
</script>

<style scoped>

</style>
