<?php
session_start();
require_once('../config/db.php');

// ✅ تم نقل استدعاء الهيدر إلى الأعلى لتعريف الدالة __() والمتغيرات قبل استخدامها
include('../includes/header.php'); 

// ✅ التحقق من تسجيل الدخول
if (!isset($_SESSION['user'])) {
    header('Location: ../auth/login.php');
    exit;
}

$error = '';
$success = '';

// تهيئة المتغيرات لعرض القيم بعد محاولة الإرسال الفاشلة
$course_name = $_POST['course_name'] ?? '';
$description = $_POST['description'] ?? '';
$price = $_POST['price'] ?? '';
$duration_weeks = $_POST['duration_weeks'] ?? '';


if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $course_name = trim($course_name);
    $description = trim($description);
    // تحويل المدة إلى رقم صحيح أو NULL
    $duration_weeks = is_numeric($duration_weeks) && $duration_weeks > 0 ? (int)$duration_weeks : null;
    $price = trim($price); // يتم التحقق من رقمه لاحقاً

    // التحقق من صحة المدخلات
    if ($course_name === '' || $price === '') {
        $error = __('error_missing_fields');
    } elseif (!is_numeric($price) || $price < 0) {
        $error = __('error_invalid_price');
    } else {
        try {
            // إعداد وتنفيذ الاستعلام
            $stmt = $conn->prepare("INSERT INTO courses (course_name, description, price, duration_weeks) VALUES (?, ?, ?, ?)");
            if ($stmt->execute([$course_name, $description, $price, $duration_weeks])) {
                // استخدام ميزة استبدال المتغيرات في الرسالة
                $success = __('success_add_course_msg', ['name' => htmlspecialchars($course_name)]);
                
                // تفريغ المتغيرات بعد النجاح
                $course_name = $description = $price = $duration_weeks = '';
            } else {
                $error = __('error_db_insert');
            }
        } catch (PDOException $e) {
            $error = __('error_db_general') . $e->getMessage();
        }
    }
}
?>

<?php include('../includes/navbar.php'); ?>

<div class="min-h-screen bg-gray-100 dark:bg-gray-900 pb-16" dir="<?= $dir ?>">
    <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-indigo-600 dark:border-indigo-500 transform hover:scale-[1.01] transition duration-300">
            
            <h2 class="text-3xl font-extrabold mb-8 text-center text-indigo-700 dark:text-indigo-400 border-b pb-4">
                <i class="fas fa-file-circle-plus <?= $dir == 'rtl' ? 'ml-2' : 'mr-2' ?>"></i> <?= __('add_new_course') ?>
            </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" class="space-y-6">
                
                <div>
                    <label for="course_name" class="block mb-2 font-bold text-gray-700 dark:text-gray-300 <?= $align ?>">
                        <?= __('course_name') ?> <span class="text-red-500">*</span>
                    </label>
                    <input type="text" name="course_name" id="course_name" required
                        value="<?= htmlspecialchars($course_name) ?>"
                        placeholder="<?= __('placeholder_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-indigo-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 <?= $align ?>">
                        <?= __('description') ?>
                    </label>
                    <textarea name="description" id="description" rows="4"
                        placeholder="<?= __('placeholder_description') ?>"
                        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-indigo-500/50 transition shadow-inner <?= $dir == 'rtl' ? 'text-right' : 'text-left' ?>"><?= htmlspecialchars($description) ?></textarea>
                </div>

                <div>
                    <label for="price" class="block mb-2 font-bold text-gray-700 dark:text-gray-300 <?= $align ?>">
                        <?= __('price') ?> <span class="text-red-500">*</span>
                    </label>
                    <div class="relative">
                        <input type="number" step="0.01" name="price" id="price" required
                            value="<?= htmlspecialchars($price) ?>"
                            placeholder="0.00"
                            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-indigo-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 <?= $align ?>">
                        <?= __('duration') ?> (<?= __('weeks') ?>)
                    </label>
                    <input type="number" name="duration_weeks" id="duration_weeks" min="1"
                        value="<?= htmlspecialchars($duration_weeks) ?>"
                        placeholder="<?= __('placeholder_duration') ?>"
                        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-indigo-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-indigo-600 text-white w-full sm:w-auto px-8 py-3 rounded-xl hover:bg-indigo-700 transition font-extrabold shadow-lg transform hover:scale-[1.02] order-1">
                        <i class="fas fa-save <?= $dir == 'rtl' ? 'ml-2' : 'mr-2' ?>"></i> <?= __('save_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'); ?>