<?php
session_start();
require_once("../config/db.php");

// --- نظام جلب اللغة ---
$current_lang = $_SESSION['lang'] ?? 'ar';
$lang_file = "../languages/" . $current_lang . ".php";
$translations = file_exists($lang_file) ? include($lang_file) : [];

// التحقق من تسجيل الدخول
if (!isset($_SESSION['user'])) {
    header('Location: ../auth/login.php');
    exit;
}

$userId = $_SESSION['user']['id'];

// معالجة طلب تغيير كلمة المرور
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $current = $_POST['current_password'];
    $new     = $_POST['new_password'];
    $confirm = $_POST['confirm_password'];

    $query = $conn->prepare("SELECT password FROM users WHERE id = ?");
    $query->execute([$userId]);
    $user = $query->fetch(PDO::FETCH_ASSOC);

    if (!password_verify($current, $user['password'])) {
        $_SESSION['error'] = $translations['error_current_pass_wrong'] ?? "كلمة المرور الحالية غير صحيحة ❌";
    } elseif ($new !== $confirm) {
        $_SESSION['error'] = $translations['error_pass_mismatch'] ?? "كلمة المرور الجديدة غير متطابقة ❌";
    } elseif (strlen($new) < 6) {
        $_SESSION['error'] = $translations['error_pass_short'] ?? "كلمة المرور يجب أن تكون 6 أحرف على الأقل ⚠️";
    } else {
        $hashed = password_hash($new, PASSWORD_BCRYPT);
        $update = $conn->prepare("UPDATE users SET password = ? WHERE id = ?");
        $update->execute([$hashed, $userId]);

        $_SESSION['success'] = $translations['success_pass_change'] ?? "تم تغيير كلمة المرور بنجاح ✅";
        header("Location: change_password.php");
        exit;
    }
}

include('../includes/header.php');
include('../includes/navbar.php');

// تحديد اتجاه العناصر بناءً على اللغة
$dir = ($current_lang == 'ar') ? 'rtl' : 'ltr';
$icon_pos = ($current_lang == 'ar') ? 'right-0 pr-4' : 'left-0 pl-4';
$input_padding = ($current_lang == 'ar') ? 'pr-11 pl-4' : 'pl-11 pr-4';
$alert_border = ($current_lang == 'ar') ? 'border-r-4' : 'border-l-4';
$back_icon = ($current_lang == 'ar') ? 'fa-arrow-right ml-2' : 'fa-arrow-left mr-2';
?>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<div class="min-h-[calc(100vh-160px)] flex items-center justify-center py-12 px-4" dir="<?= $dir ?>">
    <div class="max-w-md w-full">
        
        <div class="bg-white rounded-[2.5rem] shadow-2xl border border-gray-100 overflow-hidden transform transition-all">
            
            <div class="bg-gradient-to-r from-blue-600 to-indigo-700 h-28 relative">
                <div class="absolute -bottom-12 inset-x-0 flex justify-center">
                    <div class="bg-white p-1.5 rounded-full shadow-xl">
                        <div class="bg-blue-50 w-24 h-24 rounded-full flex items-center justify-center text-blue-600 text-4xl border-2 border-white">
                            <i class="fas fa-shield-halved"></i>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pt-16 pb-10 px-8">
                <div class="text-center mb-8">
                    <h2 class="text-2xl font-black text-gray-800"><?= $translations['account_security'] ?? 'أمان الحساب' ?></h2>
                    <p class="text-gray-400 text-sm mt-1"><?= $translations['update_pass_desc'] ?? 'تحديث كلمة المرور الخاصة بك' ?></p>
                </div>

                <?php if (!empty($_SESSION['success'])): ?>
                    <div class="mb-6 flex items-center bg-green-50 <?= $alert_border ?> border-green-500 p-4 rounded-2xl animate-bounce">
                        <i class="fas fa-check-circle text-green-500 <?= ($current_lang == 'ar' ? 'ml-3' : 'mr-3') ?> text-lg"></i>
                        <p class="text-sm text-green-700 font-bold"><?= $_SESSION['success']; unset($_SESSION['success']); ?></p>
                    </div>
                <?php endif; ?>

                <?php if (!empty($_SESSION['error'])): ?>
                    <div class="mb-6 flex items-center bg-red-50 <?= $alert_border ?> border-red-500 p-4 rounded-2xl">
                        <i class="fas fa-circle-exclamation text-red-500 <?= ($current_lang == 'ar' ? 'ml-3' : 'mr-3') ?> text-lg"></i>
                        <p class="text-sm text-red-700 font-bold"><?= $_SESSION['error']; unset($_SESSION['error']); ?></p>
                    </div>
                <?php endif; ?>

                <form method="POST" class="space-y-6">
                    
                    <div>
                        <label class="block mb-2 text-sm font-bold text-gray-700 mx-1"><?= $translations['current_pass_label'] ?? 'كلمة المرور الحالية' ?></label>
                        <div class="relative group">
                            <span class="absolute inset-y-0 <?= $icon_pos ?> flex items-center text-gray-400 group-focus-within:text-blue-500 transition-colors">
                                <i class="fas fa-lock-open"></i>
                            </span>
                            <input type="password" name="current_password" required placeholder="••••••••"
                                class="w-full <?= $input_padding ?> py-3.5 bg-gray-50 border border-gray-200 rounded-2xl focus:ring-2 focus:ring-blue-500 focus:bg-white transition-all outline-none text-gray-700">
                        </div>
                    </div>

                    <div>
                        <label class="block mb-2 text-sm font-bold text-gray-700 mx-1"><?= $translations['new_pass_label'] ?? 'كلمة المرور الجديدة' ?></label>
                        <div class="relative group">
                            <span class="absolute inset-y-0 <?= $icon_pos ?> flex items-center text-gray-400 group-focus-within:text-blue-500 transition-colors">
                                <i class="fas fa-key"></i>
                            </span>
                            <input type="password" name="new_password" required placeholder="••••••••"
                                class="w-full <?= $input_padding ?> py-3.5 bg-gray-50 border border-gray-200 rounded-2xl focus:ring-2 focus:ring-blue-500 focus:bg-white transition-all outline-none text-gray-700">
                        </div>
                    </div>

                    <div>
                        <label class="block mb-2 text-sm font-bold text-gray-700 mx-1"><?= $translations['confirm_pass_label'] ?? 'تأكيد كلمة المرور الجديدة' ?></label>
                        <div class="relative group">
                            <span class="absolute inset-y-0 <?= $icon_pos ?> flex items-center text-gray-400 group-focus-within:text-blue-500 transition-colors">
                                <i class="fas fa-check-double"></i>
                            </span>
                            <input type="password" name="confirm_password" required placeholder="••••••••"
                                class="w-full <?= $input_padding ?> py-3.5 bg-gray-50 border border-gray-200 rounded-2xl focus:ring-2 focus:ring-blue-500 focus:bg-white transition-all outline-none text-gray-700">
                        </div>
                    </div>

                    <div class="pt-4 space-y-4">
                        <button type="submit"
                            class="w-full bg-blue-600 hover:bg-blue-700 text-white font-black py-4 rounded-2xl shadow-lg shadow-blue-200 transition-all active:scale-95 flex items-center justify-center gap-3">
                            <i class="fas fa-sync-alt"></i>
                            <?= $translations['update_btn_text'] ?? 'تحديث كلمة المرور' ?>
                        </button>
                        
                        <a href="../dashboard.php" 
                           class="w-full flex items-center justify-center text-gray-500 hover:text-blue-600 font-bold text-sm py-2 transition-colors">
                            <i class="fas <?= $back_icon ?>"></i>
                            <?= $translations['back_to_dashboard'] ?? 'العودة للوحة التحكم' ?>
                        </a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<?php include('../includes/footer.php'); ?>