<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-form
          size="mini"
          :inline="true"
          class="demo-form-inline"
          label-position="right"
          label-width="80px"
          label-suffix=":"
        >
          <el-form-item label="设备类别">
            <treeselect
              v-model="query.material_type_id"
              :load-options="loadClass"
              :options="classes"
              style="width: 200px;"
              placeholder="请选择"
            />
          </el-form-item>

          <el-form-item label="设备故障">
            <treeselect
              v-model="query.device_gz"
              :load-options="loadClass"
              :options="classes2"
              style="width: 200px;"
              placeholder="请选择"
            />
          </el-form-item>

          <el-form-item label="日期">
            <date-range-picker v-model="query.createTime" class="date-item" />
          </el-form-item>

          <el-form-item label="部门">
            <el-input
              v-model="query.dept_id"
              clearable
              placeholder="编码、名称"
            />
          </el-form-item>

          <el-form-item label="设备">
            <el-input
              v-model="query.device_code"
              clearable
              placeholder="编码、名称"
            />
          </el-form-item>

          <rrOperation />
        </el-form>

      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission"/>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="mini" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="dept_name" label="使用部门" />
        <el-table-column prop="class_name" label="设备类型" />
        <el-table-column prop="device_code" label="设备编码">
          <template slot-scope="scope">
            <el-link type="warning" @click="openDtl(scope.row)">{{ scope.row.device_code }}</el-link>
          </template>
        </el-table-column>
        <el-table-column prop="device_model" label="型号规格" />
        <el-table-column prop="nunm" label="故障次数" />
        <el-table-column prop="" label="平均故障间隔时间" />
        <el-table-column prop="" label="平均故障修复时间" />
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
    <DtlDialog :dialog-show.sync="dtlDialog" :open-param="openParam" />
  </div>
</template>

<script>
import crudDevicefaultcae from '@/api/wms/sb/devicefaultcae'
import CRUD, { presenter, header, form, crud } 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'
import { download } from '@/api/data'
import { downloadFile } from '@/utils'
import crudClassstandard from '@/api/wms/basedata/master/classstandard'
import Treeselect, { LOAD_CHILDREN_OPTIONS } from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import crudMaterialbase from '@/api/wms/basedata/master/materialbase'
import DateRangePicker from '@/components/DateRangePicker'
import DtlDialog from '@/views/wms/sb/stat/devicefaultcae/DtlDialog'

const defaultForm = { }
export default {
  name: 'Devicefaultcae',
  components: { pagination, crudOperation, rrOperation, udOperation, Treeselect, DateRangePicker, DtlDialog },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '设备故障分析',
      url: 'api/devicefaultcae',
      idField: 'stockrecord_id',
      sort: 'stockrecord_id,desc',
      crudMethod: { ...crudDevicefaultcae },
      optShow: {
        add: false,
        edit: false,
        del: false,
        download: false,
        reset: true
      }
    })
  },
  data() {
    return {
      classes: [],
      classes2: [],
      class_idStr: null,
      class_idStr2: null,
      materOpt_code: '23',
      materOpt_code2: '24',
      openParam: null,
      dtlDialog: false,
      permission: {
      }
    }
  },
  created() {
    const param = {
      'materOpt_code': this.materOpt_code
    }
    crudMaterialbase.getMaterOptType(param).then(res => {
      this.class_idStr = res.class_idStr
      this.crud.query.class_idStr = this.class_idStr
      this.crud.toQuery()
      this.queryClassId()
    })
    const param2 = {
      'materOpt_code': this.materOpt_code2
    }
    crudMaterialbase.getMaterOptType(param2).then(res => {
      this.class_idStr2 = res.class_idStr
      this.crud.query.class_idStr2 = this.class_idStr2
      this.crud.toQuery()
      this.queryClassId2()
    })
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    hand(value) {
      this.crud.toQuery()
    },
    loadClass({ action, parentNode, callback }) {
      if (action === LOAD_CHILDREN_OPTIONS) {
        crudClassstandard.getClass({ pid: parentNode.id }).then(res => {
          parentNode.children = res.content.map(function(obj) {
            if (obj.hasChildren) {
              obj.children = null
            }
            return obj
          })
          setTimeout(() => {
            callback()
          }, 100)
        })
      }
    },
    queryClassId() {
      const param = {
        'class_idStr': this.class_idStr
      }
      crudClassstandard.queryClassById(param).then(res => {
        this.classes = res.content.map(obj => {
          if (obj.hasChildren) {
            obj.children = null
          }
          return obj
        })
      })
    },
    queryClassId2() {
      const param = {
        'class_idStr': this.class_idStr2
      }
      crudClassstandard.queryClassById(param).then(res => {
        this.classes2 = res.content.map(obj => {
          if (obj.hasChildren) {
            obj.children = null
          }
          return obj
        })
      })
    },
    openDtl(row) {
      this.openParam = row.devicerecord_id
      this.dtlDialog = true
    }
  }
}
</script>

<style scoped>

</style>
