<?php

/** @var Ess_M2ePro_Block_Adminhtml_Walmart_Listing_AutoAction_Mode_Global $this */

$formData = array_merge($this->getDefault(), $this->getFormData());

/** @var Ess_M2ePro_Model_Walmart_ProductType_Repository $productTypeRepository */
$productTypeRepository = Mage::getModel('M2ePro/Walmart_ProductType_Repository');
$productTypes = $productTypeRepository->retrieveByMarketplaceId($this->getListing()->getMarketplaceId());

?>

<script type="text/javascript">
    M2ePro.php.setConstants(<?php echo Mage::helper('M2ePro')->getClassConstantAsJson('Ess_M2ePro_Model_Listing'); ?>, 'Ess_M2ePro_Model_Listing');
    M2ePro.php.setConstants(<?php echo Mage::helper('M2ePro')->getClassConstantAsJson('Ess_M2ePro_Model_Walmart_Listing'); ?>, 'Ess_M2ePro_Model_Walmart_Listing');
</script>

<script type="text/javascript">

    M2ePro.url.addNewProductType = '<?php echo $this->getUrl('*/adminhtml_walmart_productType/edit', array(
        'marketplace_id' => $this->getListing()->getMarketplaceId(),
        'close_on_save' => 1
    )); ?>';
    M2ePro.url.getProductTypes = '<?php echo $this->getUrl('*/adminhtml_walmart_listing/getProductTypesList', array(
        'marketplace_id' => $this->getListing()->getMarketplaceId(),
        'close_on_save' => 1,
    )); ?>';

    $('auto_global_adding_mode')
        .observe('change', ListingAutoActionObj.addingModeChange)
        .simulate('change');

    $('reset_button')
        .observe('click', function() {
            ListingAutoActionObj.reset(false);
        });

    $('adding_product_type_id').observe('change', function(el) {
        var options = $(el.target).select('.empty');
        options.length > 0 && options[0].hide();
    });

</script>

<div class="content-header">
    <table cellspacing="0">
        <tr>
            <td><h3><?php echo Mage::helper('M2ePro')->__('Global (all Products)'); ?></h3></td>
        </tr>
    </table>
</div>

<div id="data_container" style="padding-bottom: 10px;">

    <?php
    $this->createHelpBlock(Mage::getBlockSingleton('M2ePro/adminhtml_walmart_listing_autoAction_mode')->getHelpPageUrl());
    echo $this->getChildHtml('help_block');
    ?>

    <form id="<?php echo $this->getForm()->getId(); ?>" action="<?php echo $this->getForm()->getData('action'); ?>" method="post" enctype="multipart/form-data">

        <input name="form_key" value="<?php echo $this->getFormKey(); ?>" type="hidden" />
        <input name="auto_mode" value="<?php echo Ess_M2ePro_Model_Listing::AUTO_MODE_GLOBAL ?>" type="hidden" id="auto_mode" />

        <table class="form-list">
            <tr>
                <td class="label" style="width: 200px;">
                    <label for="auto_global_adding_mode"><?php echo Mage::helper('M2ePro')->__('New Product Added to Magento') ?>:</label>
                </td>
                <td class="value">
                    <select name="auto_global_adding_mode" id="auto_global_adding_mode" style="width: 275px;">
                        <option value="<?php echo Ess_M2ePro_Model_Listing::ADDING_MODE_NONE ?>" <?php if ($formData['auto_global_adding_mode'] == Ess_M2ePro_Model_Listing::ADDING_MODE_NONE): ?>selected="selected"<?php endif; ?> ><?php echo Mage::helper('M2ePro')->__('No Action') ?></option>
                        <option value="<?php echo Ess_M2ePro_Model_Listing::ADDING_MODE_ADD ?>" <?php if ($formData['auto_global_adding_mode'] == Ess_M2ePro_Model_Listing::ADDING_MODE_ADD): ?>selected="selected"<?php endif; ?> ><?php echo Mage::helper('M2ePro')->__('Add to the Listing') ?></option>
                    </select>
                </td>
                <td class="value">
                    <img src="<?php echo $this->getSkinUrl('M2ePro/images/tool-tip-icon.png'); ?>" class="tool-tip-image">
                    <span class="tool-tip-message" style="display: none;">
                        <img src="<?php echo $this->getSkinUrl('M2ePro/images/help.png'); ?>">
                        <span><?php echo Mage::helper('M2ePro')->__('Action which will be applied automatically.'); ?></span>
                    </span>
                </td>
            </tr>

            <tr id="auto_global_adding_add_not_visible_field">
                <td class="label" style="width: 200px;">
                    <label for="auto_global_adding_add_not_visible"><?php echo Mage::helper('M2ePro')->__('Add not Visible Individually Products') ?>:</label>
                </td>
                <td class="value">
                    <select name="auto_global_adding_add_not_visible" id="auto_global_adding_add_not_visible" style="width: 275px;">
                        <option value="<?php echo Ess_M2ePro_Model_Listing::AUTO_ADDING_ADD_NOT_VISIBLE_NO ?>" <?php if ($formData['auto_global_adding_add_not_visible'] == Ess_M2ePro_Model_Listing::AUTO_ADDING_ADD_NOT_VISIBLE_NO): ?>selected="selected"<?php endif; ?> ><?php echo Mage::helper('M2ePro')->__('No') ?></option>
                        <option value="<?php echo Ess_M2ePro_Model_Listing::AUTO_ADDING_ADD_NOT_VISIBLE_YES ?>" <?php if ($formData['auto_global_adding_add_not_visible'] == Ess_M2ePro_Model_Listing::AUTO_ADDING_ADD_NOT_VISIBLE_YES): ?>selected="selected"<?php endif; ?> ><?php echo Mage::helper('M2ePro')->__('Yes') ?></option>
                    </select>
                </td>
                <td class="value">
                    <img src="<?php echo $this->getSkinUrl('M2ePro/images/tool-tip-icon.png'); ?>" class="tool-tip-image">
                    <span class="tool-tip-message" style="display: none;">
                        <img src="<?php echo $this->getSkinUrl('M2ePro/images/help.png'); ?>">
                        <span><?php echo Mage::helper('M2ePro')->__('
                            Set to <strong>Yes</strong> if you want the Magento Products with
                            Visibility \'Not visible Individually\' to be added to the Listing
                            Automatically.<br/>
                            If set to <strong>No</strong>, only Variation (i.e.
                            Parent) Magento Products will be added to the Listing Automatically,
                            excluding Child Products.
                        '); ?></span>
                    </span>
                </td>
            </tr>

            <tr id="auto_action_walmart_add_and_assign_product_type">
                <td class="label" style="width: 200px;">
                    <label for="adding_product_type_id"><?php echo Mage::helper('M2ePro')->__('Product Type') ?>: <span class="required">*</span></label>
                </td>
                <td class="value">
                    <select name="adding_product_type_id" id="adding_product_type_id" style="width: 275px;" class="M2ePro-required-when-visible">
                        <option class="empty"></option>
                        <?php foreach($productTypes as $productType) { ?>
                            <option value="<?php echo $productType->getId(); ?>" <?php echo $productType->getId() == $formData['auto_global_adding_product_type_id'] ? 'selected="selected"' : '' ?> ><?php echo $this->escapeHtml($productType->getTitle()); ?></option>
                        <?php } ?>
                    </select>
                </td>
                <td class="value">
                    <img src="<?php echo $this->getSkinUrl('M2ePro/images/tool-tip-icon.png'); ?>" class="tool-tip-image">
                    <span class="tool-tip-message" style="display: none;">
                        <img src="<?php echo $this->getSkinUrl('M2ePro/images/help.png'); ?>">
                        <span><?php echo Mage::helper('M2ePro')->__(
                            'Select Product Type you want to assign to Product(s).<br><br>
                            <strong>Note:</strong> Submitting of Product Type data is required when you create a new offer on Walmart. Product Type must be assigned to Products before they are added to M2E Pro Listing.'
                        ); ?></span>
                    </span>
                    <a href="javascript: void(0);" style="vertical-align: inherit;" onclick="ListingAutoActionObj.addNewProductType(M2ePro.url.addNewProductType, ListingAutoActionObj.reloadProductTypes);"><?php echo Mage::helper('M2ePro')->__('Add New') ?></a>
                </td>
            </tr>

        </table>

        <div style="padding-top: 20px;"></div>

        <table class="form-list">
            <tr>
                <td class="label" style="width: 200px;">
                    <label for=""><?php echo Mage::helper('M2ePro')->__('Product Deleted from Magento') ?>:</label>
                </td>
                <td class="value">
                    <select name="auto_global_deleting_mode" id="auto_global_deleting_mode" style="width: 275px;" disabled="disabled">
                        <option value="<?php echo Ess_M2ePro_Model_Listing::DELETING_MODE_STOP_REMOVE ?>"><?php echo Mage::helper('M2ePro')->__('Stop on Channel and Delete from Listing') ?></option>
                    </select>
                </td>
                <td class="value">
                    <img src="<?php echo $this->getSkinUrl('M2ePro/images/tool-tip-icon.png'); ?>" class="tool-tip-image">
                    <span class="tool-tip-message" style="display: none;">
                        <img src="<?php echo $this->getSkinUrl('M2ePro/images/help.png'); ?>">
                        <span><?php echo Mage::helper('M2ePro')->__('Action which will be applied automatically.'); ?></span>
                    </span>
                </td>
            </tr>
        </table>

    </form>

</div>

<?php if ($this->hasFormData()): ?>
<div style="float: left; margin: 10px 5px 0;">
    <a id="reset_button" href="javascript: void(0);" onclick=""><?php echo Mage::helper('M2ePro')->__('Reset Automatic Action Rule'); ?></a>
</div>
<?php endif; ?>
<div style="float: right; margin: 10px 0;">
    <?php if (!$this->hasFormData()): ?>
        <a id="reset_button" href="" style="display: none;"></a>
        <a id="close_button" href="javascript: void(0);" onclick="ListingAutoActionObj.loadAutoActionHtml();"><?php echo Mage::helper('M2ePro')->__('Cancel') ?></a>
    <?php endif; ?>
    &nbsp;
    &nbsp;
    &nbsp;
    <?php echo $this->getChildHtml('confirm_button'); ?>
    <?php echo $this->getChildHtml('continue_button'); ?>
</div>