<?php
session_start();
include "../db.php";
date_default_timezone_set('Asia/Kolkata');
$currentTime = date('M j, Y g:i a');

if (!isset($_SESSION['uid']) || !isset($_SESSION['uref']) || !isset($_SESSION['utype']) || !isset($_SESSION['uname'])) {
    header("Location: logout.php");
}
if(isset($_SESSION['cid'])) {
    // Assign 'cid' value to a session variable
    $cid = $_SESSION['cid'] ;
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <?php include "includes/meta.php"; ?>
    <?php include "includes/css.php"; ?>
</head>

<body id="page-top" class="mt-55">
    <?php
    if (!isset($_GET['n_id']) || empty($_GET['n_id'])) {
        header("Location: notes-upload.php");
    }

    $n_id = $_GET['n_id'];
    $sql = mysqli_query($con, "SELECT * FROM notes WHERE n_id = '$n_id'");
    if ($row = mysqli_fetch_assoc($sql)) {
        $note_title = $row["n_title"];
        $n_class = $row["n_class"];
    }
    ?>
    <div style="margin-top: 130px;"></div>
    <?php include "includes/header2.php"; ?>
    <?php 
    if(isset($_GET["edit_id"])){
        $edit_id = $_GET["edit_id"];
        $get_sql = mysqli_query($con, "SELECT * FROM practice_q WHERE pq_for = '$n_id' AND pq_id = '$edit_id' ");
        if($get_row = mysqli_fetch_assoc($get_sql)){
        ?>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card shadow mt-3">
                    <h5 class="card-header text-dark text-center font-weight-bold">Add Practice Question Of -
                        <?php echo $note_title; ?></h5>
                    <div class="card-body">
                        <form method="POST" action="" enctype="multipart/form-data">
                            <div class="row mt-4">
                                <div class="col-md-4 mb-3">
                                    <label for="video">Select Question Type</label>
                                    <select name="pq_type" class="form-control">
                                        <option value="<?php echo $get_row["pq_type"];?>">
                                            <?php echo $get_row["pq_type"];?></option>
                                        <option value="Easy">Easy</option>
                                        <option value="Medium">Medium</option>
                                        <option value="Hard">Hard</option>
                                    </select>
                                </div>
                                <div class="col-md-8 mb-3">
                                    <label for="video">Enter Question</label>
                                    <input type="text" class="form-control" name="pq_qus"
                                        value="<?php echo $get_row["pq_qus"];?>" required>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="video">Enter Answer</label>
                                    <input type="text" class="form-control" name="pq_ans"
                                        value="<?php echo $get_row["pq_ans"];?>" required>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="video">Option one</label>
                                    <input type="text" class="form-control" name="pq_opt_one"
                                        value="<?php echo $get_row["pq_opt_one"];?>" required>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="video">Option two</label>
                                    <input type="text" class="form-control" name="pq_opt_two"
                                        value="<?php echo $get_row["pq_opt_two"];?>" required>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="three">Option three</label>
                                    <input type="text" class="form-control" name="pq_opt_three "
                                        value="<?php echo $get_row["pq_opt_three"];?>" required>
                                </div>
                                <div class="col-md-12 mb-3">
                                    <label for="video">Answer Description</label>
                                    <textarea name="content" class="form-control ckeditor"
                                        id="content"><?php echo $get_row["pq_ans_des"];?></textarea>
                                </div>
                            </div>

                            <div class="row justify-content-center">
                                <input type="hidden" value="<?php echo $n_id; ?>" name="note_id">
                                <input type="hidden" value="<?php echo $note_title; ?>" name="note_title">
                                <button type="submit" name="practice_q_update"
                                    class="btn btn-success text-sm-center mt-4">Update Question</button>
                                &nbsp;&nbsp;&nbsp;
                                <a href="notes-practice-set.php?n_id=<?php echo $n_id;?>"
                                    class="btn btn-warning text-sm-center mt-4">Cancel</a>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php }
    } elseif(isset($_GET["view_id"])){
        $view_id = $_GET["view_id"];
        $view_sql = mysqli_query($con, "SELECT * FROM practice_q WHERE pq_for = '$n_id' AND pq_id = '$view_id' ");
        if($view_row = mysqli_fetch_assoc($view_sql)){
        ?>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-12">
                <div class="card shadow mt-3">
                    <h5 class="card-header text-dark text-center font-weight-bold">Add Practice Question Of -
                        <?php echo $note_title; ?></h5>
                    <div class="card-body">
                        <p><strong>Question Type :</strong> <?php echo $view_row["pq_type"];?></p>
                        <p><strong>Question :</strong> <?php echo $view_row["pq_qus"];?></p>
                        <p><strong>Answer :</strong> <?php echo $view_row["pq_ans"];?></p>
                        <p><strong>Option One :</strong> <?php echo $view_row["pq_opt_one"];?></p>
                        <p><strong>Option two :</strong> <?php echo $view_row["pq_opt_two"];?></p>
                        <p><strong>Option three :</strong> <?php echo $view_row["pq_opt_three"];?></p>
                        <p><strong>Answer Description :</strong> <?php echo $view_row["pq_ans_des"];?></p>
                        <a href="notes-practice-set.php?n_id=<?php echo $n_id;?>"
                            class="btn btn-warning text-sm-center mt-4">Cancel</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php }
    } else{
    ?>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card shadow mt-3">
                    <h5 class="card-header text-dark text-center font-weight-bold">Add Practice Question Of -
                        <?php echo $note_title; ?></h5>
                    <div class="card-body">
                        <form method="POST" action="" enctype="multipart/form-data">
                            <div class="row mt-4">
                                <div class="col-md-4 mb-3">
                                    <label for="video">Select Question Type</label>
                                    <select name="pq_type" class="form-control">
                                        <option value="">Select From Dropdown</option>
                                        <option value="Easy">Easy</option>
                                        <option value="Medium">Medium</option>
                                        <option value="Hard">Hard</option>
                                    </select>
                                </div>
                                <div class="col-md-8 mb-3">
                                    <label for="video">Enter Question</label>
                                    <input type="text" class="form-control" name="pq_qus" placeholder="Enter Question"
                                        required>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="video">Enter Answer</label>
                                    <input type="text" class="form-control" name="pq_ans"
                                        placeholder="Enter Correct answer" required>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="video">Option one</label>
                                    <input type="text" class="form-control" name="pq_opt_one"
                                        placeholder="Enter Option one" required>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="video">Option two</label>
                                    <input type="text" class="form-control" name="pq_opt_two"
                                        placeholder="Enter Option two" required>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="video">Option three</label>
                                    <input type="text" class="form-control" name="pq_opt_three "
                                        placeholder="Enter Option three" required>
                                </div>
                                <div class="col-md-12 mb-3">
                                    <label for="video">Answer Description</label>
                                    <textarea name="content" class="form-control ckeditor" id="content"
                                        placeholder="Write Answer Description"></textarea>
                                </div>
                            </div>

                            <div class="row justify-content-center">
                                <input type="hidden" value="<?php echo $n_id; ?>" name="note_id">
                                <input type="hidden" value="<?php echo $note_title; ?>" name="note_title">
                                <button type="submit" name="practice_q_submit"
                                    class="btn btn-success text-sm-center mt-4">Upload Question</button>
                                &nbsp;&nbsp;&nbsp;
                                <a href="view-notes.php?cid=<?php echo $n_class;?>"
                                    class="btn btn-warning text-sm-center mt-4">Cancel</a>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <?php
    $display_sql = mysqli_query($con, "SELECT * FROM practice_q WHERE pq_for = '$n_id' ");
    if(mysqli_num_rows($display_sql) > 0){
    ?>
    <div class="container mt-5">
        <div class="card p-5">
            <h4 class="text-center">Question List of <?php echo $note_title;?></h4>
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <th scope="col">#</th>
                        <th scope="col">Question</th>
                        <th scope="col">Answer</th>
                        <th scope="col">Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $c=1;
                    while($display_row = mysqli_fetch_assoc($display_sql)){
                    ?>
                    <tr>
                        <th scope="row"><?php echo $c . '> (' . $display_row["pq_type"] . ')' ;?></th>
                        <td><?php echo $display_row["pq_qus"];?></td>
                        <td><?php echo $display_row["pq_ans"];?></td>
                        <td>
                            <a class="btn btn-primary"
                                href="notes-practice-set.php?n_id=<?php echo $n_id;?>&edit_id=<?php echo $display_row["pq_id"];?>">
                                <i class="fa fa-edit"></i> Edit</a>
                            <a class="btn btn-info"
                                href="notes-practice-set.php?n_id=<?php echo $n_id;?>&view_id=<?php echo $display_row["pq_id"];?>">
                                <i class="fa fa-eye"></i> View</a>
                            <a class="btn btn-danger"
                                href="notes-practice-set.php?n_id=<?php echo $n_id;?>&del_id=<?php echo $display_row["pq_id"];?>">
                                <i class="fa fa-trash"></i> Delete</a>
                        </td>
                    </tr>
                    <?php $c++; }?>
                </tbody>
            </table>
        </div>
    </div>
    <?php }?>
    <?php }?>

    <div style="margin-top: 120px;"></div>
    <?php include "includes/ft.php"; ?>
    <?php include "includes/js.php"; ?>
    <script>
    CKEDITOR.replace('content', {
        height: 200,
        filebrowserUploadUrl: "upload.php"
    });
    </script>
</body>

</html>

<?php
if(isset($_POST['practice_q_submit'])) {
    $note_id = $_POST["note_id"];
    $note_title = $_POST["note_title"];
    $pq_type = $_POST["pq_type"];
    $pq_qus = $_POST["pq_qus"];
    $pq_ans = $_POST["pq_ans"];
    $pq_opt_one = $_POST["pq_opt_one"];
    $pq_opt_two = $_POST["pq_opt_two"];
    $pq_opt_three = $_POST["pq_opt_three"];
    $pq_ans_des = $_POST["content"];
    $pq_recycle = 1;

    // Check if file is uploaded
    $insert_query = "INSERT INTO practice_q (pq_type, pq_for, pq_qus, pq_ans, pq_opt_one, pq_opt_two, pq_opt_three, pq_ans_des, pq_creatdate, pq_recycle) VALUES ('$pq_type', '$note_id', '$pq_qus', '$pq_ans', '$pq_opt_one', '$pq_opt_two', '$pq_opt_three', '$pq_ans_des', '$currentTime', '$pq_recycle')";
    $ins_q = mysqli_query($con, $insert_query); // Execute the insert query

    
    $n_id = $note_id;
    // If at least one file was successfully uploaded
    if($ins_q) {
        // Show success alert and redirect
        echo '<script>alert("Question Upload successful!");</script>';
        echo '<script>window.location = "notes-practice-set.php?n_id=' . $n_id. '";</script>';
        // Unset the session variable
        unset($_SESSION['cid']);
        exit; // Stop further execution
    } else {
        echo '<script>alert("No Question uploaded or all Question failed to upload.");</script>';
    }
}
?>
<?php
if(isset($_POST['practice_q_update'])) {
    $note_id = $_POST["note_id"]; 
    $edit_id = $_GET["edit_id"]; 

    $pq_type = $_POST["pq_type"];
    $pq_qus = $_POST["pq_qus"];
    $pq_ans = $_POST["pq_ans"];
    $pq_opt_one = $_POST["pq_opt_one"];
    $pq_opt_two = $_POST["pq_opt_two"];
    $pq_opt_three = $_POST["pq_opt_three"];
    $pq_ans_des = $_POST["content"];

    // Check if file is uploaded
    $update_query = "UPDATE practice_q SET pq_type = '$pq_type', pq_qus = '$pq_qus', pq_ans = '$pq_ans', pq_opt_one = '$pq_opt_one', pq_opt_two = '$pq_opt_two', pq_opt_three = '$pq_opt_three', pq_ans_des = '$pq_ans_des' WHERE pq_id = '$edit_id' " ;
    $ins_q = mysqli_query($con, $update_query); // Execute the insert query

    
    $n_id = $note_id;

    // If at least one file was successfully uploaded
    if($ins_q) {
        // Show success alert and redirect
        echo '<script>alert("Question Upload successful!");</script>';
        echo '<script>window.location = "notes-practice-set.php?n_id=' . $n_id. '";</script>';
        // Unset the session variable
        unset($_SESSION['cid']);
        exit; // Stop further execution
    } else {
        echo '<script>alert("No Question uploaded or all Question failed to upload.");</script>';
    }
}
?>

<?php
if(isset($_GET["n_id"]) && isset($_GET["del_id"])){
    $del_id = $_GET["del_id"];

    $del_sql = "DELETE FROM practice_q WHERE pq_id = '$del_id' ";

    $ins_q = mysqli_query($con, $del_sql); 

    
    $n_id = $_GET["n_id"];

    if($ins_q) {
        // Show success alert and redirect
        echo '<script>alert("Question Deleted successful!");</script>';
        echo '<script>window.location = "notes-practice-set.php?n_id=' . $n_id. '";</script>';
        // Unset the session variable
        unset($_SESSION['cid']);
        exit; // Stop further execution
    } else {
        echo '<script>alert("Failed to Delete.");</script>';
    }
}
?>