{% extends "admin/dj_redis_panel/base.html" %}
{% load i18n admin_urls static %}

{% block breadcrumbs %}
<div class="breadcrumbs">
    <a href="{% url 'admin:index' %}">{% trans 'Home' %}</a>
    &rsaquo; <a href="{% url 'dj_redis_panel:index' %}">{% trans 'Redis Instances' %}</a>
    &rsaquo; <a href="{% url 'dj_redis_panel:instance_overview' instance_alias %}">{{ instance_alias }}</a>
    &rsaquo; <a href="{% url 'dj_redis_panel:key_search' instance_alias db_number %}">{% trans 'Search Keys' %} (db:{{ db_number }})</a>
    &rsaquo; {{ key_data.name }}
</div>
{% endblock %}

{% block content %}
<div class="module">
    {% if success_message %}
    <div class="messagelist">
        <div class="success">{{ success_message }}</div>
    </div>
    {% endif %}
    
    {% if error_message %}
    <div class="messagelist">
        <div class="error">{{ error_message }}</div>
    </div>
    {% endif %}

    {% if key_data %}
    <div class="module">
        
        <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
            <h1 style="margin: 0;">KEY: {{ key_data.name }}</h1>
            {% if allow_key_delete %}
            <form method="post" style="margin: 0;" onsubmit="return confirm('{% trans 'Are you sure you want to delete this key? This action cannot be undone.' %}');">
                {% csrf_token %}
                <input type="hidden" name="action" value="delete_key">
                <input type="submit" value="{% trans 'Delete Key' %}" class="deletelink delete-button">
            </form>
            {% else %}
            <input type="button" value="{% trans 'Key Delete Disabled' %}" class="deletelink delete-button" disabled style="margin: 0;">
            {% endif %}
        </div>
        
        <!-- Key Information -->
        <fieldset class="module aligned">
            <h2>{% trans 'Key Information' %}</h2>
            <br>
            <div class="form-row">
                <div class="field-box">
                    <label><strong>{% trans 'Key Name:' %}</strong></label>
                    <div class="readonly">
                        <code>{{ key_data.name }}</code>
                    </div>
                </div>
            </div>
            
            <div class="form-row">
                <div class="field-box">
                    <label><strong>{% trans 'Type:' %}</strong></label>
                    <div class="readonly">
                        <span class="key-type-badge key-type-{{ key_data.type }}">{{ key_data.type|upper }}</span>
                    </div>
                </div>
            </div>
            
            <div class="form-row">
                <div class="field-box">
                    <label><strong>{% trans 'Size:' %}</strong></label>
                    <div class="readonly">
                        {% if key_data.type == "string" %}
                            {{ key_data.size }} {% trans 'bytes' %}
                        {% else %}
                            {{ key_data.size }} {% trans 'elements' %}
                        {% endif %}
                    </div>
                </div>
            </div>
            
            <div class="form-row">
                <div class="field-box">
                    <label><strong>{% trans 'TTL:' %}</strong></label>
                    <div class="readonly">
                        {% if key_data.ttl %}
                            {{ key_data.ttl }} {% trans 'seconds' %}
                        {% else %}
                            <span class="quiet">{% trans 'No expiration' %}</span>
                        {% endif %}
                    </div>
                </div>
            </div>
        </fieldset>

        <!-- TTL Management -->
        <fieldset class="module aligned">
            <h2>{% trans 'TTL Management' %}</h2>
            <br>
            {% if allow_ttl_update %}
            <form method="post" class="ttl-form">
                {% csrf_token %}
                <input type="hidden" name="action" value="update_ttl">
                <div class="form-row">
                    <div class="field-box">
                        <label for="new_ttl">{% trans 'Set TTL (seconds):' %}</label>
                        <input type="number" id="new_ttl" name="new_ttl" 
                               value="{{ key_data.ttl|default:'' }}" 
                               placeholder="{% trans 'Enter TTL' %}"
                               class="vIntegerField">
                        <div class="help">
                            {% trans 'Set number of seconds until key expires. Leave empty or use -1 to remove expiration.' %}
                        </div>
                    </div>
                    <div class="submit-row">
                        <input type="submit" value="{% trans 'Update TTL' %}" class="default">
                    </div>
                </div>
            </form>
            {% else %}
            <div class="form-row">
                <div class="field-box">
                    <label for="disabled_ttl">{% trans 'Set TTL (seconds):' %}</label>
                    <input type="number" id="disabled_ttl" 
                           value="{{ key_data.ttl|default:'' }}" 
                           placeholder="{% trans 'Enter TTL' %}"
                           class="vIntegerField" readonly disabled>
                    <div class="help">
                        {% trans 'TTL updates are disabled for this instance.' %}
                    </div>
                </div>
                <div class="submit-row">
                    <input type="button" value="{% trans 'TTL Update Disabled' %}" class="default" disabled>
                </div>
            </div>
            {% endif %}
        </fieldset>

        <!-- Key Value Display and Editing -->
        <fieldset class="module">
            <h2>{% trans 'Key Value' %}</h2>
            <br>
            
            {% if key_data.type == "string" %}
            <!-- String value editing -->
             <h3>{% trans 'String Value' %}</h3>
             <br>
            {% if allow_key_edit %}
            <form method="post" class="value-form">
                {% csrf_token %}
                <input type="hidden" name="action" value="update_value">
                <div class="form-row">
                    <div class="field-box">
                        <textarea id="new_value" name="new_value" class="string-value-field" rows="5">{{ key_data.value }}</textarea>
                        <div class="help">{% trans 'Edit the string value directly.' %}</div>
                    </div>
                    <div class="submit-row">
                        <input type="submit" value="{% trans 'Update Value' %}" class="default">
                    </div>
                </div>
            </form>
            {% else %}
            <div class="form-row">
                <div class="field-box">
                    <textarea class="string-value-field" rows="5" readonly disabled>{{ key_data.value }}</textarea>
                    <div class="help">{% trans 'Value editing is disabled for this instance.' %}</div>
                </div>
                <div class="submit-row">
                    <input type="button" value="{% trans 'Value Edit Disabled' %}" class="default" disabled>
                </div>
            </div>
            {% endif %}
            
            {% elif key_data.type == "list" %}
            <!-- List display -->
            <div class="key-value-display">
                <h3>{% trans 'List Elements' %} ({{ key_data.size }} {% trans 'items' %})</h3>
                {% if key_data.value %}
                <table class="key-value-table">
                    <thead>
                        <tr>
                            <th>{% trans 'Index' %}</th>
                            <th>{% trans 'Value' %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for item in key_data.value %}
                        <tr>
                            <td><code>{{ forloop.counter0 }}</code></td>
                            <td><code>{{ item }}</code></td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
                {% else %}
                <p class="quiet">{% trans 'List is empty' %}</p>
                {% endif %}
                <div class="help">
                    {% trans 'List editing is not supported in this interface. Use Redis CLI or other tools for complex list operations.' %}
                </div>
            </div>
            
            {% elif key_data.type == "set" %}
            <!-- Set display -->
            <div class="key-value-display">
                <h3>{% trans 'Set Members' %} ({{ key_data.size }} {% trans 'items' %})</h3>
                {% if key_data.value %}
                <table class="key-value-table">
                    <thead>
                        <tr>
                            <th>{% trans 'Member' %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for member in key_data.value %}
                        <tr>
                            <td><code>{{ member }}</code></td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
                {% else %}
                <p class="quiet">{% trans 'Set is empty' %}</p>
                {% endif %}
                <div class="help">
                    {% trans 'Set editing is not supported in this interface. Use Redis CLI or other tools for complex set operations.' %}
                </div>
            </div>
            
            {% elif key_data.type == "zset" %}
            <!-- Sorted Set display -->
            <div class="key-value-display">
                <h3>{% trans 'Sorted Set Members' %} ({{ key_data.size }} {% trans 'items' %})</h3>
                {% if key_data.value %}
                <table class="key-value-table">
                    <thead>
                        <tr>
                            <th>{% trans 'Member' %}</th>
                            <th>{% trans 'Score' %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for member, score in key_data.value %}
                        <tr>
                            <td><code>{{ member }}</code></td>
                            <td><code>{{ score }}</code></td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
                {% else %}
                <p class="quiet">{% trans 'Sorted set is empty' %}</p>
                {% endif %}
                <div class="help">
                    {% trans 'Sorted set editing is not supported in this interface. Use Redis CLI or other tools for complex sorted set operations.' %}
                </div>
            </div>
            
            {% elif key_data.type == "hash" %}
            <!-- Hash display -->
            <div class="key-value-display">
                <h3>{% trans 'Hash Fields' %} ({{ key_data.size }} {% trans 'fields' %})</h3>
                {% if key_data.value %}
                <table class="key-value-table">
                    <thead>
                        <tr>
                            <th>{% trans 'Field' %}</th>
                            <th>{% trans 'Value' %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for field, value in key_data.value.items %}
                        <tr>
                            <td><code>{{ field }}</code></td>
                            <td><code>{{ value }}</code></td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
                {% else %}
                <p class="quiet">{% trans 'Hash is empty' %}</p>
                {% endif %}
                <div class="help">
                    {% trans 'Hash editing is not supported in this interface. Use Redis CLI or other tools for complex hash operations.' %}
                </div>
            </div>
            
            {% else %}
            <div class="key-value-display">
                <p class="quiet">{% trans 'Unsupported key type for display:' %} {{ key_data.type }}</p>
            </div>
            {% endif %}
        </fieldset>


    </div>
    {% endif %}
</div>
{% endblock %}
