@model IEnumerable<LittleShop.DTOs.OrderDto>

@{
    ViewData["Title"] = "Orders";
}

<div class="row mb-4">
    <div class="col">
        <h1><i class="fas fa-shopping-cart"></i> Orders</h1>
    </div>
    <div class="col-auto">
        <a href="@Url.Action("Create")" class="btn btn-primary">
            <i class="fas fa-plus"></i> Create Order
        </a>
    </div>
</div>

<div class="card">
    <div class="card-body">
        @if (Model.Any())
        {
            <div class="table-responsive">
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>Order ID</th>
                            <th>Customer</th>
                            <th>Shipping To</th>
                            <th>Status</th>
                            <th>Total</th>
                            <th>Created</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var order in Model)
                        {
                            <tr>
                                <td><code>@order.Id.ToString().Substring(0, 8)...</code></td>
                                <td>
                                    @if (order.Customer != null)
                                    {
                                        <div>
                                            <strong>@order.Customer.DisplayName</strong>
                                            @if (!string.IsNullOrEmpty(order.Customer.TelegramUsername))
                                            {
                                                <br><small class="text-muted">@@@order.Customer.TelegramUsername</small>
                                            }
                                            <br><small class="badge bg-info">@order.Customer.CustomerType</small>
                                        </div>
                                    }
                                    else
                                    {
                                        <span class="text-muted">@order.ShippingName</span>
                                        @if (!string.IsNullOrEmpty(order.IdentityReference))
                                        {
                                            <br><small class="text-muted">(@order.IdentityReference)</small>
                                        }
                                    }
                                </td>
                                <td>@order.ShippingCity, @order.ShippingCountry</td>
                                <td>
                                    @{
                                        var badgeClass = order.Status switch
                                        {
                                            LittleShop.Enums.OrderStatus.PendingPayment => "bg-warning",
                                            LittleShop.Enums.OrderStatus.PaymentReceived => "bg-success",
                                            LittleShop.Enums.OrderStatus.Processing => "bg-info",
                                            LittleShop.Enums.OrderStatus.Shipped => "bg-primary",
                                            LittleShop.Enums.OrderStatus.Delivered => "bg-success",
                                            LittleShop.Enums.OrderStatus.Cancelled => "bg-danger",
                                            _ => "bg-secondary"
                                        };
                                    }
                                    <span class="badge @badgeClass">@order.Status</span>
                                </td>
                                <td><strong>£@order.TotalAmount</strong></td>
                                <td>@order.CreatedAt.ToString("MMM dd, yyyy HH:mm")</td>
                                <td>
                                    <a href="@Url.Action("Details", new { id = order.Id })" class="btn btn-sm btn-outline-primary">
                                        <i class="fas fa-eye"></i> View
                                    </a>
                                    @if (order.Customer != null)
                                    {
                                        <a href="@Url.Action("Details", new { id = order.Id })" class="btn btn-sm btn-success ms-1" title="Message Customer">
                                            <i class="fas fa-comment"></i>
                                        </a>
                                    }
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
        }
        else
        {
            <div class="text-center py-4">
                <i class="fas fa-shopping-cart fa-3x text-muted mb-3"></i>
                <p class="text-muted">No orders found yet.</p>
            </div>
        }
    </div>
</div>