<template>
  <div>
    <el-dialog
      title="选择储位"
      :visible.sync="opened"
      width="600px"
      :before-close="handleClose()"
      v-bind="$attrs"
    >
      <el-tabs v-model="activeName" type="card" @tab-click="activeWarehouse = {}">
        <el-tab-pane v-if="item.warehouseId === warehouseId"   :label="item.name" :name="'' + index" v-for="(item, index) in area"  :key="index">
          <div>
            <div style="text-align: center">区域</div>
            <div style="background-color: #efefef;padding: 10px 10px 0;border: #dcdcdc solid 1px;border-radius: 2px">
              <el-row :gutter="20">
                <el-col :span="12" v-for="(warehouse, i) in item.children" :key="i">
                  <div
                    class="warehouse-block"
                    :class="{'warehouse-block-selected': warehouse.selected, 'warehouse-block-active': warehouse.id === activeWarehouse.id}"
                    @click="handleSelectWarehouse(warehouse)"
                  >
                    {{ warehouse.name }}
                  </div>
                </el-col>
              </el-row>
            </div>
          </div>
          <div>
            <div style="text-align: center">仓位</div>
            <div class="position-group">
              <div class="position" v-for="position in activeWarehouse.positionList" :key="item.id">
                <div
                  class="position-item"
                  v-for="item in position.children"
                  @click="handleSelectPosition(item)"
                  :class="{'position-item-active': item.selected}">
                  {{ item.code }}
                </div>
              </div>
            </div>
          </div>
          <el-divider></el-divider>
          已选择:{{ selected.join(', ') }}
          <el-divider></el-divider>
        </el-tab-pane>
      </el-tabs>
      <span slot="footer">
        <el-button @click="opened = false">关 闭</el-button>
        <el-button type="primary" @click="handleSubmit()">提 交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getByWarehouseId } from '@/api/ecw/warehouseArea'

export default {
  name: 'warehouseLocation',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    value: Array,
    orderId: Number,
    cityId: {
      type: Number,
      default: undefined
    },
    warehouseId: {
      type: Number,
      default: undefined
    },
  },

  data() {
    return {
      opened: false,

      area: [],
      activeName: '0',
      selectedWarehouse: [],
      selectedPosition: [],
      activeWarehouse: {}
    };
  },

  watch: {
    visible(val) {
      if (val) {
        this.opened = true

        getByWarehouseId({cityId: this.cityId,}).then(r => {
          const area = r.data
          area.forEach(e => {
            // 仓库
            e.children.forEach(f => {
              // 区域
              f.selected = false
              if(f.positionList) f.positionList.forEach(g => {
                // 位置
               if(g.children)g.children.forEach(k => {
                  // 子位置
                  k.selected = false
                })
              })
            })
          })
          this.area = area
        })
      } else {
      }
    },
    opened(val) {
      if (val) {
      } else {
        this.$emit('update:visible', false)
      }
    }
  },

  methods: {
    handleSubmit() {
      this.$emit('input', this.inputValue)
      this.opened = false
    },
    handleClose() {},
    handleSelectWarehouse(warehouse) {
      this.activeWarehouse = warehouse
      if (!!warehouse.selected) {
        warehouse.selected = false
      } else {
        warehouse.selected = true

        // 区域被选,清空该区域下的位置
        if(warehouse.positionList) warehouse.positionList.forEach(g => {
          if(g.children)g.children.forEach(k => {
            k.selected = false
          })
        })
      }
    },
    handleSelectPosition(position) {
      if (!!position.selected) {
        position.selected = false

        // 反选位置时,检查父区域下是否所有位置被反选,若是,选父区域
        const parentAre = this.area.find(e => e.id === position.domainId).children.find(f => f.id === position.areaId)
        if (!parentAre.selected) {
          // 检查父区域下是否所有位置被反选
          let hasSelected = false
          parentAre.positionList.forEach(g => {
            // 位置
            g.children.forEach(k => {
              // 子位置
              if (k.selected) hasSelected = true
            })
          })
          // 所有子位置被反选,选父区域
          if (!hasSelected) parentAre.selected = true
        }
      } else {
        position.selected = true

        // 选位置时,父区域反选
        this.area.find(e => e.id === position.domainId).children.find(f => f.id === position.areaId).selected = false

      }
    }
  },

  mounted() {
    console.log('area dialog mounted')
  },

  computed: {
    // code array
    selected() {
      const result = []

      this.area.forEach(e => {
        // 仓库
        e.children.forEach(f => {
          // 区域
          if (f.selected) result.push(f.code)
          else if(f.positionList) f.positionList.forEach(g => {
            // 位置
            if (g.selected) result.push(k.code)
             if(g.children)g.children.forEach(k => {
              // 子位置
              if (k.selected) result.push(k.code)
            })
          })
        })
      })

      return result
    },
    inputValue(){
      const result = []

      this.area.forEach(e => {
        // 仓库
        e.children.forEach(f => {
          // 区域
          if (f.selected) result.push({
            orderId: this.orderId,
            wareId: f.pid,
            areaId: f.id
          })
          else if(f.positionList) f.positionList.forEach(g => {
            // 位置
            if (g.selected) result.push({
              orderId: this.orderId,
              wareId: g.domainId,
              areaId: g.areaId,
              locationId: g.id
            })
            else if(g.children)g.children.forEach(k => {
              // 子位置
              if (k.selected) result.push({
                orderId: this.orderId,
                wareId: k.domainId,
                areaId: k.areaId,
                locationId: k.id
              })
            })
          })
        })
      })

      return result
    }
  }
}
</script>

<style scoped>
.warehouse-block{
  background-color: white;
  border-radius: 5px;
  height: 42px;
  line-height: 42px;
  text-align: center;
  margin-bottom: 15px;
  cursor: pointer;
  transition: 0.5s;
  box-shadow: #bfbfbf 3px 3px 14px 0;
  user-select: none;
}
.warehouse-block:hover{
  opacity: 0.9;
  transition: 0.5s;
  transform: scale(1.02);
  box-shadow: #8f8f8f 7px 5px 14px 0;
}
.warehouse-block-active{
  box-shadow: #7e9dbd 7px 5px 14px 0;
  transform: scale(1.04);
}
.warehouse-block-selected{
  color: #ffffff;
  background-color: #4085e3;
}
.position-group{
  display: flex;
  background-color: #EFEFEF;
  border: 1px #EFEFEF solid;
  gap: 1px;
  min-height: 64px;
}
.position{
  width: 20%;
  height: 64px;
  display: flex;
  flex-direction: column;
  gap: 1px;
  user-select: none;
}
.position-item{
  width: 100%;
  background-color: #FFFFFF;
  flex: 1;
  cursor: pointer;
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: center;
}
.position-item:hover{
  background-color: #d7dbe3;
}
.position-item-active{
  background-color: #4085e3;
  color: white;
}
.position-item-active:hover{
  background-color: #4085e3;
  opacity: 0.8;
}
</style>