<?php

    // session_start();

    include "../db.php";

    require '../auth.php';

    restrictAccess(['master_admin']);

    date_default_timezone_set('Asia/Kolkata');

    $currentTime = date('Y-m-d H:i:s');



    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"; ?>

    <script src="https://cdn.ckeditor.com/4.6.2/standard-all/ckeditor.js"></script>

</head>



<body>

    <!--  Header -->

    <?php include "include/header.php"; ?>

    <!-- #header -->



    <section id="inner-body">

        <div class="body-content">

            <div class="container-fluid">

                <div class="card mt-3">

                    <?php 

                        $edit_sql = mysqli_query($con, "SELECT * FROM data WHERE data_catagory = 't&c' ");

                        if($edit_row = mysqli_fetch_assoc($edit_sql)){

                        ?>

                    <div class="card-header text-center">

                        <h5 class="card-title mb-0">Edit Terms And Conditions</h5>

                    </div>

                    <div class="card-body">

                        <form action="" method="POST" enctype="multipart/form-data">

                            <div class="row align-items-center justify-content-center">

                                <div class="col-md-12 mb-2">

                                    <div class="d-flex align-items-center mb-1 justify-content-between">

                                        <label for="agreementContent">Write Terms And Conditions:</label>

                                    </div>

                                    <textarea id="agreementContent" class="form-control ckeditor"

                                        name="agreementContent" rows="10" cols="80"

                                        required><?php echo $edit_row["data_long_des"];?></textarea>

                                </div>

                                <div class="col-md-12 mb-2">

                                    <input type="hidden" name="tcId" value="<?php echo $edit_row["data_sl_no"];?>">

                                    <button type="submit" class="btn btn-success" name="editAgreement">Edit Terms And

                                        Conditions</button>

                                </div>

                            </div>

                        </form>

                    </div>

                    <?php } else{?>

                    <div class="card-header text-center">

                        <h5 class="card-title mb-0">Add Terms And Conditions</h5>

                    </div>

                    <div class="card-body">

                        <form action="" method="POST" enctype="multipart/form-data">

                            <div class="row align-items-center">

                                <div class="col-md-12 mb-2">

                                    <div class="d-flex align-items-center mb-1 justify-content-between">

                                        <label for="agreementContent">Write Terms And Conditions:</label>

                                    </div>

                                    <textarea class="form-control ckeditor" name="agreementContent"

                                        id="agreementContent" rows="3" rows="10" cols="80"

                                        placeholder="Type your agreement here..."></textarea>

                                </div>

                                <div class="col-md-12 mb-2">

                                    <button type="submit" class="btn btn-success" name="submit">Save Terms And

                                        Conditions</button>

                                </div>

                            </div>

                        </form>

                    </div>

                    <?php }?>

                </div>

            </div>

        </div>



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

    </section>



    <!-- JavaScript Libraries -->

    <script>

    CKEDITOR.replace('agreementContent', {

        height: 300, // You can adjust this height as needed

        filebrowserUploadUrl: "upload.php",

    });

    </script>



    <?php include "include/js.php"; ?>

</body>



</html>



<?php

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit'])) {

    $content = $_POST['agreementContent'];

    $recycle = 1;

    $catagoty = "t&c";

    $title = " Terms And Conditions";



    $stmt = $con->prepare("INSERT INTO data (data_catagory, data_title, data_long_des, data_up_date, data_recycle) VALUES (?, ?, ?, ?, ?)");

    $stmt->bind_param("sssss", $catagoty, $title, $content, $currentTime, $recycle);



    if ($stmt->execute()) {

        echo "<script>alert(' Terms And Conditions saved successfully!'); window.location.href='t&c.php';</script>";

    } else {

        echo "<script>alert('Error saving  Terms And Conditions.'); window.location.href='t&c.php';</script>";

    }



    $stmt->close();

    $con->close();

}

?>



<?php

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['editAgreement'])) {

    $tcId = $_POST['tcId'];

    $content = $_POST['agreementContent'];



    $stmt = $con->prepare("UPDATE data SET data_long_des = ? WHERE data_sl_no = ?");

    $stmt->bind_param("si", $content, $tcId);





    if ($stmt->execute()) {

        echo "<script>alert(' Terms And Conditions saved successfully!'); window.location.href='t&c.php';</script>";

    } else {

        echo "<script>alert('Error saving  Terms And Conditions.'); window.location.href='t&c.php';</script>";

    }



    $stmt->close();

    $con->close();

}

?>