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

// @codingStandardsIgnoreFile

/** @var Ess_M2ePro_Block_Adminhtml_Ebay_Template_Synchronization_Edit_Form_Tabs_List $this */

$default = $this->getDefault();
$formData = $this->getFormData();

$formData = array_merge($default, $formData);

?>

<script type="text/javascript">

    var init = function() {
        $$('#ebay_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();
            }
        });
        $("ebay_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', EbayTemplateSynchronizationObj.listMode_change)
            .simulate('change');

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

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

    <?php if ($this->getRequest()->isXmlHttpRequest()): ?>
        init();
    <?php else: ?>
        Event.observe(window, 'load', init);
    <?php endif; ?>

</script>

<div id="block_notice_ebay_template_synchronization_form_data_list" class="block_notices_module no-icon" title="<?php echo Mage::helper('M2ePro')->__('List Rules'); ?>">
    <?php echo Mage::helper('M2ePro')->__(
        'If <b>List Action</b> is enabled, M2E Pro will list Items that haven\'t been Listed yet on eBay if they meet the Conditions you set.
        (List Action will not list Items that were Listed previously but have been Stopped.)<br/><br/>
        If the automatic Listing doesn\'t work (usually because eBay has sent back errors),
        M2E Pro will only attempt to list the Item again if the Product status, Stock availabiliy or Quantity is changed.<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, 'list-action')
    ); ?>
</div>

<div class="entry-edit" id="magento_block_ebay_template_synchronization_form_data_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="synchronization[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')->__(
                                'Enables automatic Listing of unlisted Items if they meet List Conditions.
                                Choose whether you want an Item that isn\'t currently Listed on eBay to be Listed if the list Conditions are met.'
                            ); ?></span>
                        </p>
                    </td>
                </tr>

            </table>

        </div>
    </div>

</div>

<div class="entry-edit" id="magento_block_ebay_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="synchronization[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')->__(
                                '<b>Enabled:</b> List Items automatically on eBay if they have a Magento Product status of enabled.
                                <b>Any:</b> List Items with any Magento Product status automatically on eBay.'
                            ); ?></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="synchronization[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')->__(
                                    '<b>In Stock:</b> List Items automatically if Products are in Stock. (Recommended.)<br/>
                                    <b>Any:</b> List Items automatically, regardless of Stock availability.'
                                ); ?>
                            </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="synchronization[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="synchronization[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')->__(
                                    '<b>Any:</b> List Items automatically with any Quantity available.<br/>
                                    <b>More or Equal: </b> List Items automatically if the Quantity according to the Selling Policy is at least equal to the number you set.'
                                ); ?>
                            </span>
                        </p>
                    </td>
                </tr>

            </table>

        </div>
    </div>

</div>

<div class="entry-edit" id="magento_block_ebay_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="synchronization[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>