<?php 
require ("top.php");
$title = '';
$msg = '';

if (isset($_GET['id']) && $_GET['id'] != '') {
    $id = mysqli_real_escape_string($con, $_GET['id']);
    $res = mysqli_query($con, "SELECT * FROM course_title WHERE id='$id'");
    $check = mysqli_num_rows($res);
    if ($check > 0) {
        $row = mysqli_fetch_assoc($res);
        $title = $row['title']; 
    } else {
        header("location: course_title.php");
        die();
    }
}

if (isset($_POST['submit'])) {
    $title = mysqli_real_escape_string($con, $_POST['title']);
    
    // Check if the title already exists
    $res = mysqli_query($con, "SELECT * FROM course_title WHERE title='$title'");
    $check = mysqli_num_rows($res);
    if ($check > 0) {
        if (isset($_GET['id']) && $_GET['id'] != '') {
            $getData = mysqli_fetch_assoc($res);
            if ($id == $getData['id']) {
                // No error if the title is the same as the current one
            } else {
                $msg = "Title already exists.";
            }
        } else {
            $msg = "Title already exists.";
        }
    }

    // If no errors, update or insert the course title
    if ($msg == '') {
        if (isset($_GET['id']) && $_GET['id'] != '') {
            // Update existing course title
            $update_sql = "UPDATE course_title SET title=? WHERE id=?";
            if ($stmt = mysqli_prepare($con, $update_sql)) {
                mysqli_stmt_bind_param($stmt, "si", $title, $id);
                mysqli_stmt_execute($stmt);
                mysqli_stmt_close($stmt);
                $msg = "Course title updated successfully!";
            }
        } else {
            // Insert new course title
            $insert_sql = "INSERT INTO course_title (title, video_count, status) VALUES (?, 0, 0)";
            if ($stmt = mysqli_prepare($con, $insert_sql)) {
                mysqli_stmt_bind_param($stmt, "s", $title);
                mysqli_stmt_execute($stmt);
                mysqli_stmt_close($stmt);
                $msg = "New course title added successfully!";
            }
        }
        header("location: course_title.php");
        die();
    }
}
?>

<div class="container-fluid d-flex align-items-center justify-content-center vh-100">
    <div class="col-md-8">
        <div class="card shadow-lg border-0 rounded transparent-card">
            <div class="card-header bg-dark text-white text-center">
                <h4><i class="fa fa-book"></i> Create Course Title</h4>
            </div>
            <form method="post" onsubmit="return validateForm()">
                <div class="card-body">
                    <div class="form-group">
                        <label class="font-weight-bold"><i class="fa fa-pencil"></i> Course Title</label>
                        <input type="text" name="title" class="form-control form-control-lg rounded-pill" 
                               placeholder="Enter Course Title" value="<?php echo $title ?>" required>
                    </div>
                    <button class="btn btn-primary btn-block mt-3 rounded-pill font-weight-bold" name="submit">
                        <i class="fa fa-save"></i> Submit
                    </button>
                    <div class="text-danger text-center mt-3 font-weight-bold"><?php echo $msg ?></div>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Include Bootstrap and Font Awesome -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.3/dist/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>

<script>
    // JavaScript function to validate the form
    function validateForm() {
        var title = document.querySelector('[name="title"]').value;
        if (title == '') {
            alert('Course title cannot be empty.');
            return false;
        }
        return true;
    }
</script>

<style>
    /* Custom Styling */
    body {
        background: url('../img/reback.jpg') no-repeat center center fixed;
        background-size: cover;
        display: flex;
        justify-content: center;
        align-items: center;
        font-family: 'Arial', sans-serif;
    }

    /* Transparent Card Style */
    .transparent-card {
        background: rgba(0, 0, 0, 0.7);
        border-radius: 15px;
        box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.2);
        text-align: center;
        backdrop-filter: blur(10px);
        padding: 20px;
        color: white;
    }

    /* Card Header Style */
    .card-header {
        background: linear-gradient(90deg, #0066cc, #003366);
        font-size: 20px;
        padding: 15px;
        border-radius: 15px 15px 0 0;
    }

    /* Form Styling */
    .form-control-lg {
        font-size: 18px;
        padding: 10px;
        border: 2px solid #007bff;
        background: rgba(255, 255, 255, 0.2);
        color: white;
    }

    .form-control-lg::placeholder {
        color: rgba(255, 255, 255, 0.8);
    }

    /* Button Styling */
    .btn-primary {
        font-size: 18px;
        text-transform: uppercase;
        letter-spacing: 1px;
        background: linear-gradient(45deg, #007bff, #0056b3);
        border: none;
    }

    .btn-primary:hover {
        background: linear-gradient(45deg, #0056b3, #004080);
    }

    /* Mobile Responsiveness */
    @media (max-width: 768px) {
        .col-md-6 {
            width: 90%;
        }
    }
</style>

</body>
</html>
