<?= $this->Html->css('multi-image-selector-with-tag-caption.css'); ?>
<?= $this->Html->script('multi-image-selector-with-tag-caption.js'); ?>

<section class="content">
    <div class="col-12">
        <div class="submenu">
            <?= $this->element('banners/menu/mainmenu'); ?>
        </div>

        <div class="submenu">
            <?= $this->element('banners/menu/submenu'); ?>
        </div>

        <div class="card">
            <div class="card-body">
                <div class="row">
                    <h4 class="text-center fw-bold"><?= $pageContentTitle; ?></h4>
                    <hr class="hr-primary" />
                </div>

                <?= $this->element('banners/form'); ?>
            </div>
        </div>
    </div>
</section>

<script>
    function removeImage(str) {
        const xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function() {
            if (xhttp.readyState === 4 && xhttp.status === 200) {
                alert('Deleted');
                const oldPicture = document.getElementById('image_div' + str);
                if (oldPicture) {
                    oldPicture.remove();
                }
            }
        };
        xhttp.open("GET", "../removeNewsImage/" + str, true);
        xhttp.send();
    }

    $(document).ready(function() {
        $('#imag').click();
    });

    function loadimages() {
        const str = $("#search_tag").val();
        if (str === "") return;

        const xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function() {
            if (xhttp.readyState === 4 && xhttp.status === 200) {
                let myresult = xhttp.responseText.replace(/["{}\[\]]/g, '');
                const mydata = myresult.split(',');

                let inneroptions = "";
                for (const item of mydata) {
                    const current = item.split(':');
                    inneroptions += `<li><img width="95" height="70" class="search-thumb" onclick="updateSelected(this)" src="../img/${current[1].replace('\\','')}" name="${current[0]}" /></li>`;
                }
                document.getElementById("my-images").innerHTML = inneroptions;
            }
        };
        xhttp.open("GET", "getImages/" + str, true);
        xhttp.send();
    }

    $(document).ready(function () {
        $('#img')[0].click();
    });
</script>
