{% extends "base.html" %}
{% block content %}
    <section class="panel">
        <div class="action-row" style="justify-content: space-between;">
            <div>
                <h2>Player Market</h2>
                <p class="muted">List tradable items, buy from other players, or pull your own listings back.</p>
            </div>
            <div class="action-row">
                <a href="/market" class="button {{ 'primary' if not my_view else 'subtle' }}">Browse Listings</a>
                <a href="/market/my-listings" class="button {{ 'primary' if my_view else 'subtle' }}">My Listings</a>
            </div>
        </div>
    </section>

    {% if my_view %}
        <section class="grid-2">
            <article class="panel">
                <h2>Create Listing</h2>
                {% if sellable_items %}
                    <form method="post" class="stack">
                        <input type="hidden" name="action" value="list">
                        <label>
                            Item
                            <select name="item_id">
                                {% for item in sellable_items %}
                                    <option value="{{ item.id }}">{{ item.name }} ({{ item.quantity }} owned)</option>
                                {% endfor %}
                            </select>
                        </label>
                        <label>
                            Quantity
                            <input type="number" name="quantity" min="1" value="1" required>
                        </label>
                        <label>
                            Price each
                            <input type="number" name="price_each" min="1" value="100" required>
                        </label>
                        <button type="submit" class="primary">List Item</button>
                    </form>
                {% else %}
                    <p class="muted">You do not have any tradable items to list yet.</p>
                {% endif %}
            </article>
            <article class="panel">
                <h2>Listing Rules</h2>
                <p class="muted">Listed stock is reserved immediately. If you remove a listing, the remaining quantity returns to inventory.</p>
            </article>
        </section>

        <section class="panel">
            <h2>My Listings</h2>
            {% if my_listings %}
                <table>
                    <thead>
                        <tr>
                            <th>Item</th>
                            <th>Qty</th>
                            <th>Price</th>
                            <th>Status</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for listing in my_listings %}
                            <tr>
                                <td>{{ listing.item_name }}</td>
                                <td>{{ listing.quantity }}</td>
                                <td>${{ listing.price_each }}</td>
                                <td>{{ listing.status }}</td>
                                <td>
                                    {% if listing.status == "active" %}
                                        <form method="post" class="inline-form">
                                            <input type="hidden" name="action" value="remove">
                                            <input type="hidden" name="listing_id" value="{{ listing.id }}">
                                            <button type="submit" class="subtle">Remove</button>
                                        </form>
                                    {% else %}
                                        <span class="muted mini">Closed</span>
                                    {% endif %}
                                </td>
                            </tr>
                        {% endfor %}
                    </tbody>
                </table>
            {% else %}
                <p class="muted">No listings yet.</p>
            {% endif %}
        </section>
    {% else %}
        <section class="panel">
            <h2>Active Listings</h2>
            {% if listings %}
                <table>
                    <thead>
                        <tr>
                            <th>Item</th>
                            <th>Seller</th>
                            <th>Qty</th>
                            <th>Price Each</th>
                            <th>Buy</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for listing in listings %}
                            <tr>
                                <td>{{ listing.item_name }}<br><span class="muted mini">{{ listing.category|replace('_', ' ')|title }}</span></td>
                                <td>{{ listing.seller_name }}</td>
                                <td>{{ listing.quantity }}</td>
                                <td>${{ listing.price_each }}</td>
                                <td>
                                    {% if listing.seller_user_id == current_user.id %}
                                        <span class="muted mini">Your listing</span>
                                    {% else %}
                                        <form method="post" class="inline-form">
                                            <input type="hidden" name="action" value="buy">
                                            <input type="hidden" name="listing_id" value="{{ listing.id }}">
                                            <input type="number" name="quantity" min="1" max="{{ listing.quantity }}" value="1">
                                            <button type="submit" class="primary">Buy</button>
                                        </form>
                                    {% endif %}
                                </td>
                            </tr>
                        {% endfor %}
                    </tbody>
                </table>
            {% else %}
                <p class="muted">No active market listings right now.</p>
            {% endif %}
        </section>
    {% endif %}
{% endblock %}
