<template>
  <div class="dropMap drop-default">
    <div class="drop-menus">
      <a  v-for="item in menus"
          :key="item.typeKey"
          :class="`menu ${nowMenuKey===item.typeKey?'now':''}`"
          :href="pHref+item.href"
          @mouseenter="toMenuHandle(item)"
      >
        <span >{{lang===langType.cn?item.text:getAbbrText(item.text)}}</span>
      </a>
    </div>
    <div class="drop-view noScroll">
      <div
        v-for="(item,i) in showMenuItems"
        :class="`sub-item ${nowSubMenuId===item.id?'now-sub':''}`"
        :key=" `${nowMenuKey}-${item.id}-${i}`"
        @mouseenter="subItemHandle(item.id)"
        @click="subItemClickHandle(item)"
      >
        <div class="con">
          <div class="img-box">
            <img :src="'/public/'+item.image" :alt="item.name"  class="img"/>
          </div>
          <div class="text-box">
            <div class="name">{{item.name}}</div>
            <div class="remark">{{item.remark}}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import langMap from "~/map/langMap";
import handle from "~/until/handle";
const productMenus = [
  {
    text: "M2M产品",
    typeKey: "m2m",
    href: '/m2m'
  },
  {
    text: "AI摄像头",
    typeKey: "aiCam",
    href: '/aiCam'
  },
  {
    text: "智能终端",
    typeKey: "sm",
    href: '/sm'
  },
  {
    text: "4G低功耗摄像头",
    typeKey: "low",
    href: '/low'
  },
  {
    text: "自动变焦双目协同摄像头",
    typeKey: "cam",
    href: '/cam'
  }
];
/**
 * 解决方案
 * @type {[{typeKey: string, text: string}, {typeKey: string, text: string}]}
 */
const solutionMenus = [
  {
    text: "解决方案",
    typeKey: "sol",
    href: '/sol'
  },
  {
    text: "行业应用",
    typeKey: "acs",
    href: '/acs'
  },
  {
    text: "电力案例",
    typeKey: "epower",
    href: '/epower'
  },
];
export default {
  name: "menuDrop",
  props:{
    lang:{
      default: langMap.lang.cn
    },
    type:{default:'product'},
    pHref: {default:''}
  },
  data(){
    return {
      langType: langMap.lang,
      menus: [
      ],
      menuSubItems: {},
      showMenuItems: [],
      nowMenuKey: "",
      nowSubMenuId: ""
    }
  },
  beforeMount() {
    this.loadMenus();
  },
  methods:{
    getLangText(str){
      return langMap.getText(this.lang,str);
    },
    getAbbrText(str){
      return langMap.getAbbrText(this.lang,str);
    },
    loadMenus(){
      if(this.type === "solution"){
        console.log("solution data")
        this.menus = solutionMenus;
      }else{
        this.menus = productMenus;
      }
    },
    subItemHandle(id){
      this.nowSubMenuId = id;
    },
    toMenuHandle(item){
      this.nowMenuKey = item.typeKey;
      this.loadMenuItems();
    },
    async loadMenuItems(){
      if(this.menuSubItems[this.nowMenuKey]){
        console.log('load catch');
        this.showMenuItems = this.menuSubItems[this.nowMenuKey];
        return null;
      }
      let url = '';
      if(this.type === "solution"){
        url = 'api/loadSolution.php'
      }else{
        url = 'api/loadProduct.php'
      }
      url+=`?key=${this.nowMenuKey}&p=1&l=10`
      // 请求服务端接口
      let [err,result] = await handle(this.$axios.get(url));
      if(err){
        console.log(err);
        return;
      }
      let res = result.data;
      console.log(res)
      if(res.rcode === 1){
        this.menuSubItems[this.nowMenuKey] = res.data;
        this.showMenuItems = this.menuSubItems[this.nowMenuKey];
        this.nowSubMenuId = this.showMenuItems[0].id;
        console.log(this.nowSubMenuId);
        // console.log(this.menuSubItems);
      }else{
        console.error(res);
        console.log(res.msg);
      }
    },
    subItemClickHandle(item){
      if(item.sourceType == "1"){
        // 尝试加载特定静态页面
        window.location.href = item.source
      }else{
        window.location.href = `${this.pHref}/item/${this.nowMenuKey}?id=${item.id}`
      }
    }
  }
}
</script>

<style scoped>

</style>
