@using WebVentaCoche.Helpers
@using WebVentaCoche.Models
@using System.Security.Claims
@inject IHttpContextAccessor HttpContextAccessor

@{
    var isGestionPage = Context.Request.Path.ToString().StartsWith("/Products");

    var cartItemIds = CartSessionHelper.GetCartItems(HttpContextAccessor.HttpContext.Session);
    int cartItemCount = cartItemIds.Count;

    var user = HttpContextAccessor.HttpContext.User;
    var userId = user.FindFirstValue(ClaimTypes.NameIdentifier);
}

<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - WebVentaCoche</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" />
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" rel="stylesheet" />
    <link href="https://cdn.datatables.net/1.13.4/css/dataTables.bootstrap5.min.css" rel="stylesheet" />
    <link href="~/css/site.css" rel="stylesheet" />
</head>
<body class="@(isGestionPage ? "no-background" : "")">

    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container">
            <a class="navbar-brand" href="/">WebVentaCoche</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarMain"
                    aria-controls="navbarMain" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>

            <div class="collapse navbar-collapse" id="navbarMain">
                <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                    <li class="nav-item"><a class="nav-link" href="/Productos">Productos</a></li>
                    <li class="nav-item"><a class="nav-link" href="/Contacto">Contacto</a></li>

                    @if (user.Identity.IsAuthenticated && user.IsInRole("Administrador"))
                    {
                        <li class="nav-item dropdown">
                            <a class="nav-link dropdown-toggle" href="#" id="gestionDropdown" role="button"
                               data-bs-toggle="dropdown" aria-expanded="false">
                                Gestión
                            </a>
                            <ul class="dropdown-menu" aria-labelledby="gestionDropdown">
                                <li><a class="dropdown-item" asp-controller="Products" asp-action="Index">Productos</a></li>
                                <li><a class="dropdown-item" asp-controller="Order" asp-action="Index">Pedidos</a></li>
                                <li><a class="dropdown-item" asp-controller="User" asp-action="Index">Usuarios</a></li>
                                <li><a class="dropdown-item" asp-controller="Sales" asp-action="Index">Ventas</a></li>
                            </ul>
                        </li>
                    }
                </ul>

                <ul class="navbar-nav ms-auto mb-2 mb-lg-0">
                    @if (user.Identity.IsAuthenticated)
                    {
                        <li class="nav-item me-3 position-relative">
                            <a class="nav-link" href="@Url.Action("Index","Cart")">
                                <i class="fa fa-shopping-cart" style="font-size:1.5rem;"></i>
                                <span class="position-absolute top-0 start-100 translate-middle badge rounded-pill bg-danger @(cartItemCount==0 ? "d-none" : "")">
                                    @cartItemCount
                                </span>
                            </a>
                        </li>

                        <li class="nav-item dropdown">
                            <a class="nav-link dropdown-toggle" href="#" id="userDropdown" role="button"
                               data-bs-toggle="dropdown" aria-expanded="false">
                                <i class="fa fa-user" style="font-size:1.5rem;"></i>
                            </a>
                            <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="userDropdown">
                                <li><a class="dropdown-item" asp-controller="Account" asp-action="Details" asp-route-id="@userId">Detalles Cuenta</a></li>
                                <li><a class="dropdown-item" asp-controller="Account" asp-action="Settings">Configuración</a></li>
                                <li><a class="dropdown-item" asp-controller="Order" asp-action="UserOrders">Mis Pedidos</a></li>
                                <li><hr class="dropdown-divider" /></li>
                                <li>
                                    <form asp-controller="User" asp-action="Logout" method="post" class="d-inline">
                                        <button type="submit" class="dropdown-item text-danger">Cerrar Sesión</button>
                                    </form>
                                </li>

                            </ul>
                        </li>
                    }
                    else
                    {
                        <li class="nav-item">
                            <a class="nav-link" href="@Url.Action("Login","User")">Iniciar Sesión</a>
                        </li>
                    }
                </ul>
            </div>
        </div>
    </nav>

    <div class="jumbotron jumbotron-fluid bg-image mb-4"
         style="background-image: url('/images/captura.jpg'); background-size: cover;
                background-position: center; height: 300px;">
        <div class="container text-white text-center">
            <h1 class="display-4">Bienvenido a WebVentaCoche</h1>
            <p class="lead">Tu tienda de confianza para productos de coches.</p>
        </div>
    </div>

    <div class="container">
        @RenderBody()
    </div>

    <footer class="bg-dark text-light text-center py-3 mt-4">
        <p>&copy; 2024 WebVentaCoche. Todos los derechos reservados.</p>
    </footer>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.13.4/js/dataTables.bootstrap5.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://d3js.org/d3.v7.min.js"></script>
    @RenderSection("Scripts", required: false)
</body>
</html>
