<?php
// admin/edit-course.php - Edit Course
session_start();
require_once '../includes/config.php';

if(!isset($_SESSION['user_id']) || $_SESSION['user_role'] != 'admin') {
    header('Location: ../auth.php');
    exit;
}

$course_id = $_GET['id'] ?? 0;
$message = '';

// Get course
$stmt = $pdo->prepare("SELECT * FROM courses WHERE id = ?");
$stmt->execute([$course_id]);
$course = $stmt->fetch();

if(!$course) {
    header('Location: courses.php');
    exit;
}

// Handle update
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    $title_bn = trim($_POST['title_bn']);
    $title_en = trim($_POST['title_en']);
    $category_id = $_POST['category_id'];
    $description_bn = $_POST['description_bn'];
    $price = $_POST['price'];
    $duration = $_POST['duration'];
    $level = $_POST['level'];
    $status = $_POST['status'];
    
    // Handle thumbnail
    $thumbnail = $course['thumbnail'];
    if(isset($_FILES['thumbnail']) && $_FILES['thumbnail']['error'] == 0) {
        $ext = strtolower(pathinfo($_FILES['thumbnail']['name'], PATHINFO_EXTENSION));
        if(in_array($ext, ['jpg', 'jpeg', 'png', 'webp'])) {
            // Delete old thumbnail
            if(!empty($course['thumbnail']) && file_exists('../uploads/courses/' . $course['thumbnail'])) {
                unlink('../uploads/courses/' . $course['thumbnail']);
            }
            $thumbnail = time() . '_' . uniqid() . '.' . $ext;
            move_uploaded_file($_FILES['thumbnail']['tmp_name'], '../uploads/courses/' . $thumbnail);
        }
    }
    
    $stmt = $pdo->prepare("
        UPDATE courses SET 
            category_id = ?, title_bn = ?, title_en = ?, 
            description_bn = ?, price = ?, duration = ?, 
            level = ?, status = ?, thumbnail = ?
        WHERE id = ?
    ");
    $stmt->execute([$category_id, $title_bn, $title_en, $description_bn, $price, $duration, $level, $status, $thumbnail, $course_id]);
    
    $message = '✅ কোর্স আপডেট হয়েছে!';
    // Refresh course data
    $stmt = $pdo->prepare("SELECT * FROM courses WHERE id = ?");
    $stmt->execute([$course_id]);
    $course = $stmt->fetch();
}

$categories = $pdo->query("SELECT * FROM categories ORDER BY name_bn")->fetchAll();
?>
<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <title>এডিট কোর্স - <?= htmlspecialchars($course['title_bn']) ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root { --primary: #6C5CE7; --gradient: linear-gradient(135deg, #667eea, #764ba2); }
        body { background: #f5f6fa; font-family: 'Segoe UI', Arial, sans-serif; }
        .main-card { background: white; border-radius: 20px; padding: 30px; box-shadow: 0 5px 25px rgba(0,0,0,0.08); max-width: 800px; margin: 30px auto; }
        .form-control, .form-select { border-radius: 10px; padding: 10px 15px; border: 2px solid #e0e0e0; }
        .form-control:focus, .form-select:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(108,92,231,0.1); }
        .btn-primary { background: var(--gradient); border: none; border-radius: 50px; padding: 10px 30px; font-weight: 600; }
        .current-thumb { max-width: 300px; border-radius: 10px; margin-bottom: 10px; }
    </style>
</head>
<body>
<div class="container">
    <div class="main-card">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h4><i class="fas fa-edit text-primary me-2"></i>এডিট কোর্স</h4>
            <a href="courses.php" class="btn btn-outline-secondary rounded-pill">← ফিরে যান</a>
        </div>
        
        <?php if($message): ?>
            <div class="alert alert-success rounded-pill"><?= $message ?></div>
        <?php endif; ?>
        
        <form method="POST" enctype="multipart/form-data">
            <div class="row">
                <div class="col-md-6 mb-3">
                    <label class="form-label fw-bold">কোর্সের নাম (বাংলা) *</label>
                    <input type="text" name="title_bn" class="form-control" value="<?= htmlspecialchars($course['title_bn']) ?>" required>
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label fw-bold">Course Name (English) *</label>
                    <input type="text" name="title_en" class="form-control" value="<?= htmlspecialchars($course['title_en']) ?>" required>
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label fw-bold">ক্যাটাগরি</label>
                    <select name="category_id" class="form-select" required>
                        <?php foreach($categories as $cat): ?>
                            <option value="<?= $cat['id'] ?>" <?= $course['category_id'] == $cat['id'] ? 'selected' : '' ?>>
                                <?= htmlspecialchars($cat['name_bn']) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-md-3 mb-3">
                    <label class="form-label fw-bold">মূল্য (৳)</label>
                    <input type="number" name="price" class="form-control" value="<?= $course['price'] ?>">
                </div>
                <div class="col-md-3 mb-3">
                    <label class="form-label fw-bold">সময়কাল</label>
                    <input type="text" name="duration" class="form-control" value="<?= htmlspecialchars($course['duration']) ?>" placeholder="যেমন: ৩ মাস">
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label fw-bold">লেভেল</label>
                    <select name="level" class="form-select">
                        <option value="beginner" <?= $course['level']=='beginner'?'selected':'' ?>>বিগিনার</option>
                        <option value="intermediate" <?= $course['level']=='intermediate'?'selected':'' ?>>ইন্টারমিডিয়েট</option>
                        <option value="advanced" <?= $course['level']=='advanced'?'selected':'' ?>>অ্যাডভান্সড</option>
                    </select>
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label fw-bold">স্ট্যাটাস</label>
                    <select name="status" class="form-select">
                        <option value="published" <?= $course['status']=='published'?'selected':'' ?>>✅ পাবলিশড</option>
                        <option value="draft" <?= $course['status']=='draft'?'selected':'' ?>>📝 ড্রাফট</option>
                        <option value="archived" <?= $course['status']=='archived'?'selected':'' ?>>📦 আর্কাইভড</option>
                    </select>
                </div>
                <div class="col-12 mb-3">
                    <label class="form-label fw-bold">বিবরণ (বাংলা)</label>
                    <textarea name="description_bn" class="form-control" rows="5"><?= htmlspecialchars($course['description_bn']) ?></textarea>
                </div>
                <div class="col-12 mb-3">
                    <label class="form-label fw-bold">থাম্বনেইল</label>
                    <?php if(!empty($course['thumbnail'])): ?>
                        <img src="../uploads/courses/<?= $course['thumbnail'] ?>" class="current-thumb d-block">
                    <?php endif; ?>
                    <input type="file" name="thumbnail" class="form-control" accept="image/*">
                    <small class="text-muted">পরিবর্তন না করতে চাইলে খালি রাখুন</small>
                </div>
            </div>
            <button type="submit" class="btn btn-primary btn-lg w-100">
                <i class="fas fa-save me-2"></i>আপডেট সংরক্ষণ করুন
            </button>
        </form>
    </div>
</div>
</body>
</html>