{% extends 'base.html' %}

{% block content %}
<div class="animate-fade-in">
    <div class="d-flex flex-column flex-md-row justify-content-between align-items-md-center mb-5 gap-3">
        <div>
            <h1 class="section-title mb-1">Manager Dashboard</h1>
            <p class="text-secondary mb-0">Overview of your restaurant's performance and stock.</p>
        </div>
        <div class="d-flex gap-2">
            <button type="button" class="btn btn-outline-vibrant" data-bs-toggle="modal" data-bs-target="#addIngredientModal">
                <i class="bi bi-plus-circle me-1"></i> Add Ingredient
            </button>
            <a href="{% url 'manage_menu' %}" class="btn btn-outline-dark">Manage Menu</a>
            <a href="{% url 'manage_users' %}" class="btn btn-outline-dark">Manage Users</a>
            <a href="{% url 'pos' %}" class="btn btn-vibrant px-4">Go to POS</a>
        </div>
    </div>

    <div class="row g-4 mb-5">
        <div class="col-md-4">
            <div class="card p-4 h-100 border-0 shadow-sm" style="background: linear-gradient(135deg, #FF4500 0%, #FF7A45 100%); color: white;">
                <h5 class="opacity-75 mb-3 fw-bold text-uppercase small letter-spacing-1">Total Sales (All Time)</h5>
                <h2 class="display-5 fw-bold mb-0">{{ total_sales|stringformat:".2f" }} <small class="fs-4">EGP</small></h2>
                <div class="mt-4">
                    <span class="badge bg-white bg-opacity-25 rounded-pill px-3 py-2"><i class="bi bi-graph-up me-1"></i> Live tracking</span>
                </div>
            </div>
        </div>
        <div class="col-md-8">
            <div class="card p-4 h-100 border-0 shadow-sm">
                <div class="d-flex justify-content-between align-items-center mb-4">
                    <h5 class="fw-bold mb-0">Ingredient Stock Levels</h5>
                    <span class="small text-secondary"><i class="bi bi-info-circle me-1"></i> Updated in real-time</span>
                </div>
                <div class="row g-3">
                    {% for ingredient in ingredients %}
                    <div class="col-md-4">
                        <div class="p-3 rounded-4 border {% if ingredient.stock_quantity < 500 %}bg-danger bg-opacity-10 border-danger border-opacity-25{% else %}bg-light border-light{% endif %} h-100">
                            <div class="small text-secondary text-uppercase fw-bold mb-1" style="font-size: 0.7rem;">{{ ingredient.name }}</div>
                            <div class="h3 mb-0 fw-bold {% if ingredient.stock_quantity < 500 %}text-danger{% endif %}">
                                {{ ingredient.stock_quantity|floatformat:0 }}
                                <small class="fs-6 fw-normal text-secondary">{{ ingredient.unit }}</small>
                            </div>
                        </div>
                    </div>
                    {% endfor %}
                </div>
            </div>
        </div>
    </div>

    <div class="card border-0 shadow-sm">
        <div class="card-body p-4">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h4 class="fw-bold mb-0">Recent Orders</h4>
                <button class="btn btn-sm btn-light rounded-pill px-3">View All</button>
            </div>
            <div class="table-responsive">
                <table class="table table-hover align-middle">
                    <thead class="bg-light">
                        <tr class="text-secondary">
                            <th class="py-3 border-0 rounded-start">Order #</th>
                            <th class="py-3 border-0">Time</th>
                            <th class="py-3 border-0">Items</th>
                            <th class="py-3 border-0">Notes</th>
                            <th class="py-3 border-0 text-end">Total</th>
                            <th class="py-3 border-0 rounded-end"></th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for order in orders %}
                        <tr>
                            <td class="fw-bold py-3 text-vibrant">{{ order.order_number }}</td>
                            <td>
                                <div class="fw-bold text-dark">{{ order.created_at|date:"H:i" }}</div>
                                <div class="small text-secondary">{{ order.created_at|date:"d M, Y" }}</div>
                            </td>
                            <td>
                                <div class="d-flex flex-wrap gap-1">
                                    {% for item in order.items.all %}
                                    <span class="badge bg-soft text-vibrant border border-vibrant border-opacity-10 fw-normal">
                                        {{ item.quantity }}x {{ item.menu_item.name }}
                                    </span>
                                    {% endfor %}
                                </div>
                            </td>
                            <td>
                                <small class="text-secondary">
                                    {% if order.customer_notes %}
                                        <i class="bi bi-chat-left-text me-1"></i> {{ order.customer_notes|truncatechars:30 }}
                                    {% else %}
                                        -
                                    {% endif %}
                                </small>
                            </td>
                            <td class="text-end fw-bold">{{ order.total_price|floatformat:2 }} EGP</td>
                            <td class="text-end">
                                <a href="{% url 'receipt' order.order_number %}" class="btn btn-sm btn-outline-dark rounded-pill" target="_blank">
                                    <i class="bi bi-printer me-1"></i> Receipt
                                </a>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="6" class="text-center py-5">
                                <div class="py-4">
                                    <i class="bi bi-inbox display-1 text-light"></i>
                                    <p class="text-secondary mt-3">No orders found yet. Start by creating one in the POS.</p>
                                    <a href="{% url 'pos' %}" class="btn btn-vibrant mt-2">Open POS</a>
                                </div>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- Add Ingredient Modal -->
<div class="modal fade" id="addIngredientModal" tabindex="-1" aria-labelledby="addIngredientModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content border-0 shadow rounded-4">
            <div class="modal-header border-0 pb-0">
                <h5 class="modal-title fw-bold" id="addIngredientModalLabel">Add New Ingredient</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form action="{% url 'manage_ingredients' %}" method="post">
                {% csrf_token %}
                <div class="modal-body p-4">
                    <div class="mb-3">
                        <label class="form-label text-secondary small fw-bold text-uppercase">Ingredient Name</label>
                        <input type="text" name="name" class="form-control rounded-3" placeholder="e.g. Tomato, Beef Patty" required>
                    </div>
                    <div class="row g-3">
                        <div class="col-6">
                            <label class="form-label text-secondary small fw-bold text-uppercase">Initial Stock</label>
                            <input type="number" step="0.01" name="stock_quantity" class="form-control rounded-3" value="0">
                        </div>
                        <div class="col-6">
                            <label class="form-label text-secondary small fw-bold text-uppercase">Unit</label>
                            <select name="unit" class="form-select rounded-3">
                                <option value="grams">grams (g)</option>
                                <option value="ml">milliliters (ml)</option>
                                <option value="pieces">pieces (pcs)</option>
                                <option value="kg">kilograms (kg)</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="modal-footer border-0 pt-0 p-4">
                    <button type="button" class="btn btn-light rounded-pill px-4" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-vibrant rounded-pill px-4 shadow-sm">Add Ingredient</button>
                </div>
            </form>
        </div>
    </div>
</div>
{% endblock %}
