<?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_Template $this */
?>
<script type="text/javascript">
<!--
<?php $myBlock = $this->getLayout()->getBlock('roleUsersGrid'); ?>
<?php if( is_object($myBlock) && $myBlock->getJsObjectName() ): ?>
    var checkBoxes = $H(<?= $myBlock->_getUsers(true) ?>);
    var warning = false;
    if (checkBoxes.size() > 0) {
        warning = true;
    }
    $('in_role_user').value = checkBoxes.toQueryString();

    function registerUserRole(grid, element, checked)
    {
        if (checked) {
            checkBoxes.set(element.value, 0);
        } else {
            checkBoxes.unset(element.value);
        }
        $('in_role_user').value = checkBoxes.toQueryString();
        grid.reloadParams = {'in_role_user[]':checkBoxes.keys()};
    }

    function roleUsersRowClick(grid, event)
    {
        var trElement = Event.findElement(event, 'tr');
        var isInput   = Event.element(event).tagName == 'INPUT';
        if (trElement) {
            var checkbox = Element.getElementsBySelector(trElement, 'input');
            if(checkbox[0]){
                var checked = isInput ? !checkbox[0].checked : checkbox[0].checked;
                if (checked) {
                    if ( confirm('<?= Mage::helper('core')->jsQuoteEscape($this->__('Warning!\r\nThis action will remove this user from already assigned role\r\nAre you sure?')) ?>') ) {
                        checkbox[0].checked = false;
                        checkBoxes.unset(checkbox[0].value);
                        varienElementMethods.setHasChanges(checkbox[0]);
                    } else {
                        checkbox[0].checked = true;
                    }
                } else {
                    checkbox[0].checked = true;
                    checkBoxes.set(checkbox[0].value, true);
                }

                $('in_role_user').value = checkBoxes.toQueryString();
                grid.reloadParams = {'in_role_user[]':checkBoxes.keys()};
            }
        }
    }

    function roleUsersRowInit(grid, row)
    {
        var checkbox = $(row).getElementsByClassName('checkbox')[0];
        if (checkbox) {
            if (checkbox.checked) {
                checkBoxes.set(checkbox.value, checkbox.checked);
            } else {
                checkBoxes.unset(checkbox.value);
            }

            $('in_role_user').value = checkBoxes.toQueryString();
            grid.reloadParams = {'in_role_user[]':checkBoxes.keys()};
        }
    }

    function massSelectUsers(allCheckbox)
    {
        if (!allCheckbox.checked && checkBoxes.size() > 0) {
            if (!confirm('<?= Mage::helper('core')->jsQuoteEscape($this->__('Warning!\r\nThis action will remove those users from already assigned roles\r\nAre you sure?')) ?>') ) {
                allCheckbox.checked = false;
                return false;
            }
        }
        <?= $myBlock->getJsObjectName() ?>.rows.each(function(row)
        {
            $(row).getElementsByClassName('checkbox')[0].checked = allCheckbox.checked;
            roleUsersRowInit(<?= $myBlock->getJsObjectName() ?>, row);
        });
    }

<?= $myBlock->getJsObjectName() ?>.rowClickCallback = roleUsersRowClick;
<?= $myBlock->getJsObjectName() ?>.initRowCallback = roleUsersRowInit;
<?= $myBlock->getJsObjectName() ?>.checkboxCheckCallback = registerUserRole;
<?= $myBlock->getJsObjectName() ?>.checkCheckboxes = massSelectUsers;
<?= $myBlock->getJsObjectName() ?>.rows.each(function(row){roleUsersRowInit(<?= $myBlock->getJsObjectName() ?>, row)});
    $('in_role_user_old').value = $('in_role_user').value;
<?php endif ?>
//-->
</script>
