<template>
  <div class="ezj-container">
    <ezj-aside>
      <ezj-menu :menus="menus" @select="onSelect"></ezj-menu>
    </ezj-aside>
    <ezj-main :title="currentPage.title" v-if="currentPage" :key="activeId">
      <slot>
        <ezj-form-pane v-bind="currentPage" v-if="currentPage.form"></ezj-form-pane>
      </slot>
    </ezj-main>
  </div>
</template>

<script>
import EzjAside from './Aside'
import EzjMenu from './Menu'
import EzjMain from './Main'
import EzjFormPane from './FormPane'

export default {
  name: 'EzjContainer',
  props: {
    menus: {
      type: Array,
      required: true,
      default() {
        return []
      }
    },
    defaultActive: { type: String }
  },
  components: { EzjAside, EzjMenu, EzjMain, EzjFormPane },
  data() {
    return {
      activeId: this.defaultActive
    }
  },
  computed: {
    currentPage() {
      return this.getDeepItem(this.menus, this.activeId)
    }
  },
  methods: {
    getDeepItem(arr, id) {
      for (const item of arr) {
        if (item.id === id || id === undefined) {
          return item
        }
        if (item.children) {
          let found = this.getDeepItem(item.children, id)
          if (found) {
            return found
          }
        }
      }
    },
    onSelect(id) {
      this.activeId = id
    }
  }
}
</script>

<style lang="scss">
.ezj-container {
  display: flex;
}
</style>
