<?php
/**
 * Maho
 *
 * @category    design
 * @package     default_default
 * @copyright   Copyright (c) 2006-2020 Magento, Inc. (https://magento.com)
 * @copyright   Copyright (c) 2021-2024 The OpenMage Contributors (https://openmage.org)
 * @copyright   Copyright (c) 2024 Maho (https://mahocommerce.com)
 * @license     https://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */

/** @var Mage_Adminhtml_Block_Newsletter_Subscriber $this */
?>
<div class="content-header">
    <table cellspacing="0">
        <tr>
            <td style="width:50%;"><h3><?= Mage::helper('newsletter')->__('Newsletter Subscribers') ?></h3></td>
        </tr>
    </table>
</div>
<div>
    <?= $this->getChildHtml('grid') ?>
</div>
<?php if(count($this->getQueueAsOptions())>0 && $this->getShowQueueAdd()): ?>
<div class="form-buttons">
    <select id="queueList" name="queue">
    <?php foreach ($this->getQueueAsOptions() as $_queue): ?>
        <option value="<?= $_queue['value'] ?>"><?= $_queue['label'] ?> #<?= $_queue['value'] ?></option>
    <?php endforeach ?>
    </select>
    <button class="scalable" onclick="subscriberController.addToQueue();"><span><?= Mage::helper('newsletter')->__('Add to Queue') ?></span></button>
</div>
<?php endif ?>
<script type="text/javascript">
<!--
    var subscriberController = {
        checkCheckboxes: function(controlCheckbox) {
            var elements = $('subscriberGrid').getElementsByClassName('subscriberCheckbox');
            elements.each(function(obj){
                obj.checked = controlCheckbox.checked;
            });
        },
        rowClick: function (e) {
            if(!Event.element(e).hasClassName('subscriberCheckbox')) {
                var elements = Event.findElement(e, 'tr').getElementsByClassName('subscriberCheckbox');
                if(elements.length != 1) {
                    return;
                }
                elements[0].checked = !elements[0].checked;
            }
        },
        addToQueue: function () {
            var elements = $('subscriberGrid').getElementsByClassName('subscriberCheckbox');
            elements.push($('queueList'));
            var serializedElements = Form.serializeElements(elements, true);
            serializedElements.add = 'subscribers';
            serializedElements.form_key = FORM_KEY;
            var url = subscriberGridJsObject.url;

            new Ajax.Updater('subscriberGrid', url + '?ajax=1',
                             {parameters:  serializedElements,
                              evalScripts: true,
                              onComplete:subscriberGridJsObject.initGrid.bind(subscriberGridJsObject)});
        }
    }

    varienGlobalEvents.attachEventHandler('gridRowClick', subscriberController.rowClick.bind(subscriberController));
//-->
</script>
