@using Microsoft.AspNetCore.Localization
@using OneForMe.Services
@inject LocalizationService Localizer
@model OneForMe.Models.Order

@{
    ViewData["Title"] = Model.CreatorName;
}

<div class="container mt-5">
    <div class="row">
        <div class="col-md-8">
            <div class="card shadow mb-4">
                <div class="card-body">
                    <h2>@Model.Name</h2>
                    <p class="text-muted">@Localizer.Get("Code"): <strong>@Model.OrderCode</strong></p>
                    <p>@Localizer.Get("CreatedBy"): <strong>@Model.CreatorName</strong></p>
                    <p>@Localizer.Get("CreatedOn"): @Model.CreatedAt.ToString("g")</p>
                    @if (!string.IsNullOrEmpty(Model.ImagePath))
                    {
                        <img src="@Model.ImagePath" alt="Order Image" class="img-fluid mb-3 rounded" style="max-height: 300px; object-fit: cover;">
                    }
                    @if (!string.IsNullOrEmpty(Model.AdditionalInfo))
                    {
                        <p class="border rounded p-3" style="white-space: pre-wrap;">@Model.AdditionalInfo</p>
                    }
                    <p>@Localizer.Get("Status"):
                        <div class="d-flex align-items-center gap-3">

                            <div class="text-center">
                                <div class="badge bg-success")">Offen</div>
                            </div>

                            <div class="flex-grow-1 border-top" style="border-width:3px !important;"></div>

                            <div class="text-center">
                                <div class="badge @(Model.IsClosed ? "bg-success" : "bg-danger")">Abgeschlossen</div>
                            </div>

                            <div class="flex-grow-1 border-top" style="border-width:3px !important;"></div>

                            <div class="text-center">
                                <div class="badge @(Model.IsCompleted ? "bg-success" : "bg-danger")">Geliefert</div>
                            </div>
                        </div>
                    </p>
                    <div class="d-grid gap-2 mt-4">
                        @if (!Model.IsClosed && Model.CreatorName == User.Identity?.Name)
                        {
                            <a href="/order/close?code=@Model.OrderCode" class="btn btn-warning">@Localizer.Get("CloseOrder")</a>
                        }
                        @if (Model.IsClosed && !Model.IsCompleted && Model.CreatorName == User.Identity?.Name)
                        {
                            <a href="/order/complete?code=@Model.OrderCode" class="btn btn-success">@Localizer.Get("MarkCompleted")</a>
                        }
                        @if (Model.IsCompleted)
                        {
                            <button class="btn btn-success" disabled>@Localizer.Get("Completed")</button>
                        }
                    </div>
                </div>
            </div>

            <div class="card shadow mb-4">
                <div class="card-header bg-primary text-white">
                    <h5 class="mb-0">@Localizer.Get("MenuItems")</h5>
                </div>
                <div class="card-body">
                    @if (Model.MenuItems.Any())
                    {
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>@Localizer.Get("Item")</th>
                                    <th>@Localizer.Get("Price")</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach (var item in Model.MenuItems)
                                {
                                    <tr>
                                        <td>@item.Name</td>
                                        <td>@Localizer["Currency", item.Price.ToString("F2")]</td>
                                    </tr>
                                }
                            </tbody>
                        </table>
                    }
                    else
                    {
                        <p class="text-muted">@Localizer.Get("NoItemsAdded")</p>
                    }
                </div>
            </div>

            <div class="card shadow mb-4">
                <div class="card-header bg-info text-white">
                    <h5 class="mb-0">@Localizer.Get("Orders") (@Model.OrderItems.Count)</h5>
                </div>
                <div class="card-body">
                    @if (Model.OrderItems.Any())
                    {
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>@Localizer.Get("Participant")</th>
                                    <th>@Localizer.Get("Item")</th>
                                    <th>@Localizer.Get("Qty")</th>
                                    <th>@Localizer.Get("Comments")</th>
                                    <th>@Localizer.Get("Total")</th>
                                    <th>@Localizer.Get("Actions")</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach (var orderItem in Model.OrderItems)
                                {
                                    <tr>
                                        <td>@orderItem.ParticipantName</td>
                                        <td>@orderItem.MenuItem?.Name</td>
                                        <td>@orderItem.Quantity</td>
                                        <td>@orderItem.Comments</td>
                                        <td>@Localizer["Currency", (orderItem.MenuItem?.Price * orderItem.Quantity ?? 0).ToString("F2")]</td>
                                        <td>
                                            @if (User.Identity?.Name == orderItem.ParticipantEmail)
                                            {
                                                <form style="display:inline;" method="post" action="/order/deleteitem" onsubmit="return confirm('@Localizer.Get("ConfirmDeleteOrderItem")');">
                                                    <input type="hidden" name="orderItemId" value="@orderItem.Id" />
                                                    <button type="submit" class="btn btn-sm btn-danger">@Localizer.Get("Delete")</button>
                                                </form>
                                                <button type="button" class="btn btn-sm btn-primary" data-bs-toggle="modal" data-bs-target="#updateModal-@orderItem.Id">@Localizer.Get("Edit")</button>
                                                <!-- Update Modal for Order Item -->
                                                <div class="modal fade" id="updateModal-@orderItem.Id" tabindex="-1" aria-labelledby="updateModalLabel-@orderItem.Id" aria-hidden="true">
                                                    <div class="modal-dialog">
                                                        <div class="modal-content">
                                                            <div class="modal-header">
                                                                <h5 class="modal-title" id="updateModalLabel-@orderItem.Id">@Localizer.Get("EditOrderItem")</h5>
                                                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                                            </div>
                                                            <form method="post" action="/order/updateitem">
                                                                <div class="modal-body">
                                                                    <input type="hidden" name="orderItemId" value="@orderItem.Id" />
                                                                    
                                                                    <div class="mb-3">
                                                                        <label class="form-label">@Localizer.Get("Item")</label>
                                                                        <input type="text" class="form-control" value="@orderItem.MenuItem?.Name" readonly disabled>
                                                                    </div>
                                                                    
                                                                    <div class="mb-3">
                                                                        <label class="form-label">@Localizer.Get("Qty")</label>
                                                                        <input type="number" name="quantity" class="form-control" value="@orderItem.Quantity" min="1" required>
                                                                    </div>
                                                                    
                                                                    <div class="mb-3">
                                                                        <label class="form-label">@Localizer.Get("Comments")</label>
                                                                        <textarea name="comments" class="form-control" rows="3">@orderItem.Comments</textarea>
                                                                    </div>
                                                                </div>
                                                                <div class="modal-footer">
                                                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">@Localizer.Get("Cancel")</button>
                                                                    <button type="submit" class="btn btn-primary">@Localizer.Get("Update")</button>
                                                                </div>
                                                            </form>
                                                        </div>
                                                    </div>
                                                </div>
                                            }
                                        </td>
                                    </tr>
                                }
                            </tbody>
                        </table>
                    }
                    else
                    {
                        <p class="text-muted">@Localizer.Get("NoOrdersYet")</p>
                    }
                </div>
            </div>
            @if (!Model.IsClosed)
            {
                <partial name="PlaceOrder" model="Model" />
            }
        </div>

        <div class="col-md-4">
            <div class="card shadow mb-4 bg-light">
                <div class="card-body @(Model.IsClosed ? "disabled" : "")">
                    <h5>@Localizer.Get("ShareOrder")</h5>
                    <p>@Localizer.Get("SendLinkToOthers")</p>
                    <div class="input-group">
                        <input type="text" class="form-control" id="shareLink" value="@(Context.Request.Scheme)://@(Context.Request.Host)/order/join?code=@Model.OrderCode" readonly>
                        <button class="btn btn-outline-primary" onclick="copyToClipboard()">@Localizer.Get("Copy")</button>
                    </div>
                </div>
            </div>

            <div class="card shadow mb-4">
                <div class="card-body">
                    <h5>@Localizer.Get("QuickStats")</h5>
                    <p>@Localizer.Get("TotalItems"): <strong>@Model.MenuItems.Count</strong></p>
                    <p>@Localizer.Get("TotalOrders"): <strong>@Model.OrderItems.Count</strong></p>
                    <p>@Localizer.Get("TotalRevenue"): <strong>@Localizer["Currency", Model.OrderItems.Sum(oi => oi.MenuItem.Price * oi.Quantity).ToString("F2")]</strong></p>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
function copyToClipboard() {
    const link = document.getElementById('shareLink');
    link.select();
    document.execCommand('copy');
    alert('@Localizer.Get("LinkCopied")');
}
</script>