<?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_Settings */

$magentoProductVariations = $this->getListingProduct()->getMagentoProduct()->getVariationInstance()->getVariationsTypeStandard();

?>
<style>
    .scrolled-table tbody {
        max-height: 200px;
        min-width: 300px;
        display: inline-block;
        overflow-y: auto;
        margin-bottom: 10px;
    }
</style>

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

<div class="hor-scroll" style="padding-bottom: 15px; ">
    <div id="variation_settings_error_block">
        <ul class="messages">
                <?php if (count($this->getMessages()) > 0) {
                echo $this->getWarnings();
            } ?>
        </ul>
    </div>
</div>

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

            <div id="block_notice_walmart_product_manage_settings_attributes" class="block_notices_module" title="<?php echo Mage::helper('M2ePro')->__('Walmart Variant Attributes'); ?>">
                <?php echo Mage::helper('M2ePro')->__(
                    'Select Walmart Variant Attribute you will use to vary your Item on the Channel. You can set more than one Variant Attribute for Variational Item that varies by multiple attributes.<br><br>

                    <strong>Note:</strong> the list of Walmart Variant Attributes available for the selection is determined by Walmart Category assigned to your Product.'
                ); ?>
            </div>

            <?php
                $possibleAttributes = $this->getPossibleAttributes();
                $channelAttributes = $this->getListingProductTypeModel()->getChannelAttributes();
            ?>

            <table id="channel_attributes_view" class="form-list scrolled-table" cellspacing="0" cellpadding="0">

                <tbody>
                    <?php if (!$this->getListingProductTypeModel()->hasChannelAttributes()) { ?>
                        <tr>
                            <td class="label" style="width: 300px;">
                                <?php echo Mage::helper('M2ePro')->__('No Walmart Variant Attributes selected') ?>
                            </td>
                        </tr>
                    <?php } else { ?>

                        <tr>
                            <td class="label" style="font-weight: bold; border-bottom: 1px solid #D6D6D6 !important;">
                                <?php echo Mage::helper('M2ePro')->__('Walmart Variant Attributes') ?>
                            </td>
                        </tr>

                        <?php foreach ($possibleAttributes as $key => $attribute) { ?>
                            <?php if (in_array($attribute, $channelAttributes)) { ?>
                                <tr class="channel_attribute">
                                    <td class="label">
                                        <label>
                                            <input type="checkbox" value="<?php echo $attribute ?>" checked="checked" disabled="disabled">
                                            <?php echo Mage::helper('M2ePro')->__($attribute) ?>
                                        </label>
                                    </td>
                                </tr>
                            <?php } ?>
                        <?php } ?>
                    <?php } ?>
                </tbody>

                <tfoot>
                    <?php if (!$this->getListingProduct()->isSetProcessingLock('child_products_in_action')) { ?>
                        <tr id="change_channel_attributes_btn">
                            <td class="label" colspan="2" style="text-align: right;">
                                <button onclick="ListingGridObj.variationProductManageHandler.changeChannelAttributes(this)">
                                    <?php echo $this->getListingProductTypeModel()->hasChannelAttributes() ? Mage::helper('M2ePro')->__('Change') : Mage::helper('M2ePro')->__('Set Attributes'); ?>
                                </button>
                            </td>
                        </tr>
                    <?php } ?>
                </tfoot>

            </table>

            <form id="variation_manager_channel_attributes_form" action="javascript:void(0);" style="display: none;">

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

                        <?php foreach ($possibleAttributes as $key => $attribute) { ?>
                            <tr class="channel_attribute">
                                <td class="label">
                                    <label>
                                        <input name="channel_attribute[]" class="M2ePro-walmart-required-channel-attribute" type="checkbox" value="<?php echo $attribute ?>" <?php if (in_array($attribute, $channelAttributes)) echo 'checked="checked"'; ?>>
                                        <?php echo Mage::helper('M2ePro')->__($attribute) ?>
                                    </label>
                                </td>
                            </tr>
                        <?php } ?>
                    </tbody>

                    <tfoot>
                        <tr id="change_channel_attributes_action">
                            <td class="label" style="text-align: right;">
                                <a href="javascript:void(0);" onclick="ListingGridObj.variationProductManageHandler.cancelChannelAttributes(this);"><?php echo Mage::helper('M2ePro')->__('Cancel'); ?></a>&nbsp;&nbsp;
                                <button onclick="ListingGridObj.variationProductManageHandler.setChannelAttributes()"><?php echo Mage::helper('M2ePro')->__('Confirm'); ?></button>
                            </td>
                        </tr>
                    </tfoot>

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

<?php if ($this->getListingProductTypeModel()->hasChannelAttributes()) {

    /** @var Ess_M2ePro_Model_Walmart_Listing_Product $walmartListingProduct */
    $walmartListingProduct = $this->getListingProduct()->getChildObject();

    $magentoGreaterLeftSideText = Mage::helper('M2ePro')->__('This Walmart Attribute and its Value are virtualized based on the selected Magento Variational Attribute and its Value as physically this Walmart Attribute does not exist.');
    $magentoGreaterRightSideText = Mage::helper('M2ePro')->__('Select a particular Option of the Attribute to fix it for virtualized Walmart Attribute. Please, be thoughtful as only those Variations of Magento Product which contains the selected Option can be sold on Walmart.');

    $walmartGreaterLeftSideText = Mage::helper('M2ePro')->__('This Magento Attribute and its Value are virtualized based on the selected Walmart Variational Attribute and its Value as physically this Magento Attribute does not exist.');
    $walmartGreaterRightSideText = Mage::helper('M2ePro')->__('Select a particular Option of the Attribute to fix it for virtualized Magento Attribute. Please, be thoughtful as your offer will be available only for those Buyers who selected the same Option.');

    $duplicateMagentoAttributesError = Mage::helper('M2ePro')->__('The Magento Attributes which you selected in your settings have the same Labels. Such combination is invalid. Please, add the valid combination of Attributes.');
    $duplicateWalmartAttributesError = Mage::helper('M2ePro')->__('The Walmart Attributes which you selected in your settings have the same Labels. Such combination is invalid. Please, add the valid combination of Attributes.');

    $changeOption = Mage::helper('M2ePro')->__('Change option');
?>

<script type="application/javascript">
    ListingGridObj.variationProductManageHandler.virtualWalmartMatchedAttributes = false;
    ListingGridObj.variationProductManageHandler.walmartVariationSet = false;

    M2ePro.text.help_icon_magento_greater_left = '<?php echo $magentoGreaterLeftSideText ?>';
    M2ePro.text.help_icon_magento_greater_right = '<?php echo $magentoGreaterRightSideText ?>';

    M2ePro.text.help_icon_walmart_greater_left = '<?php echo $walmartGreaterLeftSideText ?>';
    M2ePro.text.help_icon_walmart_greater_right = '<?php echo $walmartGreaterRightSideText ?>';

    M2ePro.text.duplicate_magento_attribute_error = '<?php echo $duplicateMagentoAttributesError ?>';
    M2ePro.text.duplicate_walmart_attribute_error = '<?php echo $duplicateWalmartAttributesError ?>';

    M2ePro.text.change_option = '<?php echo $changeOption ?>';
</script>

<div id="product_search_help_icon_tpl" style="display: none">
    <img class="tool-tip-image" style="vertical-align: middle;" src="<?php echo $this->getSkinUrl('M2ePro/images/tool-tip-icon.png'); ?>">
    <span class="tool-tip-message tip-right" style="display: none;">
        <img src="<?php echo $this->getSkinUrl('M2ePro/images/help.png'); ?>">
        <span class="tool-tip-message-text"></span>
    </span>
</div>

<form id="variation_manager_attributes_form" action="javascript:void(0);">

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

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

    <div class="fieldset">
        <div class="hor-scroll">

            <div id="block_notice_walmart_product_manage_settings_attributes_matching" class="block_notices_module" title="<?php echo Mage::helper('M2ePro')->__('Matching Attributes'); ?>">
                <?php echo Mage::helper('M2ePro')->__(
                    'To sell Magento Variational Product as Walmart Variant Group, you need to set a correspondence between Magento Variational Attribute(s) and Walmart Variant Attribute(s).
                    Click <i>Set Attributes</i> to match the related Attributes and <i>Confirm</i> your choice.<br><br>

                    <strong>Important:</strong> If you change the Variational Attribute or Variational Option names in Magento, you will have to match Magento Variational Attribute(s) with Walmart Variant Attribute(s) again.<br><br>

                    <strong>Note:</strong> Matching of Attributes is required. Otherwise, your Variational Item cannot be listed on the Channel.'
                ); ?>
            </div>

            <?php if (!$this->hasMatchedAttributes() && !$this->getMatcherAttributes()->isAmountEqual()) { ?>

            <table class="form-list" cellspacing="0" cellpadding="0">
                <thead>
                    <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 Attributes') ?>
                        </td>
                        <td class="value" style="font-weight: bold; border-bottom: 1px solid #D6D6D6 !important;">
                            <?php echo Mage::helper('M2ePro')->__('Walmart Attributes') ?>
                        </td>
                    </tr>
                </thead>
                <tbody></tbody>
                <tfoot></tfoot>
            </table>

                <?php if ($this->getMatcherAttributes()->isSourceAmountGreater()) { ?>

            <script type="application/javascript">
                ListingGridObj.variationProductManageHandler.matchingType = ListingGridObj.variationProductManageHandler.MATCHING_TYPE_VIRTUAL_WALMART;
                ListingGridObj.variationProductManageHandler.matchedAttributes = <?php echo json_encode($this->getMatchedAttributes(), JSON_FORCE_OBJECT)?>;
                ListingGridObj.variationProductManageHandler.productAttributes = <?php echo json_encode($this->getProductAttributes())?>;
                ListingGridObj.variationProductManageHandler.destinationAttributes = <?php echo json_encode($this->getDestinationAttributes())?>;
                ListingGridObj.variationProductManageHandler.magentoVariationSet = <?php echo json_encode($magentoProductVariations['set'])?>;

                ListingGridObj.variationProductManageHandler.renderMatchedAttributesNotSetView();
            </script>

                <?php } elseif ($this->getMatcherAttributes()->isDestinationAmountGreater()) {?>

            <script type="application/javascript">
                ListingGridObj.variationProductManageHandler.matchingType = ListingGridObj.variationProductManageHandler.MATCHING_TYPE_VIRTUAL_MAGENTO;
                ListingGridObj.variationProductManageHandler.matchedAttributes = <?php echo json_encode($this->getMatchedAttributes(), JSON_FORCE_OBJECT)?>;
                ListingGridObj.variationProductManageHandler.destinationAttributes = <?php echo json_encode($this->getDestinationAttributes())?>;

                ListingGridObj.variationProductManageHandler.renderMatchedAttributesNotSetView();
            </script>

                <?php } ?>
            <?php } else { ?>

                <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 Attributes') ?>
                        </td>
                        <td class="value" style="font-weight: bold; border-bottom: 1px solid #D6D6D6 !important;">
                            <?php echo Mage::helper('M2ePro')->__('Walmart Attributes') ?>
                        </td>
                    </tr>

                    <?php
                        $attrId = 0;
                        $virtualAttributes = $this->getVirtualAttributes();
                        $virtualProductAttributes = $this->getVirtualProductAttributes();
                        $virtualChannelAttributes = $this->getVirtualChannelAttributes();

                        foreach ($this->getMatchedAttributes() as $magentoAttr => $walmartAttr) {

                            $isVirtual = ($magentoAttr == $walmartAttr) && in_array($magentoAttr, array_keys($virtualAttributes));
                    ?>
                        <tr>
                            <td class="label" style="border-right: 1px solid #D6D6D6 !important;">
                                <?php if (!$isVirtual) { ?>

                                <label for="variation_manager_attributes_walmart_<?php echo $attrId ?>">
                                    <?php echo $magentoAttr ?>
                                </label>

                                <?php } else { ?>

                                <label for="variation_manager_attributes_walmart_<?php echo $attrId ?>">
                                    <span style="<?php echo in_array($magentoAttr, array_keys($virtualProductAttributes)) ? 'border-bottom: 2px dotted grey;' : '' ?>">
                                        <?php echo $magentoAttr ?> (<?php echo $virtualAttributes[$magentoAttr] ?>)
                                    </span>
                                </label>

                                <?php } ?>
                            </td>
                            <td class="value">
                                <input type="hidden" value="<?php echo Mage::helper('M2ePro')->escapeHtml($magentoAttr) ?>" name="variation_attributes[magento_attributes][]">

                                <?php if (!$isVirtual) { ?>

                                <span class="variation_manager_attributes_walmart_value" <?php echo $this->hasMatchedAttributes() ? '' : 'style="color: red;"' ?>>
                                    <?php echo $this->hasMatchedAttributes() ? $walmartAttr : Mage::helper('M2ePro')->__('Not Set') ?>
                                </span>

                                <select id="variation_manager_attributes_walmart_<?php echo $attrId ?>"
                                        name="variation_attributes[walmart_attributes][]"
                                        class="required-entry variation_manager_attributes_walmart_select"
                                        style="display: none;">
                                    <?php
                                        $destinationAttributes = $this->getDestinationAttributes();

                                        if (empty($walmartAttr)) {
                                            echo '<option class="empty" value=""></option>';
                                        }
                                    ?>
                                    <?php foreach ($destinationAttributes as $attr) { ?>
                                        <?php
                                            if (in_array($attr, array_keys($virtualAttributes))) {
                                                continue;
                                            }
                                        ?>
                                        <option value="<?php echo $attr; ?>" <?php echo $walmartAttr == $attr ? 'selected="selected"' : '' ?> ><?php echo $attr; ?></option>
                                    <?php } ?>
                                </select>

                                <?php } else { ?>

                                    <span style="<?php echo in_array($walmartAttr, array_keys($virtualChannelAttributes)) ? 'border-bottom: 2px dotted grey;' : '' ?>"><?php echo $walmartAttr ?> (<?php echo $virtualAttributes[$walmartAttr] ?>)</span>
                                    <input type="hidden" name="variation_attributes[walmart_attributes][]" value="<?php echo $walmartAttr ?>" />

                                <?php } ?>
                                <div id="variation_manager_attributes_error_<?php echo $attrId ?>" class="validation-advice" style="display: none;"></div>
                            </td>
                        </tr>

                    <?php
                            $attrId++;
                        }
                    ?>

                    <tr>
                        <td class="label" colspan="2" style="text-align: right; <?php echo $this->isChangeMatchedAttributesAllowed() ? '' : 'display: none;' ?>">
                            <button onclick="ListingGridObj.variationProductManageHandler.changeMatchedAttributes(this)">
                                <?php echo $this->hasMatchedAttributes() ? Mage::helper('M2ePro')->__('Change') : Mage::helper('M2ePro')->__('Set Attributes'); ?>
                            </button>
                            <a href="javascript:void(0);" onclick="ListingGridObj.variationProductManageHandler.cancelMatchedAttributes(this);" style="display: none;"><?php echo Mage::helper('M2ePro')->__('Cancel'); ?></a>&nbsp;&nbsp;
                            <button onclick="ListingGridObj.variationProductManageHandler.setMatchedAttributes()" style="display: none;"><?php echo Mage::helper('M2ePro')->__('Confirm'); ?></button>
                        </td>
                    </tr>
                </table>
            <?php } ?>
        </div>
    </div>
</div>

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

    <div class="entry-edit-head">
        <h4 class="icon-head head-edit-form fieldset-legend"><?php echo Mage::helper('M2ePro')->__('Swatch Variant Attribute'); ?></h4>
    </div>

    <div class="fieldset">
        <div class="hor-scroll">

        <div id="block_notice_walmart_product_manage_settings_attributes_swatches" class="block_notices_module" title="<?php echo Mage::helper('M2ePro')->__('Swatch Variant Attribute'); ?>">
            <?php echo Mage::helper('M2ePro')->__(
                'Select Swatch Variant Attribute by which the Swatch Images will be shown on your Walmart Item page.<br><br>
                <strong>Note:</strong> In Description Policy, you may select Magento source where the Images for Walmart Item Variations will be taken from.'
            ); ?>
        </div>

        <table class="form-list" cellspacing="0" cellpadding="0">
            <tr>
                <td class="label">
                    <?php echo Mage::helper('M2ePro')->__('Swatch Variant Attribute') ?>
                </td>
                <td class="value">
                    <select name="swatch_images" id="swatch_images_attributes">
                        <option value=""><?php echo Mage::helper('M2ePro')->__('None') ?></option>
                        <?php
                            $swatchImagesAttribute = $this->getSwatchImagesAttribute();
                            foreach ($possibleAttributes as $key => $attribute) {
                                if (in_array($attribute, $channelAttributes)) { ?>
                                <option value="<?php echo $attribute ?>" <?php echo $swatchImagesAttribute !== null && $swatchImagesAttribute === $attribute ? 'selected="selected"' : ''?>>
                                    <?php echo Mage::helper('M2ePro')->__($attribute) ?>
                                </option>
                            <?php }
                            } ?>
                    </select>
                </td>
                <td class="value">
                    <button onclick="ListingGridObj.variationProductManageHandler.setSwatchImagesAttribute()">
                        <?php echo Mage::helper('M2ePro')->__('Change'); ?>
                    </button>
                </td>
            </tr>
        </table>
    </div>
</div>

</form>

<?php } ?>

</div>