<template>
  <el-dialog
    title="新增"
    append-to-body
    :visible.sync="dialogVisible"
    destroy-on-close
    :show-close="true"
    width="60%"
    @close="close"
  >
    <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="库区">
            <label slot="label">库&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;区:</label>
            <el-cascader
              v-model="query.sect"
              :options="sects"
              placeholder="请选择库区"
              clearable
              @change="sectChange"
            />
          </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 />
      <!--表格渲染-->
      <el-table
        ref="multipleTable"
        v-loading="crud.loading"
        :data="crud.data"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" width="50" label="序号" />
        <el-table-column show-overflow-tooltip prop="class_code" label="物料类别编码" />
        <el-table-column show-overflow-tooltip prop="class_name" label="物料类别名称" />
        <el-table-column show-overflow-tooltip prop="address" label="二级分类" />
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>

    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="doAdd">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import CRUD, { presenter, header, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import crudSectattr from '@/views/wms/basedata/sectattr/sectattr'
import crudMaterialbase from '@/views/wms/basedata/material/material'
import crudClassstandard from '@/views/wms/basedata/class/classstandard'
import Treeselect, { LOAD_CHILDREN_OPTIONS } from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import crudStructrelamaterial from '@/views/wms/basedata/materialSet/structrelamaterial'

export default {
  name: 'AddDialog',
  components: { pagination, rrOperation, crudOperation, Treeselect },
  mixins: [presenter(), header(), crud()],
  cruds() {
    return CRUD({
      url: 'api/Classstandard/getClassTable',
      idField: 'class_id',
      sort: 'class_id,desc',
      crudMethod: { ... crudClassstandard },
      optShow: {
        add: false,
        edit: false,
        del: false,
        download: false,
        reset: true
      }
    })
  },
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    },
    openParam: {
      type: Object
    }
  },
  data() {
    return {
      sects: [],
      classes: [],
      sect_id: '',
      dialogVisible: false,
      procStatusList: []
    }
  },
  watch: {
    dialogShow: {
      handler(newValue, oldValue) {
        this.dialogVisible = newValue
      }
    }
  },
  created() {
    crudSectattr.getSect({ 'is_materialstore': '1' }).then(res => {
      this.sects = res.content
    })
    const param = {
      'materOpt_code': '00'
    }
    crudMaterialbase.getMaterOptType(param).then(res => {
      this.class_idStr = res.class_idStr
      this.crud.query.class_idStr = res.class_idStr
      this.queryClassId()
    })
  },
  methods: {
    close() {
      this.query.material_type_id = null
      this.sect_id = null
      this.query.sect = null
      this.$emit('update:dialogShow', false)
      this.$emit('AddChanged')
    },
    sectChange(val) {
      this.sect_id = val[1]
    },
    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
        })
        this.crud.toQuery()
      })
    },
    doAdd() {
      const data = {}
      const _selectData = this.$refs.multipleTable.selection
      data.class_rows = _selectData
      if (_selectData.length === 0) {
        this.crud.notify('请选择记录', CRUD.NOTIFICATION_TYPE.INFO)
        return false
      }
      data.sect_id = this.sect_id
      if (!this.crud.query.sect) {
        this.crud.notify('请选择库区', CRUD.NOTIFICATION_TYPE.INFO)
        return false
      }
      crudStructrelamaterial.insertSet(data).then(() => {
        this.crud.notify('操作成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.close()
      })
    }
  }
}
</script>

<style scoped>

</style>
