<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="所属部门">
            <treeselect
              v-model="query.dept_id"
              :load-options="loadDepts"
              :options="depts"
              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-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="800px">
        <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="100px">
          <el-row>
            <el-col span="12">
              <el-form-item label="报修单号:" prop="request_code">
                <el-input v-model="form.request_code" style="width: 200px;" disabled placeholder="系统生成" />
              </el-form-item>
            </el-col>
            <el-col span="12">
              <el-form-item label="设备" prop="devicerecord_id">
                <el-input v-model="form.device_code" :disabled="true" style="width: 200px;">
                  <el-button slot="append" icon="el-icon-plus" :disabled="crud.status.view > 0" @click="putDevice" />
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col span="12">
              <el-form-item label="故障类型" prop="device_faultclass_id">
                <el-input v-model="form.device_faultclass_name" :disabled="true" style="width: 200px;">
                  <el-button slot="append" icon="el-icon-plus" :disabled="crud.status.view > 0" @click="putFault" />
                </el-input>
              </el-form-item>
            </el-col>
            <el-col span="12">
              <el-form-item label="故障等级:" prop="fault_level">
                <el-select
                  v-model="form.fault_level"
                  size="mini"
                  style="width: 200px;"
                  placeholder="请选择"
                  :disabled="crud.status.view > 0"
                  class="filter-item"
                >
                  <el-option
                    v-for="item in dict.EM_FAULT_LEVEL"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col span="12">
              <el-form-item label="班组配合人:" prop="product_person_name">
                <el-input v-model="form.product_person_name" style="width: 200px;" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col span="24">
              <el-form-item label="故障描述:" prop="solutions">
                <el-input v-model="form.fault_desc" style="width: 650px;" rows="2" type="textarea" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col span="24">
              <el-form-item label="备注:" prop="remark">
                <el-input v-model="form.remark" style="width: 650px;" rows="2" type="textarea" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <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="dept_name" label="所属部门" show-overflow-tooltip />
        <el-table-column prop="use_name" label="使用班组" show-overflow-tooltip />
        <el-table-column prop="product_person_name" 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-column v-permission="[]" label="操作" width="120px" align="center" fixed="right">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
              :disabled-edit="scope.row.status !== '01'"
              :disabled-dle="scope.row.status !== '01'"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
    <PicDialog ref="child" :dialog-show.sync="PicDialog" />
    <DeviceDialog :dialog-show.sync="deviceDialog" :is-single="true" @tableChanged2="tableChanged2"/>
    <FaultDialog :dialog-show.sync="faultDialog" :is-single="true" :open-param="openParam" @tableChanged="tableChanged"/>
  </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'
import DeviceDialog from '@/views/wms/sb/repair/devicevprs/DeviceDialog'
import FaultDialog from '@/views/wms/sb/repair/devicevprs/FaultDialog'
import { getDepts } from '@/api/system/dept'

const defaultForm = { material_type_id: null, product_person_name: null, device_faultclass_name: null, device_code: null, 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: 'Devicevprs',
  dicts: ['EM_FAULT_LEVEL'],
  components: { pagination, crudOperation, rrOperation, udOperation, DateRangePicker, Treeselect, PicDialog, DeviceDialog, FaultDialog },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '设备报修',
      url: 'api/devicerepairrequest/query2',
      idField: 'request_id',
      sort: 'request_id,desc',
      crudMethod: { ...crudDevicerepairrequest },
      optShow: {
        add: true,
        edit: false,
        del: false,
        download: false,
        reset: true
      }
    })
  },
  data() {
    return {
      openParam: null,
      deviceDialog: false,
      faultDialog: false,
      classes: [],
      class_idStr: null,
      depts: [],
      materOpt_code: '23',
      statusList: [
        { 'label': '生成', 'value': '01' },
        { 'label': '维修中', 'value': '02' },
        { 'label': '完毕', 'value': '03' }
      ],
      PicDialog: false,
      permission: {
      },
      rules: {
        devicerecord_id: [
          { required: true, message: '设备不能为空', trigger: 'blur' }
        ],
        device_faultclass_id: [
          { required: true, message: '故障类型不能为空', trigger: 'blur' }
        ],
        fault_level: [
          { required: true, message: '故障等级不能为空', trigger: 'blur' }
        ],
        product_person_name: [
          { required: true, message: '班组配合人不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  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()
    })
    this.getDepts()
  },
  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) {
      return this.dict.label.EM_FAULT_LEVEL[row.fault_level]
    },
    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
    },
    putDevice() {
      this.deviceDialog = true
    },
    tableChanged2(row) {
      this.form.devicerecord_id = row.devicerecord_id
      this.form.material_type_id = row.material_type_id
      this.form.device_code = row.device_name + '-' + row.device_code
    },
    putFault() {
      this.openParam = this.form
      this.faultDialog = true
    },
    tableChanged(row) {
      this.form.device_faultclass_id = row.device_faultclass_id
      this.form.device_faultclass_name = row.device_faultclass_name
    },
    // 获取弹窗内部门数据
    loadDepts({ action, parentNode, callback }) {
      if (action === LOAD_CHILDREN_OPTIONS) {
        getDepts({ enabled: true, pid: parentNode.id }).then(res => {
          parentNode.children = res.content.map(function(obj) {
            if (obj.hasChildren) {
              obj.children = null
            }
            return obj
          })
          setTimeout(() => {
            callback()
          }, 200)
        })
      }
    },
    getDepts() {
      getDepts({ enabled: true }).then(res => {
        this.depts = res.content.map(function(obj) {
          if (obj.hasChildren) {
            obj.children = null
          }
          return obj
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
