<template>
  <a-row type="flex" :gutter="20">
    <a-col flex="200px">
      <a @click="backFeama" class="logo">
        <img alt="logo" src="../assets/logo-f3.png" />
        <!--        <img alt="logo" src="../assets/nio/logo-1.jpg" />-->
      </a>
    </a-col>
    <a-col :xxl="18" :xl="16" :lg="16" :md="12" :sm="10" :xs="2">
      <a-menu v-model="current" mode="horizontal" @select="goMenu" theme='light'>
        <template v-for="menu in menuList">
          <a-menu-item :path="menu.path" :key="menu.path.split('?')[0]" v-if="!menu.children">
            <span :id="menu.path+'MenuBarBtn'">{{ $t('' + menu.name + '') }}</span>
          </a-menu-item>
          <a-sub-menu :path="menu.path" :key="menu.path.split('?')[0]" v-else>
            <span slot="title" class="submenu-title-wrapper">
              {{ menu.name }}
            </span>
            <a-menu-item v-for="mm in menu.children" :path="menu.path" :key="mm.path.split('?')[0]">
              {{ mm.name }}
            </a-menu-item>
          </a-sub-menu>
        </template>
      </a-menu>
    </a-col>
    <a-col class="headerIcon" flex="auto">
      <a>
        <HelpDocDrawer></HelpDocDrawer>
      </a>
      <a style="margin-top:2px" href="javascript:void(0);">
        <GlobalMenu></GlobalMenu>
      </a>
      <a @click="backFeama">
        <i style="font-size:20px" class="iconfont iconhome03"></i>
      </a>
    </a-col>
  </a-row>
</template>

<script>

import utils from '@/utils/urlParamUtil';
import GlobalMenu from './GlobalMenu';
import HelpDocDrawer from './HelpDocDrawer';

export default {
  name: "",
  components:{
    GlobalMenu,
    HelpDocDrawer
  },
  data() {
    return {
      current: [],
      menuList: [],
      // menus: [
      //   {
      //     path: "/project",
      //     name: "规划",
      //   },
      //   {
      //     path: "/structure",
      //     name: "结构分析",
      //   },
      //   {
      //     path: "/survey",
      //     name: "调研",
      //   },
      //   {
      //     path: "/function2",
      //     name: "功能分析",
      //   },
      //   {
      //     path: "/risk",
      //     name: "风险分析",
      //   },
      //   {
      //     path: "/measures",
      //     name: "措施追踪",
      //   },
      //   {
      //     path: "/fmea",
      //     name: "总结归档",
      //   }
      // ],
      userInfo:{},
      helpFlag:0,
    };
  },
  mounted() {
    this.current = [this.$route.path];
    this.helpDot();
  },
  watch: {
    $route({ path }) {
	  console.log(path);
      this.current = [path];
      this.initMenu();
    },
  },
  methods: {
    backFeama() {
      localStorage.removeItem("historyData")
      localStorage.removeItem("historyText")
      let back = utils.getUrlKey('back')
      let backUrl = this.$api.fmUrl + '/#' + back
      window.open(backUrl, '_top')
      /*let back = utils.getUrlKey('back');
      let rfmeaId = this.getUrlParam("rfmeaId");
      let fmeaId = this.getUrlParam("fmeaId");
      let backUrl = this.$api.fmUrl + '/#' + back;
      if (back.indexOf('list') > -1 || back.indexOf('List') > -1) {
          window.open(backUrl, "_top");
      } else {
          if (back.indexOf('list') == -1 && back.indexOf('?') == -1) {
              backUrl += '?fmeaId=' + fmeaId + '&rfmeaId=' + rfmeaId;
          } else if (back.indexOf('?') == -1) {
              backUrl += '?fmeaId=' + fmeaId + '&id=' + fmeaId;
          }
          backUrl += '&rfmeaId=' + rfmeaId;
          window.open(backUrl, "_top");
      }*/
    },
    helpDot(){
      // let u = localStorage.getItem("userInfo");
      // this.userInfo = JSON.parse(u);
      // if(this.userInfo.helpFlag){
      //   this.helpFlag = 1;
      // }else{
      //   this.helpFlag = 0;
      // }
    },
    helpPage() {
      this.userInfo.helpFlag = false;
      this.helpFlag = 0;
      localStorage.setItem("userInfo",JSON.stringify(this.userInfo));
      var url = this.$api.fmUrl + "/#/setmanager/help/index"+ '?ntimet=' + new Date().getTime();
      window.open(url, "_top");
    },
    testPage(b){
      var fmeaId = this.getUrlParam("fmeaId");
      var deriveId = this.getUrlParam("deriveId");
      var _from = this.getUrlParam("from");
      var historyText = this.$store.state.historyText;
      this.$router.push(
        "/structureTest" + "?fmeaId=" + fmeaId + "&deriveId=" + deriveId + "&from=" + _from + "&historyText=" + historyText
      );
    },
    goMenu(b) {
      var fmeaId = this.getUrlParam("fmeaId");
      var deriveId = this.getUrlParam("deriveId");
      var rfmeaId = this.getUrlParam("rfmeaId");
      var _from = this.getUrlParam("from");
      var enterState = this.getUrlParam("enterState");
      var historyText = this.$store.state.historyText;
      console.log(b)
      let backUrl = utils.getUrlKey('back');
      this.$router.push(
        b.key + "?fmeaId=" + fmeaId+
          "&rfmeaId=" + rfmeaId  +
          "&deriveId=" + deriveId +
          "&from=" + _from +
          "&back=" + backUrl +
          "&enterState=" + enterState +
          "&historyText=" + historyText+
          '&ntimet=' + new Date().getTime()
      );
      //  + "&history=" + history
    },
    // getFmeaId() {
    //   var fmeaId = this.getUrlParam("fmeaId");
    //   var deriveId = this.getUrlParam("deriveId");
    //   var _from = this.getUrlParam("from");
    //   var historyText = this.$store.state.historyText;
    //   if (!fmeaId) {
    //     this.$notification.error({
    //       message: "错误",
    //       description: `缺少编号`,
    //       duration: 2,
    //     });
    //   }
    //
    //   this.menus.forEach((m, idx) => {
    //     if (this.menus[idx].path.indexOf("?fmeaId=") == -1) {
    //       if (deriveId) {
    //         this.menus[idx].path =
    //           this.menus[idx].path +
    //           "?from=" +
    //           _from +
    //           "&fmeaId=" +
    //           fmeaId +
    //           "&deriveId=" +
    //           deriveId + "&historyText=" + historyText;
    //       } else {
    //         this.menus[idx].path =
    //           this.menus[idx].path + "?from=" + _from + "&fmeaId=" + fmeaId + "&historyText=" + historyText;
    //       }
    //     }
    //   });
    // },
    setApiUrl() {
      this.setCookie("dev", -1);
      var apiUrl = this.getCookie("apiUrl");
      if (!apiUrl) {
        this.setCookie("apiUrl", this.$api.baseUrl);
      }
    },
    getUrlParam(paraName) {
      var url = document.location.toString();
      var arrObj = url.split("?");
      if (arrObj.length > 1) {
        var arrPara = arrObj[1].split("&");
        var arr;
        for (var i = 0; i < arrPara.length; i++) {
          arr = arrPara[i].split("=");
          if (arr != null && arr[0] == paraName) {
            return arr[1];
          }
        }
        return "";
      } else {
        return "";
      }
    },
    setCookie(name, value) {
      if (value) {
        var days = 1; //定义一天
        var exp = new Date();
        exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
        // 写入Cookie, toGMTString将时间转换成字符串
        document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString;
      }
    },
    getCookie(cookie_name) {
      var allcookies = document.cookie;
      //索引长度，开始索引的位置
      var cookie_pos = allcookies.indexOf(cookie_name);

      // 如果找到了索引，就代表cookie存在,否则不存在
      if (cookie_pos != -1) {
        // 把cookie_pos放在值的开始，只要给值加1即可
        //计算取cookie值得开始索引，加的1为“=”
        cookie_pos = cookie_pos + cookie_name.length + 1;
        //计算取cookie值得结束索引
        var cookie_end = allcookies.indexOf(";", cookie_pos);

        if (cookie_end == -1) {
          cookie_end = allcookies.length;
        }
        //得到想要的cookie的值
        var value = unescape(allcookies.substring(cookie_pos, cookie_end));
      }
      return value;
    },

    // 根据src地址判定是历史版本进入还是其他方式进入
    initMenu() {
      let project = null;
      // fmeaFlag-判断是从项目DFMEA跳转回来还是家族DFMEA跳转回来
      let fmeaFlag = this.getUrlParam("from");
      if (this.current[0] === '/project/Master' || fmeaFlag === 'master') {
        // 家族DFMEA规划页面
        project = {
          path: "/project/Master",
          name: "header.plan",
        }
      } else {
        // 项目DFMEA规划页面
        project = {
          path: "/project",
          name: 'header.plan',
        }
      }
       this.menuList = [
         // 规划页面
           project,
          {
            path: "/structure",
            name: "header.structural-analysis",
          },
          // {
          //   path: "/survey",
          //   name: "header.research",
          // },
          {
            path: "/function2",
            name: "header.functional-analysis",
          },
          {
            path: "/risk",
            name: "header.risk-analysis",
          },
          {
            path: "/measureTrack",
            name: "header.measure-track",
          },
          {
            path: "/summaryArchive",
            name: "header.summary-archive",
          },
        ]
    }
  },
  created() {
    // this.getFmeaId();
    this.setApiUrl();
  },
};
</script>

<style lang="less" scoped>
// .helpIcon {
//   font-size: 25px;
//   margin-left: 10px;
//   margin-top: 2px;
//   float: right;
//   color: #1890ff
// }
.headerIcon{
  color: #1890FF;
  a{
    margin-left: 10px;
    float:right;
    i{
      font-size: 25px;
    }
  }
}
.logo {
  height: 200px;
  margin: 10px 0;
  //display: inline-block;
  vertical-align: top;

  img {
    height: 60px;
    display: block;
  }
}

.ant-menu-horizontal {
  border-color: transparent;
  margin: 8px 0;
  margin-left: 20px;
  font-size: 16px;
}

.ant-menu-horizontal > .ant-menu-item:hover,
.ant-menu-horizontal > .ant-menu-submenu:hover,
.ant-menu-horizontal > .ant-menu-item-active,
.ant-menu-horizontal > .ant-menu-submenu-active,
.ant-menu-horizontal > .ant-menu-item-open,
.ant-menu-horizontal > .ant-menu-submenu-open,
.ant-menu-horizontal > .ant-menu-item-selected,
.ant-menu-horizontal > .ant-menu-submenu-selected {
  border-bottom-color: transparent;
}
</style>
