@using Microsoft.AspNetCore.Localization
@using OneForMe.Services
@inject LocalizationService Localizer
@inject IConfiguration Configuration
@model DashboardViewModel

@{
    ViewData["Title"] = Localizer["Dashboard"];
}

<div class="container mt-5">
    <div class="row mb-4">
        <div class="col-md-12">
            <h1>@Localizer["Welcome", Configuration["AppName"] ?? "OneForMe"]</h1>
        </div>
    </div>

    <div class="row mb-4">
        <div class="col-md-6 mb-3">
            <div class="card shadow">
                <div class="card-body">
                    <h5 class="card-title">@Localizer.Get("CreateNewOrder")</h5>
                    <p class="card-text">@Localizer.Get("StartGroupOrder")</p>
                    <a href="/order/create" class="btn btn-primary">@Localizer.Get("CreateNewOrder")</a>
                </div>
            </div>
        </div>

        <div class="col-md-6">
            <div class="card shadow">
                <div class="card-body">
                    <h5 class="card-title">@Localizer.Get("JoinOrder")</h5>
                    <p class="card-text">@Localizer.Get("JoinExistingOrder")</p>
                    <form method="get" action="/order/join" class="mb-3">
                        <div class="input-group">
                            <input type="text" class="form-control" name="code" placeholder="@Localizer.Get("EnterOrderCode")" required>
                            <button class="btn btn-success" type="submit">@Localizer.Get("Join")</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <hr>

    <!-- Created Orders -->
    <div class="row mb-4">
        <div class="col-md-12">
            <h3>@Localizer.Get("MyCreatedOrders")</h3>
            @if (Model.CreatedOrders.Any())
            {
                <div class="row">
                    @foreach (var order in Model.CreatedOrders)
                    {
                        <div class="col-md-6 mb-3">
                            <div class="card shadow">
                                <div class="card-body">
                                    <h5 class="card-title">@order.Name <span class="badge @(order.IsCompleted ? "bg-success" : "bg-warning text-dark")" style="float: right">@Localizer.Get(order.IsClosed ? (order.IsCompleted ? "Completed" : "Closed") : "Open")</span></h5>
                                    <p class="card-text">
                                        <small class="text-muted">@Localizer.Get("Code"): <strong>@order.OrderCode</strong></small><br>
                                        <small class="text-muted">@Localizer.Get("Created"): @order.CreatedAt.ToString("MMM dd, yyyy HH:mm")</small><br>
                                        <small>@Localizer.Get("Items"): @order.MenuItems.Count | @Localizer.Get("Orders"): @order.OrderItems.Count</small>
                                    </p>
                                    <p class="text-success"><strong>@Localizer.Get("Total"): @Localizer["Currency", order.OrderItems.Sum(oi => oi.MenuItem.Price * oi.Quantity).ToString("F2")]</strong></p>
                                    <div class="d-flex gap-2">
                                        <a href="/order/details?code=@order.OrderCode" class="btn btn-sm btn-primary">@Localizer.Get("View")</a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    }
                </div>
            }
            else
            {
                <div class="alert alert-info">@Localizer.Get("NoCreatedOrders")</div>
            }
        </div>
    </div>

    <hr>

    <!-- Joined Orders -->
    <div class="row">
        <div class="col-md-12">
            <h3>@Localizer.Get("OrdersIJJoined")</h3>
            @if (Model.JoinedOrders.Any())
            {
                <div class="row">
                    @foreach (var order in Model.JoinedOrders)
                    {
                        var myItems = order.OrderItems.Where(oi => oi.ParticipantEmail == User.Identity?.Name || oi.ParticipantName == User.Identity?.Name).ToList();
                        <div class="col-md-6 mb-3">
                            <div class="card shadow">
                                <div class="card-body">
                                    <h5 class="card-title">@order.Name <span class="badge @(order.IsCompleted ? "bg-success" : "bg-warning text-dark")" style="float: right">@Localizer.Get(order.IsClosed ? (order.IsCompleted ? "Completed" : "Closed") : "Open")</span></h5>
                                    <p class="card-text">
                                        <small class="text-muted">@Localizer.Get("Code"): <strong>@order.OrderCode</strong></small><br>
                                        <small class="text-muted">@Localizer.Get("CreatedBy"): @order.CreatorName</small><br>
                                        <small>@Localizer.Get("MyItems"): @myItems.Count</small>
                                    </p>
                                    <p><strong>@Localizer.Get("IOwe"): @Localizer["Currency", myItems.Sum(oi => (oi.MenuItem?.Price ?? 0) * oi.Quantity).ToString("F2")]</strong></p>
                                    <div class="d-flex gap-2">
                                        <a href="/order/details?code=@order.OrderCode" class="btn btn-sm btn-primary">@Localizer.Get("View")</a>
                                        @if (!order.IsClosed)
                                        {
                                            <a href="/order/join?code=@order.OrderCode" class="btn btn-sm btn-success">@Localizer.Get("AddMore")</a>
                                        }
                                    </div>
                                </div>
                            </div>
                        </div>
                    }
                </div>
            }
            else
            {
                <div class="alert alert-info">@Localizer.Get("NoJoinedOrders")</div>
            }
        </div>
    </div>
</div>