<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="启用日期">
            <date-range-picker v-model="query.createTime" class="date-item" value-format="yyyy-MM-dd"/>
          </el-form-item>
          <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.manufacturer"
              clearable
              size="mini"
              placeholder="请输入生产厂家"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="供应商">
            <el-input
              v-model="query.supplier_code"
              clearable
              size="mini"
              placeholder="请输入供应商编码、名称"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="设备属性">
            <el-select
              v-model="query.device_type"
              clearable
              size="mini"
              placeholder="请选择"
              class="filter-item"
              @change="hand"
            >
              <el-option
                v-for="item in dict.DEVICE_ATTRIBUTE_SCODE"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </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 dict.EM_BI_DEVICESTATUS"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="生产用途">
            <el-select
              v-model="query.is_produceuse"
              clearable
              size="mini"
              placeholder="请选择"
              class="filter-item"
              @change="hand"
            >
              <el-option
                v-for="item in dict.EM_BI_DEVICEUSE"
                :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="scrap"
        >
          报废
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          type="success"
          icon="el-icon-position"
          size="mini"
          :disabled="crud.selections.length !== 1"
          @click="downloadWord"
        >
          生成鉴定申请表
        </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 prop="device_code" label="设备代码" width="120px" show-overflow-tooltip />
        <el-table-column prop="device_name" label="设备名称" width="120px" show-overflow-tooltip/>
        <el-table-column prop="extend_code" label="外部编码" width="120px" show-overflow-tooltip/>
        <el-table-column prop="device_model" label="设备型号" width="120px" show-overflow-tooltip/>
        <el-table-column prop="device_spec" label="设备规格" />
        <el-table-column prop="class_name" label="设备类类别" />
        <el-table-column prop="is_produceuse" label="生产用途" :formatter="formatProduceName" />
        <el-table-column prop="status" label="设备状态" :formatter="formatStatusName" />
        <el-table-column prop="beginuse_date" label="启用日期" width="120px" show-overflow-tooltip/>
        <el-table-column prop="use_deptid_name" label="使用部门" />
        <el-table-column prop="use_groupid_name" label="使用班组" show-overflow-tooltip />
        <el-table-column prop="supplier_name" label="供应商" show-overflow-tooltip />
        <el-table-column prop="manufacturer" label="制造商" show-overflow-tooltip />
        <el-table-column prop="leavefactory_date" label="出厂日期" width="120px" show-overflow-tooltip />
        <el-table-column prop="leavefactory_number" label="出厂编号" show-overflow-tooltip />
        <el-table-column prop="drawing_number" label="图号" show-overflow-tooltip />
        <el-table-column prop="device_type" label="设备属性" :formatter="formatTypeName" />
        <el-table-column prop="workprocedure_name" label="工序" />
        <el-table-column prop="assets_code" label="资产编码" show-overflow-tooltip />
        <el-table-column prop="assets_name" label="资产名称" show-overflow-tooltip />
        <el-table-column prop="create_name" label="生成人" />
        <el-table-column prop="create_time" label="生成时间" width="150px" show-overflow-tooltip/>
        <el-table-column prop="update_optname" label="修改人" />
        <el-table-column prop="update_time" label="修改时间" width="150px" show-overflow-tooltip/>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudDevicescrap from '@/api/wms/sb/devicescrap'
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 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 {download} from "@/api/data";
import {downloadFile} from "@/utils";

const defaultForm = { status: null }
export default {
  name: 'Devicescrap',
  dicts: ['EM_BI_DEVICEUSE', 'EM_BI_DEVICESTATUS', 'DEVICE_ATTRIBUTE_SCODE'],
  components: { pagination, crudOperation, rrOperation, udOperation, Treeselect, DateRangePicker },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '设备报废',
      url: 'api/devicescrap',
      idField: 'devicerecord_id',
      sort: 'devicerecord_id,desc',
      crudMethod: { ...crudDevicescrap },
      optShow: {
        add: false,
        edit: false,
        del: false,
        download: false,
        reset: true
      }
    })
  },
  data() {
    return {
      classes: [],
      class_idStr: null,
      materOpt_code: '23',
      deviceDialog: 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
        })
      })
    },
    formatProduceName(row, cloum) {
      return this.dict.label.EM_BI_DEVICEUSE[row.is_produceuse]
    },
    formatStatusName(row, cloum) {
      return this.dict.label.EM_BI_DEVICESTATUS[row.status]
    },
    formatTypeName(row, cloum) {
      return this.dict.label.DEVICE_ATTRIBUTE_SCODE[row.device_type]
    },
    scrap() {
      const msg = '是否继续'
      this.$confirm(msg, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const _selectData = this.$refs.table.selection
        const data = _selectData[0]
        // if (data.status !== '10') {
        //   return this.crud.notify('设备未启用或不存在', CRUD.NOTIFICATION_TYPE.INFO)
        // }
        if (data.device_is_delete === '1') {
          return this.crud.notify('设备不存在或被删除', CRUD.NOTIFICATION_TYPE.INFO)
        }
        if (data.status === '10' || data.status === '11') {
          crudDevicescrap.scrap(data).then(res => {
            this.crud.notify('操作成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
            this.crud.toQuery()
          })
        } else {
          return this.crud.notify('设备状态不正确，需启动设备或完成维修单及保养单', CRUD.NOTIFICATION_TYPE.INFO)
        }
      })
    },
    downloadWord() {
      const msg = '是否继续'
      this.$confirm(msg, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const _selectData = this.$refs.table.selection
        const data = _selectData[0]
        download('/api/devicescrap/downloadWord', { 'devicerecord_id': data.devicerecord_id }).then(result => {
          const name = data.device_name + '报废档案'
          this.crud.toQuery()
          downloadFile(result, name, 'docx')
        })
      })
    }
  }
}
</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>
