<?php
require_once __DIR__ . '/includes/header.php';

$user = current_user();
$user_id = $user['id'] ?? 0;

$message = '';
$message_type = '';

// Processar alteração de senha
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $current_password = $_POST['current_password'] ?? '';
    $new_password = $_POST['new_password'] ?? '';
    $confirm_password = $_POST['confirm_password'] ?? '';
    
    if (empty($current_password) || empty($new_password) || empty($confirm_password)) {
        $message = 'Preencha todos os campos.';
        $message_type = 'error';
    } elseif ($new_password !== $confirm_password) {
        $message = 'As senhas não coincidem.';
        $message_type = 'error';
    } else {
        $result = change_password($user_id, $current_password, $new_password);
        $message = $result['message'];
        $message_type = $result['ok'] ? 'success' : 'error';
    }
}
?>

<style>
    .page-header {
        margin-bottom: 30px;
    }
    
    .page-header h1 {
        font-size: 28px;
        margin-bottom: 8px;
        background: var(--primary-bg);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
    }
    
    .page-header p {
        color: #999;
        font-size: 14px;
    }
    
    .alert {
        padding: 15px 20px;
        border-radius: 8px;
        margin-bottom: 20px;
        animation: slideIn 0.3s ease;
    }
    
    .alert-success {
        background: rgba(46, 204, 113, 0.2);
        border: 1px solid #2ecc71;
        color: #2ecc71;
    }
    
    .alert-error {
        background: rgba(231, 76, 60, 0.2);
        border: 1px solid #e74c3c;
        color: #e74c3c;
    }
    
    .profile-grid {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 25px;
        max-width: 1200px;
    }
    
    .card {
        background: #1a1a1a;
        padding: 30px;
        border-radius: 12px;
        border: 1px solid rgba(255, 255, 255, 0.1);
    }
    
    .card h2 {
        font-size: 18px;
        margin-bottom: 20px;
        padding-bottom: 15px;
        border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        color: #fff;
    }
    
    .form-group {
        margin-bottom: 20px;
    }
    
    .form-group label {
        display: block;
        margin-bottom: 8px;
        color: #fff;
        font-weight: 500;
        font-size: 14px;
    }
    
    .form-group input {
        width: 100%;
        padding: 12px 15px;
        background: #111;
        border: 1px solid rgba(255, 255, 255, 0.2);
        border-radius: 6px;
        color: #fff;
        font-size: 14px;
        transition: all 0.2s ease;
    }
    
    .form-group input:focus {
        outline: none;
        border-color: var(--primary-bg);
        box-shadow: 0 0 0 3px rgba(192, 57, 43, 0.1);
    }
    
    .form-group input:disabled {
        opacity: 0.5;
        cursor: not-allowed;
    }
    
    .info-row {
        display: flex;
        justify-content: space-between;
        padding: 12px 0;
        border-bottom: 1px solid rgba(255, 255, 255, 0.05);
    }
    
    .info-row:last-child {
        border-bottom: none;
    }
    
    .info-row label {
        color: #999;
        font-size: 14px;
    }
    
    .info-row value {
        color: #fff;
        font-weight: 500;
    }
    
    .submit-btn {
        background: var(--primary-bg);
        color: #fff;
        padding: 12px 30px;
        border: none;
        border-radius: 6px;
        font-size: 14px;
        font-weight: 600;
        cursor: pointer;
        transition: all 0.2s ease;
        width: 100%;
    }
    
    .submit-btn:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    }
    
    @media (max-width: 768px) {
        .profile-grid {
            grid-template-columns: 1fr;
        }
    }
</style>



<?php if ($message): ?>
    <div class="alert alert-<?php echo $message_type; ?>">
        <?php echo htmlspecialchars($message); ?>
    </div>
<?php endif; ?>

    
    <div class="card">

        <form method="POST">
            <div class="form-group">
                <label for="current_password">Senha Atual</label>
                <input type="password" 
                       id="current_password" 
                       name="current_password" 
                       placeholder="Digite sua senha atual"
                       autocomplete="current-password"
                       required>
            </div>
            
            <div class="form-group">
                <label for="new_password">Nova Senha</label>
                <input type="password" 
                       id="new_password" 
                       name="new_password" 
                       placeholder="Digite a nova senha"
                       autocomplete="new-password"
                       minlength="5"
                       required>
            </div>
            
            <div class="form-group">
                <label for="confirm_password">Confirmar Nova Senha</label>
                <input type="password" 
                       id="confirm_password" 
                       name="confirm_password" 
                       placeholder="Digite novamente a nova senha"
                       autocomplete="new-password"
                       minlength="5"
                       required>
            </div>
            
            <button type="submit" class="submit-btn">Salvar</button>
        </form>
    </div>
</div>

</main>
</body>
</html>
