{% extends "admin_base.html" %}
{% block extra_head %}
{{ super() }}
<link rel="stylesheet" href="{{ static_url('/static/css/containers.css') }}">
{% endblock %}
{% block admin_content %}
<div class="admin-toolbar">
    <h2>Edit {{ instance['name'] }}</h2>
    <a class="admin-btn admin-btn-sm" href="/admin/containers/{{ instance['uid'] }}">&larr; Back to instance</a>
</div>

<form id="cm-edit-form" class="cm-form cm-card" data-uid="{{ instance['uid'] }}" role="group" aria-label="Edit {{ instance['name'] }}">
    <label>Run as user (identity + API key only; container OS user is always pravda)
        <input type="text" name="run_as_query" autocomplete="off" placeholder="Search a user (optional)" data-search="user"
               value="{{ run_as_user['username'] if run_as_user else '' }}">
        <input type="hidden" name="run_as_uid" value="{{ instance['run_as_uid'] or '' }}">
        <ul class="cm-suggest" data-suggest="user" aria-live="polite" aria-label="User suggestions"></ul>
    </label>
    <label>Boot language
        <select name="boot_language">
            {% for lang in boot_languages %}
            <option value="{{ lang }}" {% if (instance['boot_language'] or 'none') == lang %}selected{% endif %}>{{ lang }}</option>
            {% endfor %}
        </select>
    </label>
    <label data-boot="script">Boot script
        <textarea name="boot_script" rows="10" spellcheck="false">{{ instance['boot_script'] or '' }}</textarea>
    </label>
    <label data-boot="command">Boot command (used when no boot script)
        <input type="text" name="boot_command" maxlength="500" value="{{ instance['boot_command'] or '' }}">
    </label>
    <label>Restart policy
        <select name="restart_policy">
            {% for policy in restart_policies %}
            <option value="{{ policy }}" {% if (instance['restart_policy'] or 'never') == policy %}selected{% endif %}>{{ policy }}</option>
            {% endfor %}
        </select>
    </label>
    <div class="cm-form-row">
        <label>CPU limit
            <input type="text" name="cpu_limit" maxlength="16" value="{{ instance['cpu_limit'] or '' }}">
        </label>
        <label>Memory limit
            <input type="text" name="mem_limit" maxlength="16" value="{{ instance['mem_limit'] or '' }}">
        </label>
    </div>
    <label class="cm-check"><input type="checkbox" name="start_on_boot" {% if instance['start_on_boot'] %}checked{% endif %}> Start on container service boot</label>
    <div class="modal-footer">
        <button type="submit" class="btn btn-primary">Save changes</button>
    </div>
</form>
{% endblock %}
{% block extra_js %}
<script type="module">
import { ContainerEdit } from "{{ static_url('/static/js/ContainerEdit.js') }}";
const editor = new ContainerEdit(document.getElementById("cm-edit-form"));
editor.init();
window.app.containerEdit = editor;
</script>
{% endblock %}
