<?php
// change_password.php - Change Password for Logged In Users
// File location: /bidwheels.net/user/change_password.php

require_once '../includes/auth.php';
require_once '../includes/header.php';

requireLogin();

$error = '';
$success = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $current_password = $_POST['current_password'];
    $new_password = $_POST['new_password'];
    $confirm_password = $_POST['confirm_password'];
    
    $user = fetchOne("SELECT password FROM users WHERE id = {$_SESSION['user_id']}");
    
    if (md5($current_password) != $user['password']) {
        $error = 'Current password is incorrect.';
    } elseif (strlen($new_password) < 6) {
        $error = 'New password must be at least 6 characters.';
    } elseif ($new_password != $confirm_password) {
        $error = 'New passwords do not match.';
    } else {
        update('users', ['password' => md5($new_password)], "id = {$_SESSION['user_id']}");
        $success = 'Password changed successfully!';
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Change Password - <?php echo SITE_NAME; ?></title>
    
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        .change-password-page {
            min-height: calc(100vh - 200px);
            background: linear-gradient(135deg, #EFEBE0 0%, #e5e0d4 100%);
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 60px 24px;
        }
        
        .password-container {
            max-width: 550px;
            width: 100%;
            background: white;
            border-radius: 32px;
            overflow: hidden;
            box-shadow: 0 25px 50px rgba(0,0,0,0.1);
        }
        
        /* Header */
        .password-header {
            background: linear-gradient(135deg, #1a1a1a 0%, #2a2a2a 100%);
            padding: 35px 30px;
            text-align: center;
        }
        
        .password-header i {
            font-size: 50px;
            color: #FF8C3E;
            margin-bottom: 15px;
        }
        
        .password-header h1 {
            font-size: 24px;
            font-weight: 700;
            color: white;
            margin-bottom: 8px;
        }
        
        .password-header p {
            color: #aaa;
            font-size: 14px;
        }
        
        /* Form Body */
        .password-body {
            padding: 35px 30px;
        }
        
        /* Alert Styles */
        .alert {
            padding: 14px 18px;
            border-radius: 16px;
            margin-bottom: 25px;
            display: flex;
            align-items: center;
            gap: 12px;
            font-size: 13px;
        }
        
        .alert-success {
            background: #d4edda;
            color: #155724;
            border-left: 4px solid #28a745;
        }
        
        .alert-danger {
            background: #f8d7da;
            color: #721c24;
            border-left: 4px solid #dc3545;
        }
        
        .alert i {
            font-size: 18px;
        }
        
        /* Form Groups */
        .form-group {
            margin-bottom: 25px;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            font-size: 13px;
            color: #333;
        }
        
        .form-group label i {
            color: #FF8C3E;
            margin-right: 6px;
        }
        
        .input-wrapper {
            position: relative;
        }
        
        .input-wrapper i {
            position: absolute;
            left: 16px;
            top: 50%;
            transform: translateY(-50%);
            color: #aaa;
            font-size: 16px;
        }
        
        .input-wrapper input {
            width: 100%;
            padding: 14px 16px 14px 48px;
            border: 2px solid #f0f0f0;
            border-radius: 16px;
            font-size: 14px;
            transition: all 0.3s;
            background: #f8f8f8;
        }
        
        .input-wrapper input:focus {
            outline: none;
            border-color: #FF8C3E;
            background: white;
        }
        
        /* Password Toggle */
        .password-toggle {
            position: absolute;
            right: 16px;
            top: 50%;
            transform: translateY(-50%);
            cursor: pointer;
            color: #aaa;
            background: none;
            border: none;
            font-size: 16px;
        }
        
        .password-toggle:hover {
            color: #FF8C3E;
        }
        
        /* Password Strength */
        .password-strength {
            margin-top: 10px;
            display: flex;
            gap: 8px;
            align-items: center;
        }
        
        .strength-bar {
            flex: 1;
            height: 4px;
            background: #f0f0f0;
            border-radius: 4px;
            overflow: hidden;
        }
        
        .strength-fill {
            height: 100%;
            width: 0%;
            background: #dc3545;
            transition: width 0.3s, background 0.3s;
        }
        
        .strength-text {
            font-size: 11px;
            color: #888;
            min-width: 60px;
        }
        
        /* Password Hint */
        .password-hint {
            font-size: 11px;
            color: #888;
            margin-top: 8px;
            display: flex;
            align-items: center;
            gap: 8px;
            flex-wrap: wrap;
        }
        
        .password-hint i {
            color: #FF8C3E;
        }
        
        /* Submit Button */
        .btn-submit {
            width: 100%;
            padding: 16px;
            background: linear-gradient(135deg, #FF8C3E, #e67a22);
            border: none;
            border-radius: 50px;
            color: white;
            font-weight: 700;
            font-size: 16px;
            cursor: pointer;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
            margin-top: 10px;
        }
        
        .btn-submit:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(255,140,62,0.3);
        }
        
        /* Back Link */
        .back-link {
            text-align: center;
            margin-top: 25px;
        }
        
        .back-link a {
            color: #FF8C3E;
            text-decoration: none;
            font-size: 14px;
            font-weight: 500;
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }
        
        .back-link a:hover {
            text-decoration: underline;
        }
        
        /* Divider */
        .divider {
            margin: 20px 0;
            text-align: center;
            position: relative;
        }
        
        .divider::before {
            content: '';
            position: absolute;
            top: 50%;
            left: 0;
            right: 0;
            height: 1px;
            background: #f0f0f0;
        }
        
        .divider span {
            background: white;
            padding: 0 15px;
            position: relative;
            color: #999;
            font-size: 12px;
        }
        
        /* Responsive */
        @media (max-width: 768px) {
            .change-password-page {
                padding: 40px 16px;
            }
            
            .password-header {
                padding: 25px 20px;
            }
            
            .password-header i {
                font-size: 40px;
            }
            
            .password-header h1 {
                font-size: 20px;
            }
            
            .password-body {
                padding: 25px 20px;
            }
        }
        
        @media (max-width: 480px) {
            .password-header {
                padding: 20px 16px;
            }
            
            .password-header i {
                font-size: 32px;
            }
            
            .password-body {
                padding: 20px 16px;
            }
            
            .btn-submit {
                padding: 14px;
                font-size: 14px;
            }
            
            .password-hint {
                font-size: 10px;
            }
        }
    </style>
</head>
<body>

<div class="change-password-page">
    <div class="password-container">
        <!-- Header -->
        <div class="password-header">
            <i class="fas fa-key"></i>
            <h1>Change Password</h1>
            <p>Create a strong and secure password</p>
        </div>
        
        <!-- Form Body -->
        <div class="password-body">
            <?php if ($success): ?>
                <div class="alert alert-success">
                    <i class="fas fa-check-circle"></i>
                    <span><?php echo $success; ?></span>
                </div>
            <?php endif; ?>
            
            <?php if ($error): ?>
                <div class="alert alert-danger">
                    <i class="fas fa-exclamation-circle"></i>
                    <span><?php echo $error; ?></span>
                </div>
            <?php endif; ?>
            
            <form method="POST" action="" id="passwordForm">
                <!-- Current Password -->
                <div class="form-group">
                    <label><i class="fas fa-lock"></i> Current Password</label>
                    <div class="input-wrapper">
                        <i class="fas fa-shield-alt"></i>
                        <input type="password" name="current_password" id="current_password" placeholder="Enter your current password" required>
                        <button type="button" class="password-toggle" onclick="togglePassword('current_password')">
                            <i class="fas fa-eye-slash"></i>
                        </button>
                    </div>
                </div>
                
                <!-- New Password -->
                <div class="form-group">
                    <label><i class="fas fa-key"></i> New Password</label>
                    <div class="input-wrapper">
                        <i class="fas fa-lock"></i>
                        <input type="password" name="new_password" id="new_password" placeholder="Enter new password (min. 6 characters)" required>
                        <button type="button" class="password-toggle" onclick="togglePassword('new_password')">
                            <i class="fas fa-eye-slash"></i>
                        </button>
                    </div>
                    <div class="password-strength">
                        <div class="strength-bar">
                            <div class="strength-fill" id="strengthFill"></div>
                        </div>
                        <span class="strength-text" id="strengthText">Weak</span>
                    </div>
                    <div class="password-hint">
                        <i class="fas fa-info-circle"></i>
                        <span>Use at least 8 characters with letters, numbers, and symbols</span>
                    </div>
                </div>
                
                <!-- Confirm Password -->
                <div class="form-group">
                    <label><i class="fas fa-check-circle"></i> Confirm Password</label>
                    <div class="input-wrapper">
                        <i class="fas fa-check-circle"></i>
                        <input type="password" name="confirm_password" id="confirm_password" placeholder="Confirm your new password" required>
                        <button type="button" class="password-toggle" onclick="togglePassword('confirm_password')">
                            <i class="fas fa-eye-slash"></i>
                        </button>
                    </div>
                </div>
                
                <button type="submit" class="btn-submit">
                    <i class="fas fa-save"></i> Update Password
                </button>
                
                <div class="divider">
                    <span>or</span>
                </div>
                
                <div class="back-link">
                    <a href="dashboard.php">
                        <i class="fas fa-arrow-left"></i> Back to Dashboard
                    </a>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
// Toggle password visibility
function togglePassword(fieldId) {
    const input = document.getElementById(fieldId);
    const icon = input.parentElement.querySelector('.password-toggle i');
    
    if (input.type === 'password') {
        input.type = 'text';
        icon.classList.remove('fa-eye-slash');
        icon.classList.add('fa-eye');
    } else {
        input.type = 'password';
        icon.classList.remove('fa-eye');
        icon.classList.add('fa-eye-slash');
    }
}

// Password strength checker
const newPassword = document.getElementById('new_password');
const strengthFill = document.getElementById('strengthFill');
const strengthText = document.getElementById('strengthText');

if (newPassword) {
    newPassword.addEventListener('input', function() {
        const value = this.value;
        let strength = 0;
        let color = '#dc3545';
        let text = 'Weak';
        
        if (value.length >= 6) strength += 1;
        if (value.length >= 8) strength += 1;
        if (/[A-Z]/.test(value)) strength += 1;
        if (/[0-9]/.test(value)) strength += 1;
        if (/[^A-Za-z0-9]/.test(value)) strength += 1;
        
        if (strength >= 4) {
            color = '#28a745';
            text = 'Strong';
        } else if (strength >= 2) {
            color = '#ffc107';
            text = 'Medium';
        } else {
            color = '#dc3545';
            text = 'Weak';
        }
        
        const percentage = (strength / 5) * 100;
        strengthFill.style.width = percentage + '%';
        strengthFill.style.background = color;
        strengthText.textContent = text;
        strengthText.style.color = color;
    });
}

// Password confirmation validation
const confirmPassword = document.getElementById('confirm_password');
if (confirmPassword) {
    confirmPassword.addEventListener('input', function() {
        if (this.value !== newPassword.value) {
            this.setCustomValidity('Passwords do not match');
        } else {
            this.setCustomValidity('');
        }
    });
}

// Form submission validation
document.getElementById('passwordForm').addEventListener('submit', function(e) {
    if (newPassword.value !== confirmPassword.value) {
        e.preventDefault();
        alert('New passwords do not match');
        return false;
    }
    
    if (newPassword.value.length < 6) {
        e.preventDefault();
        alert('Password must be at least 6 characters');
        return false;
    }
    
    return true;
});
</script>

<?php require_once '../includes/footer.php'; ?>