<template>
  <div v-loading.fullscreen.lock="fullscreenLoading" 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="90px"
          label-suffix="："
        >
          <el-form-item label="编码名称">
            <el-input
              v-model="query.name"
              clearable
              size="mini"
              placeholder="编码名称"
              @keyup.enter.native="crud.toQuery"
            />
          </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"
          @click="synchronize()"
        >
          同步
        </el-button>
      </crudOperation>
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="600px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="160px" label-suffix="：">
          <el-row>
            <el-col :span="12">
              <el-form-item label="物料编码" prop="material_code">
                <el-input :disabled="crud.status.edit" v-model="form.material_code" clearable style="width: 300px" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="静置时间" prop="standing_time">
                <el-input v-model="form.standing_time" type="number" style="width: 300px" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="阈值时间" prop="threshold_time">
                <el-input v-model="form.threshold_time" type="number" style="width: 300px" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="单重" prop="weight">
                <el-input v-model="form.weight" type="number" style="width: 300px" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="产品编号" prop="product_code">
                <el-input v-model="form.product_code" type="number" style="width: 300px" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="长边长度" prop="a">
                <el-input v-model="form.a" type="number" style="width: 300px" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="短边长度" prop="b">
                <el-input v-model="form.b" type="number" style="width: 300px" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="梯形高度" prop="h">
                <el-input v-model="form.h" type="number" style="width: 300px" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="砖块厚度" prop="w">
                <el-input v-model="form.w" type="number" style="width: 300px" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="info" @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 prop="material_code" label="物料编码" width="260" show-overflow-tooltip />
        <el-table-column prop="standing_time" label="困料时间" width="75" />
        <el-table-column prop="threshold_time" label="超时时间" width="75" />
        <el-table-column prop="weight" label="单重" width="75" />
        <el-table-column prop="product_code" label="产品编号" width="75" />
        <el-table-column prop="a" label="长边长度" width="75" />
        <el-table-column prop="b" label="短边长度" width="75" />
        <el-table-column prop="h" label="梯形高度" width="75" />
        <el-table-column prop="w" label="砖块厚度" width="75" />
        <el-table-column prop="update_optname" label="修改人" width="75" />
        <el-table-column prop="update_time" label="修改时间" width="135" />
        <el-table-column
          v-permission="[]"
          label="操作"
          fixed="right"
          width="120px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudMaterialbase from '@/api/wms/basedata/materialbase'
import CRUD, { crud, form, header, presenter } 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 { LOAD_CHILDREN_OPTIONS } from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import crudClassstandard from '@/api/wms/basedata/classstandard'
import crudMdPbMeasureunit from '@/api/wms/basedata/mdPbMeasureunit'

const defaultForm = {
  material_id: null,
  ass_unit_id: null,
  material_code: null,
  material_name: null,
  standing_time: null,
  threshold_time: null,
  a: null,
  b: null,
  h: null,
  w: null,
  product_code: null,
  create_id: null,
  create_name: null,
  create_time: null,
  update_optid: null,
  update_optname: null,
  update_time: null,
  weight: null
}
export default {
  name: 'Materialbase',
  // 数据字典
  dicts: ['is_used'],
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '物料',
      optShow: { add: true, reset: true },
      url: 'api/Materialbase',
      idField: 'material_id',
      sort: 'material_id,desc',
      crudMethod: { ...crudMaterialbase }
    })
  },
  data() {
    return {
      classes1: [],
      classes2: [],
      classes3: [],
      fullscreenLoading: false,
      measure_unit: [],
      productSeries: [],
      permission: {},
      rules: {
        material_code: [
          { required: true, message: '物料编码不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.beforeToCU](crud, form) {
      crudMdPbMeasureunit.getUnit().then(res => {
        this.measure_unit = res.content
      })
      // 物料标识为空，新增
      if (!form.material_type_id) {
        form.material_type_id = null // 必须为null，否则会出现unknown
      } else { // 更新
        this.getSubTypes('02', form.material_type_id)
      }
      // 物料系列为空，新增
      if (!form.product_series) {
        form.product_series = null // 必须为null，否则会出现unknown
      } else { // 更新
        this.getSubTypes('03', form.product_series)
      }
    },
    getSubTypes(type, id) {
      const that = this
      crudClassstandard.getClassSuperior(id).then(res => {
        const data = res.content
        that.buildTree(data)
        if (type === '02') {
          that.classes2 = data
        }
        if (type === '03') {
          that.classes3 = data
        }
      })
    },
    synchronize() {
      this.fullscreenLoading = true
      crudMaterialbase.synchronize(this.crud.query).then(res => {
        this.fullscreenLoading = false
        this.crud.notify('同步成功！', CRUD.NOTIFICATION_TYPE.SUCCESS)
      }).catch(() => {
        this.fullscreenLoading = false
      })
    },
    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
        })
      })
    },
    buildTree(classes) {
      classes.forEach(data => {
        if (data.children) {
          this.buildTree(data.children)
        }
        if (data.hasChildren && !data.children) {
          data.children = null // 重点代码
        }
      })
    },
    // 改变状态
    changeEnabled(data, val) {
      this.$confirm('此操作将 "' + this.dict.label.is_used[val] + '" ' + data.material_name + ', 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        crudMaterialbase.edit(data).then(res => {
          this.crud.notify(this.dict.label.is_used[val] + '成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        }).catch(() => {
          if (data.is_used === '0') {
            data.is_used = '1'
            return
          }
          if (data.is_used === '1') {
            data.is_used = '0'
          }
        })
      }).catch(() => {
        if (data.is_used === '0') {
          data.is_used = '1'
          return
        }
        if (data.is_used === '1') {
          data.is_used = '0'
        }
      })
    },
    // 获取子节点数据
    loadChildNodes({ 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)
        })
      }
    }
  }
}
</script>

<style scoped>

</style>
