<?php
session_start();
require_once('../config/db.php');

// ✅ يتم استدعاء ملف الهيدر لاحقاً لتفعيل دالة __() ومتغير $dir
// لكننا نحتاج لتعريف بعض الأساسيات قبل الهيدر إذا لزم الأمر

// ✅ التحقق من تسجيل الدخول
if (!isset($_SESSION['user'])) {
    header('Location: ../auth/login.php');
    exit;
}

$id = $_GET['id'] ?? null;
if (!$id || !is_numeric($id)) {
    header('Location: list.php');
    exit;
}

$error = '';
$success = '';

// 1. جلب بيانات الكورس الحالي
$stmt = $conn->prepare("SELECT * FROM courses WHERE id = ?");
$stmt->execute([$id]);
$course = $stmt->fetch();

if (!$course) {
    header('Location: list.php');
    exit;
}

// استدعاء الهيدر لتفعيل نظام الترجمة والاتجاهات
include('../includes/header.php');

// 2. معالجة طلب التحديث
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $course_name = trim($_POST['course_name'] ?? '');
    $description = trim($_POST['description'] ?? '');
    $price = trim($_POST['price'] ?? '');
    $duration_weeks = is_numeric($_POST['duration_weeks']) && $_POST['duration_weeks'] > 0 ? (int)$_POST['duration_weeks'] : null;

    // ✅ استخدام نظام الترجمة في الرسائل
    if ($course_name === '' || $price === '') {
        $error = __('error_missing_fields');
    } elseif (!is_numeric($price) || $price < 0) {
        $error = __('error_invalid_price');
    } else {
        try {
            $stmt = $conn->prepare("UPDATE courses SET course_name = ?, description = ?, price = ?, duration_weeks = ? WHERE id = ?");
            if ($stmt->execute([$course_name, $description, $price, $duration_weeks, $id])) {
                $success = __('success_update_course_msg');
                
                $course['course_name'] = $course_name;
                $course['description'] = $description;
                $course['price'] = $price;
                $course['duration_weeks'] = $duration_weeks;
            } else {
                $error = __('error_db_update');
            }
        } catch (PDOException $e) {
            $error = __('error_db_general') . $e->getMessage();
        }
    }
}

$current_course_name = htmlspecialchars($course['course_name']);
$current_description = htmlspecialchars($course['description']);
$current_price = htmlspecialchars($course['price']);
$current_duration_weeks = htmlspecialchars($course['duration_weeks']);
?>

<?php include('../includes/navbar.php'); ?>

<div class="min-h-screen bg-gray-100 dark:bg-gray-900 pb-16">
    <div class="max-w-xl mx-auto py-10 px-4 sm:px-6 lg:px-8">
        
        <div class="bg-white dark:bg-gray-800 shadow-2xl rounded-2xl p-8 border-t-8 border-blue-600 dark:border-blue-500 transform hover:scale-[1.01] transition duration-300">
            
            <h2 class="text-3xl font-extrabold mb-8 text-center text-blue-700 dark:text-blue-400 border-b pb-4">
                <i class="fas fa-edit <?= $dir == 'rtl' ? 'ml-2' : 'mr-2' ?>"></i> 
                <?= __('edit_course_title') ?> (<?= $id ?>)
            </h2>

            <?php if ($error): ?>
                <div class="bg-red-100 dark:bg-red-900 text-red-700 dark:text-red-300 p-4 rounded-xl mb-6 shadow-lg border border-red-300">
                    <i class="fas fa-times-circle <?= $dir == 'rtl' ? 'ml-2' : 'mr-2' ?>"></i> <?= $error ?>
                </div>
            <?php elseif ($success): ?>
                <div class="bg-green-100 dark:bg-green-900 text-green-700 dark:text-green-300 p-4 rounded-xl mb-6 shadow-lg border border-green-300">
                    <i class="fas fa-check-circle <?= $dir == 'rtl' ? 'ml-2' : 'mr-2' ?>"></i> <?= $success ?>
                </div>
            <?php endif; ?>

            <form method="POST" action="" class="space-y-6">
                
                <div>
                    <label for="course_name" class="block mb-2 font-bold text-gray-700 dark:text-gray-300">
                        <?= __('course_name') ?> <span class="text-red-500">*</span>
                    </label>
                    <input type="text" name="course_name" id="course_name" required
                        value="<?= $current_course_name ?>"
                        class="w-full border-2 border-gray-300 dark:border-gray-600 bg-gray-50 dark:bg-gray-700 text-gray-900 dark:text-gray-100 rounded-xl px-4 py-3 focus:outline-none focus:ring-4 focus:ring-blue-500/50 transition shadow-inner <?= $dir == 'rtl' ? 'text-right' : 'text-left' ?>">
                </div>

                <div>
                    <label for="description" class="block mb-2 font-bold text-gray-700 dark:text-gray-300">
                        <?= __('description') ?>
                    </label>
                    <textarea name="description" id="description" rows="4"
                        class="w-full border-2 border-gray-300 dark:border-gray-600 bg-gray-50 dark:bg-gray-700 text-gray-900 dark:text-gray-100 rounded-xl px-4 py-3 focus:outline-none focus:ring-4 focus:ring-blue-500/50 transition shadow-inner <?= $dir == 'rtl' ? 'text-right' : 'text-left' ?>"><?= $current_description ?></textarea>
                </div>

                <div>
                    <label for="price" class="block mb-2 font-bold text-gray-700 dark:text-gray-300">
                        <?= __('price') ?> <span class="text-red-500">*</span>
                    </label>
                    <div class="relative">
                        <input type="number" step="0.01" name="price" id="price" required
                            value="<?= $current_price ?>"
                            class="w-full border-2 border-gray-300 dark:border-gray-600 bg-gray-50 dark:bg-gray-700 text-gray-900 dark:text-gray-100 rounded-xl px-4 py-3 focus:outline-none focus:ring-4 focus:ring-blue-500/50 transition shadow-inner <?= $dir == 'rtl' ? 'text-right' : 'text-left' ?>">
                        <span class="absolute <?= $dir == 'rtl' ? 'left-4' : 'right-4' ?> top-0 bottom-0 flex items-center text-green-600 dark:text-green-400 font-extrabold text-lg">
                            <?= __('currency') ?>
                        </span>
                    </div>
                </div>

                <div>
                    <label for="duration_weeks" class="block mb-2 font-bold text-gray-700 dark:text-gray-300">
                        <?= __('duration') ?> (<?= __('weeks') ?>)
                    </label>
                    <input type="number" name="duration_weeks" id="duration_weeks" min="1"
                        value="<?= $current_duration_weeks ?>"
                        class="w-full border-2 border-gray-300 dark:border-gray-600 bg-gray-50 dark:bg-gray-700 text-gray-900 dark:text-gray-100 rounded-xl px-4 py-3 focus:outline-none focus:ring-4 focus:ring-blue-500/50 transition shadow-inner <?= $dir == 'rtl' ? 'text-right' : 'text-left' ?>">
                </div>

                <div class="flex flex-col sm:flex-row justify-between items-center pt-4 space-y-4 sm:space-y-0 gap-4">
                    
                    <button type="submit"
                        class="bg-blue-600 text-white w-full sm:w-auto px-8 py-3 rounded-xl hover:bg-blue-700 transition font-extrabold shadow-lg transform hover:scale-[1.02] order-1">
                        <i class="fas fa-sync-alt <?= $dir == 'rtl' ? 'ml-2' : 'mr-2' ?>"></i> <?= __('update_data') ?>
                    </button>
                   
                     <a href="list.php"
                        class="w-full sm:w-auto text-center bg-gray-300 text-gray-800 font-semibold px-6 py-3 rounded-xl hover:bg-gray-400 transition shadow-md flex items-center justify-center order-2">
                        <i class="fas <?= $dir == 'rtl' ? 'fa-arrow-right ml-2' : 'fa-arrow-left mr-2' ?>"></i> <?= __('back_to_list') ?>
                    </a>
                </div>
            </form>
        </div>
    </div>
</div>

<?php include('../includes/footer.php'); ?>