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

    $fetch_file = mysqli_query($con, "SELECT bid FROM blog ORDER BY bid DESC");

    // echo "<pre>";print_r($fetch_file);exit();
    // $count = myqsli_num_rows($fetch_file);
    // $count = 1;

    // if ( $count == 0 ) {
    //     $bref = 1;
    // } else {
        $row = mysqli_fetch_assoc($fetch_file);

        $bref = $row["bid"] + 1;
    // }
    

    // $bref = mt_rand()."_".time();
?>
<!DOCTYPE html>
<html>

<head>
 <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>Write a Blog</title>

  
    <link href="css/sb-admin-2.min.css" rel="stylesheet">
    <link href="css/all.min.css" rel="stylesheet" type="text/css">
	<link rel="stylesheet" href="vendor/croppie/croppie.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="vendor/croppie/croppie.js"></script>
    <script src="js/custom.js"></script>

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

    <style>
        .custom-file-label::after {
            height: 8.25rem;
            padding-top: 50px;
        }
        .custom-file-label-edit::after{
            height: 2.25rem;
            padding-top: 5px;
        }
        #uploadimageModal .modal-dialog{
            max-width: 100% !important;
        }
    </style>
</head>

<body>
    <?php include "includes/topbar.php"; ?>
    <div class="">
        <h3 class="text-center mb-4" style="margin-top: 80px">Write a Blog</h3>
        <input type="hidden" name="bref" id="bref" value="<?php echo $bref; ?>">
        <div class="card">
            <div class="card-body">
                <?php
                    if ( isset($_GET["edit_id"]) ) {
                        $edit_sql = "SELECT * FROM blog  WHERE bid = '$_GET[edit_id]'";
                        $edit_run = mysqli_query($con, $edit_sql);
                        $edit_row = mysqli_fetch_assoc($edit_run);
                        ?>
                        <form method="post" enctype="multipart/form-data">
                            <div class="form-row">
                               
                                <div class="col-md-12">
								Title
                                    <input type="text" name="btitle" class="form-control mb-3" value="<?php echo $edit_row['btitle'];?>">
                                </div>
                            </div>
                            <div class="form-group row">
<div class="col-md-12 text-center">
                                    <div id="logo_wrapper">
                                                    <img src="<?php echo $edit_row["bimage"]; ?>" style="height:50px;" alt="none" >
                                                </div>
                                </div>
								 <div class="col-md-12">
								 Btag
                                    <input type="text" name="btag" class="form-control mb-3" value="<?php echo $edit_row['btag'];?>">
                                </div>
                                <div class="col-md-12">
								Sort Description
                                    <textarea name="bdesc" id="bdesc" class="form-control" rows="5" ><?php echo $edit_row["bdesc"]; ?></textarea>
                                </div>
                            </div>
							Blog
                            <textarea name="content" id="content" class="form-control ckeditor" ><?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
                    }
                ?>
            </div>
        </div>
    </div>
</body>
</html>



   

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



   
</script>

<?php
   
            
          
   
    if(isset($_POST['update_blog'])){
        $blog = $_POST["content"];
        $btitle = $_POST["btitle"];
		  $btag = $_POST["btag"];
        $bdesc = $_POST["bdesc"];
       
	   
     
        if ( $blog != '' ) {
           
            $up_sql = "UPDATE blog SET blog = '$blog', btitle ='$btitle',btag ='$btag',bupdated='$currentTime,', bimage = '$target_dir', bdesc = '$bdesc' ";

            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
         }
    }
?>