<?php 
    include "header.php";
   include "databaseConnect.php";  
   $cczCount ='';  
   $totalCount='';
   $ccaCount='';

   $id = '';
   if(isset($_GET['Id']) && !empty($_GET['Id'])){
    $id = $_GET['Id'];
   }else{
    exit;
   }

   $query = "SELECT * FROM tblpagecontent where id= $id"; 
   $result = mysqli_query($conn, $query);
?>
<style>
    .switch {
        position: relative;
        display: inline-block;
        width: 50px;
        height: 23px;
    }
    .switch input {
        opacity: 0;
        width: 0;
        height: 0;
    }
    .slider {
        position: absolute;
        cursor: pointer;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background-color: #ccc;
        transition: .4s;
    }
    .slider:before {
        position: absolute;
        content: "";
        height: 15px;
        width: 15px;
        left: 4px;
        bottom: 4px;
        background-color: white;
        transition: .4s;
    }
    input:checked + .slider {
        background-color: #0d6efd;
    }
    input:focus + .slider {
        box-shadow: 0 0 1px #0d6efd;
    }
    input:checked + .slider:before {
        transform: translateX(26px);
    }
    .slider.round {
        border-radius: 34px;
    }
    .slider.round:before {
        border-radius: 50%;
    }
</style>
<div class="content-wrapper">
    <div class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1 class="m-0"><i class="nav-icon fas fa-tachometer-alt"></i> Dashboard</h1>
                </div><!-- /.col -->
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item">Home</li>
                        <li class="breadcrumb-item">Edit Page Content</li>
                    </ol>
                </div><!-- /.col -->
            </div><!-- /.row -->
        </div><!-- /.container-fluid -->
    </div>
    <!-- /.content-header -->

    <!-- Main content -->
    <section class="content" style = "min-height:454px;">
        <div class="container-fluid">
            <?php if(mysqli_num_rows($result) > 0){
                $res = mysqli_fetch_assoc($result);
            ?>
            <form method="post" id="addpageContentForm" enctype="multipart/form-data">
                <input type = "hidden" name="type" value="addPageContent" />
                <div class="row mb-3">
                    <div class="col-md-6">
                        <label>Page Slug</label>
                        <select class="form-control" name="addBySlug" id="addBySlug">
                            <option value=""> Slug</option>
                            <?php 
                                $sql = "select distinct slug from tblhoteldetails where slug != ''";
                                $query = mysqli_query($conn, $sql);
                                if(mysqli_num_rows($query)>0){
                            ?>
                                    <?php while($slugs = mysqli_fetch_assoc($query)){?>
                                        <option value="<?=$slugs['slug']?>" <?=$res['slug']== $slugs['slug'] ? 'selected' : ''?>><?=$slugs['slug']?></option>
                            <?php }}?>
                        </select>
                    </div>
                    <div class="col-md-6">
                        <label>Content Category</label>
                        <select class="form-control" name="addByCategory" id="addByCategory">
                            <option value="">Select Category</option>
                            <option value=1 <?=$res['contentcategory']== 1 ? 'selected' : ''?>>Booking.com</option>
                            <option value=2 <?=$res['contentcategory']== 2 ? 'selected' : ''?>>Expedia</option>
                            <option value=3 <?=$res['contentcategory']== 3 ? 'selected' : ''?>>Hotels.com</option>
                            <option value=4 <?=$res['contentcategory']== 4 ? 'selected' : ''?>>Hotwire</option>
                        </select>
                    </div>
                </div>
                <div class="row mb-3">
                    <div class="col-sm-12">
                        <label>Page Content</label>
                        <textarea class="form-control" rows="8" name="addPageContent" id="addPageContent" placeholder="Enter Content here..."><?=$res['pagecontent']?></textarea>
                        <small class="text-danger error" id="errortimeToVisitContent"></small>
                    </div>
                </div>
                <div class="row text-right mb-3" style="justify-content:right;">
                    <button type="button"  class="btn btn-primary btn-lg" id="addHotelData" onclick ="addPagecontentData();">Submit</button>
                </div>
            </form>
            <?php }?>
        </div><!-- /.container-fluid -->
    </section>
    <!-- /.content -->
</div>
<?php include "footer.php"?>
<script src="ckeditor/ckeditor.js"></script>
<script>
    var editor_config =  {
   // Make the editing area bigger than default.
   height: 300,

   // Allow pasting any content.
   allowedContent: true,

   // Fit toolbar buttons inside 3 rows.
   toolbarGroups: [{
       name: 'document',
       groups: ['mode', 'document', 'doctools']
     },
     {
       name: 'clipboard',
       groups: ['clipboard', 'undo']
     },
     {
       name: 'editing',
       groups: ['find', 'selection', 'spellchecker', 'editing']
     },
     {
       name: 'forms',
       groups: ['forms']
     },
     {
       name: 'paragraph',
       groups: ['list', 'indent', 'blocks', 'align', 'bidi', 'paragraph']
     },
     '/',
     {
       name: 'links',
       groups: ['links']
     },
     {
       name: 'insert',
       groups: ['insert']
     },
     {
       name: 'styles',
       groups: ['styles']
     },
     {
       name: 'basicstyles',
       groups: ['basicstyles', 'cleanup']
     },
     {
       name: 'colors',
       groups: ['colors']
     },
     {
       name: 'tools',
       groups: ['tools']
     },
     {
       name: 'others',
       groups: ['others']
     },
     {
       name: 'about',
       groups: ['about']
     }
   ],
   // Remove buttons irrelevant for pasting from external sources.
   removeButtons: 'ExportPdf,Form,Checkbox,TextField,Select,Textarea,Button,ImageButton,HiddenField,NewPage,CreateDiv,Flash,Iframe,About,ShowBlocks,Maximize',
   // This is optional, but will let us define multiple different styles for multiple editors using the same CSS file.
   bodyClass: 'document-editor',

    // Add a placeholder
    placeholder: 'Review Description..',
 }
    CKEDITOR.replace('addPageContent', editor_config); 

    function addPagecontentData() {
        var slug  = $('#addBySlug').val();
        var category = $('#addByCategory').val();
        const editorData = CKEDITOR.instances.addPageContent.getData();
        // const description = $(editorData).text();

        if (slug == "") {
            event.preventDefault();
            toastr.error('Please select Page Slug');
            return false;
        }
        if (category == "") {
            event.preventDefault();
            toastr.error('Please select Content Category');
            return false;
        }
        if (editorData == "") {
            event.preventDefault();
            toastr.error('Please Enter Page Content');
            return false;
        }
        
        $.ajax({
            url: 'manage-hotels-process.php',
            type: 'POST',
            data: { 
                    type: "addPageContent",
                    addBySlug: slug,
                    addByCategory: category,
                    addPageContent: editorData
                },
            dataType: 'json',
            success: function (response) {
                if (response.status === 'success') {
                    toastr.success(response.message);
                    $('#addpageContentForm')[0].reset();
                    setTimeout(function () {
                        window.location.href = "pages-content.php";
                    }, 2000);
                } else {
                    toastr.error(response.message); 
                }
            },
            error: function () {
                toastr.error('An error occurred while processing the request.');
            }
        });
    }

</script>