import { createRouter, createWebHistory } from 'vue-router'
import Cookies from 'js-cookie'

import CommonLayout from '@/layouts/Common.vue' 
import UserLayout from '@/layouts/User.vue' 
import checkLogin from '@/pages/Login/checkLogin'


let history = createWebHistory('/m')

let routes = [
  {
    path: '/',
    name: 'Common',
    component: CommonLayout,
    redirect: '/home',
    children: [
      {
        path: '/home',
        name: 'Home',
        component: ()=> import("../pages/Home/Index.vue")
      },
      {
        path: '/intro',
        name: 'Intro',
        component: ()=> import("../pages/Intro/Index.vue")
      },
      {
        path: '/article',
        name: 'Article',
        component: ()=> import("../pages/Article/Index.vue")
      },
      {
        path: '/card',
        name: 'Card',
        component: ()=> import("../pages/Card/Index.vue")
      },
      {
        path: '/category',
        name: 'Category',
        component: ()=> import("../pages/Category/Index.vue")
      },
      {
        path: '/login',
        name: 'Login',
        component: ()=> import("../pages/Login/Index.vue"),
      },
      {
        path: '/linelogin',
        name: 'LineLogin',
        component: ()=> import("../pages/Login/LineLogin.vue")
      },
      {
        path: '/linereg',
        name: 'LineReg',
        component: ()=> import("../pages/Login/LineReg.vue")
      },
      {
        path: '/register',
        name: 'Register',
        component: ()=> import("../pages/Register/Index.vue")
      },
      {
        path: '/payment',
        name: 'Payment',
        component: ()=> import("../pages/Cart/Payment.vue"),
      },
      {
        path: '/cartfinish',
        name: 'CartFinish',
        component: ()=> import("../pages/Cart/CartFinish.vue")
      },
      {
        path: '/404',
        name: '404',
        component: ()=> import("../pages/Error/404.vue")
      },
      {
        path: '/user/info',
        name: 'UserInfo',
        meta: {isAuth: true},
        component: ()=> import("../pages/My/Index.vue")
      }
    ]
  },{
    path: '/user',
    name: 'User',
    component: UserLayout,
    redirect: '/user/info',
    children: [
      {
        path: 'order',
        name: 'UserOrder',
        meta: {isAuth: true},
        component: ()=> import("../pages/My/Order/Index.vue")
      },
      {
        path: 'account',
        name: 'UserAccount',
        meta: {isAuth: true},
        component: ()=> import("../pages/My/Account/Index.vue")
      },
      {
        path: 'share',
        name: 'UserShare',
        meta: {isAuth: true},
        component: ()=> import("../pages/My/Share/Index.vue")
      },
      {
        path: 'share/recommand',
        name: 'UserShareRecommand',
        meta: {isAuth: true},
        component: ()=> import("../pages/My/Share/recommand.vue")
      },
      {
        path: 'setting',
        name: 'UserSetting',
        meta: {isAuth: true},
        component: ()=> import("../pages/My/Setting/Index.vue")
      },
    ]  
  },
  {
    path: '/goods/:id',
    name: 'Goods',
    component: ()=> import("../pages/Goods/Index.vue")
  },
  {
    path: '/cart',
    name: 'Cart',
    component: ()=> import("../pages/Cart/Index.vue")
  },
  {
    path: '/checkout',
    name: 'Checkout',
    component: ()=> import("../pages/Cart/Checkout.vue")
  },
  {
    path: '/order/shipping',
    name: 'Shipping',
    component: ()=> import("../pages/Cart/Shipping.vue"),
  },
  {
    path: '/address',
    name: 'Address',
    component: ()=> import("../pages/Address/Index.vue")
  },
  {
    path: '/user/bank',
    name: 'UserBank',
    component: ()=> import("../pages/My/Bank/Index.vue")
  },
]

const router = createRouter({ 
  history, 
  routes,
  scrollBehavior(to, from, savedPosition) {
    return { top: 0 };
  }
 })

router.beforeEach((to, from, next)=>{
  if( to.meta.isAuth === true && !Cookies.get('token')){
    return next({name: 'Login'})
  }
  return next()
})

export default router