<?php

/** @var Ess_M2ePro_Block_Adminhtml_Walmart_Listing_AutoAction_Mode_Category_Form $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()
    )); ?>';

    ListingAutoActionObj.magentoCategoryIdsFromOtherGroups = <?php echo json_encode($this->getCategoriesFromOtherGroups()) ?>;

    $('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>

<style type="text/css">
    #category_tree_container:hover {
        overflow: auto;
    }

    #category_tree_container {
        float: left;
        width: 230px;
        max-height: 350px;
        overflow: hidden;
        border-right: 1px solid #e0e0e0;
    }

    #category_form_container {
        float: left;
        padding: 0 10px;
    }

</style>

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

    <div id="category_tree_container">
        <?php echo $this->getChildHtml('category_tree') ?>
    </div>

    <div id="category_form_container">
        <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="id" value="<?php echo $formData['id'] ?>" type="hidden" id="group_id" />
            <input name="auto_mode" value="<?php echo Ess_M2ePro_Model_Listing::AUTO_MODE_CATEGORY ?>" type="hidden" id="auto_mode" />

            <table class="form-list">
                <tr>
                    <td class="label">
                        <label for="group_title"><?php echo Mage::helper('M2ePro')->__('Title') ?>: <span class="required">*</span></label>
                    </td>
                    <td class="value">
                        <input type="group_title" name="title" id="group_title" class="input-text required-entry M2ePro-validate-category-group-title" value="<?php echo Mage::helper('M2ePro')->escapeHtml($formData['title']) ?>" style="padding: 2px 0; width: 275px;" />
                    </td>
                </tr>

                <tr>
                    <td class="label" style="width: 200px;">
                        <label for="adding_mode"><?php echo Mage::helper('M2ePro')->__('Product Assigned to Categories') ?>:</label>
                    </td>
                    <td class="value">
                        <select name="adding_mode" id="adding_mode" style="width: 275px;">
                            <option value="<?php echo Ess_M2ePro_Model_Listing::ADDING_MODE_NONE ?>" <?php if ($formData['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['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 tip-left" style="left: 650px; 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="adding_add_not_visible_field">
                    <td class="label" style="width: 200px;">
                        <label for="adding_add_not_visible"><?php echo Mage::helper('M2ePro')->__('Add not Visible Individually Products') ?>:</label>
                    </td>
                    <td class="value">
                        <select name="adding_add_not_visible" id="adding_add_not_visible" style="width: 275px;">
                            <option value="<?php echo Ess_M2ePro_Model_Listing::AUTO_ADDING_ADD_NOT_VISIBLE_NO ?>" <?php if ($formData['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['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['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 tip-left" style="left: 650px; 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 Categories') ?>:</label>
                    </td>
                    <td class="value">
                        <select name="deleting_mode" id="deleting_mode" style="width: 275px;">
                            <option value="<?php echo Ess_M2ePro_Model_Listing::DELETING_MODE_NONE ?>" <?php if ($formData['deleting_mode'] == Ess_M2ePro_Model_Listing::DELETING_MODE_NONE): ?>selected="selected"<?php endif; ?> ><?php echo Mage::helper('M2ePro')->__('No Action') ?></option>
                            <option value="<?php echo Ess_M2ePro_Model_Listing::DELETING_MODE_STOP ?>" <?php if ($formData['deleting_mode'] == Ess_M2ePro_Model_Listing::DELETING_MODE_STOP): ?>selected="selected"<?php endif; ?> ><?php echo Mage::helper('M2ePro')->__('Stop on Channel') ?></option>
                            <option value="<?php echo Ess_M2ePro_Model_Listing::DELETING_MODE_STOP_REMOVE ?>" <?php if ($formData['deleting_mode'] == Ess_M2ePro_Model_Listing::DELETING_MODE_STOP_REMOVE): ?>selected="selected"<?php endif; ?> ><?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 tip-left" style="left: 650px; 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>

</div>

<div>
    <input type="hidden" name="validate_category_selection" id="validate_category_selection" class="M2ePro-validate-category-selection" />
</div>

<div style="clear: both;"></div>

<div style="display: none;">
    <?php echo $this->getChildHtml('confirm') ?>
</div>

<div id="dialog_confirm_content" style="display: none;">
    <div style="margin: 10px; ">
        <?php echo Mage::helper('M2ePro')->__('This Category is already used in the Rule %s. If you press "Confirm" Button, Category will be removed from that Rule.') ?>
    </div>
</div>