import {createRouter} from 'vue-router';

import BodyDashboard from '../components/BodyDashboard.vue';
import Login from '../components/Login.vue';
import SignUp from '../components/SignUp.vue';
import Aset from '../components/Aset.vue';
import TambahAset from '../components/TambahAset.vue';
import EditAset from '../components/EditAset.vue';
import KategoriAset from '../components/KategoriAset.vue';
import TambahKategoriAset from '../components/TambahKategoriAset.vue';
import EditKategoriAset from '../components/EditKategoriAset.vue';
import MasterProduct from '../components/MasterProduct.vue';
import WarehouseStock from '../components/WarehouseStock.vue';
import Outbound from '../components/Outbound.vue';
import History from '../components/History.vue';
import SalesTransaction from '../components/SalesTransaction.vue';
import TambahSalesTransaction from '../components/TambahSalesTransaction.vue';
import TambahSalesTransaction2 from '../components/TambahSalesTransaction2.vue';
import TambahSalesTransaction3 from '../components/TambahSalesTransaction3.vue';
import DataUser from '../components/DataUser.vue';
import UserRole from '../components/UserRole.vue';
import Teams from '../components/Teams.vue';
import Projects from '../components/Projects.vue';
import Reports from '../components/Reports.vue';
import TambahReports from '../components/TambahReports.vue';
import Clients from '../components/Clients.vue';
import TambahClients from '../components/TambahClients.vue';
import User from '../components/User.vue';
import Karyawan from '../components/Karyawan.vue';
import TambahKaryawan from '../components/TambahKaryawan.vue';
import EditKaryawan from '../components/EditKaryawan.vue';
import { createWebHashHistory, createWebHistory } from 'vue-router';

const routes = [
    {
        name: 'dashboard',
        path: '/',
        component: BodyDashboard
    },
    {
        name: 'login',
        path: '/login',
        component: Login
    },
    {
        name: 'signup',
        path: '/signup',
        component: SignUp
    },
    {
        name: 'aset.index',
        path: '/aset',
        component: Aset
    },
    {
        path: '/tambahaset',
        component: TambahAset
    },
    {
        path: '/editaset/:id',
        component: EditAset,
        props: true
    },
    {
        path: '/kategoriaset',
        component: KategoriAset
    },
    {
        path: '/tambahkategoriaset',
        component: TambahKategoriAset
    },
    {
        path: '/editkategoriaset/:id',
        component: EditKategoriAset,
        props: true
    },
    {
        path: '/masterproduct',
        component: MasterProduct
    },
    {
        path: '/warehousestock',
        component: WarehouseStock
    },
    {
        path: '/outbound',
        component: Outbound
    },
    {
        path: '/history',
        component: History
    },
    {
        path: '/salestransaction',
        component: SalesTransaction
    },
    {
        name: 'tambahsalestransaction',
        path: '/tambahsalestransaction',
        component: TambahSalesTransaction,
        props: true
    },
    {
        name: 'tambahsalestransaction2',
        path: '/tambahsalestransaction2',
        component: TambahSalesTransaction2,
        props: true
    },
    {
        name: 'tambahsalestransaction3',
        path: '/tambahsalestransaction3',
        component: TambahSalesTransaction3,
        props: true
    },
    {
        path: '/datauser',
        component: DataUser,
        isactive : true
    },
    {
        path: '/userrole',
        component: UserRole
    },
    {
        path: '/teams',
        component: Teams
    },
    {
        path: '/projects',
        component: Projects
    },
    {
        path: '/reports',
        component: Reports
    },
    {
        path: '/reports/tambahreports',
        component: TambahReports
    },
    {
        path: '/clients',
        component: Clients
    },
    {
        path: '/clients/tambahclients',
        component: TambahClients
    },
    {
        path: '/user',
        component: User
    },
    {
        path: '/karyawan',
        component: Karyawan
    },
    {
        path: '/tambahkaryawan',
        component: TambahKaryawan
    },
    {
        path: '/editkaryawan/:id',
        component: EditKaryawan,
        props: true
    },
];

const router = createRouter ({
    history: createWebHistory(),
    routes
});

// const akses = true;

router.beforeEach((to, from, next) => {
    // console.log(to, from);
    // next();

    const isAuthenticated = JSON.parse(sessionStorage.getItem('authenticated'));
    if (to.name !== 'login' && to.name !== 'signup' && !isAuthenticated) next({ path: '/login' });
    // else if (to.name !== 'login' && !isAuthenticated) next({ path: '/' });
    else next();
});

export default router;