<?php
    session_start();
    include "../../db.php";
    date_default_timezone_set('Asia/Kolkata');
    $currentTime = date('jS  F Y');

    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>

<head>
    <title>Write a Blog</title>

    <!-- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> -->
    <link href="css/sb-admin-2.min.css" rel="stylesheet">
    <link href="css/all.min.css" rel="stylesheet" type="text/css">
    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/jquery.easing.min.js"></script>
    <script src="js/sb-admin-2.min.js"></script>
    <script src="js/custom.js"></script>

    <script src="vendor/ckeditor.js"></script>
</head>

<body>
    <?php include "includes/topbar.php"; ?>
    <div class="">
        <h3 class="text-center mb-4" style="margin-top: 80px">Write a Blog</h3>
        <div class="card">
            <div class="card-body">
                <?php
                    if ( isset($_GET["edit_id"]) ) {
                        $edit_sql = "SELECT * FROM blog AS b INNER JOIN category AS c ON b.bcat = c.cid WHERE bid = '$_GET[edit_id]'";
                        $edit_run = mysqli_query($con, $edit_sql);
                        $edit_row = mysqli_fetch_assoc($edit_run);
                        ?>
                        <form method="post">
                            <select name="bcat" id="bcat" class="form-control mb-3">
                                <option selected disabled> Previous Category : <?php echo $edit_row["cname"]; ?> </option>
                                <?php
                                    $fetch_memby = mysqli_query($con, "SELECT memberby FROM users WHERE uid = '$_SESSION[uid]'");
                                    $memberby_row = mysqli_fetch_assoc($fetch_memby);
                                    $sql = "SELECT cname, cid FROM category WHERE catby = '$memberby_row[memberby]' ORDER BY cname ASC";
                                    $run = mysqli_query($con, $sql);

                                    while ( $cat_rows = mysqli_fetch_assoc($run) ) {
                                        echo '
                                        <option value="'.$cat_rows["cid"].'"> '.$cat_rows["cname"].' </option>
                                        ';
                                    }
                                ?>
                            </select>
                            <textarea name="content" id="content" class="form-control ckeditor" required><?php echo $edit_row["blog"]; ?></textarea>
                            <hr>
                            <button type="submit" class="btn btn-success my-4" name="update_blog"> <i class="fas fa-cloud-upload-alt"></i> Update </button>

                            <a href="review-blog.php" class="btn btn-warning my-4"> Cancel </a>
                        </form>
                        <?php
                    } else {
                        ?>
                        <form method="post">
                            <select name="bcat" id="bcat" class="form-control mb-3" required>
                                <option selected disabled> Select Category </option>
                                <?php
                                    $fetch_memby = mysqli_query($con, "SELECT memberby FROM users WHERE uid = '$_SESSION[uid]'");
                                    $memberby_row = mysqli_fetch_assoc($fetch_memby);
                                    $sql = "SELECT cname, cid FROM category WHERE catby = '$memberby_row[memberby]' ORDER BY cname ASC";
                                    $run = mysqli_query($con, $sql);

                                    while ( $cat_rows = mysqli_fetch_assoc($run) ) {
                                        echo '
                                        <option value="'.$cat_rows["cid"].'"> '.$cat_rows["cname"].' </option>
                                        ';
                                    }
                                ?>
                            </select>
                            <input type="text" name="btitle" class="form-control mb-3" placeholder="Enter Blog Title">
                            <textarea name="content" id="content" class="form-control ckeditor" required></textarea>
                            <hr>
                            <button type="submit" class="btn btn-success my-4" name="upload_blog"> <i class="fas fa-cloud-upload-alt"></i> Save Changes </button>

                            <a href="index.php" class="btn btn-warning my-4"> Cancel </a>
                        </form>
                        <?php
                    }
                ?>
            </div>
        </div>
    </div>
</body>
</html>

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

<?php
    if(isset($_POST['upload_blog'])){
        $blog = $_POST["content"];
        $btitle = $_POST["btitle"];
        $bcat = $_POST["bcat"];
        if ( $blog != '' ) {

            $fetch_memby = mysqli_query($con, "SELECT memberby FROM users WHERE uid = '$_SESSION[uid]'");
            $memberby_row = mysqli_fetch_assoc($fetch_memby);
            $badmin2 = $memberby_row["memberby"];

            $ins_sql = "INSERT INTO blog (bby, btitle, blog, bcat, badmin2, bcreated) VALUES ('$_SESSION[uid]', '$btitle', '$blog', '$bcat', '$badmin2', '$currentTime')";
            if(mysqli_query($con, $ins_sql)){
                ?>
                <script>window.location = 'review-blog.php';</script>
                <?php
            }
            else{
                ?>
                <script>
                    alert('OOPS !!!!');
                    setTimeout(() => {
                        window.location = 'blog.php';
                    }, 500);
                </script>
                <?php
            }
        } else {
            ?>
            <script>
                alert('Content is Empty');
                setTimeout(() => {
                    window.location = 'blog.php';
                }, 500);
            </script>
            <?php
        }
    }
    if(isset($_POST['update_blog'])){
        $blog = $_POST["content"];
        // 
        if ( $blog != '' ) {
            // $ins_sql = "INSERT INTO blog (bby, blog, bcat, bcreated) VALUES ('$_SESSION[uid]', '$blog', '$bcat', '$currentTime')";
            $up_sql = "UPDATE blog SET blog = '$blog'";

            if ( isset($_POST["bcat"]) ) {
                $bcat = $_POST["bcat"];
                $up_sql .= ", bcat = '$bcat'";
            }

            $up_sql .= " WHERE bid = '$_GET[edit_id]'";
            if(mysqli_query($con, $up_sql)){
                ?>
                <script>window.location = 'review-blog.php';</script>
                <?php
            }
            else{
                ?>
                <script>
                    alert('OOPS !!!!');
                    setTimeout(() => {
                        window.location = 'blog.php';
                    }, 500);
                </script>
                <?php
            }
        } else {
            ?>
            <script>
                alert('Content is Empty');
                setTimeout(() => {
                    window.location = 'blog.php';
                }, 500);
            </script>
            <?php
        }
    }
?>