<?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"; ?>
    <link rel="stylesheet" href="../assets/croppie/croppie.css" />
    <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">
                <?php if(isset($_GET["edit_id"])){
                    $edit_run = mysqli_query($con, "SELECT * FROM data  WHERE data_sl_no = '$_GET[edit_id]'");
                    if($edit_row = mysqli_fetch_assoc($edit_run)){
                    ?>
                <div class="card">
                    <div class="card-header">
                        <h4 class="m-0">Edit News : <?php echo $edit_row["data_title"];?></h4>
                    </div>
                    <div class="card-body">
                        <form class="user" method="post" enctype="multipart/form-data">
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="mb-3">
                                        <label for="blogTitle" class="form-label">News Title</label>
                                        <input type="text" class="form-control mb-2" id="course_name"
                                            value="<?php echo $edit_row["data_title"]; ?>" name="data_title">
                                    </div>
                                    <div class=" mb-3">
                                        <label class="form-label" for="shortDes">News Short Description</label>
                                        <textarea name="data_short_des" class="form-control" id="" cols="30"
                                            rows="5"><?php echo $edit_row['data_short_des'];?></textarea>
                                    </div>
                                </div>
                                <div class="col-md-12 mb-3">
                                    <label for="blogABOUT">News About</label>
                                    <textarea name="content" id="content"
                                        class="form-control ckeditor"><?php echo $edit_row["data_long_des"]; ?></textarea>
                                </div>
                                <!--
                                <div class="col-md-6 mb-3">
                                    <label for="blogSeoTitle">News SEO Title</label>
                                    <input type="text" class="form-control mb-2" id="course_seotitle"
                                        value="<?php echo $edit_row["data_seo_title"]; ?>" name="data_seo_title">
                                </div>

                                <div class="col-md-6 mb-3">
                                    <label for="blogKeyword">News Keyword</label>
                                    <input type="text" class="form-control mb-2" id="course_keyword"
                                        value="<?php echo $edit_row["data_seo_keyword"]; ?>" name="data_seo_keyword">

                                </div>
                                <div class="col-md-12 mb-3">
                                    <label for="blogOtherDetails">News Other Details</label>
                                    <textarea name="content1" id="content1"
                                        class="form-control ckeditor"><?php echo $edit_row["data_seo_tag"]; ?></textarea>
                                </div>-->
                                <div class="col-md-12">
                                    <button type="submit" class="btn btn-success btn-user m-2" name="edit_blog">
                                        <i class="fa fa-edit"></i> Update
                                    </button>
                                    <a href="news.php?show_list" class="btn btn-warning btn-user m-2"> <i
                                            class="fas fa-ban"></i>
                                        Cancel </a>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
                <?php } } elseif(isset($_GET["view_id"])){
                    $view_sql = "SELECT * FROM data WHERE data_sl_no='$_GET[view_id]'";
                    $view_data = mysqli_query($con,$view_sql);
                    if($view_rows = mysqli_fetch_array($view_data)){
                    ?>
                <div class="card">
                    <div class="card-header text-center">
                        <h4 class="m-0">News Title : <span
                                class="underline"><?php echo $view_rows['data_title']; ?></span></h4>
                    </div>
                    <div class="card-body">
                        <div class="mb-2">
                            <?php
                                if (!empty($view_rows['data_img']))
                                {
                            ?>
                            <h5 class="underline">News Image</h5>
                            <a href='../upload/news/<?php echo $view_rows["data_img"];?>' title='Click to open'
                                target='_blank'>
                                (<b>Already Uploaded - <img src='<?php echo $view_rows["data_img"];?>' height='50'
                                        data-toggle='modal' data-target='#show-img' class='showModal'>
                                </b> Click To View</a> )
                            <?php }
                                if (!empty($edit_row['data_img']))
                                { 
                            ?>
                            <a href='../upload/news/<?php echo $view_rows["data_img"];?>' title='Click to open'
                                target='_blank'>
                                (<b>Already Uploaded - <img src='<?php echo $view_rows["data_img"];?>' height='50'
                                        data-toggle='modal' data-target='#show-img' class='showModal'>
                                </b> Click To View</a> )
                            <?php } ?>
                        </div>
                        <hr>
                        <div class="mb-2">
                            <h5 class="underline">News Short Description</h5>
                            <p><?php echo $view_rows['data_short_des']; ?></p>
                        </div>
                        <hr>
                        <div class="mb-2">
                            <h5 class="underline">News ABOUT</h5>
                            <p><?php echo $view_rows['data_long_des']; ?></p>
                        </div>
                        <hr>
                        <!--
                        <div class="mb-2">
                            <h5 class="underline">News Other Details </h5>
                            <p><?php echo $view_rows['data_seo_tag']; ?></p>
                        </div>
                        <hr>
                        <div class="mb-2">
                            <h5 class="underline">News SEO</h5>
                            <p><?php echo $view_rows['data_seo_title']; ?></p>
                        </div>
                        <hr>
                        <div class="mb-2">
                            <h5 class="underline">News SEO Keywords</h5>
                            <p><?php echo $view_rows['data_seo_keyword']; ?></p>
                        </div>-->
                        <hr>
                        <p><strong>News upload by : </strong><?php echo $view_rows['data_up_by']; ?></p>
                        <p><strong>News edit date : </strong><?php echo $view_rows['data_edit_date']; ?></p>
                        <p><strong>News Visitor Count : </strong><?php echo $view_rows['data_count']; ?></p>
                        <hr>
                        <div class="mb-2">
                            <h5 class="underline">News Tag list :</h5>
                            <?php
                                $catagory = "news";
                                $tag_for = $view_rows['data_title'];
                                $tag_get_for = $_GET["view_id"];;
                                $c=1;
                                $tag_sql = "SELECT * FROM all_tag WHERE all_tag_catagory = '$catagory' AND all_tag_for = '$tag_get_for' AND all_tag_recycle = '1'";
                                $tag_data = mysqli_query($con,$tag_sql);
                                while($tag_rows = mysqli_fetch_array($tag_data))
                                {
                            ?>
                            <p><?php echo $c;?> > Tag Title:
                                <a href="<?php echo $tag_rows['all_tag_url']; ?>">
                                    <?php echo $tag_rows['all_tag_title']; ?></a>
                            </p>
                            <?php $c++;
                            }?>
                        </div>
                        <br>
                        <a href="news.php?show_list" class="btn btn-warning btn-user m-2"> <i class="fas fa-ban"></i>
                            Cancel </a>
                    </div>
                </div>
                <?php } } elseif(isset($_GET["thumbnail_id"])){
                    $thumb_run = mysqli_query($con, "SELECT `data_img` FROM data WHERE data_sl_no = '$_GET[thumbnail_id]' ");
                    $thumb_row = mysqli_fetch_assoc($thumb_run);
                ?>
                <div class="card">
                    <div class="card-header">
                        <h4 class="mb-0"> Thumbnail Upload</h4>
                    </div>
                    <div class="card-body">
                        <div class="row mb-5">
                            <input type="hidden" id="thumbnailId" value="<?php echo $_GET['thumbnail_id']; ?>">
                            <div class="col-md-6">
                                <label color="black">Upload Thumbnail</label>
                                <div class="custom-file" style="height: 134px;">
                                    <input type="file" class="custom-file-input" name="up_position" id="file"
                                        accept="image/*" required>
                                    <label class="custom-file-label" for="file">Select
                                        an Image for thumbnail </label>
                                </div>
                            </div>
                            <?php    
                                if( !empty($thumb_row['data_img']) ){
                            ?>
                            <div class="col-md-6 mt-5" align='center'>
                                <img src="<?php echo $thumb_row['data_img']?>" class="img-fluid" alt=""
                                    style="border: solid 1px grey;">
                                <input type="hidden" id="previousThumbnail"
                                    value="<?php echo $thumb_row['data_img']; ?>">
                            </div>
                            <?php
                                }
                            ?>
                        </div>
                        <a href="news.php?show_list" class="btn btn-warning btn-user m-2"> <i class="fas fa-ban"></i>
                            Cancel </a>
                    </div>
                </div>
                <?php } elseif(isset($_GET["add_new"])){?>
                <div class="card">
                    <div class="card-header text-center">
                        <h4 class="card-title mb-0">Add News</h4>
                    </div>
                    <div class="card-body">
                        <form action="" method="POST" enctype="multipart/form-data">
                            <div class="row">
                                <div class="col-md-12 mb-3">
                                    <div class="mb-2">
                                        <label for="exampleFormControlInput1" class="form-label">News
                                            Name</label>
                                        <input type="Text" class="form-control" name="data_title"
                                            placeholder="Enter News Title">
                                    </div>
                                    <div>
                                        <label for="exampleFormControlTextarea1" class="form-label">News Short
                                            About</label>
                                        <textarea class="form-control " name="data_short_des" rows="3"></textarea>
                                    </div>
                                </div>
                                <div class="col-md-12 mb-3">
                                    <label for="exampleFormControlTextarea1" class="form-label">News
                                        Description</label>
                                    <textarea class="form-control ckeditor" name="content" id="content"
                                        rows="3"></textarea>
                                </div>
                                <div class="mb-3">
                                    <button class="btn btn-success text-white me-md-2" name="addsubmit" type="submit"><i
                                            class="fa fa-paper-plane"></i> Submit</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
                <?php } elseif(isset($_GET["news_tag"])){
                    $blog_id = $_GET["news_tag"];
                    $blog_sql = mysqli_query($con, "SELECT * FROM data WHERE data_sl_no = '$blog_id'");
                    if($blog_row = mysqli_fetch_assoc($blog_sql)){
                    ?>
                <div class="card">
                    <h4 class="text-center mt-3 underline">Tags For : <?php echo $blog_row["data_title"];?></h4>
                    <div class="add-tag">
                        <div class="card-header">
                            <h6>Add Tag</h6>
                        </div>
                        <div class="card-body">
                            <form action="" method="post">
                                <div class="row">
                                    <div class="col-md-6 mb-2">
                                        <label for="tagTitile">Tag Title</label>
                                        <input type="text" name="tag_title" class="form-control"
                                            placeholder="Enter Tag Title">
                                    </div>
                                    <div class="col-md-6 mb-2">
                                        <label for="tagTUrl">Tag Url</label>
                                        <input type="text" name="tag_url" class="form-control"
                                            placeholder="Enter Tag Url">
                                    </div>
                                    <div>
                                        <button type="submit" name="tag_submit" class="btn btn-primary">
                                            <i class="fa fa-plus"></i>&nbsp; Add New Tag
                                        </button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                    <div class="tag-list">
                        <div class="card-header">
                            <h6>Tag list</h6>
                        </div>
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-hover">
                                    <thead>
                                        <tr>
                                            <td>NO</td>
                                            <td>Tag Title</td>
                                            <td>Tag Url</td>
                                            <td>Action</td>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php 
                                        // Fetch and display tags
                                            $tag_sql = mysqli_query($con, "SELECT * FROM all_tag WHERE all_tag_catagory = 'blog' AND all_tag_for = '$_GET[news_tag]' ORDER BY all_tag_sl_no DESC");
                                            $c=1;
                                            while($tag_row = mysqli_fetch_assoc($tag_sql)){
                                        ?>
                                        <tr id="<?php echo $tag_row['all_tag_sl_no'];?>">
                                            <td><?php echo $c;?></td>
                                            <form action="" method="POST">
                                                <td>
                                                    <input type="text" name="tag_title" class="form-control"
                                                        value="<?php echo $tag_row['all_tag_title'];?>">
                                                </td>
                                                <td>
                                                    <input type="text" name="tag_url" class="form-control"
                                                        value="<?php echo $tag_row['all_tag_url'];?>">
                                                </td>
                                                <td class="text-center" style="max-width:82px;">
                                                    <input type="hidden" name="tag_id"
                                                        value="<?php echo $tag_row['all_tag_sl_no'];?>">
                                                    <button type="submit" name="edit_tag" class="btn btn-primary">
                                                        <i class="fa fa-upload"></i>
                                                    </button>
                                                    <a href="<?php echo $tag_row["all_tag_url"];?>" target="_blank"
                                                        class="btn btn-info text-white"><i class="fa fa-eye"></i></a>
                                                    <a href="news.php?news_tag=<?php echo $_GET['news_tag'];?>&tag_del_id=<?php echo $tag_row['all_tag_sl_no'];?>"
                                                        class="btn btn-danger"><i class="fa fa-trash"></i></a>
                                                </td>
                                            </form>
                                        </tr>
                                        <?php $c++; } ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    <div class="">
                        <a href="news.php?show_list" class="btn btn-warning m-2 text-white"> <i
                                class="fas fa-arrow-left"></i>
                            Back </a>
                    </div>
                </div>
                <?php } }?>
                <?php if(isset($_GET["show_list"])){?>
                <div class="card">
                    <?php
                        $records_per_page = 10; // Number of records per page

                        if (isset($_GET['page'])) {
                            $page = intval($_GET['page']);
                        } else {
                            $page = 1;
                        }

                        $start_from = ($page-1) * $records_per_page;
                    ?>

                    <?php
                        if (isset($_GET['recycle_table'])) {
                    ?>
                    <div class="card-body">
                        <div class="d-flex justify-content-between">
                            <div>
                                <h3>Recycle News List</h3>
                            </div>
                            <div>
                                <a href="news.php?show_list" class="btn btn-primary text-white">Active News</a>
                            </div>
                        </div>

                        <div class="table-responsive">
                            <input type="text" id="searchRecycle" class="form-control mb-3"
                                placeholder="Search Recycle News by News Title">
                            <table class="table table-hover table-striped table-bordered" id="thisismytable">
                                <thead>
                                    <tr>
                                        <th>No.</th>
                                        <th>News</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody id="table-body-recycle">
                                    <?php
                                        $sql = "SELECT * FROM data WHERE data_catagory='news' AND data_recycle = '0' ORDER BY data_sl_no DESC LIMIT $start_from, $records_per_page";
                                        $run = mysqli_query($con, $sql);
                                        $c = 1 + $start_from;
                                        while ($rows = mysqli_fetch_assoc($run)) {
                                            echo "
                                            <tr id='$rows[data_sl_no]'>
                                                <td class='align-middle'>
                                                    <span class='text-dark'>$c</span>
                                                </td>
                                                <td class='align-middle'>
                                                    $rows[data_title]
                                                </td>";
                                    ?>
                                    <td class='align-middle text-center' style="max-width:77px;">
                                        <a class="btn btn-primary btn-sm" style="margin-left:5px;"
                                            href="news.php?view_id=<?php echo $rows['data_sl_no']; ?>">View</a>
                                        <a class="btn btn-primary btn-sm" style="margin-left:5px;"
                                            href="news.php?edit_id=<?php echo $rows['data_sl_no']; ?>"> Edit</a>
                                        <a class="btn btn-primary btn-sm" style="margin-left:5px;"
                                            href="tagnews.php?news_tag=<?php echo $rows['data_sl_no']; ?>">Tag</a>
                                        <a href='news.php?rec_id=<?php echo $rows["data_sl_no"]; ?>'
                                            class='delete m-2 btn btn-danger btn-sm rounded-circle'
                                            title='Click to Delete'>
                                            <i class="fa fa-recycle" aria-hidden="true"></i>
                                        </a>
                                        <a href='news.php?del_id=<?php echo $rows["data_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>
                            <?php
                                $sql = "SELECT COUNT(data_sl_no) FROM data WHERE data_catagory='news' AND data_recycle = '0'";
                                $run = mysqli_query($con, $sql);
                                $row = mysqli_fetch_row($run);
                                $total_records = $row[0];
                                $total_pages = ceil($total_records / $records_per_page);
                            ?>
                            <nav>
                                <ul class="pagination">
                                    <?php for ($i=1; $i<=$total_pages; $i++) { ?>
                                    <li class="page-item"><a class="page-link"
                                            href="news.php?show_list&recycle_table&page=<?php echo $i; ?>"><?php echo $i; ?></a>
                                    </li>
                                    <?php } ?>
                                </ul>
                            </nav>
                        </div>
                    </div>
                    <?php } else { ?>
                    <div class="card-body">
                        <div class="row justify-content-between mb-3">
                            <div class="col-md-9 mb-2 mb-lg-0">
                                <h3>Active News</h3>
                            </div>
                            <div class="col-md-3 d-flex justify-content-between">
                                <a href="news.php?add_new" class="btn btn-primary text-white btn-sm"><i
                                        class="fa fa-plus"></i>
                                    Add News</a>
                                <a href="news.php?show_list&recycle_table"
                                    class="btn btn-info text-white btn-sm">Recycle
                                    News</a>
                            </div>
                        </div>
                        <div class="table-responsive">
                            <input type="text" id="searchActive" class="form-control form-control-sm mb-3"
                                placeholder="Search Active News By News Title">
                            <table class="table table-hover table-striped table-bordered" id="thisismytable">
                                <thead>
                                    <tr>
                                        <th>No.</th>
                                        <th>News</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody id="table-body-active">
                                    <?php
                                        $sql = "SELECT * FROM data WHERE data_catagory = 'news' AND data_recycle = '1' ORDER BY data_sl_no DESC LIMIT $start_from, $records_per_page";
                                        $run = mysqli_query($con, $sql);
                                        $c = 1 + $start_from;
                                        while ($rows = mysqli_fetch_assoc($run)) {
                                            echo "
                                            <tr id='$rows[data_sl_no]'>
                                                <td class='align-middle'>
                                                    <span class='text-dark '>$c</span>
                                                </td>
                                                <td class='align-middle'>
                                                    $rows[data_title]
                                                </td>";
                                    ?>
                                    <td class='align-middle text-center' style="max-width:77px;">
                                        <a class="btn btn-primary btn-sm" style="margin-left:5px;"
                                            href="news.php?view_id=<?php echo $rows['data_sl_no']; ?>">View</a>
                                        <a class="btn btn-primary btn-sm" style="margin-left:5px;"
                                            href="news.php?edit_id=<?php echo $rows['data_sl_no']; ?>"> Edit</a>
                                        <a class="btn btn-primary btn-sm" style="margin-left:5px;"
                                            href="news.php?news_tag=<?php echo $rows['data_sl_no']; ?>">Tag</a>
                                        <a class="btn btn-primary btn-sm" style="margin-left:5px;"
                                            href="news.php?thumbnail_id=<?php echo $rows['data_sl_no']; ?>">Thumbnail</a>
                                        <a href='news.php?rec_id=<?php echo $rows["data_sl_no"]; ?>'
                                            class='delete 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>
                            <?php
                                $sql = "SELECT COUNT(data_sl_no) FROM data WHERE data_catagory='news' AND data_recycle = '1'";
                                $run = mysqli_query($con, $sql);
                                $row = mysqli_fetch_row($run);
                                $total_records = $row[0];
                                $total_pages = ceil($total_records / $records_per_page);
                            ?>
                            <nav>
                                <ul class="pagination">
                                    <?php for ($i=1; $i<=$total_pages; $i++) { ?>
                                    <li class="page-item"><a class="page-link"
                                            href="news.php?show_list&page=<?php echo $i; ?>"><?php echo $i; ?></a></li>
                                    <?php } ?>
                                </ul>
                            </nav>
                        </div>
                    </div>
                    <?php
                        }
                    ?>
                </div>
                <?php }?>
            </div>
        </div>

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

    <!-- JavaScript Libraries -->

    <?php include "include/js.php"; ?>
    <script src="../assets/croppie/croppie.js"></script>

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

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

    <!-- thumbnail Upload-->
    <div id="uploadimageModal" class="modal" role="dialog" style="padding:15px;">
        <div class="modal-dialog" style="max-width: 100%; width:auto;">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title">Upload & Crop Image</h4>
                </div>
                <div class="modal-body">
                    <div class="row mx-0">
                        <div class="col-md-12 col-sm-12 col-12 text-center">
                            <div id="image_demo" style="margin-top:10px;height:40%;"></div>
                            <p class="small">scroll through mouse to zoom in or out</p>
                        </div>
                        <div class="col-md-12 col-sm-12 col-12 text-center">
                            <button class="btn btn-success crop_image" id="cropppp">Crop & Upload Image</button>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-warning" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>
    <!-- thumbnail Upload-->

    <!-- Thumbnail Script -->
    <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() {
        $('#show-img').on('show.bs.modal', function(e) {
            var src = $(e.relatedTarget).attr('src');
            $(this).find('.modal-body > img').attr('src', src);
        });
        $image_crop = $('#image_demo').croppie({
            enableExif: true,
            viewport: {
                width: 1200,
                height: 620,
                type: 'square'
            },
            boundary: {
                width: 1210,
                height: 630
            }
        });

        $('#file').on('change', function() {
            var reader = new FileReader();
            reader.onload = function(event) {
                $image_crop.croppie('bind', {
                    url: event.target.result
                }).then(function() {
                    console.log('jQuery bind complete');
                });
            }
            reader.readAsDataURL(this.files[0]);
            $('#uploadimageModal').modal('show');
        });

        $('.crop_image').click(function(event) {
            $('#cropppp').html('uploading <i class="fas fa-spinner fa-pulse"></i>');
            $image_crop.croppie('result', {
                type: 'canvas',
                size: 'viewport'
            }).then(function(response) {
                var refid = $('#thumbnailId').val();
                var previousThumbnail = $('#previousThumbnail').val();
                // Send cropped image data to the server for processing
                $.ajax({
                    url: "update-alldata-thumbnail-ajax.php", // Replace with your PHP processing script
                    method: "POST",
                    data: {
                        image: response,
                        refid: refid,
                        previousThumbnail: previousThumbnail
                    },
                    success: function(data) {
                        // Handle success (e.g., show success message or update UI)
                        $('#uploadimageModal').modal('hide');
                        $('#uploaded_image').html(
                            data); // Update uploaded image display
                        var refid = $('#thumbnailId').val();
                        $(location).attr('href', 'news.php' + '?thumbnail_id=' +
                            refid); // Redirect if needed
                    },
                    error: function() {
                        // Handle error (e.g., show error message)
                    }
                });
            });
        });
    });
    </script>
    <!-- Thumbnail Script -->

    <script>
    $(document).ready(function() {
        $('#searchActive').on('keyup', function() {
            var query = $(this).val();
            $.ajax({
                url: 'newsSearch.php',
                method: 'POST',
                data: {
                    query: query,
                    type: 'active'
                },
                success: function(data) {
                    $('#table-body-active').html(data);
                }
            });
        });

        $('#searchRecycle').on('keyup', function() {
            var query = $(this).val();
            $.ajax({
                url: 'newsSearch.php',
                method: 'POST',
                data: {
                    query: query,
                    type: 'recycle'
                },
                success: function(data) {
                    $('#table-body-recycle').html(data);
                }
            });
        });
    });
    </script>

</body>

</html>


<!-- Add to database  Blog-->
<?php
  if (isset($_POST['addsubmit'])) {
    $title = mysqli_real_escape_string($con, strip_tags($_POST['data_title']));
    $short_des = mysqli_real_escape_string($con, strip_tags($_POST['data_short_des']));
    // $seo_title = mysqli_real_escape_string($con, strip_tags($_POST['data_seo_title']));
    // $seo_keyword = mysqli_real_escape_string($con, strip_tags($_POST['data_seo_keyword']));
    // $seo_tag = $_POST['content1'];
    $long_des = $_POST['content'];

    $prefix = "NEWS"; // Your prefix
    $random_number = str_pad(rand(0, 99999), 5, '0', STR_PAD_LEFT);
    $timestamp = date("YmdHis"); 

    $ref_id = $prefix . $random_number . $timestamp;
    
    $up_date = date("j F, Y, g:i a");
    $recycle = "1";
    $up_by= $_SESSION['uid'];
    $catagory= 'news';

    // new File Upload
    $file = $_FILES["file"]["tmp_name"];
    $f = $_FILES["file"]["name"];
    $ext = pathinfo($f,PATHINFO_EXTENSION);
    $fname = rand(10000, 999999).".".$ext;
  

    $courseCatagory_sql = "INSERT INTO data ( data_ref_id, data_catagory, data_title, data_img, data_short_des, data_long_des, data_up_by, data_up_date, data_recycle) VALUES ( '$ref_id', '$catagory', '$title',  '$fname', '$short_des', '$long_des', '$up_by', '$up_date', $recycle)";
    
    if( mysqli_query($con, $courseCatagory_sql) ){
        // Move Files
      move_uploaded_file($file,'../upload/news/'.$fname);
        ?>
<script>
alert("Details Sent Successfully");
window.location = "news.php?show_list";
</script>
<?php
    }
    else{
        ?>
<script>
alert("Something's Wrong");
window.location = "news.php?add_new";
</script>
<?php
    }
  }
?>


<!-- Edit database  Blog-->
<?php
    if(isset($_POST['edit_blog'])){ 
	$title = mysqli_real_escape_string($con, strip_tags($_POST['data_title']));
    $short_des = mysqli_real_escape_string($con, strip_tags($_POST['data_short_des']));
    // $seo_title = mysqli_real_escape_string($con, strip_tags($_POST['data_seo_title']));
    // $seo_keyword = mysqli_real_escape_string($con, strip_tags($_POST['data_seo_keyword']));
    // $seo_tag = $_POST['content1'];
    $long_des = $_POST['content'];
        
    $edit_date = date("j F, Y, g:i a");

        
        if ($_FILES["file"]["tmp_name"]) {
            // new File Upload
            $file = $_FILES["file"]["tmp_name"];
            $f = $_FILES["file"]["name"];
            $ext = pathinfo($f, PATHINFO_EXTENSION);
            $fname = rand(10000, 999999) . "." . $ext;
            $sql_select = "SELECT data_img FROM data WHERE data_sl_no = '$_GET[edit_id]'";
            $result = $con->query($sql_select);
            if ($result && $row = $result->fetch_assoc()) {
                $previous_logo = $row['data_img'];
                if (!empty($previous_logo)) {
                    $previous_image_path = '../upload/news/' . $previous_logo;
                    if (file_exists($previous_image_path)) {
                        unlink($previous_image_path);
                    }
                }
            }
        } else {
            // If no new file is uploaded, keep the previous image filename
            $fname = $edit_row['data_img'];
        }
	
        {
            $up_sql = "UPDATE data SET data_title='$title', data_short_des='$short_des', data_long_des='$long_des', data_img = '$fname', data_edit_date = '$edit_date' ";
   
        $up_sql .= " WHERE data_sl_no = '$_GET[edit_id]'";
		 if(mysqli_query($con, $up_sql)){

         // Move Files
         move_uploaded_file($file,'../upload/news/'.$fname);
       
            ?>
<script>
alert("Data Updated Successfully");
window.location = 'news.php?show_list';
</script>
<?php
        }
     else{
?>
<script>
alert('OOPS !!!!');
setTimeout(() => {
    window.location = 'news.php?show_list';
}, 1200);
</script>
<?php
        }
        
?>
<?php
        }
    }

?>


<!-- Recycle database  News-->
<?php
  if ( isset($_GET["rec_id"]) ) {
    $find_sql = "SELECT * FROM data WHERE data_sl_no = '$_GET[rec_id]'";
    $find_run = mysqli_query($con, $find_sql);
    if ($f_rows = mysqli_fetch_assoc($find_run)) {
        $old_rec = $f_rows["data_recycle"];
    
        if ($old_rec == 1) {
            $new_rec = 0;
        } elseif ($old_rec == 0) {
            $new_rec = 1;
        }
    
        $update_sql = "UPDATE data SET data_recycle = '$new_rec' WHERE data_sl_no = '$_GET[rec_id]'";
        $update_run = mysqli_query($con, $update_sql);

        if ( $update_run ) {
            ?>
<script>
alert("Sent to Recycle Successfully");
window.location = "news.php?show_list";
</script>
<?php
        } else {
            ?>
<script>
alert('OOPS !!!!');
setTimeout(() => {
    window.location = 'news.php?show_list';
}, 1200);
</script>
<?php
        }
        
    }

}
?>


<!-- delete database  Blog-->
<?php
if (isset($_GET["del_id"])) {
    $id = mysqli_real_escape_string($con, $_GET["del_id"]);
    
    $find_sql = "SELECT * FROM data WHERE data_sl_no = '$id'";
    $find_run = mysqli_query($con, $find_sql);

    if ($f_rows = mysqli_fetch_assoc($find_run)) {
        $old_img = $f_rows['data_img'];

        if (!empty($old_img)) {
            $previous_image_path = '../upload/news/' . $old_img;

            if (file_exists($previous_image_path)) {
                if (unlink($previous_image_path)) {
                    // Image deleted successfully
                } else {
                    // Failed to delete image
                    echo "Error deleting image: " . mysqli_error($con);
                }
            }
        }
    }

    $delete_sql = "DELETE FROM data WHERE data_sl_no = '$id'";
    
    if (mysqli_query($con, $delete_sql)) {
        ?>
<script>
alert("Deleted Successfully");
window.location = "news.php?show_list";
</script>
<?php
    } else {
        ?>
<script>
alert("Failed to delete");
window.location = "news.php?show_list";
</script>
<?php
    }
}
?>


<!-- Add to databast for TAG -->
<?php
  if (isset($_POST['tag_submit'])) {
    $tag_title = mysqli_real_escape_string($con, strip_tags($_POST['tag_title']));
    $tag_url = mysqli_real_escape_string($con, strip_tags($_POST['tag_url']));

    $recycle = "1";
    $up_by= $_SESSION['uref'];
    $catagory= 'news';


    $tag_sql = "INSERT INTO all_tag ( all_tag_title, all_tag_url, all_tag_catagory, all_tag_for, all_tag_up_by, all_tag_upload_date, all_tag_recycle) VALUES ( '$tag_title', '$tag_url', '$catagory',  '$_GET[news_tag]', '$up_by', '$currentTime', '$recycle')";
    
    if( mysqli_query($con, $tag_sql) ){
        ?>
<script>
alert("Tag Added Successfully");
window.location = "news.php?news_tag=<?php echo $_GET["news_tag"];?>";
</script>
<?php
    }
    else{
        ?>
<script>
alert("Something's Wrong");
window.location = "news.php?news_tag=<?php echo $_GET["news_tag"];?>";
</script>
<?php
    }
  }
?>

<?php
// Update existing tag
if(isset($_POST['edit_tag'])) {
    $tag_title = mysqli_real_escape_string($con, $_POST['tag_title']);
    $tag_url = mysqli_real_escape_string($con, $_POST['tag_url']);
    $tag_id = mysqli_real_escape_string($con, $_POST['tag_id']);

    $update_sql = "UPDATE all_tag SET all_tag_title='$tag_title', all_tag_url='$tag_url' WHERE all_tag_sl_no='$tag_id'";
    if(mysqli_query($con, $update_sql)) {
        ?>
<script>
alert("Tag Updated Successfully");
window.location = "news.php?news_tag=<?php echo $_GET["news_tag"];?>";
</script>
<?php
    }
    else{
        ?>
<script>
alert("Something's Wrong");
window.location = "news.php?news_tag=<?php echo $_GET["news_tag"];?>";
</script>
<?php
    }
}
?>

<?php
// Delete tag
if(isset($_GET['tag_del_id'])) {
    $tag_del_id = mysqli_real_escape_string($con, $_GET['tag_del_id']);

    $delete_sql = "DELETE FROM all_tag WHERE all_tag_sl_no='$tag_del_id'";
    if(mysqli_query($con, $delete_sql)) {
        ?>
<script>
alert("Tag Deleted Successfully");
window.location = "news.php?news_tag=<?php echo $_GET["news_tag"];?>";
</script>
<?php
    }
    else{
        ?>
<script>
alert("Something's Wrong");
window.location = "news.php?news_tag=<?php echo $_GET["news_tag"];?>";
</script>
<?php
    }
}
?>