<template>
  <div class="ng-scope">
    <navbar :user="userInfo" @openLoginDialog="openLoginDialog"></navbar>
    <div class="banner-box">
      <div class="p-wrap-full clear pt30">
        <div class="training-category opacity-bg fl ">
          <img alt="" class="mimg" src="/static/swiper/1.jpg"/>
        </div>

        <div class="m-login opacity-bg fr">
          <is-login v-if="loginOk" :user="userInfo" @logout="logout"></is-login>
          <un-login v-else @login="login" :err="err"></un-login>
        </div>
      </div>
    </div>

   <IPlanList></IPlanList>

    <!-- <learn-time></learn-time> -->
    <div class="p-wrap-full mb20 ng-scope">
      <div class="title-box mt5 clear">
        <h2 class="title">课程分类</h2>
      </div>
      <el-row :gutter="20" class="shadow p10 mt20" >
        <el-col :span="+item.span" class="nopad"  v-for="(item, index) in typeList" :key="index">
          <el-card shadow="hover" class="grid-content nopad "  >
            <div class="mt10 typelist" @click="gotoCourse(item)">
              <i class="p-ico"  :class="item.icon" style="color:#69c0ff "></i>
              <p style="font-size: 13px;">{{ item.name }}</p>
              <p class="mt10" style="font-size: 16px;" v-if="!item.isOpen">(暂未开放)</p>
              <p class="mt10" style="font-size: 16px;" v-else>{{ item.content }}</p>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <div class="p-wrap-full ng-scope">
      <div class="title-box mt5 clear">
        <h2 class="title">友情链接</h2>
      </div>
      <friendlink></friendlink>
    </div>

    <el-dialog
      class="fc tc login-dialog"
      :visible.sync="loginDialog"
       :show-close='false'
      width="330px"
      style="padding:0px;"
      @close="closeDialog"
    >
      <div class="m-login" style="background: none;">
        <un-login @login="login" :err="err"></un-login>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import isLogin from "./components/isLogin.vue";
import unLogin from "./components/unLogin.vue";
import LearnTime from "./components/learnTime/learnTime.vue";
import navbar from "@/components/nav/navbar.vue";
import Friendlink from "./components/friendlink/friendlink.vue";
import IPlanList from "./components/planList/IPlanList.vue";
import { httpServer } from "@/components/httpServer/httpServer.js";
import { mapGetters,mapActions } from "vuex";

export default {
  components: {
    LearnTime,
    isLogin,
    unLogin,
    Friendlink,
    IPlanList,
    navbar,
  },
  data() {
    return {
      loginOk: false,
      periodObj: {
        userName: "",
        idNum: "",
      },
      loginDialog: false,
      err: {},
    };
  },
  computed: {
  	...mapGetters("user", ["typeList", "userInfo", "planList"])
  },
  created(){
    let agentId = this.$route.query._c||0;
    console.log("setAgentId", agentId);
    this.setAgentId( agentId );
  },
  beforeMount() {
    if( localStorage.token ){
      this.loadBaseInfo().then( res=>{
        if( res.token ){
          this.loginOk= true;
        }
      })
    }else{
      this.loginOk = false;
    }

  },
  methods: {
    ...mapActions("user", ["loadBaseInfo", "doLogout", "doLogin", "setAgentId"]),
    logout() {
      this.doLogout().then( res=>{
        this.loginOk = false;
      } )
    },
    login(param, cb) {
      this.doLogin( param ).then( res => {
        if( !res ){
          this.err.loginErrorEnter = "登入失败"
          this.loginOk = false;
          cb &&cb()
        }else{
          this.loginOk = true;
          this.loginDialog = false;
        }
      })
    },
    gotoCourse(item){
      if ( !localStorage.token ) {
        this.$message.successMsg("请先登入", 1)
        return
      }
      if( !item.isOpen ){
         this.$message.errorMsg("暂未开放", 1)
         return
      }
      this.$router.push( {path:'/center/market', query:{type: item.name}})
    },
    closeDialog() {
      this.loginDialog = false;
    },
    openLoginDialog() {
      this.loginDialog = true
    },
  },
};
</script>

<style>
@import url("login.css");
</style>
