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

// @codingStandardsIgnoreFile

/** @var Ess_M2ePro_Block_Adminhtml_Ebay_Listing_Template_Switcher $this */

?>

<script type="text/javascript">

    var init = function() {
        EbayListingTemplateSwitcherObj.updateEditVisibility('<?php echo $this->getTemplateNick() ?>');
        EbayListingTemplateSwitcherObj.updateButtonsVisibility('<?php echo $this->getTemplateNick() ?>');
        EbayListingTemplateSwitcherObj.updateTemplateLabelVisibility('<?php echo $this->getTemplateNick() ?>');

        $('<?php echo $this->getSwitcherId() ?>').observe('change', EbayListingTemplateSwitcherObj.change);

        <?php if ($this->isTemplateModeTemplate()): ?>
            $('<?php echo $this->getSwitcherId() ?>').simulate('change');
        <?php endif; ?>
    };

    <?php if ($this->getRequest()->isXmlHttpRequest()): ?>
        init();
    <?php else: ?>
        Event.observe(window, 'load', init);
    <?php endif; ?>

</script>

<div class="template-switcher-container">

    <div class="content-header">
        <table cellspacing="0">
            <tr>
                <td class="template-header" style="width: <?php echo $this->getHeaderWidth() ?>px;">
                    <h3 style="margin: 0;"><?php echo $this->getHeaderText() ?></h3>
                </td>

                <?php $display = $this->canDisplaySwitcher() ? 'cell' : 'none'; ?>
                <td class="template-switcher-td" style="display: <?php echo $display ?>;">
                    <?php if ($this->getTemplatesCount() > $this::MAX_TEMPLATE_ITEMS_COUNT) :?>
                        <span class="template" style="color: red; position: absolute; margin-top: 25px"><?php echo $this->__('Drop-down List Limit is exceeded. Only 10 000 records will be shown. Please contact Support at support@m2epro.com to resolve it.') ?></span>
                    <?php endif; ?>
                    <select name="<?php echo $this->getSwitcherName() ?>" id="<?php echo $this->getSwitcherId() ?>" class="template-switcher M2ePro-validate-ebay-template-switcher">
                        <?php if ($this->canDisplayUseDefaultOption() && $this->isTemplateModeParentForced()): ?>
                            <option value="<?php echo base64_encode(json_encode(array('mode' => null))) ?>" selected="selected" class="template-switcher-empty-option empty"></option>
                        <?php endif; ?>

                        <?php
                            $value = base64_encode(json_encode(array(
                                'id' => $this->isTemplateModeCustom() ? $this->getTemplateId() : null,
                                'mode' => Ess_M2ePro_Model_Ebay_Template_Manager::MODE_CUSTOM,
                                'nick' => $this->getTemplateNick()
                            )));
                            $selected = $this->isTemplateModeCustom();
                        ?>
                        <option value="<?php echo $value ?>" <?php if ($selected): ?>selected="selected"<?php endif; ?> class="template-switcher-custom-option">
                            <?php echo Mage::helper('M2ePro')->__('Custom Settings') ?>
                        </option>

                        <?php if ($this->canDisplayUseDefaultOption()): ?>
                            <?php
                                $value = base64_encode(json_encode(array(
                                    'id' => null,
                                    'mode' => Ess_M2ePro_Model_Ebay_Template_Manager::MODE_PARENT,
                                    'nick' => $this->getTemplateNick()
                                )));
                                $selected = $this->isTemplateModeParent() && !$this->isTemplateModeParentForced();
                            ?>
                            <option value="<?php echo $value; ?>" <?php if ($selected): ?>selected="selected"<?php endif; ?> class="template-switcher-parent-option">
                                <?php echo Mage::helper('M2ePro')->__('Use From Listing Settings') ?>
                            </option>
                        <?php endif; ?>

                        <?php if ($this->getTemplatesCount() !== 0): ?>
                            <optgroup label="<?php echo Mage::helper('M2ePro')->escapeHtml(Mage::helper('M2ePro')->__('Policies')) ?>" class="templates-group">
                                <?php foreach ($this->getTemplates() as $template): ?>
                                    <?php
                                        $value = base64_encode(json_encode(array(
                                            'id' => $template->getId(),
                                            'mode' => Ess_M2ePro_Model_Ebay_Template_Manager::MODE_TEMPLATE,
                                            'nick' => $this->getTemplateNick()
                                        )));
                                        $selected = $this->isTemplateModeTemplate() && $this->getTemplateId() == $template->getId();
                                    ?>
                                    <option value="<?php echo $value; ?>" <?php if ($selected): ?>selected="selected"<?php endif; ?> class="template-switcher-template-option">
                                        <?php echo Mage::helper('M2ePro')->escapeHtml($template->getTitle()) ?>
                                    </option>
                                <?php endforeach; ?>
                            </optgroup>
                        <?php endif; ?>
                    </select>
                </td>

                <td style="padding-left: 10px; width: 100%;" class="template-nick-td">

                    <div id="template_<?php echo $this->getTemplateNick() ?>_nick_label" style="display: none;">
                        <img src="<?php echo $this->getSkinUrl('M2ePro/images/note_cell_bg.gif'); ?>" />
                        <span class="template" style="display: none;"><?php echo Mage::helper('M2ePro')->__('Policy') ?></span>
                        <?php if ($this->isTemplateModeParentForced()): ?>
                            <span class="parent" style="display: none;"><?php echo Mage::helper('M2ePro')->__('Please, specify a value suitable for all chosen Products.') ?></span>
                        <?php endif; ?>
                    </div>

                    <div id="template_<?php echo $this->getTemplateNick() ?>_edit" style="display: none;">
                        <a href="javascript: void(0);" onclick="EbayListingTemplateSwitcherObj.editTemplate('<?php echo $this->getTemplateNick() ?>');"><?php echo Mage::helper('M2ePro')->__('View') ?>&nbsp;/&nbsp;<?php echo Mage::helper('M2ePro')->__('Edit') ?></a>
                        <?php echo Mage::helper('M2ePro')->__('or') ?>
                        <a href="javascript: void(0);" onclick="EbayListingTemplateSwitcherObj.customizeTemplate('<?php echo $this->getTemplateNick() ?>');"><?php echo Mage::helper('M2ePro')->__('Use Custom Settings') ?></a>
                    </div>

                </td>
            </tr>
            <tr>
                <td></td>
                <td id="template_switcher_<?php echo $this->getTemplateNick() ?>_messages" colspan="2" class="value" style="padding-left: 30px;"></td>
            </tr>
        </table>
    </div>

</div>