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

// @codingStandardsIgnoreFile

/** @var $this Ess_M2ePro_Block_Adminhtml_Walmart_Listing_Variation_Product_Manage_Tabs_Vocabulary */

$vocabularyHelper = Mage::helper('M2ePro/Component_Walmart_Vocabulary');

$data = $this->prepareData();
$localVocabulary = $data['local_vocabulary'];
$fixedAttributes = $data['fixed_attributes'];
?>

<div style="height: 510px; overflow: auto;">

<div id="block_notice_walmart_product_manage_vocabulary" class="block_notices_module" title="<?php echo Mage::helper('M2ePro')->__('Advanced'); ?>">
    <?php echo Mage::helper('M2ePro')->__('
        M2E Pro can remember the correspondence between Magento Variational Attributes/Options and Walmart Variant Attributes/Options that you define.<br>
        This allows you to use the configured associations in the future automatically. Please set your preferences for the options below.<br><br>
        
        <strong>Note:</strong> The remembered associations is a global setting and will be applied to Magento Variational Products in both Walmart and Amazon integrations.<br><br>

        All remembered associations can be found under the Saved Attributes and Options section.<br><br>

        <strong>Note:</strong> The list of saved Attributes and Options contains all associations that were configured for the particular Magento Variational Attribute.
    '); ?>
</div>

<form id="auto_action_settings_form" action="javascript:void(0);">
<div class="entry-edit" id="magento_block_walmart_variation_manager_vocabulary_remember">

    <div class="entry-edit-head">
        <h4 class="icon-head head-edit-form fieldset-legend"><?php echo Mage::helper('M2ePro')->__('Saving of manual Variational Attributes/Options matches'); ?></h4>
    </div>
    <div class="fieldset">
        <div class="hor-scroll">

<table class="form-list" cellspacing="0" cellpadding="0">
    <tr>
        <td class="label">
            <label for="attribute_auto_action_enabled"><?php echo Mage::helper('M2ePro')->__('Save selected matching of Attributes?')?></label>
        </td>
        <td class="value">
            <select name="attribute_auto_action">
                <option value="<?php echo Ess_M2ePro_Helper_Component_Walmart_Vocabulary::VOCABULARY_AUTO_ACTION_NOT_SET ?>" <?php echo $vocabularyHelper->isAttributeAutoActionNotSet() ? 'selected="selected"' : '' ?>><?php echo Mage::helper('M2ePro')->__('Ask Me'); ?></option>
                <option value="<?php echo Ess_M2ePro_Helper_Component_Walmart_Vocabulary::VOCABULARY_AUTO_ACTION_NO ?>" <?php echo $vocabularyHelper->isAttributeAutoActionDisabled() ? 'selected="selected"' : '' ?>><?php echo Mage::helper('M2ePro')->__('Don\'t save'); ?></option>
                <option value="<?php echo Ess_M2ePro_Helper_Component_Walmart_Vocabulary::VOCABULARY_AUTO_ACTION_YES ?>" <?php echo $vocabularyHelper->isAttributeAutoActionEnabled() ? 'selected="selected"' : '' ?>><?php echo Mage::helper('M2ePro')->__('Save'); ?></option>
            </select>
        </td>
    </tr>
    <tr>
        <td class="label">
            <label for="option_auto_action_enabled"><?php echo Mage::helper('M2ePro')->__('Save selected matching of Options?')?></label>
        </td>
        <td class="value">
            <select name="option_auto_action">
                <option value="<?php echo Ess_M2ePro_Helper_Component_Walmart_Vocabulary::VOCABULARY_AUTO_ACTION_NOT_SET ?>" <?php echo $vocabularyHelper->isOptionAutoActionNotSet() ? 'selected="selected"' : '' ?>><?php echo Mage::helper('M2ePro')->__('Ask Me'); ?></option>
                <option value="<?php echo Ess_M2ePro_Helper_Component_Walmart_Vocabulary::VOCABULARY_AUTO_ACTION_NO ?>" <?php echo $vocabularyHelper->isOptionAutoActionDisabled() ? 'selected="selected"' : '' ?>><?php echo Mage::helper('M2ePro')->__('Don\'t save'); ?></option>
                <option value="<?php echo Ess_M2ePro_Helper_Component_Walmart_Vocabulary::VOCABULARY_AUTO_ACTION_YES ?>" <?php echo $vocabularyHelper->isOptionAutoActionEnabled() ? 'selected="selected"' : '' ?>><?php echo Mage::helper('M2ePro')->__('Save'); ?></option>
            </select>
        </td>
    </tr>
    <tr>
        <td colspan="2" class="label">
            <div style="text-align: right">
                <button onclick="ListingGridObj.variationProductManageHandler.saveAutoActionSettings()">
                    <?php echo Mage::helper('M2ePro')->__('Save'); ?>
                </button>
            </div>
        </td>
    </tr>
</table>

        </div>
    </div>
</div>
</form>

<?php if (!empty($localVocabulary)) { ?>

<div class="entry-edit" id="magento_block_walmart_variation_manager_vocabulary_attributes_options">

    <div class="entry-edit-head">
        <h4 class="icon-head head-edit-form fieldset-legend"><?php echo Mage::helper('M2ePro')->__('Saved Attributes & Options'); ?></h4>
    </div>
    <div class="fieldset">
        <div class="hor-scroll">

<table class="form-list" cellspacing="0" cellpadding="0">
    <tr>
        <td class="label" style="font-weight: bold; border-bottom: 1px solid #D6D6D6 !important; border-right: 1px solid #D6D6D6 !important;">
            <?php echo Mage::helper('M2ePro')->__('Magento') ?>
        </td>
        <td class="value" style="font-weight: bold; border-bottom: 1px solid #D6D6D6 !important;">
            <?php echo Mage::helper('M2ePro')->__('Walmart') ?>
        </td>
    </tr>

<?php
    foreach ($localVocabulary as $magentoAttribute => $walmartAttributes) {
        foreach ($walmartAttributes as $walmartAttribute => $magentoOptions) {
?>

    <tr class="matched-attributes-pair">
        <td class="label" style="padding: 5px 0 0 5px !important; border-right: 1px solid #D6D6D6 !important;">
            <?php echo $magentoAttribute ?>
        </td>
        <td class="value" style="padding: 5px 0 0 5px !important;">
            <div class="magento-attribute-name" style="display: none;"><?php echo $magentoAttribute; ?></div>
            <div class="channel-attribute-name" style="display: none;"><?php echo $walmartAttribute; ?></div>
            <span><?php echo $walmartAttribute ?></span>&nbsp;
            <?php if (empty($fixedAttributes[$magentoAttribute]) || !in_array($walmartAttribute, $fixedAttributes[$magentoAttribute])) { ?>
            <a title="<?php echo Mage::helper('M2ePro')->__('Remove') ?>"
               href="javascript:void(0)"
               onclick="ListingGridObj.variationProductManageHandler.removeAttributeFromVocabulary(this)"
               style="color: red; font-size: 14px; text-decoration: none;"
            ><img style="height: 11px;" src="<?php echo $this->getSkinUrl('M2ePro/images/delete.png')?>"></a>
            <?php } ?>
        </td>
    </tr>

<?php foreach ($magentoOptions as $magentoOption => $walmartOptions) { ?>

    <tr class="magento-attribute-options">
        <td class="label" style="padding: 0 0 0 5px !important; border-right: 1px solid #D6D6D6 !important;">
            <label style="padding-left: 25px;"><?php echo $magentoOption ?></label>
        </td>
        <td class="value" style="padding: 0 0 0 5px !important;">
            <ul class="product-options-groups" style="padding-left: 25px;">

                <?php foreach ($walmartOptions as $walmartOptionsGroup) { ?>

                <li class="channel-attribute-options-group" style="margin: 0;">
                    <div class="product-option" style="display: none;"><?php echo $magentoOption ?></div>
                    <div class="product-options-group" style="display: none;"><?php echo Mage::helper('M2ePro')->escapeHtml(Mage::helper('M2ePro')->jsonEncode($walmartOptionsGroup)); ?></div>
                    <div class="channel-attribute-name" style="display: none;"><?php echo $walmartAttribute ?></div>
                    <?php
                        if(($key = array_search($magentoOption, $walmartOptionsGroup)) !== false) {
                            unset($walmartOptionsGroup[$key]);
                        }
                    ?>
                    <span><?php echo implode(', ', $walmartOptionsGroup) ?></span>&nbsp;
                    <a title="<?php echo Mage::helper('M2ePro')->__('Remove') ?>"
                       href="javascript:void(0)"
                       onclick="ListingGridObj.variationProductManageHandler.removeOptionFromVocabulary(this)"
                       style="color: red; font-size: 14px; text-decoration: none;"
                    ><img style="height: 11px;" src="<?php echo $this->getSkinUrl('M2ePro/images/delete.png')?>"></a>
                </li>

                <?php } ?>

            </ul>
        </td>
    </tr>

<?php
            }
        }
    }
?>
</table>

        </div>
    </div>
</div>

<?php } ?>

</div>