<template>
  <div class="main-layout">
    <header class="main-header">
      <menu class="menu" :class="{ 'is-show': menuVisible }">
        <div class="inner">
          <div class="menu-icon" @click="toggleMenu">
            <span></span><span></span><span></span>
          </div>
          <ul class="menu-list">
            <li @click="toPage({ name: 'channel' })">首页</li>
            <li
              v-for="item in affairTypes"
              :key="item.id"
              @click="
                toPage({
                  name: 'list',
                  query: { channel: item.form_name, channel_id: item.id }
                })
              "
            >
              {{ item.affair_name }}
            </li>
            <li @click="$router.back()">返回上一页</li>
          </ul>
        </div>
      </menu>
    </header>
    <div class="main-body">
      <router-view />
    </div>
  </div>
</template>

<script>
export default {
  name: 'PageContainer',
  data() {
    return {
      menuVisible: false
    }
  },
  computed: {
    affairTypes() {
      return this.$store.state.affairTypes
    }
  },
  methods: {
    toggleMenu() {
      this.menuVisible = !this.menuVisible
    },
    toPage(route) {
      this.menuVisible = false
      this.$router.push({
        name: route.name,
        query: Object.assign({}, this.$route.query, route.query)
      })
    },
    getList() {
      if (!this.affairTypes.length) {
        this.$store.dispatch('getAffairTypes')
      }
    }
  },
  beforeMount() {
    this.getList()
  }
}
</script>

<style lang="scss">
.menu {
  height: 60px;
}
.menu .inner {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  min-width: 70px;
  max-width: 750px;
  height: 60px;
  margin: 0 auto;
  padding: 0 30px;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  background-color: #fff;
  border-top-left-radius: 30px;
  border-bottom-left-radius: 30px;
}
.menu.is-show {
  .menu-list {
    display: block;
  }
  .menu-icon {
    width: 18px;
    height: 20px;
    flex-direction: inherit;
  }
  .menu-icon span {
    width: 2px;
    height: 18px;
  }
}
.menu-icon {
  width: 20px;
  height: 18px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
.menu-icon span {
  width: 20px;
  height: 2px;
  background: #333;
}

.menu-list {
  position: absolute;
  top: 20px;
  right: 25px;
  display: none;
  margin-top: 30px;
  width: 180px;
  background: #fff;
  box-shadow: 0px 2px 20px 0px rgba(0, 0, 0, 0.2);
  li {
    height: 60px;
    line-height: 60px;
    color: #333;
    text-align: center;
    border-top: 1px solid #f5f5f5;
    cursor: pointer;
    &:first-child {
      border: 0;
    }
  }
  &::before {
    position: absolute;
    top: -6px;
    right: 8px;
    content: ' ';
    display: block;
    width: 0;
    height: 0;
    border-color: transparent;
    border-style: solid;
    border-width: 6px;
    border-top-width: 0;
    border-bottom-color: #fff;
  }
}
</style>
