import {createRouter, createWebHistory, useRoute} from 'vue-router';
import CategoriesList from '@/components/settings/categories/CategoriesList.vue';
import CreateCategoryModal from "@/components/settings/categories/CreateCategoryModal.vue";
import CategoryListItem from "@/components/settings/categories/CategoryListItem.vue"; // Импортируем новый компонент
import BudgetList from "../components/budgets/BudgetList.vue";
import BudgetView from "@/components/budgets/BudgetView.vue";
import SettingsView from "@/components/settings/SettingsView.vue";
import RecurrentList from "@/components/settings/recurrent/RecurrentList.vue";
import TransactionList from "@/components/transactions/TransactionList.vue";
import LoginView from "@/components/auth/LoginView.vue";

const routes = [

    {path: '/', name: 'Budgets main', component: BudgetList, meta: { requiresAuth: true }},
    { path: '/login', component: LoginView },
    {path: '/budgets', name: 'Budgets', component: BudgetList, meta: { requiresAuth: true }},
    {path: '/budgets/:id', name: 'BudgetView', component: BudgetView, meta: { requiresAuth: true }},
    {path: '/transactions/:mode*', name: 'Transaction List', component: TransactionList, meta: { requiresAuth: true }},
    // {path: '/transactions/create', name: 'Transaction List', component: TransactionList},
    {path: '/settings/', name: 'Settings', component: SettingsView, meta: { requiresAuth: true }},
    {path: '/settings/categories', name: 'Categories', component: CategoriesList, meta: { requiresAuth: true }},
    {path: '/settings/recurrents', name: 'Recurrent operations list', component: RecurrentList, meta: { requiresAuth: true }},
    {path: '/settings/categories/create', name: "Categories Creation", component: CreateCategoryModal, meta: { requiresAuth: true }},// Добавляем новый маршрут
    {path: '/settings/categories/one', name: "Categories Creation", component: CategoryListItem, meta: { requiresAuth: true }}// Добавляем новый маршрут
];

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

router.beforeEach((to, from, next) => {
    const token = localStorage.getItem('token');
    if (to.meta.requiresAuth && !token) {
        const router= useRoute()
        console.log(to)
        console.log(router.path)
        console.log(router.params)
        next('/login?back='+to.fullPath);
    } else {
        next();
    }
});
export default router;
