<template>
  <div class="down-item" ref="layout">
    <h3 @click="parentClickHandle" :class="{active: showContent}" v-show="!key">
      <div>
        <span :class="{aactive: aaaactive}">{{title}}</span>
        <a>{{children.length}}</a>
      </div>
      <span>
          <i class="iconfont" :class="showContent ? 'icon-shouqi' : 'icon-xl'"></i>
      </span>
    </h3>
    <ul v-show="showContent && children.length > 0">
      <li v-for="(item, key) in children" :key="key" @click="clickHandle(item)" :class="{active: item.name === active}">
        <p>• {{item.name}} <i class="iconfont icon-wancheng1" v-if="item.link || item.data"></i> </p>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  props: ['setting', 'keyword', 'show', 'aaaactive'],
  data () {
    return {
      key: this.keyword,
      showAll: false,
      title: this.setting.title,
      showContent: false,
      active: null,
      isReq: false
    }
  },
  methods: {
    clickHandle (item) {
      this.$emit('checkItem', item)
    },
    parentClickHandle () {
      this.showContent = !this.showContent
      if (this.showContent) {
        this.$emit('showcontent')
      }
    }
  },
  computed: {
    children () {
      let children = [...this.setting.children]

      if (this.key) {
        children = children.filter(item => {
          return ~item.name.indexOf(this.keyword)
        })
      }
      return children
    }
  },
  watch: {
    show (newVal) {
      if (newVal === this.title) {
        this.showContent = false
        this.parentClickHandle()
      }
    },
    '$route': {
      deep: true,
      immediate: true,
      handler (newVal) {
        this.active = newVal.params.show
      }
    },
    keyword (newVal) {
      this.key = newVal
      this.showContent = !!this.key
    },
    async showAll (newVal) {
      if (newVal) {
        if (!this.isReq) {
          await this.getData()
        }

        this.content.forEach(item => {
          if (!~this.actives.indexOf(item)) {
            this.actives.push(item)
            this.$emit('selected', item)
          }
        })
      } else {
        this.actives.forEach(item => {
          this.$emit('cancel', item)
        })
        this.actives = []
      }
    },
    showContent: {
      immediate: true,
      handler: function (newVal) {
        if (newVal) {
          setTimeout(() => {
            this.$refs.layout.parentNode.scrollTop = this.$refs.layout.offsetTop
          }, 50)
        }
      }
    }
  },
  mounted () {
    this.quxiaoHandle = () => {
      this.showAll = false
    }
    this.$bus.$on('quxiao', this.quxiaoHandle)
  },
  beforeDestroy () {
    this.$bus.$off('quxiao', this.quxiaoHandle)
  }
}
</script>

<style scoped>
.down-item {
  margin-top: 10px;
}
.down-item > h3 {
  background-color: #ffffff;
  /* padding: 8px 20px; */
  height: 30px;
  display: flex;
  color: #fff;
  font-size: 16px;
  align-items: center;
  font-weight: normal;
  cursor: pointer;
}

.down-item > h3 input {
  -webkit-appearance: checkbox;
  cursor: pointer;
}

.down-item > h3 div {
  flex: 1;
  color: #2b2521;
  font-size: 14px;
  flex: 0 0 'auto';
}

.down-item > h3 i {
  color: #eac1a3;
  font-size: 16px;
}

.down-item > h3 div span {
  font-weight: bold;
}

.down-item > ul > li {
  margin: 16px 0;
  display: flex;
  align-items: center;
  cursor: pointer;
}

.down-item > ul > li img {
  flex: 0 0 auto;
  height: 20px;
  margin-right: 10px;
}

.down-item > ul > li p {
  flex: 1;
  color: #2b2521;
  font-size: 14px;
}

.down-item > ul > li.active p {
  color: #f13800;
}

.aactive {
  color: #f13800 !important;
}

.down-item > h3 a {
  background-color: #f13800;
  color: #fff;
  font-size: 12px;
  text-align: center;
  border-radius: 9px;
  width: 24px;
  height: 18px;
  line-height: 18px;
  display: inline-block;
  margin-left: 5px;
}
</style>
