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

    if ( !isset($_SESSION['uid']) && !isset($_SESSION['uref']) && !isset($_SESSION['utype']) ) {
        header("Location: logout.php");
        exit();
    }

    if ( $_SESSION['utype'] != 'master_admin' && $_SESSION['uref'] != 'fssdf4543frts' ) {
        header("Location: logout.php");
        exit();
    }
?>

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

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

<body>
    <!--  Header -->
    <?php 
    include "include/header.php"; 
    ?>
    <!-- #header -->

    <section id="inner-body">
        <div class="body-content">
            <div class="container-fluid">
                <?php
                        if ( isset($_GET["faq"]) ) {
                        $run = mysqli_query($con, "SELECT * FROM clg_course  WHERE sl_no = '$_GET[faq]'");
                        $row = mysqli_fetch_assoc($run);
                        $name = $row['course_name'];
                        $get_id = $row['course_id'];
                    ?>
                <?php
                    if(isset($_GET['edit_id'])){
                        $id = $_GET['edit_id'];
                        $find_edit_run = mysqli_query($con, "SELECT * FROM faq WHERE faq_sl_no = '$id'");
                        $find_edit_row = mysqli_fetch_assoc($find_edit_run);
                    ?>
                <div class="card">
                    <div class="card-header">
                        <h5 class="m-0 text-center"> <?php echo $name; ?></h5>
                    </div>
                    <div class="card-body">
                        <form class="user" method="post" enctype="multipart/form-data">
                            <div class="col-md-12 mb-3">
                                <label for="faq_q" class="form-label">FAQ Question</label>
                                <input type="text" class="form-control form-control-sm mb-2"
                                    value="<?php echo $find_edit_row["faq_q"]; ?>" name="faq_q">
                            </div>
                            <div class="col-md-12 mb-3">
                                <label class="form-label" for="content">FAQ Answer</label>
                                <textarea class="form-control ckeditor" name="content" id="content"
                                    rows="3"><?php echo $find_edit_row['faq_ans'];?></textarea>
                            </div>
                            <div class="col-md-12">
                                <button type="submit" class="btn btn-success btn-user" name="Editsubmit">
                                    <i class="fa fa-edit"></i>Edit FAQ
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
                <?php
                    }
                    elseif(isset($_GET['view_id'])){
                        $id = $_GET['view_id'];
                        $find_edit_run = mysqli_query($con, "SELECT * FROM faq WHERE faq_sl_no = '$id'");
                        $find_edit_row = mysqli_fetch_assoc($find_edit_run);
                        ?>
                <div class="card mt-3">
                    <div class="card-header">
                        <h5 class="m-0 text-center"> <?php echo $name; ?></h5>
                    </div>
                    <div class="card-body">
                        <div class="mb-3">
                            <h5>FAQ Question:</h5><span><?php echo $find_edit_row["faq_q"]; ?></span>
                        </div>
                        <div>
                            <h5>FAQ Answer:</h5>
                            <p><?php echo $find_edit_row['faq_ans'];?></p>
                        </div>
                    </div>
                </div>
                <?php
                    }
                    else {
                    ?>
                <div class="card mt-3">
                    <div class="card-header">
                        <h5 class="m-0 text-center"> <?php echo $row["course_name"]; ?></h5>
                    </div>
                    <div class="card-body">
                        <form class="user" method="post" enctype="multipart/form-data">
                            <div class="row">
                                <div class="col-md-12 mb-3">
                                    <label for="faq_q" class="form-label">FAQ Question:</label>
                                    <input type="text" class="form-control form-control-sm mb-2" value="" name="faq_q">
                                </div>
                                <div class="col-md-12 mb-3">
                                    <label class="form-label" for="content">FAQ Answer:</label>
                                    <textarea class="form-control ckeditor" name="content" id="content" rows="3"
                                        placeholder="Enter the answer"></textarea>
                                </div>
                                <div class="col-md-12">
                                    <button type="submit" class="btn btn-success btn-user" name="submit">
                                        <i class="fa fa-edit"></i>Add FAQ
                                    </button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>

                <div class="card mt-2">
                    <div class="row m-0" style="padding:20px;">
                        <?php 
                            if(isset($_GET['recycle_table']))
                            {
                            ?>
                        <div class="col-md-12 p-0">
                            <div class="d-flex justify-content-between mb-2">
                                <div>
                                    <h5>Recycle FAQ List</h5>
                                </div>
                                <div>
                                    <a href="faqcourse.php?faq=<?php echo $_GET['faq']; ?>"
                                        class="btn btn-primary btn-sm">Active
                                        FAQ</a>
                                </div>
                            </div>
                            <div class="table-responsive">
                                <table class="table table-hover table-bordered table-sm" id="thisismytable">
                                    <thead>
                                        <tr>
                                            <th>No.</th>
                                            <th>FAQ Question</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                            $table_sql = "SELECT * FROM faq WHERE faq_recycle = '0' AND faq_catagory = 'course' AND faq_get_id = '$get_id'  ORDER BY faq_sl_no DESC";

                                            $table_run = mysqli_query($con, $table_sql);

                                            $c = 1;
                                            while ($table_rows = mysqli_fetch_assoc($table_run)) {
                                        ?>
                                        <tr id='<?php echo $table_rows["faq_sl_no"]; ?>'>
                                            <td>
                                                <span
                                                    class='badge badge-info badge-pill text-dark'><?php echo $c; ?></span>
                                            </td>
                                            <td>
                                                <?php echo $table_rows['faq_q']; ?>
                                            </td>
                                            <td class='text-center align-middle'>
                                                <a class="btn btn-primary btn-sm" style="margin-left:5px;"
                                                    href="faqcourse.php?faq=<?php echo $_GET['faq']; ?>&view_id=<?php echo $table_rows['faq_sl_no']; ?>">
                                                    View
                                                </a>
                                                <a class="btn btn-primary btn-sm" style="margin-left:5px;"
                                                    href="faqcourse.php?faq=<?php echo $_GET['faq']; ?>&edit_id=<?php echo $table_rows['faq_sl_no']; ?>">
                                                    Edit
                                                </a>
                                                <a href="faqcourse.php?faq=<?php echo $_GET['faq']; ?>&rec_id=<?php echo $table_rows['faq_sl_no']; ?>"
                                                    class='worning m-2 btn btn-danger btn-sm rounded-circle'
                                                    title='Click to Recycle'>
                                                    <i class="fa fa-recycle" aria-hidden="true"></i>
                                                </a>
                                                <a href='faqcourse.php?faq=<?php echo $_GET['faq']; ?>&del_id=<?php echo $table_rows["faq_sl_no"]; ?>'
                                                    class='delete m-2 btn btn-danger btn-sm rounded-circle'
                                                    title='Click to Delete'>
                                                    <i class="fa fa-trash" aria-hidden="true"></i>
                                                </a>
                                            </td>
                                        </tr>
                                        <?php
                                            $c++;
                                                }
                                            ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                        <?php
                            }
                            else{
                            ?>
                        <div class="col-md-12 p-0">
                            <div class="d-flex justify-content-between mb-2">
                                <div>
                                    <h5>Active FAQ List</h5>
                                </div>
                                <div>
                                    <a href="faqcourse.php?faq=<?php echo $_GET['faq']; ?>&recycle_table"
                                        class="btn btn-info btn-sm text-white">Recycle FAQ</a>
                                </div>
                            </div>
                            <div class="table-responsive">
                                <table class="table table-hover table-bordered table-sm" id="thisismytable">
                                    <thead>
                                        <tr>
                                            <th>No.</th>
                                            <th>FAQ Question</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                            $sql = "SELECT * FROM faq WHERE faq_recycle = '1' AND faq_catagory = 'course' AND faq_get_id = '$get_id'  ORDER BY faq_sl_no DESC";
                                            $run = mysqli_query($con, $sql);

                                            $c = 1;
                                            while ( $rows = mysqli_fetch_assoc($run) ) {                                                   
                                                echo "
                                                <tr id='$rows[faq_sl_no]'>
                                                <td>
                                                <span class='badge badge-info badge-pill text-dark'>$c</span>
                                                </td>
                                                <td>
                                                $rows[faq_q]
                                                </td>";
                                            ?>
                                        <td class='text-center align-middle'>
                                            <a class="btn btn-primary btn-sm" style="margin-left:5px;"
                                                href="faqcourse.php?faq=<?php echo $_GET['faq'];?>&view_id=<?php echo $rows['faq_sl_no']; ?>">View</a>
                                            <a class="btn btn-primary btn-sm" style="margin-left:5px;"
                                                href="faqcourse.php?faq=<?php echo $_GET['faq'];?>&edit_id=<?php echo $rows['faq_sl_no']; ?>">
                                                Edit</a>

                                            <a href="faqcourse.php?faq=<?php echo $_GET['faq'];?>&rec_id=<?php echo $rows['faq_sl_no']; ?>"
                                                class='worning m-2 btn btn-danger btn-sm rounded-circle'
                                                title='Click to Recycle'> <i class="fa fa-recycle"
                                                    aria-hidden="true"></i>
                                            </a>
                                        </td>
                                        </tr>
                                        <?php $c++; } ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                        <?php
                            }
                        ?>
                    </div>
                </div>
                <?php } } ?>
            </div>
        </div>

        <?php include "include/footer.php"; ?>
    </section>

    <!-- JavaScript Libraries -->

    <?php include "include/js.php"; ?>
    <script>
    CKEDITOR.replace('content', {
        height: 300,
        filebrowserUploadUrl: "upload.php"
    });
    </script>

</body>

</html>


<?php
    if(isset($_POST['submit'])){ 
	
        $faq_q = mysqli_real_escape_string($con, strip_tags($_POST['faq_q']));
        $faq_ans = $_POST['content'];

        $faq_id = str_pad(rand(0, 99999999), 8, '0', STR_PAD_LEFT);
        $upload_date = date("j F, Y, g:i a");
        $recycle="1";
        $up_by= $_SESSION['utype'];
        $faq_catagory = "course";

        $find_run = mysqli_query($con, "SELECT * FROM clg_course  WHERE sl_no = '$_GET[faq]'");
        $find_row = mysqli_fetch_assoc($find_run);
        $faq_for = $find_row['course_name'];
        $faq_get_id = $find_row['course_id'];

  		
        {
            $up_sql = "INSERT INTO faq ( faq_id, faq_catagory, faq_get_id, faq_q, faq_ans, faq_upload_date, faq_up_by, faq_recycle) VALUES ( '$faq_id', '$faq_catagory', '$faq_get_id', '$faq_q', '$faq_ans', '$upload_date', '$up_by', $recycle)";
   
		 if(mysqli_query($con, $up_sql)){
            ?>
<script>
alert("Data Updated Successfully  <?php echo $faq_for;?>");
window.location = 'faqcourse.php?faq=<?php echo $find_row['sl_no']; ?>';
</script>
<?php
            }
            else{
                
                ?>
<script>
alert('OOPS !!!! <?php echo $faq_for;?>');
setTimeout(() => {
    window.location = 'faqcourse.php?faq=<?php echo $find_row['sl_no']; ?>';
}, 1200);
</script>
<?php
            }
            ?>
<?php
        }
    }
?>

<?php
    if(isset($_POST['Editsubmit'])){ 
	
        $faq_q = mysqli_real_escape_string($con, strip_tags($_POST['faq_q']));
        $faq_ans = $_POST['content'];

        $edit_date = date("j F, Y, g:i a");

        $find_run = mysqli_query($con, "SELECT * FROM clg_course  WHERE sl_no = '$_GET[faq]'");
        $find_row = mysqli_fetch_assoc($find_run);

  		
        {
            $up_sql = "UPDATE faq SET faq_q='$faq_q', faq_ans='$faq_ans', faq_edit_date='$edit_date'";
   
            $up_sql .= " WHERE faq_sl_no = '$_GET[edit_id]'";
		 if(mysqli_query($con, $up_sql)){
            ?>
<script>
alert("Data Updated Successfully  <?php echo $faq_for;?>");
window.location = 'faqcourse.php?faq=<?php echo $find_row['sl_no']; ?>';
</script>
<?php
            }
            else{
                
                ?>
<script>
alert('OOPS !!!! <?php echo $faq_for;?>');
setTimeout(() => {
    window.location = 'faqcourse.php?faq=<?php echo $find_row['sl_no']; ?>';
}, 1200);
</script>
<?php
            }
            ?>
<?php
        }
    }
?>

<?php
  if ( isset($_GET["del_id"]) ) {
    $find_run = mysqli_query($con, "SELECT * FROM clg_course  WHERE sl_no = '$_GET[faq]'");
    $find_row = mysqli_fetch_assoc($find_run);

    $find1_sql = "SELECT * FROM faq WHERE faq_sl_no = '$_GET[del_id]'";
    $find1_run = mysqli_query($con, $find1_sql);
    
        $del_sql = "DELETE FROM faq WHERE faq_sl_no = '$_GET[del_id]'";
        $del_run = mysqli_query($con, $del_sql);

        if ( $del_run ) {
            ?>
<script>
alert("Deleted Successfully");
window.location = "faqcourse.php?faq=<?php echo $find_row['sl_no']; ?>";
</script>
<?php
        } else {
            ?>
<script>
alert('OOPS !!!!');
setTimeout(() => {
    window.location = 'faqcourse.php?faq=<?php echo $find_row['sl_no']; ?>';
}, 1200);
</script>
<?php
        }
        
    }
?>

<?php
  if ( isset($_GET["rec_id"]) ) {
    $find_run = mysqli_query($con, "SELECT * FROM clg_course  WHERE sl_no = '$_GET[faq]'");
    $find_row = mysqli_fetch_assoc($find_run);

    $find1_sql = "SELECT * FROM faq WHERE faq_sl_no = '$_GET[rec_id]'";
    $find1_run = mysqli_query($con, $find1_sql);
    if ($f_rows = mysqli_fetch_assoc($find1_run)) {
        $old_rec = $f_rows["faq_recycle"];
    
        if ($old_rec == 1) {
            $new_rec = 0;
        } elseif ($old_rec == 0) {
            $new_rec = 1;
        }
    
        $update_sql = "UPDATE faq SET faq_recycle = '$new_rec' WHERE faq_sl_no = '$_GET[rec_id]'";
        $update_run = mysqli_query($con, $update_sql);

        if ( $update_run ) {
            ?>
<script>
alert("Sent to Recycle Successfully");
window.location = "faqcourse.php?faq=<?php echo $find_row['sl_no']; ?>";
</script>
<?php
        } else {
            ?>
<script>
alert('OOPS !!!!');
setTimeout(() => {
    window.location = 'faqcourse.php?faq=<?php echo $find_row['sl_no']; ?>';
}, 1200);
</script>
<?php
        }
        
    }

}
?>