<template>
  <div>
    <div style=" margin-top:10px;">
      <div v-for="(menu, index) in sidebarItems" :key="index">
        <!-- menu按鈕 -->
        <b-button :disabled="
          menu.children.filter((child) =>
            child.meta === undefined ? false : child.meta.isOnSidebar
          ).length !== 0
        " @click="toRouteName(menu.name)" class="subMenuButton" :class="{
  choosedsMB: $route.matched[2].name === menu.name,
  notchoosedsMB: $route.matched[2].name !== menu.name,
}">
          <div v-if="
            menu.children.filter((child) =>
              child.meta === undefined ? false : child.meta.isOnSidebar
            ).length === 0
          ">
            {{ menu.meta.title }}
          </div>
          <div v-else-if="
            menu.children.filter((child) =>
              child.meta === undefined ? false : child.meta.isOnSidebar
            ).length !== 0
          " @click="
  menu.meta.status = !menu.meta.status;
$forceUpdate();
            " style="margin-left: 0px; display: inline">
            <span v-if="menu.meta.status">{{ menu.meta.title }}▼</span>
            <span v-else>{{ menu.meta.title }}►</span>
          </div>
        </b-button>
        <!-- 三角形按鈕 -->
        <!-- <b-button
      v-if="menu.children.filter(child => child.meta === undefined ? false : child.meta.isOnSidebar).length !== 0" @click="menu.meta.status = !menu.meta.status" class="triangle">
        <span v-if="menu.meta.status">▼</span>
        <span v-else>►</span >
        </b-button> -->

        <b-collapse v-if="menu.children !== undefined" v-model="menu.meta.status">
          <b-card-body class="cardbody">
            <!-- subMenu按鈕 -->
            <b-button class="subMenu" :class="{
              choosedsM: $route.matched[3].name === subMenu.name,
              notchoosedsM: $route.matched[3].name !== subMenu.name
            }" v-for="(subMenu, index) in menu.children.filter((child) =>
  child.meta === undefined ? false : child.meta.isOnSidebar
)" :key="index" @click="$router.push({ name: subMenu.name })">
              {{ subMenu.meta.title }}
            </b-button>
          </b-card-body>
        </b-collapse>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      sidebarItems: []
    }
  },
  mounted() {
    this.setSideBarItems()
  },
  watch: {
    $route: function (from, to) {
      if (
        from.matched.find((matchRoute) => matchRoute.meta.isOnNavbar).name !==
        to.matched.find((matchRoute) => matchRoute.meta.isOnNavbar).name
      ) { this.setSideBarItems() }
    }
  },
  methods: {
    setSideBarItems() {
      const childrenRoute = this.$recursiveChildrenSearch({
        name: this.$route.matched.find(
          (matchRoute) => matchRoute.meta.isOnNavbar
        ).name
      })
      this.sidebarItems =
        childrenRoute === null
          ? null
          : childrenRoute
            .filter((child) =>
              child.meta === undefined ? false : child.meta.isOnSidebar
            )
            .map((child) => {
              child.meta.status = false
              return child
            })
    },
    toRouteName(view) {
      if (view === 'QualityConfirmation') {
        this.$router.push({ name: view })
        window.open('http://125.227.151.69/2000R-02/#/signin')
      } else {
        this.$router.push({ name: view })
      }
    }
  }
}
</script>
<style scoped>
.subMenuButton {
  color: rgb(255, 255, 255) !important;
  background-color: #1d325f !important;
  border: none;
  margin-left: 0px;
  font-size: 16px;
  display: inline;
  position: relative;
  box-shadow: 0px;
  width: 100%;
  text-align: left;
  border-radius: 0px;
  font-weight: 600;
  padding: 0.625rem 1.25rem;
}

.choosedsMB {
  color: #192b51 !important;
  background-color: #ffffff !important;
  font-weight: 700;
  border-left: 15px solid #192b51 !important;
  border-radius: 50px 0px 0px 50px;
  font-size: 18px;
  opacity: 1;
}

.notchoosedsMB {
  color: rgb(255, 255, 255) !important;
  font-size: 16px;
}

.subMenuButton:hover {
  color: #ffffff !important;
  background-color: #4673d497 !important;
  font-weight: 700;
  font-size: 18px;
}

.triangle {
  color: rgb(255, 255, 255) !important;
  background-color: #1d325f !important;
  border: none;
  margin-left: 20px;
  display: inline;
}

.triangle:hover {
  color: rgb(255, 255, 255) !important;
  background-color: #2a47868f !important;
  font-size: 15px;
}

.subMenu {
  border: none;
  padding-top: 10px;
  padding-bottom: 15px;
  background-color: #142242 !important;
  color: rgb(255, 255, 255) !important;
  width: 100%;
  text-align: left;
  padding-left: 40px;
  border-radius: 0px;
}

.choosedsM {
  color: rgb(255, 255, 255) !important;
  background-color: #142242 !important;
  border-left: 7px solid #fd5d00 !important;
  font-size: .875rem;
}

.notchoosedsM {
  color: rgb(255, 255, 255) !important;
  font-size: .875rem;
}

.cardbody {
  padding: 0px;
  background-color: #142242 !important;
}

.btn.disabled, .btn:disabled {
  opacity: 1;
}
</style>
