{% extends '__layout__.html' %}
{% block title %}Manage User{% endblock %}

{% block head %}
<link href="/static/css/admin.css" rel="stylesheet"/>
{% endblock %}

{% block content %}
<div id="main">
    <div class="container" style="max-width: 900px;">
        <a class="mb-2 d-block" href="/admin/manage-users/{{userObj.id}}">← Return to User Page</a>
        <div class="d-flex align-items-center mb-3">
            <img height="100px" style="aspect-ratio: 1/1;" class="rounded-2" src="/Thumbs/Avatar.ashx?userId={{userObj.id}}&x=100&y=100">
            <div class="ms-3">
                <h1 class="m-0 text-secondary">Manage Inventory for <span class="text-body">{{userObj.username}}</span></h1>
                <p class="text-secondary m-0" style="font-size: 12px;">UserId: <span class="text-body">{{userObj.id}}</span></p>
            </div>
        </div>

        {% with messages = get_flashed_messages() %}
        {% if messages %}
        <div>
            {% for message in messages %}
                <div class="alert border border-danger p-2 text-center messagealerts">
                    {{ message }}
                </div>
            {% endfor %}
        </div>
        {% endif %}
        {% endwith %}

        <div class="linebreak"></div>

        <div class="mb-4 p-3 border rounded-2" style="background-color: var(--card-bg);">
            <h3>Give Item</h3>
            <form method="POST" action="/admin/manage-users/{{userObj.id}}/manage-inventory/give">
                <div class="form-floating mt-2">
                    <input type="number" class="form-control" name="assetid" id="assetid-input" placeholder="Asset ID" required>
                    <label for="assetid-input">Asset ID</label>
                </div>
                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                <div class="linebreak"></div>
                <button type="submit" class="btn btn-primary mt-2 w-100 btn-sm">Give Item</button>
            </form>
        </div>

        <h3>Current Inventory ( {{UserInventory.total}} )</h3>
        <div class="row mt-2">
            {% for UserAssetObj in UserInventory.items %}
            <div class="col-6 col-sm-4 col-md-3 mb-2">
                <div class="border rounded p-2 h-100 d-flex flex-column">
                    <a href="/catalog/{{UserAssetObj.assetid}}/" target="_blank">
                        <img width="100%" src="/Thumbs/Asset.ashx?assetId={{UserAssetObj.assetid}}&x=150&y=150" style="aspect-ratio: 1/1;" class="rounded">
                    </a>
                    <p class="text-body m-0 mt-1 text-truncate" style="font-size: 13px;">{{UserAssetObj.asset.name if UserAssetObj.asset else "Unknown Asset"}}</p>
                    <p class="text-secondary m-0" style="font-size: 11px;">
                        AssetId: {{UserAssetObj.assetid}}{% if UserAssetObj.serial != None %} | Serial: {{UserAssetObj.serial}}{% endif %}
                    </p>
                    <form method="POST" action="/admin/manage-users/{{userObj.id}}/manage-inventory/take/{{UserAssetObj.id}}" class="mt-auto" onsubmit="return confirm('Take {{UserAssetObj.asset.name if UserAssetObj.asset else 'this item'}} away from {{userObj.username}}?');">
                        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                        <button type="submit" class="btn btn-outline-danger btn-sm w-100 mt-2">Take Away</button>
                    </form>
                </div>
            </div>
            {% endfor %}
            {% if UserInventory.total == 0 %}
            <p class="mt-3 mb-5 text-secondary w-100 text-center">This user has no items</p>
            {% endif %}
        </div>

        <div class="d-flex w-100 mt-2" style="font-size: 14px;">
            <a class="text-decoration-none ms-auto {% if not UserInventory.has_prev: %}text-secondary{%endif%}" {% if UserInventory.has_prev: %}href="/admin/manage-users/{{userObj.id}}/manage-inventory?page={{PageNumber - 1}}"{%endif%}>Previous</a>
            <p class="m-0 ms-2 me-2 text-body">Page {{PageNumber}} of {{UserInventory.pages}}</p>
            <a class="text-decoration-none me-auto {% if not UserInventory.has_next: %}text-secondary{%endif%}" {% if UserInventory.has_next: %}href="/admin/manage-users/{{userObj.id}}/manage-inventory?page={{PageNumber + 1}}"{%endif%}>Next</a>
        </div>
    </div>
</div>
{% endblock %}
