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

// @codingStandardsIgnoreFile

/** @var $this Ess_M2ePro_Block_Adminhtml_Amazon_Template_Description_Edit_Tabs_General */

?>

<div id="block_notice_amazon_template_description_general" class="block_notices_module" title="<?php echo Mage::helper('M2ePro')->__('General'); ?>">
    <?php echo Mage::helper('M2ePro')->__(
        'This Tab contains main Settings of Description Policy such as selection of Amazon Category and preparing Description Policy for New ASIN/ISBN Creation. <br/><br/>

        The Description Policy has to be Created for a particular Marketplace, so it can be used only for Listings with the same Marketplace. <br/>

        You can select Amazon Category, in which Amazon Products should be placed. Category must be selected to Create New ASIN/ISBN. <br/>

        In case you are planning to use Description Policy for New ASIN/ISBN Creation, you should enable New ASIN/ISBN Creation feature.<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, 'description-policies')
    ); ?>
</div>

<?php

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

    $formData = array('general' => array());
    $isEdit && $formData = array('general' => Mage::helper('M2ePro/Data_Global')->getValue('temp_data')->getData());

    $default = array(
        'general' => array_merge(
            Mage::getModel('M2ePro/Amazon_Template_Description_Builder')->getDefaultData(),
            array(
                'marketplace_id' => $this->getRequest()->getParam('marketplace_id', ''),
                'is_new_asin_accepted' => $this->getRequest()->getParam('is_new_asin_accepted', 0),
            )
        )
    );

    $formData = Mage::helper('M2ePro')->arrayReplaceRecursive($default, $formData);

    if (!Mage::helper('M2ePro/Component')->isSingleActiveComponent()) {
        $duplicateHeaderText = Mage::helper('M2ePro')->escapeJs(
            Mage::helper('M2ePro')->__('Add %component_name% Description Policy',
                Mage::helper('M2ePro/Component_Amazon')->getTitle()
            )
        );
    } else {
        $duplicateHeaderText = Mage::helper('M2ePro')->escapeJs(
            Mage::helper('M2ePro')->__('Add Description Policy')
        );
    }

    /** @var Ess_M2ePro_Helper_Magento_Attribute $magentoAttributeHelper */
    $magentoAttributeHelper = Mage::helper('M2ePro/Magento_Attribute');

    $attributesByInputTypes = array(
        'text' => $magentoAttributeHelper->filterByInputTypes($this->getData('all_attributes'), array('text')),
        'text_select' => $magentoAttributeHelper->filterByInputTypes($this->getData('all_attributes'), array('text', 'select')),
    );
?>

<script type="text/javascript">

    // General for all tabs
    //-----------------------------
    M2ePro.php.setConstants(<?php echo Mage::helper('M2ePro')->getClassConstantAsJson(
        'Ess_M2ePro_Model_Amazon_Template_Description'); ?>, 'Ess_M2ePro_Model_Amazon_Template_Description'
    );
    M2ePro.php.setConstants(<?php echo Mage::helper('M2ePro')->getClassConstantAsJson(
        'Ess_M2ePro_Helper_Component_Amazon'); ?>, 'Ess_M2ePro_Helper_Component_Amazon'
    );

    M2ePro.url.add(<?php echo json_encode(Mage::helper('M2ePro')->getControllerActions('adminhtml_amazon_template_description')); ?>);
    M2ePro.url.add(<?php echo json_encode(array(
        'formSubmit'    => $this->getUrl('M2ePro/adminhtml_amazon_template_description/save', array('id' => $this->getRequest()->getParam('id'))),
        'formSubmitNew' => $this->getUrl('M2ePro/adminhtml_amazon_template_description/save'),
        'deleteAction'  => $this->getUrl('M2ePro/adminhtml_amazon_template_description/delete', array('id' => $this->getRequest()->getParam('id'))),

        'adminhtml_amazon_marketplace/index' => $this->getUrl('M2ePro/adminhtml_amazon_marketplace/index')
    )); ?>);

    M2ePro.translator.add(<?php echo json_encode(array(
        'Add Description Policy' => Mage::helper('M2ePro')->__($duplicateHeaderText),

        'Change Category' => Mage::helper('M2ePro')->__('Change Category'),
        'Not Selected'    => Mage::helper('M2ePro')->__('Not Selected'),
        'Select'          => Mage::helper('M2ePro')->__('Select'),

        'The specified Title is already used for another Policy. Policy Title must be unique.' => Mage::helper('M2ePro')->__('The specified Title is already used for another Policy. Policy Title must be unique.'),
        'You should select Marketplace first.' => Mage::helper('M2ePro')->__('You should select Marketplace first.'),
        'You should select Category and Product Type first' => Mage::helper('M2ePro')->__('You should select Category and Product Type first'),

        'Recommended' => Mage::helper('M2ePro')->__('Recommended'),
        'Recent'      => Mage::helper('M2ePro')->__('Recent'),
    )); ?>);

    M2ePro.formData.general = <?php echo json_encode($formData['general']); ?>;

    M2ePro.customData.is_edit = <?php echo json_encode($isEdit); ?>;

    M2ePro.customData.category_locked             = <?php echo json_encode($this->getData('category_locked')); ?>;
    M2ePro.customData.marketplace_locked          = <?php echo json_encode($this->getData('marketplace_locked')); ?>;
    M2ePro.customData.marketplace_force_set       = <?php echo json_encode((bool)(int)$this->getRequest()->getParam('marketplace_id')); ?>;
    M2ePro.customData.new_asin_switcher_locked    = <?php echo json_encode($this->getData('new_asin_switcher_locked')); ?>;
    M2ePro.customData.new_asin_switcher_force_set = <?php echo json_encode((bool)(int)$this->getRequest()->getParam('is_new_asin_accepted')); ?>;

    Event.observe(window, 'load', function() {

        AttributeObj = new Attribute();
        AttributeObj.setAvailableAttributes(<?php echo json_encode($this->getData('all_attributes')); ?>);

        AmazonTemplateDescriptionObj                 = new AmazonTemplateDescription();
        AmazonTemplateDescriptionCategoryChooserObj  = new AmazonTemplateDescriptionCategoryChooser();
        AmazonTemplateDescriptionCategorySpecificObj = new AmazonTemplateDescriptionCategorySpecific();

        AmazonTemplateDescriptionObj.setSpecificHandler(AmazonTemplateDescriptionCategorySpecificObj);
        AmazonTemplateDescriptionCategoryChooserObj.setSpecificHandler(AmazonTemplateDescriptionCategorySpecificObj);

        $('marketplace_id').observe('change', AmazonTemplateDescriptionObj.onChangeMarketplace);

        $('new_asin_accepted').observe('change', AmazonTemplateDescriptionObj.onChangeNewAsinAccepted);

        $('edit_category_link').observe('click', AmazonTemplateDescriptionObj.onClickEditCategory);
        $('product_data_nick_select').observe('change', AmazonTemplateDescriptionObj.onChangeProductDataNick);

        $('worldwide_id_mode')
            .observe('change', AmazonTemplateDescriptionObj.onChangeWorldwideId)
            .simulate('change');

        $('registered_parameter')
            .observe('change', AmazonTemplateDescriptionObj.onChangeRegisteredParameter)
            .simulate('change');

        <?php if ($isEdit) { ?>
            AmazonTemplateDescriptionObj.prepareEditMode();
        <?php } ?>

        editForm = new varienForm('edit_form', '<?php echo $this->getValidationUrl(); ?>');
    });

    //-----------------------------

</script>

<input name="form_key" value="<?php echo $this->getFormKey(); ?>" type="hidden" />
<input name="general[id]" value="<?php echo $formData['general']['id']; ?>" type="hidden" />

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

    <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 for="title"><?php echo Mage::helper('M2ePro')->__('Title'); ?>: <span class="required">*</span></label>
                    </td>
                    <td class="value">
                        <input id="title" name="general[title]" value="<?php echo $this->escapeHtml($formData['general']['title']); ?>" type="text" class="input-text required-entry M2ePro-description-template-title"/>
                        <p class="note">
                            <span><?php echo Mage::helper('M2ePro')->__('Short meaningful Policy Title for your internal use.'); ?></span>
                        </p>
                    </td>
                </tr>

                <tr>
                    <td class="label">
                        <label for="title"><?php echo Mage::helper('M2ePro')->__('Marketplace'); ?>: <span class="required">*</span></label>
                    </td>
                    <td class="value">

                        <select id="marketplace_id" name="general[marketplace_id]" <?php if ($this->getData('marketplace_locked') || $this->getRequest()->getParam('marketplace_id')) { ?>disabled="disabled"<?php } ?> class="required-entry">
                            <option style="display:none;"></option>
                            <?php foreach ($this->getData('marketplaces') as $marketplaceRow) { ?>
                                <option value="<?php echo $marketplaceRow['id']; ?>" <?php if ($formData['general']['marketplace_id'] == $marketplaceRow['id']) { ?>selected="selected"<?php } ?>>
                                    <?php echo Mage::helper('M2ePro')->__($marketplaceRow['title']); ?>
                                </option>
                            <?php } ?>
                        </select>

                        <?php if ($this->getData('marketplace_locked') || $this->getRequest()->getParam('marketplace_id')) : ?>
                            <input id="marketplace_hidden_input" type="hidden" name="general[marketplace_id]" value="<?php echo $formData['general']['marketplace_id']; ?>" />
                        <?php endif; ?>
                    </td>

                    <td class="value">

                        <?php if($this->getData('marketplace_locked')) : ?>
                        <span id="marketplace_locked_warning_message">
                            <img src="<?php echo $this->getSkinUrl('M2ePro/images/warning.png'); ?>" class="tool-tip-image">
                            <span class="tool-tip-message" style="font-size: 12px; display: none; border-color: #ffd967; background-color: #fffbf0;">
                                <img src="<?php echo $this->getSkinUrl('M2ePro/images/i_notice.gif'); ?>" style="margin-top: -21px;">
                                <span><?php echo Mage::helper('M2ePro')->__(
                                    'Marketplace cannot be changed because this Description Policy is assigned to the Product(s) in Listing(s).'
                                ); ?></span>
                            </span>
                        </span>
                        <?php endif; ?>
                    </td>

                </tr>

            </table>

        </div>
    </div>

</div>

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

    <div class="entry-edit-head">
        <h4 class="icon-head head-edit-form fieldset-legend"><?php echo Mage::helper('M2ePro')->__('Category'); ?></h4>
    </div>

    <div class="fieldset">
        <div class="hor-scroll">

            <div id="block_notice_template_description_edit_category" class="block_notices_module" title="<?php echo Mage::helper('M2ePro')->__('Category'); ?>">
                <?php echo Mage::helper('M2ePro')->__('
                You can choose <strong>Amazon Category</strong>, in which your Amazon Products should be shown and the <strong>Product Type</strong>.<br/><br/>
                Selection of Category is necessary in case you are going to create New ASIN/ISBN using this Description Policy.<br/><br/>
                Since Amazon does not have a single structural Categories Tree and there are several of them, a tree that
                is presented in M2E Pro can be rather different from those you are used earlier on Amazon or in other Applications.<br/>
                The list of available Specifics on Specifics tab depends on the selected Product Type.<br/><br/>
                <strong>Note:</strong> Possibility to specify Product Specifics becomes available only when Category
                and Product Type are selected.
                '); ?>
            </div>

            <table class="form-list" cellspacing="0" cellpadding="0">

                <tr>
                    <td class="label">
                        <label><?php echo Mage::helper('M2ePro')->__('Category'); ?>:</label>
                    </td>

                    <td class="value" style="min-width: 280px;">
                        <span id="category_path_span">
                            <?php if (!empty($formData['general']['category_path']) && !empty($formData['general']['browsenode_id'])) { ?>
                                <span><?php echo Mage::helper('M2ePro')->escapeHtml("{$formData['general']['category_path']} ({$formData['general']['browsenode_id']})"); ?></span>
                            <?php } else { ?>
                                <span style="font-style: italic; color: grey;"><?php echo Mage::helper('M2ePro')->__('Not Selected'); ?></span>
                            <?php } ?>
                        </span>

                        <input type="hidden" name="general[category_path]" id="category_path" value="<?php echo Mage::helper('M2ePro')->escapeHtml($formData['general']['category_path']); ?>" />
                        <input type="hidden" name="general[browsenode_id]" id="browsenode_id" value="<?php echo $formData['general']['browsenode_id']; ?>" />
                    </td>

                    <td class="value" style="min-width: 80px;">
                        <span id="category_warning_messages" style="display: none; float: left;">
                            <img src="<?php echo $this->getSkinUrl('M2ePro/images/warning.png'); ?>" class="tool-tip-image">
                            <span class="tool-tip-message" style="font-size: 12px; display: none; border-color: #ffd967; background-color: #fffbf0;">
                                <img src="<?php echo $this->getSkinUrl('M2ePro/images/i_notice.gif'); ?>" style="margin-top: -21px;">

                                <span id="category_locked_warning_message" class="category-warning-item" style="display: none;"><?php echo Mage::helper('M2ePro')->__(
                                    'You cannot edit existing Category because currently the new ASIN/ISBN is being created based on this Policy or there is possibility of <br/>
                                     creating new Child Products based for the Parent Product with this Description Policy. <br/><br/>
                                     It is done to make sure that Parent and Child Products will be Listed to the same Category.'
                                ); ?></span>
                                <span class="additional-br" style="display: none;"><br/><br/></span>
                                <span id="category_is_not_accessible_message" class="category-warning-item" style="display: none;"><?php echo Mage::helper('M2ePro')->__(
                                    'This category is no longer available. Replace it with the category that suits your product the best and set Specifics.'
                                ); ?></span>
                                <span class="additional-br" style="display: none;"><br/><br/></span>
                                <span id="category_variation_warning_message" class="category-warning-item" style="display: none;"><?php echo Mage::helper('M2ePro')->__(
                                    'You have chosen Category in which Amazon Parent/Child Products are not allowed.'
                                ); ?></span>
                            </span>
                        </span>

                        <a id="edit_category_link" href="#" style="margin-left: 10px;"><?php echo Mage::helper('M2ePro')->__('Edit'); ?></a>
                    </td>

                </tr>

                <tr id="product_data_nick_tr" style="display: none;">

                    <td class="label">
                        <label><?php echo Mage::helper('M2ePro')->__('Product Type'); ?>: <span class="required">*</span></label>
                    </td>

                    <td class="value" style="min-width: 280px;">
                        <select id="product_data_nick_select" class="select M2ePro-required-when-visible">
                        </select>
                        <input type="hidden" name="general[product_data_nick]" id="product_data_nick" value="<?php echo Mage::helper('M2ePro')->escapeHtml($formData['general']['product_data_nick']); ?>" />
                    </td>

                </tr>

            </table>

        </div>
    </div>

</div>

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

    <div class="entry-edit-head">
        <h4 class="icon-head head-edit-form fieldset-legend"><?php echo Mage::helper('M2ePro')->__('New ASIN/ISBN Creation'); ?></h4>
    </div>

    <div class="fieldset">
        <div class="hor-scroll">

            <div id="block_notice_template_description_new_asin_creation" class="block_notices_module" title="<?php echo Mage::helper('M2ePro')->__('New ASIN/ISBN Creation'); ?>">
                <?php echo Mage::helper('M2ePro')->__('In case you are planning to create New ASIN/ISBN based on this Description Policy you have to obviously enable this Option. <br/><br/>

                To create New ASIN/ISBN it is also necessary to specify some required fields such as Title, Brand, Manufacturer, Main Image, Category and required Specifics. <br/><br/>

                To create New Amazon Product it is necessary to enter a valid value for UPC/EAN. This UPC/EAN should not be used on Amazon at the moment. If UPC/EAN does not exist for your Product you can use Product ID Override feature.'); ?>
            </div>

            <table class="form-list" cellspacing="0" cellpadding="0">

                <tr>
                    <td class="label">
                        <label for="new_asin_accepted"><?php echo Mage::helper('M2ePro')->__('Enabled'); ?>: </label>
                    </td>
                    <td class="value">

                        <select id="new_asin_accepted" name="general[is_new_asin_accepted]" <?php if ($this->getData('new_asin_switcher_locked') || $this->getRequest()->getParam('is_new_asin_accepted')) { ?>disabled="disabled"<?php } ?> class="required-entry">
                            <option value="0" <?php if ($formData['general']['is_new_asin_accepted'] == 0) : echo ' selected="selected"'; endif; ?>><?php echo Mage::helper('M2ePro')->__('No'); ?></option>
                            <option value="1" <?php if ($formData['general']['is_new_asin_accepted'] == 1) : echo ' selected="selected"'; endif; ?>><?php echo Mage::helper('M2ePro')->__('Yes'); ?></option>
                        </select>

                        <?php if ($this->getData('new_asin_switcher_locked') || $this->getRequest()->getParam('is_new_asin_accepted')) : ?>
                            <input id="new_asin_accepted_hidden_input" type="hidden" name="general[is_new_asin_accepted]" value="<?php echo $formData['general']['is_new_asin_accepted']; ?>" />
                        <?php endif; ?>

                    </td>

                    <?php if($this->getData('new_asin_switcher_locked')) : ?>
                    <td class="value">
                        <span id="new_asin_locked_warning_message">
                            <img src="<?php echo $this->getSkinUrl('M2ePro/images/warning.png'); ?>" class="tool-tip-image">
                            <span class="tool-tip-message" style="font-size: 12px; display: none; border-color: #ffd967; background-color: #fffbf0;">
                                <img src="<?php echo $this->getSkinUrl('M2ePro/images/i_notice.gif'); ?>" style="margin-top: -21px;">
                                <span><?php echo Mage::helper('M2ePro')->__(
                                    'You cannot turn off this Option because there are Item(s) for which new ASIN(s)/ISBN(s) will be Created or being created based on this Policy. <br/>
                                     Also this Option cannot be turned off in case there is a possibility of Creation New Child ASIN(s)/ISBN(s) for your Parent Product based on this Policy.'
                                ); ?></span>
                            </span>
                        </span>
                    </td>
                    <?php endif; ?>
                </tr>

                <tr class="hide-when-asin-is-disabled">
                    <td colspan="2" style="padding: 10px 0">
                        <hr style="border: 1px solid silver; border-bottom: none;">
                    </td>
                </tr>

                <tr class="hide-when-asin-is-disabled entry-edit">
                    <td class="label">
                        <label for="registered_parameter"><?php echo Mage::helper('M2ePro')->__('Product ID Override'); ?>: </label>
                    </td>
                    <td class="value">
                        <select id="registered_parameter" name="general[registered_parameter]">
                            <option value="" <?php if ($formData['general']['registered_parameter'] == '') echo 'selected'; ?>><?php echo Mage::helper('M2ePro')->__('None'); ?></option>
                            <option value="PrivateLabel" <?php if ($formData['general']['registered_parameter'] == 'PrivateLabel') echo 'selected'; ?>><?php echo Mage::helper('M2ePro')->__('Private Label'); ?></option>
                            <option value="Specialized" <?php if ($formData['general']['registered_parameter'] == 'Specialized') echo 'selected'; ?>><?php echo Mage::helper('M2ePro')->__('Specialized'); ?></option>
                            <option value="NonConsumer" <?php if ($formData['general']['registered_parameter'] == 'NonConsumer') echo 'selected'; ?>><?php echo Mage::helper('M2ePro')->__('Non Consumer'); ?></option>
                            <option value="PreConfigured" <?php if ($formData['general']['registered_parameter'] == 'PreConfigured') echo 'selected'; ?>><?php echo Mage::helper('M2ePro')->__('Pre Configured'); ?></option>
                        </select>
                        <p class="note">
                            <span>
                                <?php echo Mage::helper('M2ePro')->__('
                                In case your Product has no UPC/EAN, you can try to set this Option to one of the values from the Dropdown. <br/>
                                You have to be approved by Amazon in order to use it. <br/><br/>
                                <b>Note:</b> Those approvals can be received only for the specific Categories.
                                Please, contact Amazon Support to apply for approval. '); ?>
                            </span>
                        </p>
                    </td>
                </tr>

                <tr class="hide-when-asin-is-disabled entry-edit" id="worldwide_id_mode_tr">
                    <td class="label">
                        <label for="worldwide_id_mode"><?php echo Mage::helper('M2ePro')->__('UPC / EAN') ?>: <span class="required">*</span></label>
                    </td>
                    <td class="value">
                        <input type="hidden" id="worldwide_id_custom_attribute" name="general[worldwide_id_custom_attribute]" value="<?php echo $formData['general']['worldwide_id_custom_attribute']; ?>" />

                        <select name="general[worldwide_id_mode]" id="worldwide_id_mode" class="M2ePro-required-when-visible M2ePro-custom-attribute-can-be-created" allowed_attribute_types="text" data-current-value="<?php echo $formData['general']['worldwide_id_custom_attribute'];?>" >
                            <option value="<?php echo Ess_M2ePro_Model_Amazon_Template_Description::WORLDWIDE_ID_MODE_NONE ?>" <?php if ($formData['general']['worldwide_id_mode'] == Ess_M2ePro_Model_Amazon_Template_Description::WORLDWIDE_ID_MODE_NONE) echo 'selected="selected"' ?>><?php echo Mage::helper('M2ePro')->__('None') ?></option>

                            <optgroup class="M2ePro-custom-attribute-optgroup" label="Magento Attribute">
                                <?php if ($formData['general']['worldwide_id_custom_attribute'] != '' && !$magentoAttributeHelper->isExistInAttributesArray($formData['general']['worldwide_id_custom_attribute'], $attributesByInputTypes['text'])) : ?>
                                    <option attribute_code="<?php echo $formData['general']['worldwide_id_custom_attribute']; ?>" value="<?php echo Ess_M2ePro_Model_Amazon_Template_Description::WORLDWIDE_ID_MODE_CUSTOM_ATTRIBUTE ?>" selected="selected">
                                        <?php echo Mage::helper('M2ePro')->escapeHtml($magentoAttributeHelper->getAttributeLabel($formData['general']['worldwide_id_custom_attribute'])); ?>
                                    </option>
                                <?php endif; ?>
                                <?php foreach($attributesByInputTypes['text'] as $attribute) : ?>
                                    <option attribute_code="<?php echo $attribute['code']; ?>" value="<?php echo Ess_M2ePro_Model_Amazon_Template_Description::WORLDWIDE_ID_MODE_CUSTOM_ATTRIBUTE ?>" <?php if (!empty($formData['general']['worldwide_id_custom_attribute']) && $attribute['code'] == $formData['general']['worldwide_id_custom_attribute']): ?>selected="selected"<?php endif; ?>>
                                        <?php echo Mage::helper('M2ePro')->escapeHtml($attribute['label']); ?>
                                    </option>
                                <?php endforeach; ?>
                            </optgroup>
                        </select>
                        <p class="note">
                            <span><?php echo Mage::helper('M2ePro')->__(
                                'For Creating New ASIN/ISBN it is required to provide valid UPC or EAN of the respective Product.<br/><br/>
                                 UPC/EAN for which ASIN/ISBN has already been Created in Amazon Catalog cannot be used for Creating New ASIN/ISBN.<br/><br/>
                                 In this case you need to assign Item to the Existing ASIN/ISBN. In case UPC/EAN doesn’t exist for your Product you can try to use Product ID Override Option.'
                            ); ?></span>
                        </p>
                    </td>
                </tr>

            </table>

        </div>
    </div>

</div>
