@*
    EN: Waiter Pad — Order taking by course with special requests, send to kitchen.
    VI: Pad Phục vụ — Gọi món theo course với yêu cầu đặc biệt, gửi bếp.
*@
@page "/pos/restaurant/waiter-pad"
@layout PosLayout
@inherits PosBase

<div class="pos-product-panel" style="display:flex;flex-direction:column;overflow:hidden;">
    @* ═══ HEADER / TIÊU ĐỀ ═══ *@
    <div style="padding:12px 16px;border-bottom:1px solid var(--pos-border-subtle);display:flex;align-items:center;gap:12px;">
        <button class="pos-category-tab" @onclick="@(() => NavigateTo("restaurant"))">
            <i data-lucide="arrow-left" style="width:14px;"></i> Quay lại
        </button>
        <span style="font-size:16px;font-weight:700;">Gọi món — Bàn 3</span>
        <span style="font-size:12px;color:var(--pos-text-tertiary);">PV: Nguyễn Văn A</span>
    </div>

    @* ═══ COURSE TABS / TAB MÓN THEO COURSE ═══ *@
    <div class="pos-category-tabs">
        @foreach (var c in _courses)
        {
            <button class="pos-category-tab @(c == _activeCourse ? "pos-category-tab--active" : "")"
                    @onclick="() => _activeCourse = c">@c</button>
        }
    </div>

    @* ═══ MENU ITEMS / DANH SÁCH MÓN ═══ *@
    <div class="pos-product-grid" style="grid-template-columns:repeat(auto-fill,minmax(150px,1fr));">
        @foreach (var item in FilteredMenu)
        {
            <div class="pos-product-card" @onclick="() => AddToOrder(item)">
                <div class="pos-product-card__image" style="display:flex;align-items:center;justify-content:center;">
                    <i data-lucide="@item.Icon" style="width:28px;color:var(--pos-text-tertiary);"></i>
                </div>
                <span class="pos-product-card__name">@item.Name</span>
                <span class="pos-product-card__price">@FormatPrice(item.Price)</span>
            </div>
        }
    </div>
</div>

@* ═══ ORDER PANEL / PANEL ĐƠN GỌI ═══ *@
<div class="pos-cart-panel">
    <div class="pos-cart-header">
        <span class="pos-cart-header__title">Đơn gọi (@_orderItems.Count món)</span>
        <button style="background:none;border:none;color:var(--pos-danger);font-size:12px;cursor:pointer;"
                @onclick="ClearOrder">Xóa hết</button>
    </div>

    <div class="pos-cart-items">
        @foreach (var item in _orderItems)
        {
            <div class="pos-cart-item">
                <div class="pos-cart-item__info">
                    <span class="pos-cart-item__name">@item.Name</span>
                    <span class="pos-cart-item__price">@FormatPrice(item.Price)</span>
                    @if (!string.IsNullOrEmpty(item.Note))
                    {
                        <span style="font-size:11px;color:var(--pos-warning);font-style:italic;">📝 @item.Note</span>
                    }
                </div>
                <div class="pos-cart-item__qty">
                    <button @onclick="() => ChangeQty(item, -1)">−</button>
                    <span style="font-size:13px;font-weight:600;">@item.Qty</span>
                    <button @onclick="() => ChangeQty(item, 1)">+</button>
                </div>
            </div>
        }
    </div>

    @* ═══ SPECIAL REQUEST / YÊU CẦU ĐẶC BIỆT ═══ *@
    <div style="padding:8px 16px;">
        <input type="text" @bind="_specialRequest" placeholder="Ghi chú đặc biệt..."
               style="width:100%;padding:10px 12px;border-radius:var(--pos-radius);border:1px solid var(--pos-border-default);
                      background:var(--pos-bg-interactive);color:var(--pos-text-primary);font-size:13px;font-family:var(--pos-font);" />
    </div>

    <div class="pos-cart-footer">
        <div class="pos-cart-total">
            <span class="pos-cart-total__label">Tổng</span>
            <span class="pos-cart-total__value">@FormatPrice(_orderItems.Sum(i => i.Price * i.Qty))</span>
        </div>
        <button class="pos-btn-checkout" @onclick="SendToKitchen">
            <i data-lucide="send"></i> Gửi bếp
        </button>
    </div>
</div>

@code {
    private string _activeCourse = "Khai vị";
    private string _specialRequest = string.Empty;
    private readonly string[] _courses = { "Khai vị", "Món chính", "Tráng miệng", "Đồ uống" };

    // EN: Menu items by course / VI: Thực đơn theo course
    private readonly List<MenuItem> _menu = new()
    {
        new("Gỏi cuốn", 45_000, "Khai vị", "salad"), new("Chả giò", 40_000, "Khai vị", "flame"),
        new("Súp cua", 55_000, "Khai vị", "soup"), new("Nộm bò bóp thấu", 65_000, "Khai vị", "leaf"),
        new("Phở bò tái", 75_000, "Món chính", "beef"), new("Cơm tấm sườn", 65_000, "Món chính", "utensils"),
        new("Bún bò Huế", 80_000, "Món chính", "flame"), new("Cá kho tộ", 120_000, "Món chính", "fish"),
        new("Lẩu thái", 250_000, "Món chính", "soup"), new("Gà nướng mật ong", 180_000, "Món chính", "drumstick"),
        new("Chè thái", 30_000, "Tráng miệng", "ice-cream-cone"), new("Bánh flan", 25_000, "Tráng miệng", "cake"),
        new("Trái cây dĩa", 45_000, "Tráng miệng", "apple"), new("Trà đá", 10_000, "Đồ uống", "glass-water"),
        new("Cà phê sữa", 29_000, "Đồ uống", "coffee"), new("Nước mía", 15_000, "Đồ uống", "cup-soda"),
        new("Bia Sài Gòn", 25_000, "Đồ uống", "beer"),
    };

    private IEnumerable<MenuItem> FilteredMenu => _menu.Where(m => m.Course == _activeCourse);

    // EN: Current order / VI: Đơn gọi hiện tại
    private readonly List<OrderLine> _orderItems = new()
    {
        new("Gỏi cuốn", 45_000, 2, ""), new("Phở bò tái", 75_000, 1, "Ít hành"),
    };

    private void AddToOrder(MenuItem item)
    {
        var existing = _orderItems.FirstOrDefault(o => o.Name == item.Name);
        if (existing is not null) existing.Qty++;
        else _orderItems.Add(new OrderLine(item.Name, item.Price, 1, ""));
    }

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

    private void ClearOrder() => _orderItems.Clear();
    private void SendToKitchen() => NavigateTo("restaurant/kitchen-display");

    private record MenuItem(string Name, decimal Price, string Course, string Icon);
    private class OrderLine(string name, decimal price, int qty, string note)
    {
        public string Name { get; set; } = name;
        public decimal Price { get; set; } = price;
        public int Qty { get; set; } = qty;
        public string Note { get; set; } = note;
    }
}
