{% extends "group_admin/base.html" %}
{% block title %}Database{% endblock %}
{% block content %}
<h2 class="mb-4"><i class="bi bi-database-fill-gear me-2"></i>Configure MC Database</h2>

<div class="row g-3">
    <div class="col-md-7">
        <div class="card border-secondary">
            <div class="card-header">Connection Details</div>
            <div class="card-body">
                {% if error %}
                <div class="alert alert-danger">
                    <i class="bi bi-x-circle-fill me-2"></i>{{ error }}
                </div>
                {% endif %}

                <form method="post">
                    <input type="hidden" name="_csrf_token" value="{{ csrf_token() }}">
                    <div class="row g-3">
                        <div class="col-md-8">
                            <label class="form-label">Host *</label>
                            <input type="text" name="host" class="form-control" required
                                   placeholder="localhost"
                                   value="{{ creds.host if creds else request.form.get('host', '') }}">
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Port *</label>
                            <input type="number" name="port" class="form-control" required
                                   value="{{ creds.port if creds else request.form.get('port', '3306') }}">
                        </div>
                        <div class="col-12">
                            <label class="form-label">Database *</label>
                            <input type="text" name="database" class="form-control" required
                                   placeholder="mclogger"
                                   value="{{ creds.database if creds else request.form.get('database', '') }}">
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">User *</label>
                            <input type="text" name="user" class="form-control" required
                                   value="{{ creds.user if creds else request.form.get('user', '') }}">
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Password</label>
                            <input type="password" name="password" class="form-control"
                                   placeholder="{{ '(unchanged)' if creds else '' }}">
                            {% if creds %}
                            <div class="form-text">Leave blank to keep the existing password.</div>
                            {% endif %}
                        </div>
                    </div>

                    <div class="mt-4">
                        <button type="submit" name="action" value="test_save" class="btn btn-success">
                            <i class="bi bi-plug-fill me-1"></i>Test & Save
                        </button>
                    </div>
                </form>

                {% if creds %}
                <form method="post" action="{{ url_for('group_admin.database_delete') }}" class="d-inline">
                    <input type="hidden" name="_csrf_token" value="{{ csrf_token() }}">
                    <button type="submit" class="btn btn-outline-danger mt-2"
                            onclick="return confirm('Delete DB configuration?')">
                        <i class="bi bi-trash3 me-1"></i>Remove
                    </button>
                </form>
                {% endif %}
            </div>
        </div>
    </div>

    <div class="col-md-5">
        <div class="card border-secondary">
            <div class="card-header"><i class="bi bi-info-circle me-2"></i>Info</div>
            <div class="card-body">
                <p class="small text-muted">
                    Enter the connection details for your <strong>MCLogger MySQL database</strong>.
                    The panel only reads data (SELECT) — write access is not required.
                </p>
                <p class="small text-muted">
                    Credentials are stored <strong>encrypted</strong> and are only visible to your group.
                </p>
                <hr>
                <p class="small text-muted mb-1"><strong>Required tables:</strong></p>
                <ul class="small text-muted">
                    <li>player_sessions</li>
                    <li>player_chat</li>
                    <li>player_commands</li>
                    <li>block_events</li>
                    <li>player_deaths</li>
                    <li>proxy_events</li>
                    <li>server_events</li>
                    <li>plugin_events</li>
                </ul>
            </div>
        </div>
    </div>
</div>
{% endblock %}
