永康嘉持电器有限公司前端
小小儁爺
2025-03-03 ebe7bf1b9118958f6a93406305f5728bfd5d90c9
src/views/basicSettings/storageList.vue
@@ -1,887 +1,892 @@
<template>
  <div>
    <div class="body" :style="{height:mainHeight+'px'}">
      <div style="display: flex">
        <div style="width: 300px;margin: 10px 10px 0  0;background:#fff">
          <div style="margin: 20px 10px 0 10px;display: flex;justify-content: space-between;">
            <div style="display: flex;">
              <div
                style="width: 5px;height: 100%;border-radius: 5px;"
                :style="{background:$store.state.settings.theme}"
              />
              <div style="margin-left: 8px;">仓库</div>
            </div>
            <!--            <div style="margin-right:10px">-->
            <!--              <el-tooltip v-del-tab-index class="item" effect="dark" content="新增" placement="top">-->
            <!--                <i class="el-icon-plus" style="cursor: pointer;color: #999" @click="treeAddClick('add')" />-->
            <!--              </el-tooltip>-->
            <!--            </div>-->
          </div>
          <el-tree
            ref="treeLeftRef"
            style="padding: 10px;overflow: auto"
            :style="{height:(tableHeight+222)+'px'}"
            :data="treeLeft"
            node-key="code"
            highlight-current
            :props="defaultPropsLeft"
            :default-expand-all="true"
            :expand-on-click-node="false"
            @node-click="getTSecLocaData"
          >
            <!--            <span slot-scope="{ node, data }" class="custom-tree-node">-->
            <!--              <span v-if="!data.isEdit">{{ data.name }}</span>-->
            <!--              <span v-if="!data.isEdit">-->
            <!--                <el-tooltip v-del-tab-index class="item" effect="dark" content="编辑" placement="top">-->
            <!--                  <i-->
            <!--                    v-if="data.code!=='-1'"-->
            <!--                    class="el-icon-edit"-->
            <!--                    style="margin-right:10px;color: #999"-->
            <!--                    @click.stop="treeEditClick(node,data,'edit')"-->
            <!--                  />-->
            <!--                </el-tooltip>-->
            <!--                <el-tooltip v-del-tab-index class="item" effect="dark" content="删除" placement="top">-->
            <!--                  <i-->
            <!--                    v-if="data.code!=='-1'"-->
            <!--                    class="el-icon-delete"-->
            <!--                    style="margin-right: 4px;color: #999"-->
            <!--                    @click.stop="treeDeleteClick(node,data)"-->
            <!--                  />-->
            <!--                </el-tooltip>-->
            <!--              </span>-->
            <!--            </span>-->
          </el-tree>
        </div>
        <div
          style=" width:calc(100% - 300px);"
        >
          <div class="bodyTopButtonGroup" style="justify-content: space-between">
            <el-button v-waves type="primary" icon="el-icon-circle-plus-outline" @click="add('add')">新增</el-button>
            <el-button
              v-waves
              type="success"
              icon="el-icon-download"
              @click="$router.push('./../systemSetting/dataImport?fileCode=7')"
            >导入
            </el-button>
          </div>
          <div class="bodyTopFormGroup">
            <el-form
              ref="form"
              :model="form"
              label-width="100px"
              inline
              style="display: flex;"
            >
              <div class="elForm">
                <el-form-item label="货位编码" style=" display: flex;">
                  <el-input v-model="form.locacode" placeholder="请输入" style="width: 200px" />
                </el-form-item>
                <el-form-item label="货位名称" style=" display: flex;">
                  <el-input v-model="form.locaname" placeholder="请输入" style="width: 200px" />
                </el-form-item>
                <!--                <el-form-item label="存货规格" style=" display: flex;">-->
                <!--                  <el-input v-model="form.partspec" placeholder="请输入" style="width: 200px" />-->
                <!--                </el-form-item>-->
                <!--                <el-form-item label="用户编码" style=" display: flex;">-->
                <!--                  <el-input v-model="form.usercode" placeholder="请输入" style="width: 200px" />-->
                <!--                </el-form-item>-->
                <!--                <el-form-item v-show="isExpandForm" label="用户名称" style=" display: flex;">-->
                <!--                  <el-input v-model="form.username" placeholder="请输入" style="width: 200px" />-->
                <!--                </el-form-item>-->
                <!--                <el-form-item v-show="isExpandForm" label="用户编码" style=" display: flex;">-->
                <!--                  <el-input v-model="form.usercode" placeholder="请输入" style="width: 200px" />-->
                <!--                </el-form-item>-->
                <!--                <el-form-item v-show="isExpandForm" label="用户名称" style=" display: flex;">-->
                <!--                  <el-input v-model="form.username" placeholder="请输入" style="width: 200px" />-->
                <!--                </el-form-item>-->
                <!--                <el-form-item v-show="isExpandForm" label="用户编码" style=" display: flex;">-->
                <!--                  <el-input v-model="form.usercode" placeholder="请输入" style="width: 200px" />-->
                <!--                </el-form-item>-->
                <!--                <el-form-item v-show="isExpandForm" label="用户名称" style=" display: flex;">-->
                <!--                  <el-input v-model="form.username" placeholder="请输入" style="width: 200px" />-->
                <!--                </el-form-item>-->
              </div>
              <div
                class="bodySearchReset"
                :style="{marginLeft:$store.state.app.sidebar.opened? $store.state.settings.menuIsHorizontal?'15%':'3%':'10%'}"
              >
                <el-button
                  v-waves
                  type="primary"
                  icon="el-icon-search"
                  @click="getTSecLocaData($refs.treeLeftRef.getCurrentNode())"
                >查询
                </el-button>
                <el-button v-waves type="info" icon="el-icon-refresh" @click="reset">重置</el-button>
              </div>
            </el-form>
            <div
              class="bodyTopFormExpand"
              style="height: 5px;"
            >
              <!--              <svg-icon-->
              <!--                v-show="mouseHoverType==='mouseout'"-->
              <!--                style="cursor: pointer"-->
              <!--                :icon-class="!isExpandForm?'doubleDown3':'doubleUp3'"-->
              <!--                @mouseenter="mouseHoverType=$event.type"-->
              <!--              />-->
              <!--              <svg-icon-->
              <!--                v-show="mouseHoverType==='mouseenter'"-->
              <!--                style="cursor: pointer"-->
              <!--                :icon-class="!isExpandForm?'doubleDown':'doubleUp'"-->
              <!--                @click="isExpandForm=!isExpandForm"-->
              <!--                @mouseout="mouseHoverType=$event.type"-->
              <!--              />-->
            </div>
          </div>
          <div class="elTableDiv">
            <el-table
              ref="tableDataRef"
              class="tableFixed"
              :data="tableData"
              :height="isExpandForm?tableHeight:(tableHeight+80)+'px'"
              border
              row-class-name="custom-row"
              :style="{width: 100+'%',height:isExpandForm?tableHeight:(tableHeight+80)+'px',}"
              highlight-current-row
              :header-cell-style="this.$headerCellStyle"
              :cell-style="this.$cellStyle"
              @sort-change="sortChange"
            >
              <!--            prop="RowNum"-->
              <el-table-column
                prop="rowNum"
                width="50"
                fixed
                label="序号"
              />
              <el-table-column
                prop="code"
                label="货位编码"
                sortable="custom"
              />
              <el-table-column
                prop="name"
                label="货位名称"
                show-overflow-tooltip
                sortable="custom"
              />
              <el-table-column
                prop="stckname"
                label="对应仓库"
                sortable="custom"
              />
              <el-table-column
                prop="status"
                label="状态"
                width="110"
                sortable="custom"
              >
                <template slot-scope="{row}">
                  <el-tag v-if="row.status==='0'" size="small" type="success">正常</el-tag>
                  <el-tag v-if="row.status==='1'" size="small" type="danger">停用</el-tag>
                </template>
              </el-table-column>
              <el-table-column
                prop="description"
                label="备注"
                sortable="custom"
              >
                <template slot-scope="{row}">
                  {{ row.description ? row.description : '/' }}
                </template>
              </el-table-column>
              <!--              <el-table-column-->
              <!--                label="数据来源"-->
              <!--                prop="data_sources"-->
              <!--                sortable="custom"-->
              <!--                width="110"-->
              <!--              />-->
              <el-table-column
                label="创建人员"
                prop="username"
                sortable="custom"
                width="110"
              />
              <el-table-column
                label="创建时间"
                prop="lm_date"
                sortable="custom"
                width="160"
              />
              <el-table-column
                label="操作"
                width="120"
                fixed="right"
              >
                <template slot-scope="{row}">
                  <div class="operationClass">
                    <el-tooltip class="item" effect="dark" content="编辑" placement="top">
                      <i
                        class="el-icon-edit-outline"
                        :style="{color:$store.state.settings.theme}"
                        @click="edit('edit',row)"
                      />
                    </el-tooltip>
                    <el-tooltip v-del-tab-index class="item" effect="dark" content="删除" placement="top">
                      <i
                        class="el-icon-delete"
                        :style="{color:$store.state.settings.theme}"
                        @click="del(row)"
                      />
                    </el-tooltip>
                  </div>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <!--分页-->
          <pagination
            :total="total"
            :page.sync="form.page"
            :limit.sync="form.rows"
            align="right"
            layout="total,prev, pager, next,sizes,jumper"
            popper-class="select_bottom"
            @pagination="getTSecLocaData($refs.treeLeftRef.getCurrentNode())"
          />
        </div>
      </div>
    </div>
    <!--    往来类别新增修改-->
    <el-dialog
      v-el-drag-dialog
      :title="operation==='add'?'新增':'编辑'"
      :visible.sync="dialogClassVisible"
      width="800px"
      :close-on-click-modal="false"
      top="15vh"
      @closed="handleClassClose"
      @close="handleClassClose"
    >
      <el-form ref="dialogClassForm" inline :rules="dialogClassFormRules" :model="dialogClassForm" label-width="80px">
        <el-form-item label="分类编码" prop="customerclasscode">
          <el-input v-model="dialogClassForm.customerclasscode" :disabled="operation!=='add'" style="width: 200px" />
        </el-form-item>
        <el-form-item label="分类名称" prop="customerclassname">
          <el-input v-model="dialogClassForm.customerclassname" style="width: 200px" />
        </el-form-item>
        <el-form-item label="上级分类">
          <el-select
            v-model="dialogClassForm.parentcode"
            style="width:200px"
            placeholder="请选择"
            :popper-append-to-body="false"
            clearable
          >
            <el-option
              v-for="item in treeLeftArr"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <div class="footerButton">
          <el-button v-waves @click="dialogVisibleClassCancel">取 消</el-button>
          <el-button
            v-waves
            type="primary"
            :loading="$store.state.app.buttonIsDisabled"
            :disabled="$store.state.app.buttonIsDisabled"
            @click="dialogVisibleClassConfirm"
          >确 定</el-button>
        </div>
      </span>
    </el-dialog>
    <!--    往来单位新增修改-->
    <el-dialog
      v-el-drag-dialog
      :title="operation==='add'?'新增':'编辑'"
      :visible.sync="dialogVisible"
      width="800px"
      :close-on-click-modal="false"
      top="15vh"
      @closed="handleClose"
      @close="handleClose"
    >
      <el-form ref="dialogForm" class="" inline :rules="dialogFormRules" :model="dialogForm" label-width="110px">
        <!--        <el-divider content-position="left">基本信息</el-divider>-->
        <el-form-item label="货位编码" prop="locacode">
          <el-input v-model="dialogForm.locacode" :disabled="operation!=='add'" style="width: 200px" />
        </el-form-item>
        <el-form-item label="货位名称" prop="locaname">
          <el-input v-model="dialogForm.locaname" style="width: 200px" />
        </el-form-item>
        <el-form-item label="对应仓库" prop="stckcode">
          <el-select
            v-model="dialogForm.stckcode"
            style="width:200px"
            placeholder="请选择"
            filterable
            :popper-append-to-body="false"
            :disabled="!isEdit&&operation==='edit'"
            @change="stckcodeChange"
          >
            <el-option
              v-for="item in stckcodeArr"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="上级货位">
          <el-select
            v-model="dialogForm.parentlocacode"
            style="width:200px"
            placeholder="请选择"
            clearable
            filterable
            :disabled="dialogForm.stckcode===''"
            :popper-append-to-body="false"
          >
            <el-option
              v-for="item in parentlocacodeArr"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </el-form-item>
        <el-form-item required label="状态">
          <el-radio-group v-model="dialogForm.status" style="width: 200px">
            <el-radio label="0">正常</el-radio>
            <el-radio label="1">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            v-model="dialogForm.description"
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4}"
            style="width: 200px;"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <div class="footerButton">
          <el-button v-waves @click="dialogVisibleCancel">取 消</el-button>
          <el-button
            v-waves
            type="primary"
            :loading="$store.state.app.buttonIsDisabled"
            :disabled="$store.state.app.buttonIsDisabled"
            @click="dialogVisibleConfirm"
          >确 定</el-button>
        </div>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import { validateCode } from '@/utils/global'
import elDragDialog from '@/directive/el-drag-dialog'
import arrayToTree from 'array-to-tree'
import { TCunstomerClassTreeAddUpdate, TCunstomerClassTreeDelete, TSecLocaAddUpdate, TSecLocaData, TSecLocaDelete, TSecLocaTree } from '@/api/basicSettings'
import { WareHouseSelectLocation } from '@/api/GeneralBasicData'
const _ = require('lodash')
export default {
  name: 'StorageList',
  directives: { waves, elDragDialog },
  components: {
    Pagination
  },
  data() {
    return {
      mouseHoverType: 'mouseout',
      isExpandForm: false,
      mainHeight: 0,
      tableHeight: 0,
      operation: '',
      treeLeft: [
        {
          code: '-1',
          name: '全部'
        }
      ], // 左侧树
      treeLeftArr: [],
      defaultPropsLeft: {
        children: 'children',
        label: 'name'
      },
      tableData: [],
      form: {
        stckcode: '',
        locacode: '',
        locaname: '',
        partspec: '',
        flag: '',
        page: 1,
        rows: 20,
        prop: 'lm_date', // 排序字段
        order: 'desc' // 排序字段
      },
      total: 0,
      dialogClassVisible: false,
      dialogClassForm: {
        data_sources: '', // 数据来源:ERP/MES
        customerclasscode: '', // 存货分类编码
        customerclassname: '', // 存货分类名称
        parentcode: '', // 上级分类编码
        OperType: ''// OperType
      },
      classArr: [],
      dialogClassFormRules: {
        customerclasscode: [
          { required: true, validator: validateCode, trigger: ['blur', 'change'] }
        ],
        customerclassname: [
          { required: true, message: '请输往来单位名称', trigger: ['blur', 'change'] }
        ]
      },
      dialogVisible: false,
      dialogForm: {
        data_sources: 'MES', // 数据来源;ERP/MES
        locacode: '', //
        locaname: '', //
        stckcode: '', // 对应仓库编码
        parentlocacode: '', // 上级货位编码
        status: '0', // 使用状态: 正常(0)停用(1)
        description: '',
        depth: '', // 层级
        OperType: '' // 操作类型
      },
      dialogFormRules: {
        locacode: [
          { required: true, validator: validateCode, trigger: ['blur', 'change'] }
        ],
        locaname: [
          { required: true, message: '请输入往来单位名称', trigger: ['blur', 'change'] }
        ],
        stckcode: [
          { required: true, message: '请选择对应仓库', trigger: ['blur', 'change'] }
        ]
      },
      stckcodeArr: [],
      parentlocacodeArr: [],
      isEdit: true
    }
  },
  activated() {   window.addEventListener('resize', this.getHeight)   this.getHeight() }, created() {
  },
  mounted() {
    window.addEventListener('resize', this.getHeight)
    this.getHeight()
    this.getTSecLocaTree()
  },
  methods: {
    async getTSecLocaTree() {
      const { data: res } = await TSecLocaTree()
      res.forEach(e => {
        e.name = e.code + ' ' + e.name
      })
      //  获取仓库
      const ck = res.filter(i => i.depth === 0)// 层级为0的  是仓库
      const kw = res.filter(i => i.depth !== 0)// 反之 为仓位
      this.stckcodeArr = ck
      const tree = arrayToTree(kw, {
        parentProperty: 'idparent',
        customID: 'code',
        childrenProperty: 'children'
      })
      const group = _.groupBy(tree, i => i.warhouse)
      const newArr = []
      ck.forEach(j => {
        if (Object.keys(group).includes(j.code)) {
          newArr.push({
            code: j.code,
            name: j.name,
            depth: 0,
            idparent: '-1',
            children: group[j.code]
          })
        } else {
          newArr.push({
            code: j.code,
            name: j.name,
            depth: 0,
            idparent: '-1',
            children: []
          })
        }
      })
      this.treeLeft = [
        {
          code: '-1',
          name: '全部',
          idparent: '',
          children: newArr
        }
      ]// 左侧树
      this.$nextTick(() => {
        this.$refs.treeLeftRef.setCurrentKey('-1')
        this.getTSecLocaData(this.$refs.treeLeftRef.getCurrentNode())
      })
      console.log(JSON.parse(JSON.stringify(this.treeLeft)))
    },
    async getTSecLocaData(node) {
      let result = ''
      if (node.code === '-1') {
        result = node.children.map(i => i.code)
      } else {
        result = [node.code]
      }
      // const result = this.getChildrenCodeMethod(node, [])
      // if (result.includes('-1')) {
      //   result.shift()
      // }
      this.form.stckcode = result.join(',')
      if (this.$refs.treeLeftRef.getCurrentNode().idparent === '') {
        this.form.flag = -1
      } else {
        this.form.flag = this.$refs.treeLeftRef.getCurrentNode().depth
      }
      const res = await TSecLocaData(this.form)
      this.tableData = res.data
      this.total = res.count
    },
    // 获取页面高度
    getHeight() {
      this.$nextTick(() => {
        this.mainHeight = window.innerHeight - 85
        this.tableHeight = this.mainHeight - 280
        this.$refs.tableDataRef.doLayout()
      })
    },
    sortChange({ column, prop, order }) {
      if (order === 'descending') {
        order = 'desc'
      } else if (order === 'ascending') {
        order = 'asc'
      } else {
        order = 'desc'
      }
      this.form.order = order
      this.form.prop = prop
      this.getTSecLocaData(this.$refs.treeLeftRef.getCurrentNode())
    },
    // 新增按钮
    add(operation) {
      this.operation = operation
      this.dialogVisible = true
      this.dialogForm.OperType = 'Add'
    },
    // 修改按钮
    async edit(operation, row) {
      this.operation = operation
      this.dialogVisible = true
      this.dialogForm.OperType = 'Update'
      const temp = this.$refs.treeLeftRef.getCurrentNode()
      if (temp.idparent === '-1' || temp.idparent === '') {
        this.isEdit = temp.depth !== 1
      } else {
        this.isEdit = false
      }
      // this.$nextTick(() => {
      this.dialogForm.data_sources = row.data_sources
      this.dialogForm.locacode = row.code
      this.dialogForm.locaname = row.name
      this.dialogForm.stckcode = row.stckcode
      const { data: res } = await WareHouseSelectLocation({ warhousecode: row.stckcode })
      this.parentlocacodeArr = res
      // this.parentlocacodeArr = this.treeLeftArr.filter(i => i.idparent === this.dialogForm.stckcode)
      this.dialogForm.parentlocacode = row.parentname ? row.parentcode : null
      this.dialogForm.status = row.status
      this.dialogForm.description = row.description
      // })
    },
    del(row) {
      this.$confirm('是否确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        TSecLocaDelete({ locacode: row.code, data_sources: row.data_sources }).then(res => {
          if (res.code === '200') {
            this.$notify.success('删除成功!')
            this.getTSecLocaTree()
            this.getTSecLocaData(this.$refs.treeLeftRef.getCurrentNode())
          }
        })
      }).catch(() => {
        this.$notify.info('已取消删除')
      })
    },
    reset() {
      this.form.stckcode = ''
      this.form.locacode = ''
      this.form.locaname = ''
      this.$refs.treeLeftRef.setCurrentKey('-1')
      this.getTSecLocaData(this.$refs.treeLeftRef.getCurrentNode())
    },
    // 对话框关闭事件
    handleClose() {
      this.dialogForm.data_sources = 'MES'
      this.dialogForm.locacode = ''
      this.dialogForm.locaname = ''
      this.dialogForm.stckcode = ''
      this.dialogForm.parentlocacode = ''
      this.dialogForm.description = ''
      this.dialogForm.status = '0'
      if (this.$refs.treeLeftRef.getCurrentNode().idparent === '') {
        this.form.flag = ''
      } else {
        this.form.flag = this.$refs.treeLeftRef.getCurrentNode().depth
      }
      this.$refs.dialogForm.clearValidate()
    },
    // 对话框取消
    dialogVisibleCancel() {
      this.dialogVisible = false
    },
    // 对话框确认
    dialogVisibleConfirm() {
      this.$refs.dialogForm.validate(valid => {
        if (valid) {
          // console.log(JSON.parse(JSON.stringify(this.dialogForm)), 2)
          if (this.dialogForm.locacode === this.dialogForm.parentlocacode) {
            return this.$message.error('上级货位不能是货位本身!')
          }
          if (this.dialogForm.parentlocacode === '') {
            this.dialogForm.depth = 1
          } else {
            this.dialogForm.depth = parseFloat(this.parentlocacodeArr.find(i => i.code === this.dialogForm.parentlocacode).depth) + 1
          }
          this.$store.state.app.buttonIsDisabled = true
          TSecLocaAddUpdate(this.dialogForm).then(res => {
            if (res.code === '200') {
              this.$notify.success(this.operation === 'add' ? '添加成功!' : '修改成功!')
              this.dialogVisible = false
              this.$store.state.app.buttonIsDisabled = false
              this.getTSecLocaTree()
            } else {
              this.$store.state.app.buttonIsDisabled = false
              this.$notify.error(this.operation === 'add' ? '添加失败!' : '修改失败!')
            }
          })
        }
      })
    },
    async stckcodeChange(val) {
      const { data: res } = await WareHouseSelectLocation({ warhousecode: val })
      this.parentlocacodeArr = res
      this.dialogForm.parentlocacode = ''
      // const res = this.recursiveLookupArray([], val)// 递归查找数组
      // this.parentlocacodeArr = res
    },
    recursiveLookupArray(newArr, val) {
      const temp = this.treeLeftArr.filter(i => i.idparent === val)
      if (temp.length > 0 && temp) {
        temp.forEach(i => {
          newArr.push(i)
          this.recursiveLookupArray(newArr, i.code)
        })
      }
      return newArr
    },
    // 递归取子集的所有code
    getChildrenCodeMethod(node, result) {
      result.push(node.code)
      if (node.children && node.children.length > 0) {
        node.children.forEach(i => {
          this.getChildrenCodeMethod(i, result)
        })
      }
      return result
    },
    treeEditClick(node, data, operation) {
      this.dialogClassForm.data_sources = data.data_sources
      this.dialogClassForm.customerclasscode = data.code
      this.dialogClassForm.customerclassname = data.name
      this.dialogClassForm.parentcode = data.idparent === '-1' ? '' : data.idparent
      this.dialogClassForm.OperType = 'Update'
      this.operation = operation
      this.dialogClassVisible = true
    },
    treeDeleteClick(node, data) {
      this.$confirm('是否确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const result = this.getChildrenCodeMethod(data, [])
        TCunstomerClassTreeDelete({ customerclasscode: result.join(',') }).then(res => {
          if (res.code === '200') {
            this.$notify.success('删除成功!')
            this.getTSecLocaTree()
          }
        })
      }).catch(() => {
        this.$notify.info('已取消删除')
      })
    },
    treeAddClick(operation) {
      this.operation = operation
      this.dialogClassVisible = true
      this.dialogClassForm.OperType = 'Add'
      this.dialogClassForm.data_sources = 'MES'
    },
    handleClassClose() {
      this.dialogClassForm.data_sources = ''
      this.dialogClassForm.customerclasscode = ''
      this.dialogClassForm.customerclassname = ''
      this.dialogClassForm.parentcode = ''
      this.dialogClassForm.OperType = ''
      this.$refs.dialogClassForm.clearValidate()
    },
    dialogVisibleClassCancel() {
      this.dialogClassVisible = false
    },
    dialogVisibleClassConfirm() {
      this.$refs.dialogClassForm.validate(valid => {
        if (valid) {
          TCunstomerClassTreeAddUpdate(this.dialogClassForm).then(res => {
            if (res.code === '200') {
              this.$notify.success(this.operation === 'add' ? '添加成功!' : '修改成功!')
              this.dialogClassVisible = false
              this.$store.state.app.buttonIsDisabled = false
              this.getTSecLocaTree()
            } else {
              this.$store.state.app.buttonIsDisabled = false
              this.$notify.error(this.operation === 'add' ? '添加失败!' : '修改失败!')
            }
          })
        }
      })
    },
    unittypcodeChange(val) {
      console.log(val)
      this.dialogForm.unitcode = ''
      this.dialogForm.unitsubcode = ''
      this.dialogForm.idSubUnitByReport = ''
      this.dialogForm.idUnitByStock = ''
      this.dialogForm.idUnitByPurchase = ''
      this.dialogForm.idUnitBySale = ''
      this.dialogForm.idunitbymanufacture = ''
      // this.$refs.dialogForm.clearValidate()
      // this.$forceUpdate()
    },
    unitcodeChange(val, type) {
      console.log(val)
      if (type === '1') {
        // this.dialogForm.unitcode = val
        this.dialogForm.unitsubcode = ''
        this.dialogForm.idSubUnitByReport = ''
        const unitname = this.unitcodeSingleArr.find(i => i.unitcode === val).unitname
        this.dialogForm.idUnitByStock = unitname
        this.dialogForm.idUnitByPurchase = unitname
        this.dialogForm.idUnitBySale = unitname
        this.dialogForm.idunitbymanufacture = unitname
      }
      if (type === '0') {
        const t = this.unitcodeGroupArr.find(i => i.unitcode === val).children
        this.idSubUnitByReportArr = t.filter(i => i.isMainUnit !== '1')
        this.idUnitBvStockArr = t
        const mainUnitCode = t.find(i => i.isMainUnit === '1').unitcode
        this.dialogForm.unitsubcode = mainUnitCode
        this.dialogForm.idSubUnitByReport = this.idSubUnitByReportArr[0].unitcode
        this.dialogForm.idUnitByStock = mainUnitCode
        this.dialogForm.idUnitByPurchase = mainUnitCode
        this.dialogForm.idUnitBySale = mainUnitCode
        this.dialogForm.idunitbymanufacture = mainUnitCode
      }
    }
  }
}
</script>
<style scoped lang="scss">
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
//::v-deep .el-checkbox__label {
//  width: 55px;
//}
//
.el-icon-plus:hover, .el-icon-edit:hover, .el-icon-delete:hover {
  color: #000 !important;
}
</style>
<template>
  <div>
    <div class="body" :style="{height:mainHeight+'px'}">
      <div style="display: flex">
        <div style="width: 300px;margin: 10px 10px 0  0;background:#fff">
          <div style="margin: 20px 10px 0 10px;display: flex;justify-content: space-between;">
            <div style="display: flex;">
              <div
                style="width: 5px;height: 100%;border-radius: 5px;"
                :style="{background:$store.state.settings.theme}"
              />
              <div style="margin-left: 8px;">仓库</div>
            </div>
            <!--            <div style="margin-right:10px">-->
            <!--              <el-tooltip v-del-tab-index class="item" effect="dark" content="新增" placement="top">-->
            <!--                <i class="el-icon-plus" style="cursor: pointer;color: #999" @click="treeAddClick('add')" />-->
            <!--              </el-tooltip>-->
            <!--            </div>-->
          </div>
          <el-tree
            ref="treeLeftRef"
            style="padding: 10px;overflow: auto"
            :style="{height:(tableHeight+222)+'px'}"
            :data="treeLeft"
            node-key="code"
            highlight-current
            :props="defaultPropsLeft"
            :default-expand-all="true"
            :expand-on-click-node="false"
            @node-click="getTSecLocaData"
          >
            <!--            <span slot-scope="{ node, data }" class="custom-tree-node">-->
            <!--              <span v-if="!data.isEdit">{{ data.name }}</span>-->
            <!--              <span v-if="!data.isEdit">-->
            <!--                <el-tooltip v-del-tab-index class="item" effect="dark" content="编辑" placement="top">-->
            <!--                  <i-->
            <!--                    v-if="data.code!=='-1'"-->
            <!--                    class="el-icon-edit"-->
            <!--                    style="margin-right:10px;color: #999"-->
            <!--                    @click.stop="treeEditClick(node,data,'edit')"-->
            <!--                  />-->
            <!--                </el-tooltip>-->
            <!--                <el-tooltip v-del-tab-index class="item" effect="dark" content="删除" placement="top">-->
            <!--                  <i-->
            <!--                    v-if="data.code!=='-1'"-->
            <!--                    class="el-icon-delete"-->
            <!--                    style="margin-right: 4px;color: #999"-->
            <!--                    @click.stop="treeDeleteClick(node,data)"-->
            <!--                  />-->
            <!--                </el-tooltip>-->
            <!--              </span>-->
            <!--            </span>-->
          </el-tree>
        </div>
        <div
          style=" width:calc(100% - 300px);"
        >
          <div class="bodyTopButtonGroup" style="justify-content: space-between">
            <el-button v-waves type="primary" icon="el-icon-circle-plus-outline" @click="add('add')">新增</el-button>
            <el-button
              v-waves
              type="success"
              icon="el-icon-download"
              @click="$router.push('./../systemSetting/dataImport?fileCode=7')"
            >导入
            </el-button>
          </div>
          <div class="bodyTopFormGroup">
            <el-form
              ref="form"
              :model="form"
              label-width="100px"
              inline
              style="display: flex;"
            >
              <div class="elForm">
                <el-form-item label="货位编码" style=" display: flex;">
                  <el-input v-model="form.locacode" placeholder="请输入" style="width: 200px" />
                </el-form-item>
                <el-form-item label="货位名称" style=" display: flex;">
                  <el-input v-model="form.locaname" placeholder="请输入" style="width: 200px" />
                </el-form-item>
                <!--                <el-form-item label="存货规格" style=" display: flex;">-->
                <!--                  <el-input v-model="form.partspec" placeholder="请输入" style="width: 200px" />-->
                <!--                </el-form-item>-->
                <!--                <el-form-item label="用户编码" style=" display: flex;">-->
                <!--                  <el-input v-model="form.usercode" placeholder="请输入" style="width: 200px" />-->
                <!--                </el-form-item>-->
                <!--                <el-form-item v-show="isExpandForm" label="用户名称" style=" display: flex;">-->
                <!--                  <el-input v-model="form.username" placeholder="请输入" style="width: 200px" />-->
                <!--                </el-form-item>-->
                <!--                <el-form-item v-show="isExpandForm" label="用户编码" style=" display: flex;">-->
                <!--                  <el-input v-model="form.usercode" placeholder="请输入" style="width: 200px" />-->
                <!--                </el-form-item>-->
                <!--                <el-form-item v-show="isExpandForm" label="用户名称" style=" display: flex;">-->
                <!--                  <el-input v-model="form.username" placeholder="请输入" style="width: 200px" />-->
                <!--                </el-form-item>-->
                <!--                <el-form-item v-show="isExpandForm" label="用户编码" style=" display: flex;">-->
                <!--                  <el-input v-model="form.usercode" placeholder="请输入" style="width: 200px" />-->
                <!--                </el-form-item>-->
                <!--                <el-form-item v-show="isExpandForm" label="用户名称" style=" display: flex;">-->
                <!--                  <el-input v-model="form.username" placeholder="请输入" style="width: 200px" />-->
                <!--                </el-form-item>-->
              </div>
              <div
                class="bodySearchReset"
                :style="{marginLeft:$store.state.app.sidebar.opened? $store.state.settings.menuIsHorizontal?'15%':'3%':'10%'}"
              >
                <el-button
                  v-waves
                  type="primary"
                  icon="el-icon-search"
                  @click="getTSecLocaData($refs.treeLeftRef.getCurrentNode())"
                >查询
                </el-button>
                <el-button v-waves type="info" icon="el-icon-refresh" @click="reset">重置</el-button>
              </div>
            </el-form>
            <div
              class="bodyTopFormExpand"
              style="height: 5px;"
            >
              <!--              <svg-icon-->
              <!--                v-show="mouseHoverType==='mouseout'"-->
              <!--                style="cursor: pointer"-->
              <!--                :icon-class="!isExpandForm?'doubleDown3':'doubleUp3'"-->
              <!--                @mouseenter="mouseHoverType=$event.type"-->
              <!--              />-->
              <!--              <svg-icon-->
              <!--                v-show="mouseHoverType==='mouseenter'"-->
              <!--                style="cursor: pointer"-->
              <!--                :icon-class="!isExpandForm?'doubleDown':'doubleUp'"-->
              <!--                @click="isExpandForm=!isExpandForm"-->
              <!--                @mouseout="mouseHoverType=$event.type"-->
              <!--              />-->
            </div>
          </div>
          <div class="elTableDiv">
            <el-table
              ref="tableDataRef"
              class="tableFixed"
              :data="tableData"
              :height="isExpandForm?tableHeight:(tableHeight+80)+'px'"
              border
              row-class-name="custom-row"
              :style="{width: 100+'%',height:isExpandForm?tableHeight:(tableHeight+80)+'px',}"
              highlight-current-row
              :header-cell-style="this.$headerCellStyle"
              :cell-style="this.$cellStyle"
              @sort-change="sortChange"
            >
              <!--            prop="RowNum"-->
              <el-table-column
                prop="rowNum"
                width="50"
                fixed
                label="序号"
              />
              <el-table-column
                prop="code"
                label="货位编码"
                sortable="custom"
              />
              <el-table-column
                prop="name"
                label="货位名称"
                show-overflow-tooltip
                sortable="custom"
              />
              <el-table-column
                prop="stckname"
                label="对应仓库"
                sortable="custom"
              />
              <el-table-column
                prop="status"
                label="状态"
                width="110"
                sortable="custom"
              >
                <template slot-scope="{row}">
                  <el-tag v-if="row.status==='0'" size="small" type="success">正常</el-tag>
                  <el-tag v-if="row.status==='1'" size="small" type="danger">停用</el-tag>
                </template>
              </el-table-column>
              <el-table-column
                prop="description"
                label="备注"
                sortable="custom"
              >
                <template slot-scope="{row}">
                  {{ row.description ? row.description : '/' }}
                </template>
              </el-table-column>
              <!--              <el-table-column-->
              <!--                label="数据来源"-->
              <!--                prop="data_sources"-->
              <!--                sortable="custom"-->
              <!--                width="110"-->
              <!--              />-->
              <el-table-column
                label="创建人员"
                prop="username"
                sortable="custom"
                width="110"
              />
              <el-table-column
                label="创建时间"
                prop="lm_date"
                sortable="custom"
                width="160"
              />
              <el-table-column
                label="操作"
                width="120"
                fixed="right"
              >
                <template slot-scope="{row}">
                  <div class="operationClass">
                    <el-tooltip class="item" effect="dark" content="编辑" placement="top">
                      <i
                        class="el-icon-edit-outline"
                        :style="{color:$store.state.settings.theme}"
                        @click="edit('edit',row)"
                      />
                    </el-tooltip>
                    <el-tooltip v-del-tab-index class="item" effect="dark" content="删除" placement="top">
                      <i
                        class="el-icon-delete"
                        :style="{color:$store.state.settings.theme}"
                        @click="del(row)"
                      />
                    </el-tooltip>
                  </div>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <!--分页-->
          <pagination
            :total="total"
            :page.sync="form.page"
            :limit.sync="form.rows"
            align="right"
            layout="total,prev, pager, next,sizes,jumper"
            popper-class="select_bottom"
            @pagination="getTSecLocaData($refs.treeLeftRef.getCurrentNode())"
          />
        </div>
      </div>
    </div>
    <!--    往来类别新增修改-->
    <el-dialog
      v-el-drag-dialog
      :title="operation==='add'?'新增':'编辑'"
      :visible.sync="dialogClassVisible"
      width="800px"
      :close-on-click-modal="false"
      top="15vh"
      @closed="handleClassClose"
      @close="handleClassClose"
    >
      <el-form ref="dialogClassForm" inline :rules="dialogClassFormRules" :model="dialogClassForm" label-width="80px">
        <el-form-item label="分类编码" prop="customerclasscode">
          <el-input v-model="dialogClassForm.customerclasscode" :disabled="operation!=='add'" style="width: 200px" />
        </el-form-item>
        <el-form-item label="分类名称" prop="customerclassname">
          <el-input v-model="dialogClassForm.customerclassname" style="width: 200px" />
        </el-form-item>
        <el-form-item label="上级分类">
          <el-select
            v-model="dialogClassForm.parentcode"
            style="width:200px"
            placeholder="请选择"
            :popper-append-to-body="false"
            clearable
          >
            <el-option
              v-for="item in treeLeftArr"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <div class="footerButton">
          <el-button v-waves @click="dialogVisibleClassCancel">取 消</el-button>
          <el-button
            v-waves
            type="primary"
            :loading="$store.state.app.buttonIsDisabled"
            :disabled="$store.state.app.buttonIsDisabled"
            @click="dialogVisibleClassConfirm"
          >确 定</el-button>
        </div>
      </span>
    </el-dialog>
    <!--    往来单位新增修改-->
    <el-dialog
      v-el-drag-dialog
      :title="operation==='add'?'新增':'编辑'"
      :visible.sync="dialogVisible"
      width="800px"
      :close-on-click-modal="false"
      top="15vh"
      @closed="handleClose"
      @close="handleClose"
    >
      <el-form ref="dialogForm" class="" inline :rules="dialogFormRules" :model="dialogForm" label-width="110px">
        <!--        <el-divider content-position="left">基本信息</el-divider>-->
        <el-form-item label="货位编码" prop="locacode">
          <el-input v-model="dialogForm.locacode" :disabled="operation!=='add'" style="width: 200px" />
        </el-form-item>
        <el-form-item label="货位名称" prop="locaname">
          <el-input v-model="dialogForm.locaname" style="width: 200px" />
        </el-form-item>
        <el-form-item label="对应仓库" prop="stckcode">
          <el-select
            v-model="dialogForm.stckcode"
            style="width:200px"
            placeholder="请选择"
            filterable
            :popper-append-to-body="false"
            :disabled="!isEdit&&operation==='edit'"
            @change="stckcodeChange"
          >
            <el-option
              v-for="item in stckcodeArr"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="上级货位">
          <el-select
            v-model="dialogForm.parentlocacode"
            style="width:200px"
            placeholder="请选择"
            clearable
            filterable
            :disabled="dialogForm.stckcode===''"
            :popper-append-to-body="false"
          >
            <el-option
              v-for="item in parentlocacodeArr"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </el-form-item>
        <el-form-item required label="状态">
          <el-radio-group v-model="dialogForm.status" style="width: 200px">
            <el-radio label="0">正常</el-radio>
            <el-radio label="1">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            v-model="dialogForm.description"
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4}"
            style="width: 200px;"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <div class="footerButton">
          <el-button v-waves @click="dialogVisibleCancel">取 消</el-button>
          <el-button
            v-waves
            type="primary"
            :loading="$store.state.app.buttonIsDisabled"
            :disabled="$store.state.app.buttonIsDisabled"
            @click="dialogVisibleConfirm"
          >确 定</el-button>
        </div>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import { validateCode } from '@/utils/global'
import elDragDialog from '@/directive/el-drag-dialog'
import arrayToTree from 'array-to-tree'
import { TCunstomerClassTreeAddUpdate, TCunstomerClassTreeDelete, TSecLocaAddUpdate, TSecLocaData, TSecLocaDelete, TSecLocaTree } from '@/api/basicSettings'
import { WareHouseSelectLocation } from '@/api/GeneralBasicData'
const _ = require('lodash')
export default {
  name: 'StorageList',
  directives: { waves, elDragDialog },
  components: {
    Pagination
  },
  data() {
    return {
      mouseHoverType: 'mouseout',
      isExpandForm: false,
      mainHeight: 0,
      tableHeight: 0,
      operation: '',
      treeLeft: [
        {
          code: '-1',
          name: '全部'
        }
      ], // 左侧树
      treeLeftArr: [],
      defaultPropsLeft: {
        children: 'children',
        label: 'name'
      },
      tableData: [],
      form: {
        stckcode: '',
        locacode: '',
        locaname: '',
        partspec: '',
        flag: '',
        page: 1,
        rows: 20,
        prop: 'lm_date', // 排序字段
        order: 'desc' // 排序字段
      },
      total: 0,
      dialogClassVisible: false,
      dialogClassForm: {
        data_sources: '', // 数据来源:ERP/MES
        customerclasscode: '', // 存货分类编码
        customerclassname: '', // 存货分类名称
        parentcode: '', // 上级分类编码
        OperType: ''// OperType
      },
      classArr: [],
      dialogClassFormRules: {
        customerclasscode: [
          { required: true, validator: validateCode, trigger: ['blur', 'change'] }
        ],
        customerclassname: [
          { required: true, message: '请输往来单位名称', trigger: ['blur', 'change'] }
        ]
      },
      dialogVisible: false,
      dialogForm: {
        data_sources: 'MES', // 数据来源;ERP/MES
        locacode: '', //
        locaname: '', //
        stckcode: '', // 对应仓库编码
        parentlocacode: '', // 上级货位编码
        status: '0', // 使用状态: 正常(0)停用(1)
        description: '',
        depth: '', // 层级
        OperType: '' // 操作类型
      },
      dialogFormRules: {
        locacode: [
          { required: true, validator: validateCode, trigger: ['blur', 'change'] }
        ],
        locaname: [
          { required: true, message: '请输入往来单位名称', trigger: ['blur', 'change'] }
        ],
        stckcode: [
          { required: true, message: '请选择对应仓库', trigger: ['blur', 'change'] }
        ]
      },
      stckcodeArr: [],
      parentlocacodeArr: [],
      isEdit: true
    }
  },
  activated() {
    window.addEventListener('resize', this.getHeight)
    this.getHeight()
    this.getTSecLocaTree()
  },
  created() {
  },
  mounted() {
    window.addEventListener('resize', this.getHeight)
    this.getHeight()
    this.getTSecLocaTree()
  },
  methods: {
    async getTSecLocaTree() {
      const { data: res } = await TSecLocaTree()
      res.forEach(e => {
        e.name = e.code + ' ' + e.name
      })
      //  获取仓库
      const ck = res.filter(i => i.depth === 0)// 层级为0的  是仓库
      const kw = res.filter(i => i.depth !== 0)// 反之 为仓位
      this.stckcodeArr = ck
      const tree = arrayToTree(kw, {
        parentProperty: 'idparent',
        customID: 'code',
        childrenProperty: 'children'
      })
      const group = _.groupBy(tree, i => i.warhouse)
      const newArr = []
      ck.forEach(j => {
        if (Object.keys(group).includes(j.code)) {
          newArr.push({
            code: j.code,
            name: j.name,
            depth: 0,
            idparent: '-1',
            children: group[j.code]
          })
        } else {
          newArr.push({
            code: j.code,
            name: j.name,
            depth: 0,
            idparent: '-1',
            children: []
          })
        }
      })
      this.treeLeft = [
        {
          code: '-1',
          name: '全部',
          idparent: '',
          children: newArr
        }
      ]// 左侧树
      this.$nextTick(() => {
        this.$refs.treeLeftRef.setCurrentKey('-1')
        this.getTSecLocaData(this.$refs.treeLeftRef.getCurrentNode())
      })
      console.log(JSON.parse(JSON.stringify(this.treeLeft)))
    },
    async getTSecLocaData(node) {
      let result = ''
      if (node.code === '-1') {
        result = node.children.map(i => i.code)
      } else {
        result = [node.code]
      }
      // const result = this.getChildrenCodeMethod(node, [])
      // if (result.includes('-1')) {
      //   result.shift()
      // }
      this.form.stckcode = result.join(',')
      if (this.$refs.treeLeftRef.getCurrentNode().idparent === '') {
        this.form.flag = -1
      } else {
        this.form.flag = this.$refs.treeLeftRef.getCurrentNode().depth
      }
      const res = await TSecLocaData(this.form)
      this.tableData = res.data
      this.total = res.count
    },
    // 获取页面高度
    getHeight() {
      this.$nextTick(() => {
        this.mainHeight = window.innerHeight - 85
        this.tableHeight = this.mainHeight - 280
        this.$refs.tableDataRef.doLayout()
      })
    },
    sortChange({ column, prop, order }) {
      if (order === 'descending') {
        order = 'desc'
      } else if (order === 'ascending') {
        order = 'asc'
      } else {
        order = 'desc'
      }
      this.form.order = order
      this.form.prop = prop
      this.getTSecLocaData(this.$refs.treeLeftRef.getCurrentNode())
    },
    // 新增按钮
    add(operation) {
      this.operation = operation
      this.dialogVisible = true
      this.dialogForm.OperType = 'Add'
    },
    // 修改按钮
    async edit(operation, row) {
      this.operation = operation
      this.dialogVisible = true
      this.dialogForm.OperType = 'Update'
      const temp = this.$refs.treeLeftRef.getCurrentNode()
      if (temp.idparent === '-1' || temp.idparent === '') {
        this.isEdit = temp.depth !== 1
      } else {
        this.isEdit = false
      }
      // this.$nextTick(() => {
      this.dialogForm.data_sources = row.data_sources
      this.dialogForm.locacode = row.code
      this.dialogForm.locaname = row.name
      this.dialogForm.stckcode = row.stckcode
      const { data: res } = await WareHouseSelectLocation({ warhousecode: row.stckcode })
      this.parentlocacodeArr = res
      // this.parentlocacodeArr = this.treeLeftArr.filter(i => i.idparent === this.dialogForm.stckcode)
      this.dialogForm.parentlocacode = row.parentname ? row.parentcode : null
      this.dialogForm.status = row.status
      this.dialogForm.description = row.description
      // })
    },
    del(row) {
      this.$confirm('是否确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        TSecLocaDelete({ locacode: row.code, data_sources: row.data_sources }).then(res => {
          if (res.code === '200') {
            this.$notify.success('删除成功!')
            this.getTSecLocaTree()
            this.getTSecLocaData(this.$refs.treeLeftRef.getCurrentNode())
          }
        })
      }).catch(() => {
        this.$notify.info('已取消删除')
      })
    },
    reset() {
      this.form.stckcode = ''
      this.form.locacode = ''
      this.form.locaname = ''
      this.$refs.treeLeftRef.setCurrentKey('-1')
      this.getTSecLocaData(this.$refs.treeLeftRef.getCurrentNode())
    },
    // 对话框关闭事件
    handleClose() {
      this.dialogForm.data_sources = 'MES'
      this.dialogForm.locacode = ''
      this.dialogForm.locaname = ''
      this.dialogForm.stckcode = ''
      this.dialogForm.parentlocacode = ''
      this.dialogForm.description = ''
      this.dialogForm.status = '0'
      if (this.$refs.treeLeftRef.getCurrentNode().idparent === '') {
        this.form.flag = ''
      } else {
        this.form.flag = this.$refs.treeLeftRef.getCurrentNode().depth
      }
      this.$refs.dialogForm.clearValidate()
    },
    // 对话框取消
    dialogVisibleCancel() {
      this.dialogVisible = false
    },
    // 对话框确认
    dialogVisibleConfirm() {
      this.$refs.dialogForm.validate(valid => {
        if (valid) {
          // console.log(JSON.parse(JSON.stringify(this.dialogForm)), 2)
          if (this.dialogForm.locacode === this.dialogForm.parentlocacode) {
            return this.$message.error('上级货位不能是货位本身!')
          }
          if (this.dialogForm.parentlocacode === '') {
            this.dialogForm.depth = 1
          } else {
            this.dialogForm.depth = parseFloat(this.parentlocacodeArr.find(i => i.code === this.dialogForm.parentlocacode).depth) + 1
          }
          this.$store.state.app.buttonIsDisabled = true
          TSecLocaAddUpdate(this.dialogForm).then(res => {
            if (res.code === '200') {
              this.$notify.success(this.operation === 'add' ? '添加成功!' : '修改成功!')
              this.dialogVisible = false
              this.$store.state.app.buttonIsDisabled = false
              this.getTSecLocaTree()
            } else {
              this.$store.state.app.buttonIsDisabled = false
              this.$notify.error(this.operation === 'add' ? '添加失败!' : '修改失败!')
            }
          })
        }
      })
    },
    async stckcodeChange(val) {
      const { data: res } = await WareHouseSelectLocation({ warhousecode: val })
      this.parentlocacodeArr = res
      this.dialogForm.parentlocacode = ''
      // const res = this.recursiveLookupArray([], val)// 递归查找数组
      // this.parentlocacodeArr = res
    },
    recursiveLookupArray(newArr, val) {
      const temp = this.treeLeftArr.filter(i => i.idparent === val)
      if (temp.length > 0 && temp) {
        temp.forEach(i => {
          newArr.push(i)
          this.recursiveLookupArray(newArr, i.code)
        })
      }
      return newArr
    },
    // 递归取子集的所有code
    getChildrenCodeMethod(node, result) {
      result.push(node.code)
      if (node.children && node.children.length > 0) {
        node.children.forEach(i => {
          this.getChildrenCodeMethod(i, result)
        })
      }
      return result
    },
    treeEditClick(node, data, operation) {
      this.dialogClassForm.data_sources = data.data_sources
      this.dialogClassForm.customerclasscode = data.code
      this.dialogClassForm.customerclassname = data.name
      this.dialogClassForm.parentcode = data.idparent === '-1' ? '' : data.idparent
      this.dialogClassForm.OperType = 'Update'
      this.operation = operation
      this.dialogClassVisible = true
    },
    treeDeleteClick(node, data) {
      this.$confirm('是否确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const result = this.getChildrenCodeMethod(data, [])
        TCunstomerClassTreeDelete({ customerclasscode: result.join(',') }).then(res => {
          if (res.code === '200') {
            this.$notify.success('删除成功!')
            this.getTSecLocaTree()
          }
        })
      }).catch(() => {
        this.$notify.info('已取消删除')
      })
    },
    treeAddClick(operation) {
      this.operation = operation
      this.dialogClassVisible = true
      this.dialogClassForm.OperType = 'Add'
      this.dialogClassForm.data_sources = 'MES'
    },
    handleClassClose() {
      this.dialogClassForm.data_sources = ''
      this.dialogClassForm.customerclasscode = ''
      this.dialogClassForm.customerclassname = ''
      this.dialogClassForm.parentcode = ''
      this.dialogClassForm.OperType = ''
      this.$refs.dialogClassForm.clearValidate()
    },
    dialogVisibleClassCancel() {
      this.dialogClassVisible = false
    },
    dialogVisibleClassConfirm() {
      this.$refs.dialogClassForm.validate(valid => {
        if (valid) {
          TCunstomerClassTreeAddUpdate(this.dialogClassForm).then(res => {
            if (res.code === '200') {
              this.$notify.success(this.operation === 'add' ? '添加成功!' : '修改成功!')
              this.dialogClassVisible = false
              this.$store.state.app.buttonIsDisabled = false
              this.getTSecLocaTree()
            } else {
              this.$store.state.app.buttonIsDisabled = false
              this.$notify.error(this.operation === 'add' ? '添加失败!' : '修改失败!')
            }
          })
        }
      })
    },
    unittypcodeChange(val) {
      console.log(val)
      this.dialogForm.unitcode = ''
      this.dialogForm.unitsubcode = ''
      this.dialogForm.idSubUnitByReport = ''
      this.dialogForm.idUnitByStock = ''
      this.dialogForm.idUnitByPurchase = ''
      this.dialogForm.idUnitBySale = ''
      this.dialogForm.idunitbymanufacture = ''
      // this.$refs.dialogForm.clearValidate()
      // this.$forceUpdate()
    },
    unitcodeChange(val, type) {
      console.log(val)
      if (type === '1') {
        // this.dialogForm.unitcode = val
        this.dialogForm.unitsubcode = ''
        this.dialogForm.idSubUnitByReport = ''
        const unitname = this.unitcodeSingleArr.find(i => i.unitcode === val).unitname
        this.dialogForm.idUnitByStock = unitname
        this.dialogForm.idUnitByPurchase = unitname
        this.dialogForm.idUnitBySale = unitname
        this.dialogForm.idunitbymanufacture = unitname
      }
      if (type === '0') {
        const t = this.unitcodeGroupArr.find(i => i.unitcode === val).children
        this.idSubUnitByReportArr = t.filter(i => i.isMainUnit !== '1')
        this.idUnitBvStockArr = t
        const mainUnitCode = t.find(i => i.isMainUnit === '1').unitcode
        this.dialogForm.unitsubcode = mainUnitCode
        this.dialogForm.idSubUnitByReport = this.idSubUnitByReportArr[0].unitcode
        this.dialogForm.idUnitByStock = mainUnitCode
        this.dialogForm.idUnitByPurchase = mainUnitCode
        this.dialogForm.idUnitBySale = mainUnitCode
        this.dialogForm.idunitbymanufacture = mainUnitCode
      }
    }
  }
}
</script>
<style scoped lang="scss">
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
//::v-deep .el-checkbox__label {
//  width: 55px;
//}
//
.el-icon-plus:hover, .el-icon-edit:hover, .el-icon-delete:hover {
  color: #000 !important;
}
</style>