@*
    EN: Café POS Tablet — 2-column layout: product grid + cart sidebar, touch-friendly.
    VI: POS Café Tablet — Bố cục 2 cột: lưới sản phẩm + giỏ hàng bên, thân thiện cảm ứng.
*@
@page "/pos/cafe/tablet"
@layout PosLayout
@inherits PosBase

@* ═══ PRODUCT PANEL ═══ *@
<div class="pos-product-panel">
    <div class="pos-category-tabs">
        @foreach (var cat in _categories)
        {
            <button class="pos-category-tab @(cat == _selectedCategory ? "pos-category-tab--active" : "")"
                    style="padding:12px 20px;font-size:15px;"
                    @onclick="() => _selectedCategory = cat">
                @cat
            </button>
        }
    </div>

    <div class="pos-product-grid" style="grid-template-columns:repeat(auto-fill, minmax(160px, 1fr));gap:16px;padding:20px;">
        @foreach (var product in FilteredProducts)
        {
            <div class="pos-product-card" style="padding:16px;" @onclick="() => AddToCart(product)">
                <div class="pos-product-card__image" style="display:flex;align-items:center;justify-content:center;">
                    <i data-lucide="coffee" style="width:36px;height:36px;color:var(--pos-text-tertiary);"></i>
                </div>
                <span class="pos-product-card__name" style="font-size:15px;">@product.Name</span>
                <span class="pos-product-card__price" style="font-size:16px;">@FormatPrice(product.Price)</span>
            </div>
        }
    </div>
</div>

@* ═══ CART SIDEBAR ═══ *@
<div class="pos-cart-panel" style="width:340px;min-width:340px;">
    <div class="pos-cart-header">
        <span class="pos-cart-header__title" style="font-size:17px;">Đơn hàng</span>
        <button style="background:none;border:none;color:var(--pos-danger);font-size:13px;cursor:pointer;"
                @onclick="() => _cartItems.Clear()">Xóa tất cả</button>
    </div>

    <div class="pos-cart-items">
        @foreach (var item in _cartItems)
        {
            <div class="pos-cart-item" style="padding:14px 12px;">
                <div class="pos-cart-item__info">
                    <span class="pos-cart-item__name" style="font-size:15px;">@item.Name</span>
                    <span class="pos-cart-item__price">@FormatPrice(item.Price)</span>
                </div>
                <div class="pos-cart-item__qty">
                    <button style="width:36px;height:36px;border-radius:10px;border:1px solid var(--pos-border-default);background:transparent;color:var(--pos-text-primary);font-size:18px;cursor:pointer;"
                            @onclick="() => ChangeQty(item, -1)">−</button>
                    <span style="font-size:16px;font-weight:600;min-width:24px;text-align:center;">@item.Qty</span>
                    <button style="width:36px;height:36px;border-radius:10px;border:1px solid var(--pos-border-default);background:transparent;color:var(--pos-text-primary);font-size:18px;cursor:pointer;"
                            @onclick="() => ChangeQty(item, 1)">+</button>
                </div>
            </div>
        }
    </div>

    <div class="pos-cart-footer">
        <div class="pos-cart-total">
            <span class="pos-cart-total__label">Tổng cộng</span>
            <span class="pos-cart-total__value">@FormatPrice(CartTotal)</span>
        </div>
        <button class="pos-btn-checkout" style="height:56px;font-size:17px;" @onclick="Checkout">
            Thanh toán — @FormatPrice(CartTotal)
        </button>
    </div>
</div>

@code {
    private readonly string[] _categories = { "Tất cả", "Cà phê", "Trà", "Sinh tố", "Đồ ăn" };
    private string _selectedCategory = "Tất cả";

    private readonly List<Product> _products = new()
    {
        new("Cà phê sữa đá", 35_000, "Cà phê"),
        new("Cà phê đen", 29_000, "Cà phê"),
        new("Bạc xỉu", 39_000, "Cà phê"),
        new("Cappuccino", 55_000, "Cà phê"),
        new("Latte", 55_000, "Cà phê"),
        new("Trà đào", 45_000, "Trà"),
        new("Trà vải", 45_000, "Trà"),
        new("Sinh tố bơ", 55_000, "Sinh tố"),
        new("Sinh tố xoài", 49_000, "Sinh tố"),
        new("Bánh mì", 25_000, "Đồ ăn"),
        new("Croissant", 35_000, "Đồ ăn"),
    };

    private readonly List<CartItem> _cartItems = new();
    private IEnumerable<Product> FilteredProducts =>
        _selectedCategory == "Tất cả" ? _products : _products.Where(p => p.Category == _selectedCategory);
    private decimal CartTotal => _cartItems.Sum(i => i.Price * i.Qty);

    private void AddToCart(Product product)
    {
        var existing = _cartItems.FirstOrDefault(i => i.Name == product.Name);
        if (existing != null) existing.Qty++;
        else _cartItems.Add(new CartItem(product.Name, product.Price));
    }

    private void ChangeQty(CartItem item, int delta)
    {
        item.Qty += delta;
        if (item.Qty <= 0) _cartItems.Remove(item);
    }

    private void Checkout() => NavigateTo("cafe/order-customize");

    private record Product(string Name, decimal Price, string Category);
    private class CartItem(string name, decimal price)
    {
        public string Name { get; set; } = name;
        public decimal Price { get; set; } = price;
        public int Qty { get; set; } = 1;
    }
}
