<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="overflow: visible; padding-left: 40px; text-align: left; list-style-type: disc !important">
              <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/smartFarm/waterInspection',
      },
      {
        name: 'supplier',
        title: '病虫害监测',
        icon: 'menu1.png',
        path: '/sub-operation-service/smartFarm/bugInspection',
      },
    ],
  },
  {
    name: 'control',
    title: '生产管理控制',
    icon: 'menu3.png',
    path: '/sub-operation-service/smartFarm/main',
    isOpen: false,
    children: [
      {
        name: 'control',
        title: '一体育苗',
        icon: 'menu3.png',
        path: '/sub-operation-service/smartFarm/growSeedlings',
      },
    ],
  },
]);

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) => {
  console.info('index', index);
  console.info('c', c);
  currentCIndex.value = c;
  let path = leftMenu[index].children[c].path;
  if (path) {
    console.info('path', 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;
  width: 20px;
  height: 20px;
}
.active {
  color: $color-main;
}
.smartFarm-left-menu-warp {
  padding: 0 30px;
  width: 100%;
  height: 100%;
  .left-menu {
    .left-menu-item {
      padding: 16px 0;
      width: 100%;
      cursor: pointer;
      &.active {
        color: $color-main;
      }
      .item-img,
      .item-title {
        vertical-align: middle;
      }
      .item-img {
        display: inline-block;
        width: 32px;
        height: 32px;
      }
      .item-title {
        padding-left: 8px;
        font-size: 18px;
        font-weight: 400;
      }
      .item-children {
        margin-top: 8px;
        font-size: 16px;
        text-align: center;
        transition: transform 0.3s ease;
        .dot {
          display: inline-block;
          margin-right: 15px;
          width: 4px;
          height: 4px;
          border-radius: 90px;
          background-color: black;
          vertical-align: middle;
        }
        li {
          margin: 5px auto;
          height: 35px;
          line-height: 35px;
        }
      }
    }
  }
}
</style>
