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

// @codingStandardsIgnoreFile

/** @var $this Ess_M2ePro_Block_Adminhtml_Amazon_Template_Synchronization_Edit_Tabs_Relist */

?>

<div id="block_notice_amazon_template_synchronization_relist" class="block_notices_module" title="<?php echo Mage::helper('M2ePro')->__('Relist Rules'); ?>">
    <?php echo Mage::helper('M2ePro')->__('If <strong>Relist Action</strong> is enabled, M2E Pro will Relist Items on Amazon if they meet all the Conditions you set.<br/><br/>
    More detailed information about ability to work with this Page you can find <a href="%url%" target="_blank">here</a>.',
    Mage::helper('M2ePro/Module_Support')->getDocumentationUrl(null, null, 'relist-rules')
    ); ?>
</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/Amazon_Template_Synchronization_Builder')->getDefaultData();
    $formData = array_merge($defaults, $formData);

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

<script type="text/javascript">

    Event.observe(window, 'load', function() {
        $$('#amazon_template_synchronization_relist_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();
            }
        });
        $("amazon_template_synchronization_relist_advanced_rules")
            .observe('change', function(event) {
                    let target = event.target;
                    if (target.value == '??' || target.value == '!??') {
                        $(target.parentElement.parentElement.nextElementSibling).hide();
                    }
                }
            )
            .simulate('change');

        $('relist_mode')
            .observe('change', AmazonTemplateSynchronizationObj.relistMode_change)
            .simulate('change');

        $('relist_qty_calculated')
            .observe('change', AmazonTemplateSynchronizationObj.relistQty_change)
            .simulate('change');

        $('relist_advanced_rules_mode')
            .observe('change', AmazonTemplateSynchronizationObj.relistAdvancedRules_change)
            .simulate('change');
    });

</script>

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

    <div class="entry-edit-head">
        <h4 class="icon-head head-edit-form fieldset-legend"><?php echo Mage::helper('M2ePro')->__('General'); ?></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')->__('Relist Action'); ?>: </label>
                    </td>
                    <td class="value" style="width: auto;">
                        <select id="relist_mode" name="relist_mode" class="required-entry">
                            <option value="0" <?php if ($formData['relist_mode'] == 0): echo ' selected="selected"'; endif; ?>><?php echo Mage::helper('M2ePro')->__('Disabled'); ?></option>
                            <option value="1" <?php if ($formData['relist_mode'] == 1): echo ' selected="selected"'; endif; ?>><?php echo Mage::helper('M2ePro')->__('Enabled'); ?></option>
                        </select>
                        <p class="note">
                            <span><?php echo Mage::helper('M2ePro')->__('Enables/Disables the Relist Action for the Listings, which use current Synchronization Policy.'); ?></span>
                        </p>
                    </td>
                </tr>

                <tr id="relist_filter_user_lock_tr_container" <?php if ($formData['relist_mode'] == 0): echo ' style="display:none;"'; endif; ?>>
                    <td class="label">
                        <label><?php echo Mage::helper('M2ePro')->__('Relist When Stopped Manually'); ?>: </label>
                    </td>
                    <td class="value" style="width: auto;">
                        <select id="relist_filter_user_lock" name="relist_filter_user_lock" class="required-entry">
                            <option value="1" <?php if ($formData['relist_filter_user_lock'] == 1): echo ' selected="selected"'; endif; ?>><?php echo Mage::helper('M2ePro')->__('No'); ?></option>
                            <option value="0" <?php if ($formData['relist_filter_user_lock'] == 0): echo ' selected="selected"'; endif; ?>><?php echo Mage::helper('M2ePro')->__('Yes'); ?></option>
                        </select>
                        <p class="note">
                            <span><?php echo Mage::helper('M2ePro')->__('Automatically Relists Item(s) even it has been Stopped manually within M2E Pro.'); ?></span>
                        </p>
                    </td>
                </tr>

           </table>

       </div>
   </div>

</div>

<div class="entry-edit" id="magento_block_amazon_template_synchronization_relist_rules" <?php if ($formData['relist_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')->__('Relist 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="relist_status_enabled" name="relist_status_enabled" class="required-entry M2ePro-validate-stop-relist-conditions-product-status">
                            <option value="0" <?php if ($formData['relist_status_enabled'] == 0): echo ' selected="selected"'; endif; ?>><?php echo Mage::helper('M2ePro')->__('Any'); ?></option>
                            <option value="1" <?php if ($formData['relist_status_enabled'] == 1): echo ' selected="selected"'; endif; ?>><?php echo Mage::helper('M2ePro')->__('Enabled'); ?></option>
                        </select>
                        <p class="note">
                            <span><?php echo Mage::helper('M2ePro')->__('Product Status at which the Item(s) have to be Relisted.'); ?></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="relist_is_in_stock" name="relist_is_in_stock" class="required-entry M2ePro-validate-stop-relist-conditions-stock-availability">
                            <option value="0" <?php if ($formData['relist_is_in_stock'] == 0): echo ' selected="selected"'; endif; ?>><?php echo Mage::helper('M2ePro')->__('Any'); ?></option>
                            <option value="1" <?php if ($formData['relist_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')->__('Stock Availability at which the Item(s) have to be Relisted.'); ?></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="relist_qty_calculated" name="relist_qty_calculated" class="required-entry M2ePro-validate-stop-relist-conditions-item-qty">
                            <option value="<?php echo Ess_M2ePro_Model_Template_Synchronization::QTY_MODE_NONE; ?>" <?php if ($formData['relist_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['relist_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="relist_qty_calculated_value" name="relist_qty_calculated_value" value="<?php echo $this->escapeHtml($formData['relist_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')->__('Quantity according to the Selling Policy at which the Item(s) have to be Relisted.'); ?></span>
                        </p>
                    </td>
                </tr>

            </table>

        </div>
    </div>

</div>

<div class="entry-edit" id="magento_block_amazon_template_synchronization_relist_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="relist_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="relist_advanced_rules_mode"><?php echo Mage::helper('M2ePro')->__('Enabled'); ?>: </label>
                    </td>
                    <td class="value" style="width: auto;">
                        <select id="relist_advanced_rules_mode" name="relist_advanced_rules_mode" class="required-entry">
                            <option value="0" <?php if ($formData['relist_advanced_rules_mode'] == 0): echo ' selected="selected"'; endif; ?>><?php echo Mage::helper('M2ePro')->__('No'); ?></option>
                            <option value="1" <?php if ($formData['relist_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 Relist action should
                                be run basing on the Attributes’ values of the Magento Product.<br> So, when all the Conditions
                                (both general Relist Conditions and Advanced Conditions) are met,
                                the Product will be relisted on Channel.'
                            ); ?></span>
                        </p>
                    </td>
                </tr>

                <tr id="relist_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>
