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

// @codingStandardsIgnoreFile

/** @var $this Ess_M2ePro_Block_Adminhtml_Walmart_Template_Synchronization_Edit_Tabs_List */

?>

<div id="block_notice_walmart_template_synchronization_general" class="block_notices_module" title="<?php echo Mage::helper('M2ePro')->__('List Rules'); ?>">
    <?php echo Mage::helper('M2ePro')->__('
    Synchronization Policy includes rules and conditions based on which M2E Pro automatically transfers your Magento data to the Channel. You may configure the List, Revise, Relist and Stop Rules.<br><br>

    Enable the List Action and define the List Conditions based on which M2E Pro will automatically list the Not Listed Items on Walmart.
    If the initial list fails, the Module will reattempt the Item listing after the Product Status, Stock Availability or Quantity are changed.<br><br>

    <strong>Note:</strong> Inventory Synchronization must be enabled under the <i>%navigation%</i>. Otherwise, Synchronization Policy Rules will not take effect.<br><br>

    <strong>Note:</strong> Synchronization Policy is required when you create a new offer on Walmart.<br><br>
    
    The detailed information can be found <a href="%url%" target="_blank">here</a>.',

    Mage::helper('M2ePro/View_Walmart')->getPageNavigationPath('configuration', 'Synchronization'),
    Mage::helper('M2ePro/Module_Support')->getDocumentationUrl(null, null, 'walmart-integration')
    ); ?>
</div>

<?php
    $formData = Mage::helper('M2ePro/Data_Global')->getValue('temp_data') ? Mage::helper('M2ePro/Data_Global')->getValue('temp_data')->toArray() : array();
    $defaults = Mage::getModel('M2ePro/Walmart_Template_Synchronization_Builder')->getDefaultData();
    $formData = array_merge($defaults, $formData);

    $isEdit = (bool)$this->getRequest()->getParam('id');
?>

<script type="text/javascript">

    Event.observe(window, 'load', function() {
        $$('#walmart_template_synchronization_list_advanced_rules select.element-value-changer option').each(function(el) {
            if ((el.value == '??' && el.selected) || (el.value == '!??' && el.selected)) {
                $(el.parentElement.parentElement.parentElement.nextElementSibling).hide();
            }
        });
        $("walmart_template_synchronization_list_advanced_rules")
            .observe('change', function(event) {
                    let target = event.target;
                    if (target.value == '??' || target.value == '!??') {
                        $(target.parentElement.parentElement.nextElementSibling).hide();
                    }
                }
            )
            .simulate('change');

        $('list_mode')
            .observe('change', WalmartTemplateSynchronizationObj.listMode_change)
            .simulate('change');

        $('list_qty_calculated')
            .observe('change', WalmartTemplateSynchronizationObj.listQty_change)
            .simulate('change');

        $('list_advanced_rules_mode')
            .observe('change', WalmartTemplateSynchronizationObj.listAdvancedRules_change)
            .simulate('change');
    });

</script>

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

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

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

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

                <tr>
                    <td class="label">
                        <label><?php echo Mage::helper('M2ePro')->__('List Action'); ?>: </label>
                    </td>
                    <td class="value" style="width: auto;">
                        <select id="list_mode" name="list_mode" class="required-entry">
                            <option value="0" <?php if ($formData['list_mode'] == 0): echo ' selected="selected"'; endif; ?>><?php echo Mage::helper('M2ePro')->__('Disabled'); ?></option>
                            <option value="1" <?php if ($formData['list_mode'] == 1): echo ' selected="selected"'; endif; ?>><?php echo Mage::helper('M2ePro')->__('Enabled'); ?></option>
                        </select>
                        <p class="note">
                            <span><?php echo Mage::helper('M2ePro')->__('Enable to automatically list the Not Listed Item(s) when the List Conditions are met.'); ?></span>
                        </p>
                    </td>
                </tr>

            </table>

        </div>
    </div>

</div>

<div class="entry-edit" id="magento_block_walmart_template_synchronization_list_rules" <?php if ($formData['list_mode'] == 0): echo ' style="display:none;"'; endif; ?>>

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

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

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

                <tr>
                    <td class="label">
                        <label><?php echo Mage::helper('M2ePro')->__('Product Status'); ?>: </label>
                    </td>
                    <td class="value" style="width: auto;">
                        <select id="list_status_enabled" name="list_status_enabled" class="required-entry">
                            <option value="0" <?php if ($formData['list_status_enabled'] == 0): echo ' selected="selected"'; endif; ?>><?php echo Mage::helper('M2ePro')->__('Any'); ?></option>
                            <option value="1" <?php if ($formData['list_status_enabled'] == 1): echo ' selected="selected"'; endif; ?>><?php echo Mage::helper('M2ePro')->__('Enabled'); ?></option>
                        </select>
                        <p class="note">
                            <span><?php echo Mage::helper('M2ePro')->__('Magento Product Status at which the Item(s) have to be listed.'); ?></span>
                        </p>
                    </td>
                </tr>

                <tr>
                    <td class="label">
                        <label><?php echo Mage::helper('M2ePro')->__('Stock Availability'); ?>: </label>
                    </td>
                    <td class="value" style="width: auto;">
                        <select id="list_is_in_stock" name="list_is_in_stock" class="required-entry">
                            <option value="0" <?php if ($formData['list_is_in_stock'] == 0): echo ' selected="selected"'; endif; ?>><?php echo Mage::helper('M2ePro')->__('Any'); ?></option>
                            <option value="1" <?php if ($formData['list_is_in_stock'] == 1): echo ' selected="selected"'; endif; ?>><?php echo Mage::helper('M2ePro')->__('In Stock'); ?></option>
                        </select>
                        <p class="note">
                            <span><?php echo Mage::helper('M2ePro')->__('Magento Stock Availability at which the Item(s) have to be listed.'); ?></span>
                        </p>
                    </td>
                </tr>

                <tr>
                    <td class="label">
                        <label><?php echo Mage::helper('M2ePro')->__('Quantity'); ?>: </label>
                    </td>
                    <td class="value" style="width: auto;">
                        <select id="list_qty_calculated" name="list_qty_calculated" class="required-entry">
                            <option value="<?php echo Ess_M2ePro_Model_Template_Synchronization::QTY_MODE_NONE; ?>" <?php if ($formData['list_qty_calculated'] == Ess_M2ePro_Model_Template_Synchronization::QTY_MODE_NONE): echo ' selected="selected"'; endif; ?>><?php echo Mage::helper('M2ePro')->__('Any'); ?></option>
                            <option value="<?php echo Ess_M2ePro_Model_Template_Synchronization::QTY_MODE_YES; ?>" <?php if ($formData['list_qty_calculated'] == Ess_M2ePro_Model_Template_Synchronization::QTY_MODE_YES): echo ' selected="selected"'; endif; ?>><?php echo Mage::helper('M2ePro')->__('More or Equal'); ?></option>
                        </select>
                        <input id="list_qty_calculated_value" name="list_qty_calculated_value" value="<?php echo $this->escapeHtml($formData['list_qty_calculated_value']); ?>" type="text" class="M2ePro-required-when-visible input-text validate-digits" style="width: 72px;" />
                        <p class="note">
                            <span><?php echo Mage::helper('M2ePro')->__(
                                'Item Quantity calculated based on the Selling Policy settings at which the Item(s) have to be listed.<br>
                                <strong>Note:</strong> This option will be ignored for Magento Variational Product listed as Walmart Variant Group.'
                            ); ?></span>
                        </p>
                    </td>
                </tr>

            </table>

        </div>
    </div>

</div>

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

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

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

            <div id="list_advanced_rules_filters_warning">
                <ul class="messages">
                    <li class="warning-msg">
                        <ul>
                            <li>
                                <?php echo Mage::helper('M2ePro')->__(
                                    'Please be very thoughtful before enabling this option as this functionality
                                    can have a negative impact on the Performance of your system.<br> It can decrease the speed
                                    of running in case you have a lot of Products with the high number of changes made to them.'
                                ); ?>
                            </li>
                        </ul>
                    </li>
                </ul>
            </div>

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

                <tr>
                    <td class="label">
                        <label for="list_advanced_rules_mode"><?php echo Mage::helper('M2ePro')->__('Enabled'); ?>: </label>
                    </td>
                    <td class="value" style="width: auto;">
                        <select id="list_advanced_rules_mode" name="list_advanced_rules_mode" class="required-entry">
                            <option value="0" <?php if ($formData['list_advanced_rules_mode'] == 0): echo ' selected="selected"'; endif; ?>><?php echo Mage::helper('M2ePro')->__('No'); ?></option>
                            <option value="1" <?php if ($formData['list_advanced_rules_mode'] == 1): echo ' selected="selected"'; endif; ?>><?php echo Mage::helper('M2ePro')->__('Yes'); ?></option>
                        </select>
                        <p class="note">
                            <span><?php echo Mage::helper('M2ePro')->__(
                                'You can provide flexible Advanced Conditions to manage when the List action should
                                be run basing on the Attributes’ values of the Magento Product.<br> So, when all the Conditions
                                (both general List Conditions and Advanced Conditions) are met,
                                the Product will be listed on Channel.'
                            ); ?></span>
                        </p>
                    </td>
                </tr>

                <tr id="list_advanced_rules_filters_container">
                    <td class="label">
                        <label><?php echo Mage::helper('M2ePro')->__('Conditions'); ?>: </label>
                    </td>
                    <td class="value" style="width: auto;">
                        <?php echo $this->getAdvancedRulesBlock()->toHtml(); ?>
                    </td>
                </tr>

            </table>

        </div>
    </div>

</div>
