<?php
session_start();
include 'include/config.php';

if(!isset($_SESSION['id'])){
    // echo "<script> window.location.href = 'index.php' </script>";
  }
?>

<?php  include 'include/head.php'; 

$id = $_GET['id'];
$sel = "select * from `service_category` where `id`='$id'";
$qu = mysqli_query($conn,$sel);
$res = mysqli_fetch_array($qu);

// $fullImagePath = $res['offer_banner_image_url'];
// $prefixToRemove = 'https://createapp.co.in/ftc_services/upload/offer_banner/';
// $imageFilename = str_replace($prefixToRemove, '', $fullImagePath);
?>
                    
    <!-- Start Content-->
    <div class="container-fluid">
                        
                        <!-- start page title -->
                        <div class="row">
                            <div class="col-12">
                                <div class="page-title-box">
                                    <div class="page-title-right">
                                        <ol class="breadcrumb m-0">
                                            <li class="breadcrumb-item"><a href="dashboard.php">Home</a></li>
                                            <li class="breadcrumb-item active">Edit Banner</li>
                                        </ol>
                                    </div>
                                    <h4 class="page-title">Edit Banner</h4>
                                </div>
                            </div>
                        </div>
                        <!-- end page title --> 

                        <div class="row">
                        <div class="col-12">
                            <div class="card">
                                <div class="card-body">

                    
                    
                    <?php
       if(isset($_POST['submit'])){
    $name = $_POST['name'];
   

    // Check if a new image is uploaded
    if(!empty($_FILES['image']['name'])) {
        $image = $_FILES['image']['name'];
        $tmp_name = $_FILES['image']['tmp_name'];
        $image_url = 'https://createapp.co.in/ftc_services/upload/cat_img/' . $image;

        // Update the database with the new image information
        $sql = "UPDATE `service_category` SET `cat_name`='$name',`cat_image`='$image_url'  WHERE `id`='$id'";
        $res = mysqli_query($conn, $sql);

        if($res) {
            // Move the new image to the upload directory
           move_uploaded_file($tmp_name, '../upload/cat_img/'.$image);
            echo "<h6> Category Updated </h6>";
            echo "<script>document.location('add_service_category.php');</script>";
        } else {
            echo "<h6> Update Failed </h6>";
        }
    } else {
        // If no new image is provided, update only the non-image fields in the database
        $sql = "UPDATE `service_category` SET `cat_name`='$name'  WHERE `id`='$id'";
        $res = mysqli_query($conn, $sql);

        if($res) {
            echo "<h6> Category Updated </h6>";
            echo "<script>document.location('add_service_category.php');</script>";
        } else {
            echo "<h6> Update Failed </h6>";
        }
    }
}
?>
                    <form action=""  method="POST" enctype="multipart/form-data">
                                
                                    <!-- end nav-->
                                    <div class="tab-content">
                                        <div class="tab-pane show active">
                                            <div class="row">
                                                
                                                <div class="col-lg-12">
                                                    <!-- Date Range Picker With Times -->
                                                    <div class="mb-3">
                                                        <label for="example-select" class="form-label">Banner Image</label>
                                                        <input type="file" value="" name="image" class="form-control" placeholder="Give image">
                                                                <?php  
                                                             $fullImagePath = $res['cat_image'];

                                                             $prefixToRemove = 'https://createapp.co.in/ftc_services/upload/cat_img/';

                                                             $imageFilename = str_replace($prefixToRemove, '', $fullImagePath);
                                                             ?>
                                                            <img src="<?= '../upload/cat_img/' . $imageFilename; ?>" style="width:100px; height:60px;">                                                   </div>
                                                </div>
                                                
                                                <div class="col-lg-12">
                                                    <!-- Date Range Picker With Times -->
                                                    <div class="mb-3">
                                                        <label for="example-select" class="form-label">Category Name</label>
                                                        <input type="text" value="<?php echo $res['cat_name'];?>" name="name" class="form-control" placeholder="category name">
                                                    </div>
                                                </div>
                                                
                                               


                                            </div>

                                            <div class="row">
                                                <div class="clearfix"></div>
                                                <div class="col-lg-6">
                                                    <!-- Predefined Date Ranges -->
                                                    <div class="mb-3">
                                                        <input type="submit" class="btn btn-success" value="submit" name="submit">
                                                        
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <!-- end preview-->

                                        
                                        <!-- end preview code-->
                                    </div>
                                    <!-- end tab-content-->
                    </form>
                                </div>
                                <!-- end card-body -->
                            </div>
                            <!-- end card-->
                        </div>
                            <!-- end col -->
            </div> <!-- end row-->


                    
    </div> <!-- container -->


<?php  
include 'include/footer.php'; 
?>
<!-- editor -->
        <script src='https://cdn.ckeditor.com/4.13.0/standard/ckeditor.js'></script>
        <script src='https://code.jquery.com/jquery-3.4.1.min.js'></script>
        <script id="rendered-js" >
            CKEDITOR.replace('editor1');
            CKEDITOR.on('instanceReady', function (evt)
            {
            var editor = evt.editor;

            editor.on('change', function (e) {
                var contentSpace = editor.ui.space('contents');
                var ckeditorFrameCollection = contentSpace.$.getElementsByTagName('iframe');
                var ckeditorFrame = ckeditorFrameCollection[0];
                var innerDoc = ckeditorFrame.contentDocument;
                var innerDocTextAreaHeight = $(innerDoc.body).height();
                console.log(innerDocTextAreaHeight);
            });
            });
            //# sourceURL=pen.js
        </script>