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

?>

<div id="block_notice_amazon_template_synchronization_stop" class="block_notices_module" title="<?php echo Mage::helper('M2ePro')->__('Stop Rules'); ?>">
    <?php echo Mage::helper('M2ePro')->__('Stop Rules define the Conditions when Amazon Items Listing must be
    inactivated, depending on Magento Product state.<br/><br/>
    <b>Note:</b> If all Stop Conditions are set to <i>No</i> or <i>No Action</i>, then the Stop Option for Amazon Items is disabled.<br/>
    If all Stop Conditions are enabled, then an Item will be inactivated if at least one of the Stop Conditions is met.<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, 'stop-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_stop_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_stop_advanced_rules")
            .observe('change', function(event) {
                    let target = event.target;
                    if (target.value == '??' || target.value == '!??') {
                        $(target.parentElement.parentElement.nextElementSibling).hide();
                    }
                }
            )
            .simulate('change');

        $('stop_mode')
            .observe('change', AmazonTemplateSynchronizationObj.stopMode_change)
            .simulate('change');

        $('stop_qty_calculated')
            .observe('change', AmazonTemplateSynchronizationObj.stopQty_change)
            .simulate('change');

        $('stop_advanced_rules_mode')
            .observe('change', AmazonTemplateSynchronizationObj.stopAdvancedRules_change)
            .simulate('change');
    });

</script>

<div class="entry-edit" id="magento_block_amazon_template_synchronization_stop_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')->__('Stop Action'); ?>: </label>
                    </td>
                    <td class="value" style="width: auto;">
                        <select id="stop_mode" name="stop_mode" class="required-entry">
                            <option value="0" <?php if ($formData['stop_mode'] == 0): echo ' selected="selected"'; endif; ?>><?php echo Mage::helper('M2ePro')->__('Disabled'); ?></option>
                            <option value="1" <?php if ($formData['stop_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 Stop Action for the Listings, which use current Synchronization Policy.'); ?></span>
                        </p>
                    </td>
                </tr>

            </table>

        </div>
    </div>

</div>

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

    <div class="entry-edit-head">
        <h4 class="icon-head head-edit-form fieldset-legend"><?php echo Mage::helper('M2ePro')->__('Stop 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')->__('Stop When Status Disabled'); ?>: </label>
                    </td>
                    <td class="value" style="width: auto;">
                        <select id="stop_status_disabled" name="stop_status_disabled" class="required-entry">
                            <option value="0" <?php if ($formData['stop_status_disabled'] == 0): echo ' selected="selected"'; endif; ?>><?php echo Mage::helper('M2ePro')->__('No'); ?></option>
                            <option value="1" <?php if ($formData['stop_status_disabled'] == 1): echo ' selected="selected"'; endif; ?>><?php echo Mage::helper('M2ePro')->__('Yes'); ?></option>
                        </select>
                        <p class="note">
                            <span><?php echo Mage::helper('M2ePro')->__('Automatically stops Item(s) if its status has been changed to \'Disabled\' in Magento.'); ?></span>
                        </p>
                    </td>
                </tr>

                <tr>
                    <td class="label">
                        <label><?php echo Mage::helper('M2ePro')->__('Stop When Out Of Stock'); ?>: </label>
                    </td>
                    <td class="value" style="width: auto;">
                        <select id="stop_out_off_stock" name="stop_out_off_stock" class="required-entry">
                            <option value="0" <?php if ($formData['stop_out_off_stock'] == 0): echo ' selected="selected"'; endif; ?>><?php echo Mage::helper('M2ePro')->__('No'); ?></option>
                            <option value="1" <?php if ($formData['stop_out_off_stock'] == 1): echo ' selected="selected"'; endif; ?>><?php echo Mage::helper('M2ePro')->__('Yes'); ?></option>
                        </select>
                        <p class="note">
                            <span><?php echo Mage::helper('M2ePro')->__('Automatically stops Item(s) if its Stock availability has been changed to \'Out of Stock\' in Magento.'); ?></span>
                        </p>
                    </td>
                </tr>

                <tr>
                    <td class="label">
                        <label><?php echo Mage::helper('M2ePro')->__('Stop When Quantity Is'); ?>: </label>
                    </td>
                    <td class="value" style="width: auto;">
                        <select id="stop_qty_calculated" name="stop_qty_calculated" class="required-entry">
                            <option value="<?php echo Ess_M2ePro_Model_Template_Synchronization::QTY_MODE_NONE; ?>" <?php if ($formData['stop_qty_calculated'] == Ess_M2ePro_Model_Template_Synchronization::QTY_MODE_NONE): echo ' selected="selected"'; endif; ?>><?php echo Mage::helper('M2ePro')->__('No Action'); ?></option>
                            <option value="<?php echo Ess_M2ePro_Model_Template_Synchronization::QTY_MODE_YES; ?>" <?php if ($formData['stop_qty_calculated'] == Ess_M2ePro_Model_Template_Synchronization::QTY_MODE_YES): echo ' selected="selected"'; endif; ?>><?php echo Mage::helper('M2ePro')->__('Less or Equal'); ?></option>
                        </select>
                        <input id="stop_qty_calculated_value" name="stop_qty_calculated_value" value="<?php echo $this->escapeHtml($formData['stop_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')->__('Automatically stops Item(s) if Quantity according to the Selling Policy has been changed and meets the Conditions.'); ?></span>
                        </p>
                    </td>
                </tr>

            </table>

        </div>
    </div>

</div>

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

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