<?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']) ) {
        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 "includes/meta.php"; ?>             
    <?php include "includes/css.php"; ?>
    <link href="css/all.min.css" rel="stylesheet" type="text/css">
    <link href="css/alltable.css" rel="stylesheet"> 
       
 <style> 
        #logo_wrapper img {
              max-width: 100%;
              height: auto;
            }
    </style>
    
</head>

<body id="page-top" class="mt-55">

  <!--==========================
  Header
  ============================-->
  
  <?php include "includes/header2.php"; ?>

            <div class="container">
            <div class="row mx-0 just ify-content-center">
                <div class="col-12 col-sm-12 col-md-12">
                        <div class="card-body">

                            <h1 class="h3 mb-2 text-gray-800 text-center" style="margin-top: 70px">View Blog</h1>

                            <div class="row m-0">

                                <div class="col-md-12 p-0">
                                    <?php
                                        $sql = mysqli_query($con, "SELECT blog,bimage,bdesc,  btitle FROM blog WHERE bid = '$_GET[id]'");
                                        $row = mysqli_fetch_assoc($sql);
                                    ?>
                                      <div class="row" >
                                            <div class="card col-md-12 mb-3 shadow text-center">
                                            <div  class="card-header text-center" >
                                                   <font size="5" > Title </font> 
                                               </div>
                                               <br align="justify"><div align="justify"><?php echo $row["btitle"]; ?></div>
											   <br> <br>
												<div id="logo_wrapper">
                                                    <img src="<?php echo $row["bimage"]; ?>" style="height:100px;" alt="none" >
                                                </div>
												 <div  class="card-header ">
                                                  <font size="5" > Description</font>
                                                </div>
                                               <br ><div align="justify">
                                                    <?php echo $row["bdesc"]; ?>
													</div>
                                               
												<div class="card-header text-center">
                                                    <h3 cla ss="text-gray-800">Blog</h3>
                                                </div>
                                                <div class="card-body" id="logo_wrapper">
												<div align="justify">
                                                    <?php echo $row["blog"]; ?>
                                                </div>
												</div>
                                            </div>
                                         
                                        </div>

                                    <a href="blog.php" class="btn btn-warning my-3"> <i class="fa fa-chevron-left" aria-hidden="true"></i> Go Back </a>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>

            </div>

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



    



    <script>
        function checkFileDetails() {
            var fi = document.getElementById('file');
            if (fi.files.length > 0) {
                for (var i = 0; i <= fi.files.length - 1; i++) {
                    var fileName, fileExtension, fileSize, fileType, dateModified;
                    fileName = fi.files.item(i).name;
                    fileExtension = fileName.replace(/^.*\./, '');
                    if (fileExtension == 'png' || fileExtension == 'jpg' || fileExtension == 'jpeg') {
                        readImageFile(fi.files.item(i));
                    }
                }
                function readImageFile(file) {
                    var reader = new FileReader();
                    reader.onload = function (e) {
                        var img = new Image();
                        img.src = e.target.result;
                        img.onload = function () {
                            var imgWidth = this.width;
                            var imgHeight = this.height;

                            if ( imgWidth == 1200 && imgHeight == 512 || imgWidth == 1500 && imgHeight == 640 ) {
                                var imgCheck = "<i class='fas fa-check-circle text-success'></i>";
                            } else {
                                var imgCheck = "<i class='fas fa-times-circle text-danger'></i>";
                            }
                            document.getElementById('fileInfo').innerHTML =
                                document.getElementById('fileInfo').innerHTML + '<br> ' +
                                'Name: <b>' + file.name + '</b> <br>' +
                                'Size: <b>' + Math.round((file.size / 1024)) + '</b> KB <br>' +
                                'Dimension: <b>' + imgWidth + '</b> x <b>' + imgHeight + '</b> ' + imgCheck + ' <br>';
                        }
                    };
                    reader.readAsDataURL(file);
                }
            }
        }
    </script>



    <script>
		$(document).ready(function(){
            $('#thisismytable').DataTable();
            $('#show-img').on('show.bs.modal', function (e) {
                var src = $(e.relatedTarget).attr('src');
                $(this).find('.modal-body > img').attr('src', src);
            });
		});

        var $sortable = $("#thisismytable > tbody");

        $sortable.sortable({
            stop: function ( event, ui ) {
                var parameters = $sortable.sortable( "toArray" );
                $.post("slider_position.php",{value:parameters},function(result){
                    $(document).ready(function() {
                        const Toast = Swal.mixin({
                            toast: true,
                            position: 'bottom',
                            showConfirmButton: false,
                            timer: 2000
                        });
                        Toast.fire({
                            type: 'success',
                            title: result
                        });
                    });
                });
            }
        });
	</script>

</body>
</html>



<?php
    if ( isset($_GET["del_id"]) ) {
        $del_sql = "DELETE FROM blog WHERE bid = '$_GET[del_id]'";
        // echo '<pre>';print_r($del_sql);exit();
        if ( mysqli_query($con, $del_sql) ) {
            ?>
            <script>window.location = "blog.php";</script>
            <?php
        } else {
            ?>
            <script>
                alert('OOPS !!!!');
                setTimeout(() => {
                    window.location = 'blog.php';
                }, 1200);
            </script>
            <?php
        }
    }
    if(isset($_POST['edit_users'])){
        $uname = mysqli_real_escape_string($con, strip_tags($_POST['uname']));
        $uphone = mysqli_real_escape_string($con, strip_tags($_POST['uphone']));
        $upass = mysqli_real_escape_string($con, strip_tags($_POST['upass']));
        $uabout = mysqli_real_escape_string($con, strip_tags($_POST['uabout']));
        $chk_ph = mysqli_query($con, "SELECT uphone FROM users WHERE uname != '$uname' AND uphone = '$uphone'");
        $chk_ph_count = mysqli_num_rows($chk_ph);
        if ( $chk_ph_count < 1 ) {
            $up_sql = "UPDATE users SET uname = '$uname', uphone = '$uphone', upass = '$upass', uabout = '$uabout' WHERE uid = '$_GET[edit_id]'";
    
            if(mysqli_query($con, $up_sql)){
                ?>
                <script>window.location = 'review-blog.php';</script>
                <?php
            }
            else{
                ?>
                <script>
                    alert('OOPS !!!!');
                    setTimeout(() => {
                        window.location = 'review-blog.php';
                    }, 1200);
                </script>
                <?php
            }
        } else {
            ?>
            <script>
                alert('Mobile Number Exists');
                setTimeout(() => {
                    window.location = 'admin_lvl2.php?edit_id=<?php echo $_GET["edit_id"]; ?>';
                }, 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 = '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
        }
    }

?>