<?php
/*
 * @author     M2E Pro Developers Team
 * @copyright  M2E LTD
 * @license    Commercial use is forbidden
 */

// @codingStandardsIgnoreFile

/** @var Ess_M2ePro_Block_Adminhtml_Switcher $this */

?>

<?php if ($items = $this->getItems()) { ?>

    <p class="switcher">

        <label for="<?php echo $this->getParamName(); ?>_switcher"><?php echo $this->getLabel(); ?>:</label>

        <select id="<?php echo $this->getParamName(); ?>_switcher"
                name="<?php echo $this->getParamName(); ?>_switcher"
                onchange="return <?php echo $this->getSwitchCallback() ?>(this);"
                <?php if ($this->hasIsDisabled()): ?>disabled="disabled"<?php endif; ?>
        >
            <?php if ($this->hasDefaultOption()) { ?>
                <option value="<?php echo $this->getDefaultOptionValue(); ?>"><?php echo Mage::helper('M2ePro')->escapeHtml($this->getDefaultOptionName()); ?></option>
            <?php } ?>

            <?php foreach ($items as $item) { ?>

                <?php if (count($items) > 1) { ?>
                    <optgroup label="<?php echo $item['label'] ?>">
                <?php } ?>

                <?php foreach ($item['value'] as $nestedItem) { ?>
                    <option
                        value="<?php echo $nestedItem['value'] ?>"
                        <?php if ($this->getSelectedParam() == $nestedItem['value']) echo ' selected="selected"' ?>
                    >
                        <?php echo $nestedItem['label'] ?>
                    </option>
                <?php } ?>

                <?php if (count($items) > 1) { ?>
                    </optgroup>
                <?php } ?>

            <?php } ?>
        </select>

    </p>

    <script type="text/javascript">

        <?php echo $this->getSwitchCallback() ?> = function(obj)
        {
            var switchUrl        = '<?php echo Mage::helper('M2ePro')->escapeJs($this->getSwitchUrl()); ?>';
            var paramName        = '<?php echo Mage::helper('M2ePro')->escapeJs($this->getParamName()); ?>';
            var paramPlaceHolder = '<?php echo Mage::helper('M2ePro')->escapeJs($this->getParamPlaceHolder()); ?>';

            if (obj.value == '<?php echo $this->getDefaultOptionValue(); ?>') {
                switchUrl = switchUrl.replace(paramName +'/'+ paramPlaceHolder +'/', '');
            } else {
                switchUrl = switchUrl.replace(paramPlaceHolder, obj.value);
            }

            <?php if ($this->getUseConfirm()) { ?>

                if (confirm('<?php echo Mage::helper('M2ePro')->escapeJs($this->getConfirmMessage()) ?>')){
                    setLocation(switchUrl);
                    return true;
                } else {
                    obj.value = '<?php echo Mage::helper('M2ePro')->escapeJs($this->getSelectedParam()); ?>';
                }
                return false;

            <?php } else { ?>
                setLocation(switchUrl);
            <?php } ?>
        }

    </script>

<?php } ?>