<template>
  <div id="app">
    <HeaderBanner></HeaderBanner>
    <Navigation :navTitle='navTitle'></Navigation>
    <div class="app_content">
      <div class="router">
        <LoginInfo
        :userAccount='userAccount'
        ></LoginInfo>
        <Routerbanner
          @change_nav="changeNav"
          :isManufacturer="isManufacturer"
        />
      </div>
      <div class="router_view">
        <router-view
          :isManufacturer="isManufacturer"
          @change_nav="changeNav"
        ></router-view>
      </div>
    </div>
    <FooterBanner/>
  </div>
</template>

<script>
import HeaderBanner from "./components/HeaderBanner";
import Navigation from "./components/Navigation";
import LoginInfo from "./components/LoginInfo";
import Routerbanner from "./components/RouterBanner";
import FooterBanner from "./components/Footerbanner";
export default {
  components: {
    HeaderBanner,
    Navigation,
    LoginInfo,
    Routerbanner,
    FooterBanner
  },
  data() {
    return {
      navTitle: [
        {
          title: '数据管理',
          url: '/uploadInfor'
        }
      ],
      isManufacturer: 'distributor', // distributor 经销商 manufacturer 厂商
      userAccount: '',
      userToken:''
    }
  },
  methods: {
    // 点击路由，导航栏对应变化
    changeNav: function(navTitle){
      console.log(navTitle, 'ti')
      this.navTitle = navTitle
    },
    getTokenRequest(){
      this.$http({
        method: 'post',
        url: '/auth/checkSign',
        data:this.userToken
      }).then((res) => {
        console.log(res);
        if(res.data.code === 200) {
          let userInfo = res.data.data;
          this.userAccount = userInfo.userCode;
          localStorage.setItem('userOutId',userInfo.outId);
          localStorage.setItem("userId", userInfo.agentId);
          sessionStorage.setItem("userNameAccount", userInfo.userCode);
          console.log(userInfo.agentId)
          if (!userInfo.agentId) {
            this.isManufacturer = 'manufacturer';
            this.$router.replace({ path: "/uploadInfor" });
          } else {
            if (userInfo.userRole == '1' || userInfo.userRole == '2') {
              this.isManufacturer = 'distributor';
              if(this.$route.path !== '/inforList') this.$router.replace({ path: "/inforList" });
            } else {
              this.isManufacturer = 'manufacturer';
              if(this.$route.path !== '/uploadInfor') this.$router.replace({ path: "/uploadInfor" });
            }
          }
          sessionStorage.setItem('userPath',this.isManufacturer)
        }else {
          let message = res.data.message;
          console.log('message', message);
          alert(message);
        }
      }).catch((err) => {
        alert('无法校验的字符串');
        console.log(err);
      })
    },

    // 测试接口
    getData() {
      // 正常token
      // dXNlcklkPTEwMCZhY2NvdW50PUwwUDAxX01NJmZyb209c2l3ZWkmdGltZXN0YW1wPTE2MTkwNjI1OTcmc2lnbj0zNTJiMmE3MDc4OTlhNDY0ZmY0Nzc1M2IxMzU3NTg0Ng== //测试经销商TOKEN
      // dXNlcklkPTE1NiZhY2NvdW50PUwwTTE0X01NJmZyb209c2l3ZWkmdGltZXN0YW1wPTE2MTg1NTYzMDAmc2lnbj1lNDZmM2Q3MmFiZDI3ODYwMDYyMmY2N2NmZjFiNjZmNw== //测试厂商TOKEN
      this.userToken = sessionStorage.getItem('setToken');
      if(this.userToken){
        console.log('已登录',this.userToken,this.$route.path,sessionStorage.getItem('userNameAccount'));
        this.userAccount = sessionStorage.getItem('userNameAccount');
        if(sessionStorage.getItem('userPath') === 'distributor'){
          this.isManufacturer = 'distributor';
          if(this.$route.path !== '/inforList') this.$router.replace({ path: "/inforList" });
        }else if (sessionStorage.getItem('userPath') === 'manufacturer') {
          this.isManufacturer = 'manufacturer';
          if(this.$route.path !== '/uploadInfor') this.$router.replace({ path: "/uploadInfor" });
        }else {
          console.log(this.isManufacturer)
        }
      }else {
        sessionStorage.clear();
        localStorage.clear();
        let query = this.$route.query || {};
        sessionStorage.setItem('setToken',query.token);
        this.userToken = query.token;
        console.log('token:', this.userToken);
        this.getTokenRequest();
      }
    }
  },
  mounted() {
  },
  created() {
    this.getData();
    console.log(process.env.NODE_ENV);
  }
};
</script>
<style lang="less">
* {
  padding: 0;
  margin: 0;
  list-style: none;
  color: #333;
  font-size: 12px;
}
button{
  border: none;
  background-color: #0056A0;
  height: 30px;
  width: 58px;
  color: #fff;
  font-weight: 400px;
  font-family: Arial;
  margin-left: 18px;
  font-size: 14px;
  &:hover{
    cursor: pointer;
  }
}
.current_button{
  background-color: #0056A0;
  height: 30px;
  width: 58px;
  line-height: 30px;
  text-align: center;
  color: #fff;
  font-weight: 400px;
  font-family: Arial;
  margin-left: 18px;
  font-size: 14px;
  &:hover{
    cursor: pointer;
  }
}
#app {
  background-color: #eeeeee;
  height: 100%;
  min-height: 100vh;
  .app_content {
    width: 1190px;
    margin: 0 auto;
    font-size: 12px;
    color: #333;
    font-family: "微软雅黑";
    display: flex;
    justify-content: space-between;
    .router{
      width: 150px;
      height: 100%;
      background-color: #d4d4d4;
      height: auto;
      min-height: 50vh;
    }
    .router_view{
      width: 1030px;
    }
  }
}
@media screen and (max-width: 1190px) {
  #app{
    width: 1190px;
  }
}
</style>
