{% extends 'base.html' %}

{% block content %}
<div class="row g-4 mt-1">
    <div class="col-12 d-flex justify-content-between align-items-center">
        <h4 class="mb-0 fw-bold"><i class="bi bi-person-badge me-2"></i>{{ _('Account Management') }}</h4>
    </div>

    <!-- Password Change Section -->
    <div class="col-lg-5">
        <div class="card shadow-sm border-0 h-100">
            <div class="card-header bg-white pt-3 pb-2 border-bottom-0">
                <h5 class="card-title fw-bold text-primary mb-0"><i class="bi bi-shield-lock me-2"></i>{{ _('Change Password') }}</h5>
            </div>
            <div class="card-body">
                <form action="{{ url_for('main.account') }}" method="POST">
                    <input type="hidden" name="action" value="change_password">
                    <div class="mb-3">
                        <label class="form-label text-muted small fw-bold">{{ _('Current Password') }}</label>
                        <input type="password" name="current_password" class="form-control" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label text-muted small fw-bold">{{ _('New Password') }}</label>
                        <input type="password" name="new_password" class="form-control" required minlength="6">
                    </div>
                    <div class="mb-4">
                        <label class="form-label text-muted small fw-bold">{{ _('Confirm New Password') }}</label>
                        <input type="password" name="confirm_password" class="form-control" required minlength="6">
                    </div>
                    <button type="submit" class="btn btn-primary w-100 fw-bold rounded-pill"><i class="bi bi-check-circle me-2"></i>{{ _('Update Password') }}</button>
                </form>
            </div>
        </div>
    </div>

    <!-- Active Sessions Section -->
    <div class="col-lg-7">
        <div class="card shadow-sm border-0 h-100">
            <div class="card-header bg-white pt-3 pb-2 border-bottom-0 d-flex justify-content-between align-items-center">
                <h5 class="card-title fw-bold text-success mb-0"><i class="bi bi-laptop me-2"></i>{{ _('Active Sessions') }}</h5>
                <span class="badge bg-success rounded-pill">{{ sessions|length }}</span>
            </div>
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-hover table-borderless align-middle mb-0">
                        <thead class="table-light text-muted small">
                            <tr>
                                <th class="ps-4 fw-normal">{{ _('Device') }}</th>
                                <th class="fw-normal">{{ _('IP Address') }}</th>
                                <th class="fw-normal">{{ _('Last Active') }}</th>
                                <th class="text-end pe-4 fw-normal">{{ _('Action') }}</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for s in sessions %}
                            <tr class="{% if s.session_token == current_token %}bg-light{% endif %}">
                                <td class="ps-4">
                                    <div class="d-flex align-items-center">
                                        <i class="bi bi-display me-2 text-secondary fs-5"></i>
                                        <div>
                                            <div class="text-truncate" style="max-width: 150px" title="{{ s.user_agent }}">{{ s.user_agent.split(' ')[0] if s.user_agent else _('Unknown Device') }}</div>
                                            {% if s.session_token == current_token %}
                                            <span class="badge bg-primary mt-1">{{ _('This Device') }}</span>
                                            {% endif %}
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    <span class="badge bg-secondary font-monospace">{{ s.ip_address }}</span>
                                </td>
                                <td>
                                    <small class="text-muted">{{ s.last_active.strftime('%Y-%m-%d %H:%M:%S') }}</small>
                                </td>
                                <td class="text-end pe-4">
                                    {% if s.session_token != current_token %}
                                    <form action="{{ url_for('main.account') }}" method="POST" class="d-inline" id="form-{{ s.id }}">
                                        <input type="hidden" name="action" value="terminate_session">
                                        <input type="hidden" name="session_id" value="{{ s.id }}">
                                        <input type="hidden" name="session_token" value="{{ s.session_token }}">
                                        <button type="button" class="btn btn-sm btn-outline-danger px-3 rounded-pill" onclick="customConfirm('{{ _('Are you sure you want to terminate this session?') }}', () => document.getElementById('form-{{ s.id }}').submit());"><i class="bi bi-x-octagon me-1"></i>{{ _('Logout') }}</button>
                                    </form>
                                    {% else %}
                                    <button class="btn btn-sm btn-outline-secondary px-3 rounded-pill" onclick="customConfirm('{{ _('Logout from this device?') }}', () => window.location.href='{{ url_for('auth.logout') }}');"><i class="bi bi-box-arrow-right me-1"></i>{{ _('Logout') }}</button>
                                    {% endif %}
                                </td>
                            </tr>
                            {% else %}
                            <tr>
                                <td colspan="4" class="text-center py-4 text-muted">{{ _('No active sessions found.') }}</td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
