<?php
include_once("../include/main.inc.php");
check_session('MyCpanel', 'index.php');

################ Paging Starts From Here ##################
@extract($_REQUEST);
$start = (intval($start) == 0 or $start == "") ? $start = 0 : $start;
$pagesize = intval($pagesize) == 0 ? $pagesize = 100 : $pagesize;
if ($parentID == "") {
    $parentID = 0;
}

$heading = "Manage blog";
$record_type = "blog";
$script_name = "manage-blog.php";
$table_name = "blog_tbl";
#################### DELETE Record ##################

if ($id == "delete_record") {
    $categoryRes = getResult($table_name, " where catID='$catID'");
    $filename = $categoryRes['catImage'];
    if ($filename != "") {
        $file = SITE_DIR_PATH . "/uploaded-files/blog/images/" . $filename;
        if (file_exists($file)) {
            unlink($file);
        }

        $file_thumb = SITE_DIR_PATH . "/uploaded-files/blog/images/thumbs/" . $filename;
        if (file_exists($file_thumb)) {
            unlink($file_thumb);
        }
    }

    mysqli_query($dbconn, "delete from $table_name where catID='$catID'");
    $_SESSION['session_message'] = $record_type . ' has been deleted successfully';


    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;
}


if ($action == "update_heading") {
  
    if ($_FILES['imagePop']['name'] != "") {

        $img_name = 'newsevents'.uniqid();
        $img = upload_file($img_name, "imagePop", "uploaded-files/blog/images");
  
        $width = "636";
        $height = "333";
        
        $source_folder = SITE_DIR_PATH . "/uploaded-files/blog/images";
        $destination_folder = SITE_DIR_PATH . "/uploaded-files/blog/images/thumbs/";
        saveResizedPhoto($img, $width, $height, $source_folder, $destination_folder);
  
  
        updateTable('blog_heading_tbl', " image='$img' where id='1'");
      
      }

    $heading1 =  htmlentitiesDecode($heading1);
    $heading2 =  htmlentitiesDecode($heading2);
    $content =  htmlentitiesDecode($content);
    $heading3 =  htmlentitiesDecode($heading3);
    $content1 =  htmlentitiesDecode($content1);
  
    updateTable('blog_heading_tbl', " heading1='$heading1',heading2='$heading2',content='$content',heading3='$heading3',content1='$content1'");
    
    header("Location: ".SITE_ADMIN_URL."/".$script_name);
	exit;
}

####################################################
if (isset($_POST['active'])) {
    if (!empty($_POST['cat_arr'])) {
        $catid = $_POST['cat_arr'];
        foreach ($catid as $id) {
            mysqli_query($dbconn, "UPDATE $table_name SET status = 'Y' WHERE catID = '$id'");
        }
        $_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been updated successfully</div>';
    } else {
        $_SESSION['session_message'] = '<div class="btn btn-danger">No Category selected</div>';
    }
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name);
    exit;
}

if (isset($_POST['inactive'])) {
    if (!empty($_POST['cat_arr'])) {
        $catid = $_POST['cat_arr'];
        foreach ($catid as $id) {
            mysqli_query($dbconn, "UPDATE $table_name SET status = 'N'  WHERE catID = '$id'");
        }
        $_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been updated successfully</div>';
    } else {
        $_SESSION['session_message'] = '<div class="btn btn-danger">No Category selected</div>';
    }
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name);
    exit;
}


if ($action == "Update") {
    $input = $_POST;
    $part = array_splice($input, 0, 2);

    while (list($key, $val) = each($input)) {
        if (substr($key, 0, 6) == "order_") {
            $key1 = substr($key, 6, strlen($key) - 6);

            $qry1 = db_query("update $table_name set catOrder='$val' where catID='$key1'");
        }
    }
    //$_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been updated successfully  </div>';

    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;
}


if (isset($_POST['delete'])) {
    if (!empty($_POST['cat_arr'])) {
        $catid = $_POST['cat_arr'];
        foreach ($catid as $id) {
            mysqli_query($dbconn, "delete from wps_meta_tags WHERE entity_id='$id' AND entity_name='blog' AND  page_table='$table_name'");
            mysqli_query($dbconn, "DELETE FROM $table_name WHERE catID = '$id'");
        }
        $_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been deleted successfully</div>';
    } else {
        $_SESSION['session_message'] = '<div class="btn btn-danger">No Category selected</div>';
    }
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name);
    exit;
}
########################################################
admin_header("Manage $record_type");
$qry = db_query("select * from $table_name");
$resTop = getResult('blog_heading_tbl');
?>
<div class="content-wrapper">
    <div class="page-header">
        <h3 class="page-title">
            <?= $heading; ?>
        </h3>

        <nav aria-label="breadcrumb">
            <a class="nav-link" href="#" data-toggle="modal" data-target="#edtPop">
                <span class="btn btn-primary">Manage Blog Listing Top Content</span>
            </a>

        </nav>
        <nav aria-label="breadcrumb">
            <a class="nav-link" href="<?= SITE_ADMIN_URL . '/addeditblog.php' ?>">
                <span class="btn btn-primary">+ Add New Blog</span>
            </a>

        </nav>

    </div>
    <div class="card">
        <div class="card-body">
            <div class="row">
                <div class="col-12">
                    <form action="" method="post">
                        <div class="table-responsive">
                            <table id="order-listing" class="table table-bordered table-hover">
                                <thead>
                                    <tr>
                                        <th class="_reab">
                                            <div class="form-check mt-0">
                                                <label class="form-check-label">
                                                    <input type="checkbox" class="form-check-input">
                                                    <i class="input-helper"></i>
                                                </label>
                                            </div>
                                        </th>
                                        <th>S.No.</th>
                                        <th>Blog Title</th>
                                        <th>Blog Image</th>
                                        <th>Order</th>
                                        <th>Status</th>
                                        <th>Added On</th>
                                        <th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $sl = $start + 1;
                                    while ($arr = mysqli_fetch_array($qry)) { ?>
                                    <tr>
                                        <td>
                                            <div class="form-check mt-0"><label class="form-check-label"><input
                                                        type="checkbox" class="form-check-input" name="cat_arr[]"
                                                        value="<?= $arr['catID'] ?>"><i
                                                        class="input-helper"></i></label>
                                            </div>
                                        </td>
                                        <td>
                                            <?= $sl ?>
                                        </td>
                                        <td>
                                            <p class="ordern">
                                                <?= $arr['blogDname']; ?>
                                            </p>
                                        </td>
                                        <td>
                                            <p class="dongle-dcs">
                                                <?php
                                                    if ($arr['catImage'] != "") {
                                                        $file1 = SITE_DIR_PATH . "/uploaded-files/blog/images/" . $arr['catImage'];

                                                        $dimens = GetImageSize($file1);
                                                        if ($dimens[0] > 130) {
                                                            $dimens[0] = $dimens[0] + 30;
                                                            $dimens[1] = $dimens[1] + 80;
                                                        } else {
                                                            $dimens[0] = 200;
                                                            $dimens[1] = 200;
                                                        }
                                                        ?>
                                                <a href="javascript:void(0);"
                                                    onClick="openwin('<?= SITE_ADMIN_URL; ?>/popup_new.php?img=blog/images/<?= $arr['catImage']; ?>', '<?= $dimens[0]; ?>', '<?= $dimens[1]; ?>')">

                                                    <span class="adminThumb">
                                                        <!-- <img src="<? //= SITE_ADMIN_URL; ?>/popup_new.php?img=category/images/<? //= $arr['catImage']; ?>', '<? //= $dimens[0]; ?>', '<? //= $dimens[1]; ?>'"> -->
                                                        <img
                                                            src="<?=site_url?>/uploaded-files/blog/images/thumbs/<?= $arr['catImage']; ?>">
                                                    </span>



                                                </a>
                                            </p>
                                            <?php } ?>
                                        </td>
                                        <td>
                                            <input name="order_<?= $arr['catID']; ?>" class="form-control order"
                                                type="text" size="2" value="<?= $arr['catOrder']; ?>">
                                        </td>
                                        <td>
                                            <?php if ($arr['status'] == 'Y') { ?><label
                                                class="badge badge-success badge-pill">Active</label>
                                            <?php } else if ($arr['status'] == 'N') { ?> <label
                                                class="badge badge-danger badge-pill">In-Active</label>
                                            <?php } ?>
                                        </td>

                                        <td>
                                            <p class="ordern">
                                                <?= $arr['en_date']; ?>
                                            </p>
                                        </td>
                                        <td>
                                            <p class="download-dcs">
                                                <a
                                                    href="<?= SITE_ADMIN_URL ?>/addeditblog.php?catID=<?= $arr['catID']; ?>"><svg
                                                        xmlns="http://www.w3.org/2000/svg" width="24" height="24"
                                                        viewBox="0 0 24 24" fill="none" stroke="currentColor"
                                                        stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
                                                        class="feather feather-edit-3">
                                                        <path d="M12 20h9"></path>
                                                        <path
                                                            d="M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4L16.5 3.5z">
                                                        </path>
                                                    </svg></a>
                                            </p>
                                        </td>
                                    </tr>

                                    <?php $sl++;
                                    } ?>

                                </tbody>
                            </table>
                            <div class="btn-grp">
                                <button type="submit" name="active" onclick="return select_chk()"
                                    class="btn btn-success btn-icon-text btn-sm">
                                    <i class="fas fa-toggle-on mr-1"></i>Activate
                                </button>
                                <button type="submit" name="inactive" onclick="return select_chk()"
                                    class="btn btn-warning btn-icon-text btn-sm">
                                    <i class="far fa-times-circle mr-1"></i>Deactivate
                                </button>

                                <button type="submit" name="action" value="Update" onclick="return select_chk()"
                                    class="btn btn-default btn-icon-text btn-sm">
                                    <i class="far fa-check-square mr-1"></i>Update Order
                                </button>

                                <button type="submit" name="delete" onclick="return select_chk()"
                                    class="btn btn-danger btn-icon-text btn-sm">
                                    <i class="fas fa-trash-alt mr-1"></i>Delete
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>


<div class="modal" id="edtPop" tabindex="-1" role="dialog">

    <div class="modal-dialog" role="document">

        <div class="modal-content">

            <div class="modal-header p-2">

                <h5 class="modal-title">Manage Heading/Content</h5>

                <button type="button" class="close" data-dismiss="modal" aria-label="Close">

                    <span aria-hidden="true">&times;</span>

                </button>

            </div>

            <form action="" class="form_submit" enctype="multipart/form-data" method="post">

                <div class="modal-body p-2">

                    <div class="form-group row">


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

                            <label>Heading 1<sup class="text-danger">*</sup></label>

                            <div id="the-basics">

                                <input class="form-control form-control-sm" type="text"
                                    value="<?= $resTop['heading1'] ?>" name="heading1" placeholder="" required>

                            </div>

                        </div>

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

                            <label>Heading 2<sup class="text-danger">*</sup></label>

                            <div id="the-basics">

                                <input class="form-control form-control-sm" type="text"
                                    value="<?= $resTop['heading2'] ?>" name="heading2" placeholder="">

                            </div>

                        </div>



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

                            <label>Content Box Top<sup class="text-danger">*</sup></label>

                            <div id="the-basics">


                                <div id="the-basics">
                                    <textarea class="form-control editor" name="content"
                                        rows="4"><?= $resTop['content'] ?></textarea>


                                </div>

                            </div>

                        </div>

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

                            <label>Image<sup class="text-danger">*</sup></label>

                            <div id="the-basics">


                                <div id="the-basics">
                                <input type="file" name="imagePop">

                                   <?php if($resTop['image']!='')
                                   {?>

                                   <img src="<?=upload_url;?>blog/images/thumbs/<?=$resTop['image'];?>"
                                   alt="image" width="90" height="90">
                                   <?php }?>
                                </div>

                            </div>

                        </div>


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

                            <label>Heading Bottom<sup class="text-danger">*</sup></label>

                            <div id="the-basics">

                                <input class="form-control form-control-sm" type="text"
                                    value="<?= $resTop['heading3'] ?>" name="heading3" placeholder="">

                            </div>

                        </div>


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

                            <label>Content Box Bottom<sup class="text-danger">*</sup></label>

                            <div id="the-basics">


                                <div id="the-basics">
                                    <textarea class="form-control" name="content1"
                                        rows="4"><?= $resTop['content1'] ?></textarea>


                                </div>

                            </div>

                        </div>

                        <!-- 

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

                        <label>Button Name<sup class="text-danger">*</sup></label>

                        <div id="the-basics">

                            <input class="form-control form-control-sm" type="text"
                                value="<?= $resTop['bn'] ?>" name="bn" placeholder="" >

                        </div>

                    </div>

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

                        <label>Button Link<sup class="text-danger">*</sup></label>

                        <div id="the-basics">

                            <input class="form-control form-control-sm" type="text"
                                value="<?= $resTop['bl'] ?>" name="bl" placeholder="">

                        </div>

                    </div> -->



                    </div>

                </div>

                <div class="modal-footer">



                    <button type="submit" class="btn btn-primary mr-2" name="update_heading">Submit</button>
                    <input type="hidden" name="action" value="update_heading">


                    <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>

                </div>

            </form>

        </div>

    </div>

</div>

<script language="javascript">
function select_chk() {
    var chks = document.getElementsByName('cat_arr[]');
    var hasChecked = false;
    for (var i = 0; i < chks.length; i++) {
        if (chks[i].checked) {
            hasChecked = true;
            break;
        }
    }
    if (hasChecked == false) {
        alert("Please Select At Least One.");
        return false;
    }
}
</script>
<?php
admin_footer();
exit;
?>