<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="110px"
          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="设备">
            <el-input
              v-model="query.device_code"
              clearable
              size="mini"
              placeholder="请输入设备编码、名称"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="报修单号">
            <el-input
              v-model="query.request_code"
              clearable
              size="mini"
              placeholder="请输入编码、名称"
              @keyup.enter.native="crud.toQuery"
            />
          </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-select
              v-model="query.status"
              clearable
              size="mini"
              placeholder="请选择"
              class="filter-item"
              @change="hand"
            >
              <el-option
                v-for="item in statusList"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <rrOperation />
        </el-form>
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
        <el-button
          slot="right"
          class="filter-item"
          type="success"
          icon="el-icon-position"
          size="mini"
          :disabled="crud.selections.length !== 1"
          @click="repair"
        >
          维修
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          type="success"
          icon="el-icon-position"
          size="mini"
          :disabled="crud.selections.length !== 1"
          @click="notRepair"
        >
          不维修
        </el-button>
      </crudOperation>
      <!--表格渲染-->
      <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 v-if="false" prop="request_id" label="报修标识" />
        <el-table-column prop="request_code" width="120px" label="报修单号" show-overflow-tooltip />
        <el-table-column prop="path" label="故障图片" width="80px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-folder-opened" circle @click="Picconfirm(scope.$index, scope.row)" />
          </template>
        </el-table-column>
        <el-table-column prop="device_code" label="设备编码" show-overflow-tooltip />
        <el-table-column prop="device_name" label="设备名称" show-overflow-tooltip />
        <el-table-column prop="extend_code" label="设备自编码" width="90px" show-overflow-tooltip />
        <el-table-column prop="device_faultclass_name" label="故障分类" />
        <el-table-column prop="fault_level" label="故障等级" :formatter="formaLevelName" />
        <el-table-column prop="status" label="状态" :formatter="formatStatusName"/>
        <el-table-column prop="is_passed" label="是否通过" :formatter="formatPassedName"/>
        <el-table-column prop="fault_desc" label="故障描述" show-overflow-tooltip width="120px" />
        <el-table-column prop="remark" label="备注" show-overflow-tooltip width="120px" />
        <el-table-column prop="fault_time" label="故障时间" width="150px" />
        <el-table-column prop="create_name" label="创建人" />
        <el-table-column prop="create_time" label="创建时间" width="150px" />
        <el-table-column prop="process_name" label="处理人" />
        <el-table-column prop="process_time" label="处理时间" width="150px" />
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
    <PicDialog ref="child" :dialog-show.sync="PicDialog" />
  </div>
</template>

<script>
import crudDevicerepairrequest from '@/api/wms/sb/devicerepairrequest'
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 DateRangePicker from '@/components/DateRangePicker'
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 PicDialog from '@/views/wms/sb/repair/devicerepairrequest/PicDialog'

const defaultForm = { request_id: null, request_code: null, devicerecord_id: null, fault_time: null, device_faultclass_id: null, fault_desc: null, fault_level: null, remark: null, status: null, create_id: null, create_name: null, create_time: null, is_passed: null, process_id: null, process_name: null, process_time: null, finish_id: null, finish_name: null, finish_time: null, is_delete: null, sysdeptid: null, syscompanyid: null }
export default {
  name: 'Devicerepairrequest',
  components: { pagination, crudOperation, rrOperation, udOperation, DateRangePicker, Treeselect, PicDialog },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '设备报修处理',
      url: 'api/devicerepairrequest',
      idField: 'request_id',
      sort: 'request_id,desc',
      crudMethod: { ...crudDevicerepairrequest },
      optShow: {
        add: false,
        edit: false,
        del: false,
        download: false,
        reset: true
      }
    })
  },
  data() {
    return {
      classes: [],
      class_idStr: null,
      materOpt_code: '23',
      statusList: [
        { 'label': '生成', 'value': '01' },
        { 'label': '维修中', 'value': '02' },
        { 'label': '完毕', 'value': '03' }
      ],
      PicDialog: 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()
    })
  },
  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
        })
      })
    },
    formaLevelName(row, cloum) {
      if (row.fault_level === '01') {
        return '一级'
      } else if (row.fault_level === '02') {
        return '二级'
      } else if (row.fault_level === '03') {
        return '三级'
      }
    },
    formatStatusName(row, cloum) {
      if (row.status === '01') {
        return '生成'
      } else if (row.status === '02') {
        return '维修中'
      } else if (row.status === '99') {
        return '完毕'
      }
    },
    formatPassedName(row, cloum) {
      if (row.is_passed === '0') {
        return '否'
      } else if (row.is_passed === '1') {
        return '是'
      }
    },
    repair() {
      const _selectData = this.$refs.table.selection
      const data = _selectData[0]
      if (data.status !== '01') {
        return this.crud.notify('只能对生成状态的单据进行维修', CRUD.NOTIFICATION_TYPE.INFO)
      }
      crudDevicerepairrequest.repair(data).then(res => {
        this.crud.notify('操作成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.crud.toQuery()
      })
    },
    notRepair() {
      const _selectData = this.$refs.table.selection
      const data = _selectData[0]
      if (data.status !== '01') {
        return this.crud.notify('只能对生成状态的单据进行操作', CRUD.NOTIFICATION_TYPE.INFO)
      }
      crudDevicerepairrequest.notRepair(data).then(res => {
        this.crud.notify('操作成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.crud.toQuery()
      })
    },
    Picconfirm(index, row) {
      debugger
      this.$refs.child.setForm(row.request_code)
      this.PicDialog = true
    }
  }
}
</script>

<style scoped>

</style>
