<?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 `other` 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 Other</li>
                                        </ol>
                                    </div>
                                    <h4 class="page-title"> Other Edit</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'])){
    $about_us = $_POST['about_us'];
    $privacy_policy = $_POST['privacy_policy'];
    $term_and_condition = $_POST['term_and_condition'];
    $link               = $_POST['link'];
    $distance           = $_POST['distance'];
    // Check if a new image is uploaded
    if (!empty($_FILES['image']['name']) || !empty($_FILES['image2']['name'])) {
    // Check if the first image is provided
    if (!empty($_FILES['image']['name'])) {
        $image = $_FILES['image']['name'];
        $tmp_name = $_FILES['image']['tmp_name'];
        $image_url = 'https://bemangalam.com/MAGLAM/other_image/' . $image;
    } else {
        // If not provided, use the existing URL
        $image_url = ''; // Assuming you have the existing URL stored somewhere    
    }

    // Check if the second image is provided
    if (!empty($_FILES['image2']['name'])) {
        $image2 = $_FILES['image2']['name'];
        $tmp_name2 = $_FILES['image2']['tmp_name'];
        $image_url2 = 'https://bemangalam.com/MAGLAM/other_image/' . $image2;
    } else {
        // If not provided, use the existing URL
        $image_url2 = ''; // Assuming you have the existing URL stored somewhere
    }

    // Update the database with the new image information
    $sql = "UPDATE `other` SET `about_us`='$about_us',`privacy_policy`='$privacy_policy',`term_and_condition`='$term_and_condition',`vendor_apk_link`='$link',`set_distance`='$distance'";
    if (!empty($image_url)) {
        $sql .= ",`home_page_banner`='$image_url'";
    }
    if (!empty($image_url2)) {
        $sql .= ",`proud_movement_img`='$image_url2'";
    }
    $sql .= " WHERE `id`='$id'";

    $res = mysqli_query($conn, $sql);

    if ($res) {
        // Move the new image to the upload directory
        if (!empty($image_url)) {
            move_uploaded_file($tmp_name, '../other_image/' . $image);
        }
        if (!empty($image_url2)) {
            move_uploaded_file($tmp_name2, '../other_image/' . $image2);
        }
        echo "<h6> Data Updated </h6>";
    } else {
        echo "<h6> Update Failed </h6>";
    }
} else {
    // If no new image is provided, update only the non-image fields in the database
    $sql = "UPDATE `other` SET `about_us`='$about_us',`privacy_policy`='$privacy_policy',`term_and_condition`='$term_and_condition',`vendor_apk_link`='$link',`set_distance`='$distance' WHERE `id`='$id'";
    $res = mysqli_query($conn, $sql);

    if ($res) {
        echo "<h6> Data Updated </h6>";
    } 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">About Us</label>
                                                        <input type="text" value="<?php echo $res['about_us'];?>" name="about_us" class="form-control" placeholder="About Us">
                                                    </div>
                                                </div>
                                                
                                                <div class="col-lg-12">
                                                    <!-- Date Range Picker With Times -->
                                                    <div class="mb-3">
                                                        <label for="example-select" class="form-label">Privacy Policy</label>
                                                        <input type="text" value="<?php echo $res['privacy_policy'];?>" name="privacy_policy" class="form-control" placeholder="Privacy Policy">
                                                    </div>
                                                </div>
                                                
                                                <div class="col-lg-12">
                                                    <!-- Date Range Picker With Times -->
                                                    <div class="mb-3">
                                                        <label for="example-select" class="form-label">Term And Condition</label>
                                                        <input type="text" value="<?php echo $res['term_and_condition'];?>" name="term_and_condition" class="form-control" placeholder="term_and_condition">
                                                    </div>
                                                </div>
                                                
                                                <!--<div class="col-lg-12">-->
                                                    <!-- Date Range Picker With Times -->
                                                <!--    <div class="mb-3">-->
                                                <!--        <label for="example-select" class="form-label">Home Page Banner</label>-->
                                                <!--        <input type="file" value="" name="image" class="form-control" placeholder="Give image">-->
                                                        <?php  
                                                            //  $fullImagePath = $res['home_page_banner'];

                                                            //  $prefixToRemove = 'https://createapp.co.in/MAGLAM/';

                                                            //  $imageFilename = str_replace($prefixToRemove, '', $fullImagePath);
                                                             ?>
                                                <!--             <img src="<?= '../' . $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">Proud movement img(image Type:JPEG,PNG,Jpg)</label>
                                                        <input type="file" value="" name="image2" class="form-control" placeholder="Give image">
                                                         <?php  
                                                             $fullImagePath = $res['proud_movement_img'];

                                                             $prefixToRemove = 'https://bemangalam.com/MAGLAM/other_image/';

                                                             $imageFilename = str_replace($prefixToRemove, '', $fullImagePath);
                                                             ?>
                                                             <td><img src="<?= '../other_image/' . $imageFilename; ?>" style="width:100px; height:60px;"></td>
                                                    </div>
                                                </div>
                                                
                                                <div class="col-lg-12">
                                                    <!-- Date Range Picker With Times -->
                                                    <div class="mb-3">
                                                        <label for="example-select" class="form-label">Url Link</label>
                                                        <input type="text" value="<?php echo $res['vendor_apk_link'];?>" name="link" class="form-control" placeholder="url link">
                                                    </div>
                                                </div>
                                                
                                                <div class="col-lg-12">
                                                    <!-- Date Range Picker With Times -->
                                                    <div class="mb-3">
                                                        <label for="example-select" class="form-label">Set Distance(example:1000 = 1Km)</label>
                                                        <input type="text" value="<?php echo $res['set_distance'];?>" name="distance" class="form-control" placeholder="distance">
                                                    </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>