<!--  -->
<template>
  <div class="statistics0">
    <TabList />
    <div class="right">
      <div class="top">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item to="">首页</el-breadcrumb-item>
          <el-breadcrumb-item to="">库房管理</el-breadcrumb-item>
          <el-breadcrumb-item id="mytitle">库房设置</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div class="conten">
        <div class="conten_left">
          <div class="btn">
            <el-button type="primary" @click="addOne" v-show="userLogo.add">新建库区</el-button>
            <el-button type="danger" @click="delOne" v-show="userLogo.del">删除</el-button>
          </div>
          <div class="box" v-for="(item, index) in allList" :key="item.id">
            <div class="title" :style="arrowsShow === index?'background-color: #f2f2f2;':''">
              <i
                :class="
                  arrowsShow === index
                    ? 'el-icon-arrow-up'
                    : 'el-icon-arrow-down'
                "
                @click="arrowsShowClick(index)"
              ></i>
              <span @click="arrowsShowClick(index)">{{ item.name }}</span>
              <i class="el-icon-plus tiny" @click="addTow(item.id)" v-show="userLogo.add"></i>
              <i class="el-icon-edit tiny" @click="editOne(item)" v-show="userLogo.edit"></i>
            </div>
            <ul class="area" v-show="arrowsShow === index">
              <li
                :class="{ active: entrepotShow === ind }"
                @click="entrepotShowClick(ind, val)"
                v-for="(val, ind) in item.children"
                :key="val.id"
              >
                {{ val.name }} 编号：{{ val.num }}
              </li>
            </ul>
          </div>
        </div>
        <div class="conten_right">
          <div class="title" v-show="towList.name">
            <b>{{ towList.name }}</b>
            <span>仓库编号：{{ towList.num }}</span>
            <i class="el-icon-plus" @click="addThree" v-show="userLogo.add"></i>
            <i class="el-icon-edit" @click="editTow" v-show="userLogo.edit"></i>
            <i class="el-icon-delete" @click="delTow" v-show="userLogo.del"></i>
          </div>
          <!-- 表格 -->
          <div class="table" v-if="towList.name">
            <el-table
              :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
              :data="tableData"
              border
              style="width: 100%"
            >
              <el-table-column
              :resizable="false"
                prop="num"
                label="货架编号"
                width="200"
              ></el-table-column>
              <el-table-column
              :resizable="false"
                prop="name"
                label="货架名称"
                width="200"
              ></el-table-column>
              <el-table-column
              :resizable="false"
                prop="description"
                label="备注"
              ></el-table-column>
              <el-table-column label="操作" width="150" :resizable="false">
                <template #default='{row}'>
                  <i class="el-icon-edit" @click="editThree(row)" v-show="userLogo.edit"></i>
                  <i class="el-icon-delete" @click="delThree(row.id)" v-show="userLogo.del"></i>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <div v-else class="mydis">请先新建仓库</div>
          <!-- 分页器 -->
          <div class="paging" v-if="0">
            <el-pagination
              background
              layout="prev, pager, next,sizes,jumper"
              :total="30"
            >
            </el-pagination>
          </div>
        </div>
      </div>
    </div>
    <!-- 新建库区弹出层--第一层 -->
    <el-dialog title="库区名称" :visible="popupOne" @close="popupOneX()">
      <el-input v-model="popupOneName" placeholder="请输入库区名称"></el-input>
      <div slot="footer" class="dialog-footer">
        <el-button @click="popupOneX">取 消</el-button>
        <el-button type="primary" @click="popupOneOK">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 新建库区弹出层--第二层 -->
    <el-dialog title="库房信息" :visible="popupTow" @close="popupTowX()">
      仓库编号：<el-input v-model="popupTowNum" placeholder="请输入仓库编号" style="width:91%;margin-bottom:20px"></el-input>
      仓库名称：<el-input v-model="popupTowName" placeholder="请输入仓库名称" style="width:91%"></el-input>
      <div slot="footer" class="dialog-footer">
        <el-button @click="popupTowX">取 消</el-button>
        <el-button type="primary" @click="popupTowOK">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 新建库区弹出层--第三层 -->
    <el-dialog title="货架信息" :visible="popupThree" @close="popupThreeX()">
      货架编号：<el-input v-model="popupThreeNum" placeholder="请输入货架编号" style="width:91%;margin-bottom:20px"></el-input>
      货架名称：<el-input v-model="popupThreeName" placeholder="请输入货架名称" style="width:91%;margin-bottom:20px"></el-input>
      <div class="row">
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;备注：<el-input type="textarea" :rows="3" v-model="popupThreeTxt" placeholder="请输入备注说明" style="width:91%"></el-input>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="popupThreeX">取 消</el-button>
        <el-button type="primary" @click="popupThreeOK">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getTreeList, addEntrepot, remove } from '../../apis/statistics0'
import TabList from '@/components/tabLeft3.vue'
export default {
  name: 'system0',
  // import引入的组件需要注入到对象中才能使用
  components: { TabList },
  data () {
    // 这里存放数据
    return {
      userLogo: {
        add: true, // 新增
        edit: true, // 编辑
        del: true// 删除
      },
      myId: null,
      // 第一层：箭头的显示隐藏
      arrowsShow: 0,
      popupOneName: '',
      popupOne: false,
      // 第二层：库区的显示和隐藏
      // 添加第二层的时候临时储存的第一层父亲的id
      popupTowId: null,
      entrepotShow: 0,
      popupTow: false,
      popupTowName: '',
      popupTowNum: '',
      // 第三层：
      popupThree: false,
      popupThreeNum: '',
      popupThreeName: '',
      popupThreeTxt: '',
      // 第二层数据
      towList: {},
      // 总数据
      allList: [],
      // 表格数据
      tableData: []
    }
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {
    arrowsShow (val) {
      // console.log(999, val)
      if (val !== null) {
        if (this.allList[val].children) {
          this.tableData = this.allList[val].children[0].children
          this.$nextTick(() => {
            setTimeout(() => {
              this.towList = this.allList[val].children[0]
            }, 100)
          })
        } else {
          this.tableData = []
          this.towList = {}
        }
      }
    },
    entrepotShow (val) {
      // console.log(666, val)
      this.tableData = this.allList[this.arrowsShow].children[val].children
    }
  },
  // 方法集合
  methods: {
    // 点击新增库区--第一层
    editOne (val) {
      this.popupOneName = val.name
      this.myId = val.id
      this.popupOne = true
    },
    delOne () {
      if (!this.arrowsShow) return this.$message.error('未选中库区')
      this.$confirm('确定删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        remove(this.allList[this.arrowsShow].id)
        this.arrowsShow = null
        this.$nextTick(() => {
          setTimeout(() => {
            this.getTreeList()
          }, 100)
        })
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    addOne () {
      this.myId = null
      this.popupOne = true
    },
    popupOneX () {
      this.popupOne = false
      this.popupOneName = ''
    },
    async popupOneOK () {
      if (this.popupOneName.trim() === '') return this.$message.error('库区名称不能为空')
      if (this.popupOneName.length > 8) return this.$message.error('库区名称不能超过15个字符')
      await addEntrepot({
        level: 1,
        name: this.popupOneName,
        parentId: null,
        id: this.myId
      })
      this.popupOne = false
      this.popupOneName = ''
      this.getTreeList()
      this.myId = null
      this.$message.success('库区操作成功')
    },
    // 点击第二层+号
    delTow () {
      this.$confirm('确定删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // console.log(666, this.allList[this.arrowsShow].children[this.entrepotShow].id)
        remove(this.allList[this.arrowsShow].children[this.entrepotShow].id)
        this.entrepotShow = 0
        this.arrowsShow = null
        this.towList.name = ''
        this.$nextTick(() => {
          setTimeout(() => {
            this.getTreeList()
          }, 100)
        })
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    editTow () {
      // console.log(666, this.towList.name)
      this.popupTowName = this.towList.name
      this.popupTowNum = this.towList.num
      this.myId = this.towList.id
      this.popupTow = true
    },
    addTow (id) {
      this.myId = null
      this.popupTowId = id
      this.popupTow = true
    },
    popupTowX () {
      this.popupTow = false
      this.popupTowName = ''
      this.popupTowNum = ''
    },
    async popupTowOK () {
      if (this.popupTowNum.trim() === '') return this.$message.error('仓库编号不能为空')
      if (this.popupTowNum.length > 8) return this.$message.error('仓库编号不能超过15个字符')
      if (this.popupTowName.trim() === '') return this.$message.error('仓库名称不能为空')
      if (this.popupTowName.length > 8) return this.$message.error('仓库名称不能超过15个字符')
      await addEntrepot({
        level: 2,
        name: this.popupTowName,
        num: this.popupTowNum,
        parentId: this.popupTowId,
        id: this.myId
      })
      this.popupTow = false
      this.popupTowName = ''
      this.popupTowNum = ''
      this.getTreeList()
      this.myId = null
      this.$message.success('仓库操作成功')
      this.$nextTick(() => {
        setTimeout(() => {
          // console.log(999, (this.allList[this.arrowsShow].children).length - 1)
          // console.log(999, this.allList[this.arrowsShow].children)
          this.entrepotShow = (this.allList[this.arrowsShow].children).length - 1
          this.towList = this.allList[this.arrowsShow].children[(this.allList[this.arrowsShow].children).length - 1]
          // console.log(999, this.towList)
        }, 100)
      })
    },
    // 点击第三层
    delThree (id) {
      this.$confirm('确定删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        remove(id)
        // 刷新页面
        this.$nextTick(() => {
          setTimeout(() => {
            this.getTreeList()
            this.tableData = this.tableData.filter(v => v.id !== id)
          }, 100)
        })
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    editThree (val) {
      this.popupThreeName = val.name
      this.popupThreeNum = val.num
      this.popupThreeTxt = val.description
      this.myId = val.id
      this.popupThree = true
    },
    addThree () {
      this.myId = null
      this.popupThree = true
    },
    popupThreeX () {
      this.popupThree = false
      this.popupThreeName = ''
      this.popupThreeNum = ''
      this.popupThreeTxt = ''
    },
    async popupThreeOK () {
      // console.log(this.towList.id)
      if (this.popupThreeNum.trim() === '') return this.$message.error('货架编号不能为空')
      if (this.popupThreeName.trim() === '') return this.$message.error('货架名称不能为空')
      await addEntrepot({
        level: 3,
        name: this.popupThreeName,
        num: this.popupThreeNum,
        parentId: this.towList.id,
        description: this.popupThreeTxt,
        id: this.myId
      })
      this.popupThree = false
      this.popupThreeName = ''
      this.popupThreeNum = ''
      this.popupThreeTxt = ''
      this.getTreeList()
      this.myId = null
      this.$message.success('货架操作成功')
      // 刷新页面
      this.$nextTick(() => {
        setTimeout(() => {
          this.tableData = this.allList[this.arrowsShow].children[this.entrepotShow].children
        }, 100)
      })
    },
    // 点击箭头
    arrowsShowClick (index) {
      if (this.arrowsShow === index) this.arrowsShow = null
      else {
        this.arrowsShow = index
        this.entrepotShow = 0
      }
    },
    // 点击第二层
    entrepotShowClick (ind, val) {
      // console.log(9999999, ind, val)
      this.entrepotShow = ind
      this.towList = val
      // console.log(666, val)
    },
    // 封装获取库房列表方法
    async getTreeList () {
      const res = await getTreeList()
      this.allList = res.data
      // console.log(9999, res)
    }
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created () {},
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted () {
    // 调用获取库房列表
    this.getTreeList()
    this.$nextTick(() => {
      setTimeout(() => {
        this.towList = this.allList[0].children[0]
        this.tableData = this.towList.children
      }, 100)
    })
    // 获取用户角色权限标识
    let temp = localStorage.getItem('daliCK')
    temp = JSON.parse(temp)
    if (temp) {
      const temp2 = temp.role[0]
      if (temp2 === 'sys_edit') {
        this.userLogo.del = false
        this.userLogo.add = false
        this.userLogo.edit = false
      } // 保管部成员
      if (temp2 === 'sys_look') {
        this.userLogo.del = false
        this.userLogo.add = false
        this.userLogo.edit = false
      } // 其他部门人员
      if (temp2 === 'sys_collect') {
        this.userLogo.del = false
        this.userLogo.add = false
        this.userLogo.edit = false
      } // 征集部主任
    }
  },
  beforeCreate () {}, // 生命周期 - 创建之前
  beforeMount () {}, // 生命周期 - 挂载之前
  beforeUpdate () {}, // 生命周期 - 更新之前
  updated () {}, // 生命周期 - 更新之后
  beforeDestroy () {}, // 生命周期 - 销毁之前
  destroyed () {}, // 生命周期 - 销毁完成
  activated () {} // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style lang='less' scoped>
//@import url(); 引入公共css类
.statistics0 {
  .row{
    display: flex;
    align-items: center;
  }
  /deep/#mytitle > span {
    font-weight: 800;
  }
  display: flex;
  .right {
    width: 100%;
    display: flex;
    flex-direction: column;
    .top {
      padding-left: 35px;
      display: flex;
      align-items: center;
      box-shadow: 1px 1px 10px 1px;
      margin-left: 2px;
      height: 40px;
      background-color: #fff;
    }
  }
  .conten {
    flex: 1;
    display: flex;
    background-color: #fff;
    margin: 20px 20px 40px;
    padding: 30px 60px 0;
    .conten_left {
      width: 380px;
      height: 650px;
      border: 1px solid #ccc;
      margin-right: 30px;
      .btn {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 80px;
      }
      .title {
        span {
          cursor: pointer;
        }
        position: relative;
        display: flex;
        align-items: center;
        height: 55px;
        // background-color: #f2f2f2;
        color: black;
        & > i {
          cursor: pointer;
          font-size: 30px;
        }
        & > i:nth-of-type(2) {
          right: 35px;
        }
        .tiny {
          position: absolute;
          right: 0;
          top: 50%;
          transform: translateY(-50%);
          float: right;
          margin: 0 10px;
          font-size: 24px;
          color: #a5acb3;
        }
      }
      .area {
        color: black;
        li {
          cursor: pointer;
          // width: 100%;
          // height: 33px;
          // line-height: 33px;
          margin-left: 50px;
        }
        li:hover {
          color: #1890ff;
        }
        .active {
          color: #1890ff;
        }
      }
    }
    .conten_right {
      .mydis{
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%,-50%);
        color: black;
        font-size: 30px;
      }
      position: relative;
      .table {
        /deep/.el-table__body-wrapper{
          max-height: 524px;
          max-width: 1150px !important;
          overflow-y: auto;
        }
        i {
          cursor: pointer;
          font-size: 24px;
          color: black;
          margin-left: 15px;
        }
      }
      .paging {
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        bottom: 20px;
      }
      flex: 1;
      height: 650px;
      border: 1px solid #ccc;
      .title {
        height: 75px;
        color: black;
        display: flex;
        align-items: center;
        & > b {
          font-size: 24px;
          margin: 0 20px;
        }
        & > span {
          margin-right: 20px;
        }
        & > i {
          cursor: pointer;
          font-size: 24px;
          margin: 0 10px;
        }
      }
    }
  }
}
</style>
