<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="80px"
          label-suffix=":"
        >
          <el-form-item label="模糊搜索">
            <el-input
              v-model="query.device_bom_code"
              clearable
              size="mini"
              placeholder="请BOM设备编码、名称"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>

          <el-form-item label="是否启用">
            <el-select
              v-model="query.is_used"
              clearable
              size="mini"
              placeholder="是否启用"
              class="filter-item"
              @change="hand"
            >
              <el-option
                v-for="item in dict.IS_OR_NOT"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </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>
          <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 v-if="false" prop="device_bom_id" label="设备BOM标识" />
        <el-table-column prop="is_used" label="是否启用">
          <template slot-scope="scope">
            <el-switch
              :value="format_is_active(scope.row.is_used)"
              active-color="#409EFF"
              inactive-color="#F56C6C"
              @change="changeEnabled(scope.row, scope.row.is_used)"
            />
          </template>
        </el-table-column>
        <el-table-column prop="class_name" label="设备分类" />
        <el-table-column prop="device_bom_code" label="BOM编码">
          <template slot-scope="scope">
            <el-link type="warning" @click="crud.toView(scope.row)">{{ scope.row.device_bom_code }}</el-link>
          </template>
        </el-table-column>
        <el-table-column prop="material_name" label="BOM名称" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column v-permission="[]" label="操作" width="120px" align="center" fixed="right">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
    <AddDialog />
  </div>
</template>

<script>
import crudDevicebom from '@/api/wms/basedata/em/devicebom'
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 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 AddDialog from '@/views/wms/basedata/em/devicebom/AddDialog'

export default {
  name: 'Devicebom',
  dicts: ['IS_OR_NOT'],
  components: { pagination, crudOperation, rrOperation, udOperation, Treeselect, AddDialog },
  mixins: [presenter(), header(), crud()],
  cruds() {
    return CRUD({ title: '设备标准BOM维护',
      url: 'api/devicebom',
      idField: 'device_bom_id',
      sort: 'device_bom_id,desc',
      crudMethod: { ...crudDevicebom },
      optShow: {
        add: true,
        edit: false,
        del: false,
        download: false,
        reset: true
      }
    })
  },
  data() {
    return {
      classes: [],
      class_idStr: null,
      materOpt_code: '23',
      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()
    },
    format_is_active(is_used) {
      return is_used === '1'
    },
    changeEnabled(data, val) {
      let msg = '此操作将停用设备BOM，是否继续！'
      if (val !== '1') {
        msg = '此操作将启用设备BOM，是否继续！'
      }
      this.$confirm(msg, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        crudDevicebom.changeActive(data).then(res => {
          this.crud.toQuery()
          this.crud.notify('操作成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        }).catch(() => {
          data.is_used = !data.is_used
        })
      }).catch(() => {
      })
    },
    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
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
