<template>
  <div class="classBox">
    <div class="allType">
      <div :class="{ isClickCss: isClick === 0 }" @click="cleanAllType" class="tNameBox">全部</div>
      <div v-for="(item, index) in CourseType[0]" :key="index" >
        <div :class="{ isClickCss: isClick === index + 1 }" class="tNameBox" @click="typeClick(item.id)">{{ item.name }}</div>
      </div>
    </div>
    <div v-if="isClick !== 0 && isOpen === true">
      <div class="childTypeBox">
        <div
          v-for="(item1, index1) in CourseTypeJson[cTypeId]"
          :key="index1"
          :class="{ iscTypeClickCss: CourseChildType[cTypeId] === item1.id }"
        >
          <div @click="chooseChildType(item1.id)">
            {{ item1.name }}
          </div>
          <div v-if="CourseChildType[cTypeId] === item1.id" class="checkIcon">
            <img src="../../../assets/images/home/checkIcon.png" alt="" />
          </div>
        </div>
      </div>
    </div>
    <div v-if="isOpen === true" @click="isOpen = false"></div>
  </div>
</template>

<script>
import { selectAllType } from '@/api/home'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      isClick: 0,
      CourseType: [],
      CourseTypeJson: {},
      cTypeId: '',
      CourseChildType: {},
      zoneClass: [],
      isOpen: false
    }
  },
  computed: {
    ...mapGetters(['userinfo'])
  },
  methods: {
    typeClick(id) {
      for (var i = 0; i < this.CourseType[0].length; i++) {
        if (id === this.CourseType[0][i].id) {
          this.isOpen = true
          this.isClick = i + 1
        }
      }
      this.cTypeId = id
    },
    chooseChildType(id) {
      if (this.CourseChildType[this.cTypeId] === id) {
        this.CourseChildType[this.cTypeId] = ''
      } else {
        this.CourseChildType[this.cTypeId] = id
      }
      this.$forceUpdate()
      this.getAll()
    },
    cleanAllType() {
      this.isClick = 0
      this.CourseChildType = {}
      this.getAll()
    },
    getAll() {
      this.$emit('update:zoneClass', [])
      this.$emit('getAll', this.CourseChildType)
    },
    getType() {
      const params = {
        org: this.userinfo.org && this.userinfo.org !== '' ? this.userinfo.org : '',
        oid: this.userinfo.organizeid && this.userinfo.organizeid !== '' ? this.userinfo.organizeid : ''
      }
      selectAllType(params)
        .then(res => {
          this.CourseType = res
          for (var i = 0; i < res[0].length; i++) {
            if (!this.CourseTypeJson[res[0][i].id]) {
              this.CourseTypeJson[res[0][i].id] = []
            }
            if (this.userinfo.organizeid === '69893dca-1d47-11ed-8c78-005056b86db5') {
              if (res[0][i].name === '栏目') {
                this.CourseType[0][i].name = '主题'
              }
            }

            if (res[2].length === 0 && res[3].length === 0) {
              for (var j = 0; j < res[1].length; j++) {
                if (res[0][i].id === res[1][j].pid) {
                  if (!this.CourseTypeJson[res[0][i].id]) {
                    this.CourseTypeJson[res[0][i].id] = []
                  }
                  this.CourseTypeJson[res[0][i].id].push(res[1][j]) // 去除公共分类
                }
              }
            } else {
              if (res[2].length > 0) {
                for (var js = 0; js < res[2].length; js++) {
                  if (res[0][i].id === res[2][js].pid) {
                    this.CourseTypeJson[res[0][i].id].push(res[2][js]) // 去除公共分类
                  }
                }
              }
              if (res[3].length > 0) {
                for (var jc = 0; jc < res[3].length; jc++) {
                  if (res[0][i].id === res[3][jc].pid) {
                    this.CourseTypeJson[res[0][i].id].push(res[3][jc]) // 去除公共分类
                  }
                }
              }
            }
          }
        })
        .catch(err => {
          console.log(err)
        })
    }
  },
  mounted() {
    this.getType()
  }
}
</script>

<style lang="scss" scoped>
.classBox {
  height: 50px;
  box-sizing: border-box;
  position: relative;
  > div:nth-child(2) {
    position: absolute;
    width: 100%;
    background: #fff;
    z-index: 9;
  }
  > div:nth-child(3) {
    z-index: 5;
    height: 100%;
    width: 100%;
    position: absolute;
    min-height: 700px;
  }
  .allType {
    // margin-top: 60px;
    display: flex;
    width: 100%;
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: flex-start;
    justify-content: flex-start;
    padding-top: 15px;
    > div:nth-child(1) {
      font-size: 16px;
    }
    > div:nth-child(1),
    .tNameBox {
      padding: 0 8px 3px 8px;
      margin: 0 10px;
      font-size: 16px;
    }
    .tNameBox {
      color: #8c8c8c;
    }
    .isClickCss {
      font-weight: bold;
      color: #000;
      border-bottom: 3px solid #5089d6;
    }
  }
  .childTypeBox {
    width: 97%;
    height: 250px;
    margin: 0 auto;
    box-shadow: 0 2px 10px 0px #ccc;
    overflow: auto;
    > div {
      font-size: 15px;
      padding: 10px;
      border-bottom: 1px solid #ededed;
      position: relative;
    }
    > div:last-child {
      border: none;
    }
    .iscTypeClickCss {
      background: #e7f3ff;
    }
    > div > .checkIcon {
      width: 20px;
      height: 20px;
      position: absolute;
      top: 10px;
      right: 20px;
    }
    > div > .checkIcon > img {
      width: 100%;
      height: 100%;
    }
  }
}
</style>
