<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.repair_code"
              clearable
              size="mini"
              placeholder="请输入编码、名称"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="维修类型">
            <el-select
              v-model="query.maintenancecycle"
              clearable
              size="mini"
              placeholder="请选择"
              class="filter-item"
              @change="hand"
            >
              <el-option
                v-for="item in dict.EM_DEVICE_WX_INVTYPE"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </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.invstatus"
              clearable
              size="mini"
              placeholder="请选择"
              class="filter-item"
              @change="hand"
            >
              <el-option
                v-for="item in dict.EM_DEVICE_WX_INVSTATUS"
                :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="start_flag"
          @click="startRepair"
        >
          开始维修
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          type="success"
          icon="el-icon-position"
          size="mini"
          :disabled="execu_flag"
          @click="outRepair"
        >
          委外维修
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          type="success"
          icon="el-icon-position"
          size="mini"
          :disabled="open_flag"
          @click="openReceive"
        >
          维修领用
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          type="success"
          icon="el-icon-position"
          size="mini"
          :disabled="end_flag"
          @click="endRepair"
        >
          结束维修
        </el-button>
      </crudOperation>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :row-style="tableRowClassName" :data="crud.data" size="mini" style="width: 100%;" @selection-change="mySelectionChange">
        <el-table-column fixed="left" type="selection" min-width="35" />
        <el-table-column fixed="left" prop="repair_code" label="维修单号" min-width="105"  />
        <el-table-column fixed="left" prop="device_code" label="设备编码" min-width="85"  />
        <el-table-column fixed="left" prop="device_name" label="设备名称"  min-width="150" />
        <el-table-column fixed="left" prop="dept_name" label="所属部门"  min-width="130" />
        <el-table-column fixed="left" prop="use_name" label="使用班组"  min-width="130" />
        <el-table-column fixed="left" prop="extend_code" label="设备自编码" min-width="100" />
        <el-table-column fixed="left" prop="invstatus" label="单据状态" :formatter="formatStatusName" min-width="70" />
        <el-table-column fixed="left" prop="maintenancecycle" label="单据类型" :formatter="formatTypeName" min-width="70" />
        <el-table-column fixed="left" prop="fault_level" label="故障等级" :formatter="formatLevelName" min-width="70" />
        <el-table-column fixed="left" prop="plan_start_date" label="计划维修日期" width="95"  />
        <el-table-column prop="fault_desc" label="故障描述" min-width="150"  />
        <el-table-column prop="request_name" label="报修人" min-width="90" />
        <el-table-column prop="estimaterepair_times" label="预计修复时间H" min-width="130"  />
        <el-table-column prop="input_optname" label="制单人" min-width="90" />
        <el-table-column prop="input_time" label="制单时间" min-width="140"  />
        <el-table-column prop="update_optname" label="维修人"  min-width="150" />
        <el-table-column prop="real_start_date" label="开始时间" min-width="140"  />
        <el-table-column prop="real_end_date" label="结束时间" min-width="140"  />
        <el-table-column prop="remark" label="备注"  min-width="200" />
        <el-table-column prop="confirm_optname" label="验收人" min-width="90" />
        <el-table-column prop="confirm_time" label="验收时间" min-width="140"  />
        <el-table-column prop="audit_optname" label="审核人" min-width="90" />
        <el-table-column prop="audit_time" label="审核时间" min-width="140"  />
        <el-table-column prop="source_bill_code" label="源单号"  min-width="100" />
        <el-table-column prop="source_bill_type" label="源单业务类型" min-width="95" />
        <el-table-column v-permission="[]" label="操作" min-width="125" align="center" fixed="right">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
              :disabled-edit="formterStatus(scope.row)"
              :disabled-dle="scope.row.invstatus !== '01'"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
    <ExecuteDialog :dialog-show.sync="executeDialog" :open-param="openParam" />
    <resuftDialog :dialog-show.sync="resuftDialog" :open-param="openParam" />
    <ReceiveDialog :dialog-show.sync="receiveDialog" :open-param="openParam" />
    <StartDialog :dialog-show.sync="startDialog" :open-param="openParam" />
    <AddDialog/>
  </div>
</template>

<script>
import crudDevicerepairmst from '@/api/wms/sb/devicerepairmst'
import CRUD, { presenter, header, 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 ExecuteDialog from '@/views/wms/sb/repair/devicerepairpa/ExecuteDialog'
import resuftDialog from '@/views/wms/sb/repair/devicerepairpa/resuftDialog'
import ReceiveDialog from '@/views/wms/sb/repair/devicerepairpa/ReceiveDialog'
import StartDialog from '@/views/wms/sb/repair/devicerepairpa/StartDialog'
import AddDialog from '@/views/wms/sb/repair/devicerepair/AddDialog'

export default {
  name: 'Devicerepairpa',
  dicts: ['EM_DEVICE_WX_INVTYPE', 'EM_DEVICE_WX_INVSTATUS', 'EM_FAULT_LEVEL'],
  components: { pagination, crudOperation, rrOperation, udOperation, DateRangePicker, Treeselect, ExecuteDialog, resuftDialog, ReceiveDialog, StartDialog, AddDialog },
  mixins: [presenter(), header(), crud()],
  cruds() {
    return CRUD({
      title: '维修单填报',
      url: 'api/devicerepairmst/query3',
      idField: 'repair_id',
      sort: 'repair_id,desc',
      crudMethod: { ...crudDevicerepairmst },
      optShow: {
        add: true,
        edit: false,
        del: false,
        download: false,
        reset: true
      }
    })
  },
  data() {
    return {
      start_flag: true,
      execu_flag: true,
      end_flag: true,
      open_flag: true,
      confirm_flag: true,
      result_flag: true,
      classes: [],
      class_idStr: null,
      materOpt_code: '23',
      openParam: null,
      executeDialog: false,
      resuftDialog: false,
      receiveDialog: false,
      startDialog: 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()
    },
    formterStatus(row) {
      return '06,07,99'.includes(row.invstatus)
    },
    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
        })
      })
    },
    formatStatusName(row, cloum) {
      return this.dict.label.EM_DEVICE_WX_INVSTATUS[row.invstatus]
    },
    formatTypeName(row, cloum) {
      return this.dict.label.EM_DEVICE_WX_INVTYPE[row.maintenancecycle]
    },
    formatLevelName(row, cloum) {
      return this.dict.label.EM_FAULT_LEVEL[row.fault_level]
    },
    startRepair() {
      const _selectData = this.$refs.table.selection
      const data = _selectData[0]
      this.openParam = data
      this.startDialog = true
    },
    endRepair() {
      const _selectData = this.$refs.table.selection
      const data = _selectData[0]
      if (data.invstatus === '03' || data.invstatus === '05') {
        crudDevicerepairmst.getDtl(data).then(res => {
          this.openParam = {
            'form': data,
            'tableData': res
          }
          this.executeDialog = true
        })
      } else {
        return this.crud.notify('只能对开始或者委外验收的单据结束维修', CRUD.NOTIFICATION_TYPE.INFO)
      }
    },
    executeRepair() {
      const _selectData = this.$refs.table.selection
      const data = _selectData[0]
      if (data.invstatus === '03' || data.invstatus === '04' || data.invstatus === '05') {
        crudDevicerepairmst.getDtl(data).then(res => {
          this.openParam = {
            'form': data,
            'tableData': res
          }
          this.executeDialog = true
        })
      } else {
        return this.crud.notify('单据状态不正确', CRUD.NOTIFICATION_TYPE.INFO)
      }
    },
    confirmRepair() {
      const _selectData = this.$refs.table.selection
      if (_selectData.length === 0) {
        return this.crud.notify('请选择需要确认的记录！', CRUD.NOTIFICATION_TYPE.INFO)
      }
      for (let i = 0; i < _selectData.length; i++) {
        const now = _selectData[i]
        if (now.invstatus !== '06') {
          return this.crud.notify('只能对结束状态的单据确认:' + now.repair_code, CRUD.NOTIFICATION_TYPE.INFO)
        }
      }
      crudDevicerepairmst.confirmRepair({ invstatus: '07', rows: _selectData }).then(res => {
        this.crud.notify('操作成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.crud.toQuery()
      })
    },
    resuftRepair() {
      const _selectData = this.$refs.table.selection
      const data = _selectData[0]
      crudDevicerepairmst.getResult(data).then(res => {
        this.openParam = res
        this.resuftDialog = true
      })
    },
    openReceive() {
      const _selectData = this.$refs.table.selection
      const data = _selectData[0]
      if (data.invstatus === '03' || data.invstatus === '04' || data.invstatus === '05') {
        crudDevicerepairmst.getDtl(data).then(res => {
          this.openParam = {
            'form3': data,
            'itemData': res
          }
          this.receiveDialog = true
        })
      } else {
        this.crud.notify('状态不正确', CRUD.NOTIFICATION_TYPE.INFO)
      }
    },
    outRepair() {
      const _selectData = this.$refs.table.selection
      const data = _selectData[0]
      if (data.invstatus !== '03') {
        return this.crud.notify('只能对开始状态的单据维修', CRUD.NOTIFICATION_TYPE.INFO)
      }
      crudDevicerepairmst.outRepair(data).then(res => {
        this.crud.notify('操作成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.crud.toQuery()
      })
    },
    mySelectionChange(rows) {
      this.buttonChange(rows)
    },
    // 处理按钮是否显示
    buttonChange(rows) {
      if (rows.length !== 0) {
        this.start_flag = false
        this.execu_flag = false
        this.end_flag = false
        this.open_flag = false
        this.confirm_flag = false
        this.result_flag = false
        for (let i = 0; i < rows.length; i++) {
          if ('03,04,05,06,07,99'.includes(rows[i].invstatus)) {
            this.start_flag = true
          }
          if ('01,02,04,05,06,07,99'.includes(rows[i].invstatus)) {
            this.execu_flag = true
          }
          if ('01,02,04,06,07,99'.includes(rows[i].invstatus)) {
            this.end_flag = true
          }
          if ('01,02,06,07,99'.includes(rows[i].invstatus)) {
            this.open_flag = true
          }
          if ('01,02,03,04,05,07,99'.includes(rows[i].invstatus)) {
            this.confirm_flag = true
          }
          if ('01,02,07,99'.includes(rows[i].invstatus)) {
            this.result_flag = true
          }
          if (rows.length > 1) {
            this.handleCurrentChange()
          }
        }
      } else {
        this.handleCurrentChange()
      }
    },
    handleCurrentChange() {
      this.start_flag = true
      this.execu_flag = true
      this.end_flag = true
      this.open_flag = true
      this.confirm_flag = true
      this.result_flag = true
    },
    tableRowClassName({ row, rowIndex }) {
      const stylejson = {}
      if (row.is_red === '1') {
        stylejson.background = '#e0838f'
        stylejson.color = 'green'
        return stylejson
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep {

.vue-treeselect__menu {

  overflow-x: auto !important;

  width: 300px;

  max-height: 300px !important;

}

.vue-treeselect__label {

  overflow: unset;

  text-overflow: unset;

}

.vue-treeselect__control {

  height: 20px !important;

}

.vue-treeselect__multi-value-item-container,

.vue-treeselect--has-value .vue-treeselect__multi-value {

  height: 30px;

  line-height: 24px;

  padding: 0;

}

.vue-treeselect__limit-tip,

.vue-treeselect--searchable.vue-treeselect--multi.vue-treeselect--has-value

.vue-treeselect__input-container {

  padding-top: 0;

}

.vue-treeselect--has-value .vue-treeselect__multi-value {

// margin-bottom: 15px;

}

.vue-treeselect__placeholder,

.vue-treeselect__single-value {

  height: 28px;

  line-height: 32px;

  font-size: small;

  color: "#CCCFD6";


}

.vue-treeselect--has-value .vue-treeselect__input {

  height: 18px !important;

  line-height: 18px !important;

}

.vue-treeselect div,

.vue-treeselect span {

  box-sizing: content-box;

}

// 选中后的溢出隐藏

   .vue-treeselect__multi-value-label {

     display: block;

     width: 140px;

     overflow: hidden;

     white-space: nowrap;

     text-overflow: ellipsis;

   }

.vue-treeselect__value-container {

  display: block;

  height: 32px;

}

}
</style>
