<template>
  <div class="smartFarm-left-menu-warp">
    <div class="left-menu">
      <div v-for="(n, index) in leftMenu" :key="index" class="left-menu-item" style="position: relative">
        <div style="display: flex; justify-content: flex-start; align-items: center" @click.stop="toLink(index)">
          <div class="item-img">
            <img :src="getAssetsFile('images/smartFarm/' + n.icon)?.href ?? ''" alt="" />
          </div>
          <span :class="n.isOpen ? 'active' : ''" class="item-title">{{ n.title }}</span>
          <img
            v-if="n.children && n.isOpen"
            alt=""
            :src="getAssetsFile('images/smartFarm/closing.png')"
            class="isOpen"
            @click.stop="openList(index)"
          />
          <img
            v-if="n.children && !n.isOpen"
            alt=""
            :src="getAssetsFile('images/smartFarm/down_1@2x.png')"
            class="isOpen fz"
            @click.stop="openList(index)"
          />
        </div>
        <div v-if="n.children && n.isOpen" class="item-children">
          <div v-for="(item, indexC) in n.children" :key="indexC">
            <ul style="list-style-type: disc !important; padding-left: 40px; text-align: left; overflow: visible">
              <li :class="indexC === currentCIndex ? 'active' : ''" @click.stop="toLinkSub(index, indexC)">
                <div class="dot"></div>
                {{ item.title }}
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup>
import { ref, reactive, onMounted, watch } from 'vue';
import { isEmpty, getAssetsFile } from '@/utils';
import { useRoute, useRouter } from 'vue-router';
const route = useRoute();
const router = useRouter();

// const props = defineProps({
//   currentName: { type: String, default: 'dashboard' },
// });

const leftMenu = reactive([
  {
    name: 'supplier',
    title: '农业环境监测',
    icon: 'menu1.png',
    path: '/sub-operation-service/smartFarm/main',
    isOpen: true,
    children: [
      {
        name: 'supplier',
        title: '田间监测',
        icon: 'menu1.png',
        path: '/sub-operation-service/smartFarm/fieldInspection',
      },
      {
        name: 'supplier',
        title: '水质监测',
        icon: 'menu1.png',
        path: '/sub-operation-service/ecommerce-supplier',
      },
      {
        name: 'supplier',
        title: '病虫害监测',
        icon: 'menu1.png',
        path: '/sub-operation-service/ecommerce-supplier',
      },
    ],
  },
  {
    name: 'purchaser',
    title: '生产管理控制',
    icon: 'menu3.png',
    path: '/sub-operation-service/ecommerce-purchaser',
    isOpen: false,
    children: [],
  },
]);

let currentIndex = ref(0);
let currentCIndex = ref(-1);

// watch(
//   () => props.currentName,
//   () => {
//     console.info('currentName', props.currentName);
//     currentIndex.value = leftMenu.findIndex((m) => {
//       return m.name === props.currentName;
//     });
//   },
//   { deep: true, immediate: true }
// );

const toLink = (index) => {
  currentIndex.value = index;
  currentCIndex.value = -1;
  let path = index !== undefined ? leftMenu[index].path : null;
  if (path) {
    router.push(path);
  }
};
const toLinkSub = (index, c) => {
  currentCIndex.value = c;
  let path = leftMenu[index].children[c].path;
  if (path) {
    router.push(path);
  }
};
const openList = (index) => {
  currentIndex.value = index;
  leftMenu[index].isOpen = !leftMenu[index].isOpen;
};
</script>
<style lang="scss" scoped>
.fz {
  transform: rotate(180deg);
}
.isOpen {
  position: absolute;
  right: -24px;
  height: 20px;
  width: 20px;
}
.active {
  color: $color-main;
}
.smartFarm-left-menu-warp {
  width: 100%;
  height: 100%;
  padding: 0 30px;
  .left-menu {
    .left-menu-item {
      width: 100%;
      padding: 16px 0;
      cursor: pointer;
      &.active {
        color: $color-main;
      }
      .item-img,
      .item-title {
        vertical-align: middle;
      }
      .item-img {
        display: inline-block;
        width: 32px;
        height: 32px;
      }
      .item-title {
        font-size: 18px;
        font-weight: 400;
        padding-left: 8px;
      }
      .item-children {
        text-align: center;
        font-size: 16px;
        margin-top: 8px;
        transition: transform 0.3s ease;
        .dot {
          display: inline-block;
          height: 4px;
          margin-right: 15px;
          width: 4px;
          border-radius: 90px;
          background-color: black;
          vertical-align: middle;
        }
        li {
          margin: 5px auto;
          height: 35px;
          line-height: 35px;
        }
      }
    }
  }
}
</style>
