<?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 `registration` where `id`='$id'";
$qu = mysqli_query($conn,$sel);
$res = mysqli_fetch_array($qu);




?>
                    
    <!-- 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 Shop</li>
                                        </ol>
                                    </div>
                                    <h4 class="page-title">Edit Shop</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'])){
    $unique_id = $_POST['unique_id'];
    $name = $_POST['name'];
    $shop_name = $_POST['shop_name'];
    $mobile = $_POST['mobile'];
    $address = $_POST['address'];
    $gst = $_POST['gst'];
    $about_shop = $_POST['about_shop'];
    $map_address = $_POST['map_address'];
    $rating      = $_POST['rating'];
    $status = $_POST['status'];

    // Check if a new image is uploaded
    if(!empty($_FILES['image1']['name'])) {
        $image = $_FILES['image1']['name'];
        $tmp_name = $_FILES['image1']['tmp_name'];
        $image_url = 'upload/image/' . $image;

        // Update the database with the new image information
        $sql = "UPDATE `registration` SET `unique_id`='$unique_id',`name`='$name',`shop_name`='$shop_name',`phone_number`='$mobile',`address`='$address',
               `gst_number`='$gst',`about_shop`='$about_shop',`img_1`='$image_url',`map_address`='$map_address',`rating`='$rating',`status`='$status' WHERE `id`='$id'";
        $res = mysqli_query($conn, $sql);

        if($res) {
            // Move the new image to the upload directory
            move_uploaded_file($tmp_name, '../upload/image/' . $image);
            echo "<h6> Shop Updated </h6>";
            echo "<script>window.location.href='applied_shop.php'</script>";
        } else {
            echo "<h6> Update Failed </h6>";
        }
    } elseif(!empty($_FILES['image2']['name'])) {
        $image2 = $_FILES['image2']['name'];
        $tmp_name2 = $_FILES['image2']['tmp_name'];
        $image_url2 = 'upload/image/' . $image2;

        // Update the database with the new image information
        $sql = "UPDATE `registration` SET `unique_id`='$unique_id',`name`='$name',`shop_name`='$shop_name',`phone_number`='$mobile',`address`='$address',
               `gst_number`='$gst',`about_shop`='$about_shop',`img_2`='$image_url2',`map_address`='$map_address',`rating`='$rating',`status`='$status' WHERE `id`='$id'";
        $res = mysqli_query($conn, $sql);

        if($res) {
            // Move the new image to the upload directory
            move_uploaded_file($tmp_name2, '../upload/image/' . $image2);
            echo "<h6> Shop Updated </h6>";
             echo "<script>window.location.href='applied_shop.php'</script>";
        } else {
            echo "<h6> Update Failed </h6>";
        }
    }elseif(!empty($_FILES['image3']['name'])) {
        $image3 = $_FILES['image3']['name'];
        $tmp_name3 = $_FILES['image3']['tmp_name'];
        $image_url3 = 'upload/image/' . $image3;

        // Update the database with the new image information
        $sql = "UPDATE `registration` SET `unique_id`='$unique_id',`name`='$name',`shop_name`='$shop_name',`phone_number`='$mobile',`address`='$address',
               `gst_number`='$gst',`about_shop`='$about_shop',`img_3`='$image_url3',`map_address`='$map_address',`rating`='$rating',`status`='$status' WHERE `id`='$id'";
        $res = mysqli_query($conn, $sql);

        if($res) {
            // Move the new image to the upload directory
            move_uploaded_file($tmp_name3, '../upload/image/' . $image3);
            echo "<h6> Shop Updated </h6>";
             echo "<script>window.location.href='applied_shop.php'</script>";
        } else {
            echo "<h6> Update Failed </h6>";
        }
    }elseif(!empty($_FILES['image4']['name'])) {
        $image4 = $_FILES['image4']['name'];
        $tmp_name4 = $_FILES['image4']['tmp_name'];
        $image_url4 = 'upload/image/' . $image4;

        // Update the database with the new image information
        $sql = "UPDATE `registration` SET `unique_id`='$unique_id',`name`='$name',`shop_name`='$shop_name',`phone_number`='$mobile',`address`='$address',
               `gst_number`='$gst',`about_shop`='$about_shop',`img_4`='$image_url4',`map_address`='$map_address',`rating`='$rating',`status`='$status' WHERE `id`='$id'";
        $res = mysqli_query($conn, $sql);

        if($res) {
            // Move the new image to the upload directory
            move_uploaded_file($tmp_name4, '../upload/image/' . $image4);
            echo "<h6> Shop Updated </h6>";
             echo "<script>window.location.href='applied_shop.php'</script>";
        } else {
            echo "<h6> Update Failed </h6>";
        }
    }elseif(!empty($_FILES['image5']['name'])) {
        $image5 = $_FILES['image5']['name'];
        $tmp_name5 = $_FILES['image5']['tmp_name'];
        $image_url5 = 'upload/image/' . $image5;

        // Update the database with the new image information
        $sql = "UPDATE `registration` SET `unique_id`='$unique_id',`name`='$name',`shop_name`='$shop_name',`phone_number`='$mobile',`address`='$address',
               `gst_number`='$gst',`about_shop`='$about_shop',`img_5`='$image_url5',`map_address`='$map_address',`rating`='$rating',`status`='$status' WHERE `id`='$id'";
        $res = mysqli_query($conn, $sql);

        if($res) {
            // Move the new image to the upload directory
            move_uploaded_file($tmp_name5, '../upload/image/' . $image5);
            echo "<h6> Shop Updated </h6>";
             echo "<script>window.location.href='applied_shop.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 `registration` SET `unique_id`='$unique_id',`name`='$name',`shop_name`='$shop_name',`phone_number`='$mobile',`address`='$address',
               `gst_number`='$gst',`about_shop`='$about_shop',`map_address`='$map_address',`rating`='$rating',`status`='$status' WHERE `id`='$id'";
        $res = mysqli_query($conn, $sql);

        if($res) {
            echo "<h6> Shop Updated </h6>";
             echo "<script>window.location.href='applied_shop.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">Unique Id</label>
                                                        <input type="text" value="<?php echo $res['unique_id'];?>" name="unique_id" class="form-control" placeholder="Unique Id">
                                                    </div>
                                                </div>
                                                
                                                <div class="col-lg-12">
                                                    <!-- Date Range Picker With Times -->
                                                    <div class="mb-3">
                                                        <label for="example-select" class="form-label">Name</label>
                                                        <input type="text" value="<?php echo $res['name'];?>" name="name" class="form-control" placeholder="Name">
                                                    </div>
                                                </div>
                                                
                                                <div class="col-lg-12">
                                                    <!-- Date Range Picker With Times -->
                                                    <div class="mb-3">
                                                        <label for="example-select" class="form-label">Shop Name</label>
                                                        <input type="text" value="<?php echo $res['shop_name'];?>" name="shop_name" class="form-control" placeholder="Shop Name">
                                                    </div>
                                                </div>
                                                
                                                <div class="col-lg-12">
                                                    <!-- Date Range Picker With Times -->
                                                    <div class="mb-3">
                                                        <label for="example-select" class="form-label">Mobile Number</label>
                                                        <input type="text" value="<?php echo $res['phone_number'];?>" name="mobile" class="form-control" placeholder="mobile">
                                                    </div>
                                                </div>
                                                
                                                <div class="col-lg-12">
                                                    <!-- Date Range Picker With Times -->
                                                    <div class="mb-3">
                                                        <label for="example-select" class="form-label">Address</label>
                                                        <input type="text" value="<?php echo $res['address'];?>" name="address" class="form-control" placeholder="address">
                                                    </div>
                                                </div>
                                                
                                                <div class="col-lg-12">
                                                    <!-- Date Range Picker With Times -->
                                                    <div class="mb-3">
                                                        <label for="example-select" class="form-label">Gast Number</label>
                                                        <input type="text" value="<?php echo $res['gst_number'];?>" name="gst" class="form-control" placeholder="GSt">
                                                    </div>
                                                </div>
                                                
                                                <div class="col-lg-12">
                                                    <!-- Date Range Picker With Times -->
                                                    <div class="mb-3">
                                                        <label for="example-select" class="form-label">About Shop</label>
                                                        <input type="text" value="<?php echo $res['about_shop'];?>" name="about_shop" class="form-control" placeholder="About Shop">
                                                    </div>
                                                </div>
                                                
                                                <div class="col-lg-12">
                                                    <!-- Date Range Picker With Times -->
                                                    <div class="mb-3">
                                                        <label for="example-select" class="form-label">Image(image Type:JPEG,PNG,Jpg)</label>
                                                        <input type="file" value="" name="image1" class="form-control" placeholder="Give image">
                                                        <?php  
                                                             $fullImagePath = $res['img_1'];

                                                             $prefixToRemove = 'upload/image/';

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

                                                             $prefixToRemove = 'upload/image/';

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

                                                             $prefixToRemove = 'upload/image/';

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

                                                             $prefixToRemove = 'upload/image/';

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

                                                             $prefixToRemove = 'upload/image/';

                                                             $imageFilename = str_replace($prefixToRemove, '', $fullImagePath);
                                                             ?>
                                                             <img src="<?= '../upload/image/' . $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">Map Address</label>
                                                        <input type="text" value="<?php echo $res['map_address'];?>" name="map_address" class="form-control" placeholder="Map Address">
                                                    </div>
                                                </div>
                                                
                                                <div class="col-lg-12">
                                                    <!-- Date Range Picker With Times -->
                                                    <div class="mb-3">
                                                        <label for="example-select" class="form-label">Rating</label>
                                                        <input type="text" value="<?php echo $res['rating'];?>" name="rating" class="form-control" placeholder="Rating">
                                                    </div>
                                                </div>
                                                
                                                    <div class="mb-3">
                                                   <label for="example-select" class="form-label">Status</label>
                                                   <select class="form-select" id="example-select" name="status">
                                                    <!-- Add your dropdown options here -->
                                                    <option value="">Select</option>
                                                    <option <?php if($res['status']=='mangal') echo 'selected'; ?> value="mangal">Mangal</option>
                                                    <option <?php if($res['status']=='amangal') echo 'selected'; ?> value="amangal">Amangal</option>
                                                    <!-- Add more options as needed -->
                                                </select>
                                               </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'; 
?>
            <script src="https://cdn.ckeditor.com/ckeditor5/11.0.1/classic/ckeditor.js"></script>

<!-- 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>
        
        <script>

        ClassicEditor
            .create( document.querySelector( '#editor' ) )
            .catch( error => {
                console.error( error );
            } );
    </script>