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

// @codingStandardsIgnoreFile

/** @var $this Ess_M2ePro_Block_Adminhtml_Amazon_Template_ProductTaxCode_Edit_Form */

/** @var Ess_M2ePro_Model_Amazon_Template_ProductTaxCode $model */
$model = Mage::helper('M2ePro/Data_Global')->getValue('temp_data');

$formData = array();
if ($model) {
    $formData = $model->toArray();
}

$default = Mage::getModel('M2ePro/Amazon_Template_ProductTaxCode_Builder')->getDefaultData();

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

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

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

$attributes = $magentoAttributeHelper->getAll();

$attributesByInputTypes = array(
    'text_select' => $magentoAttributeHelper->filterByInputTypes($attributes, array('text', 'select'))
);

?>

<script type="text/javascript">

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

    M2ePro.php.setConstants(
        <?php echo Mage::helper('M2ePro')->getClassConstantAsJson('Ess_M2ePro_Model_Amazon_Template_ProductTaxCode'); ?>,
        'Ess_M2ePro_Model_Amazon_Template_ProductTaxCode'
    );

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

    M2ePro.translator.add(<?php echo json_encode(array(
        'Add Product Tax Code Policy' => Mage::helper('M2ePro')->__($duplicateHeaderText),
        'The specified Title is already used for other Policy. Policy Title must be unique.' => Mage::helper('M2ePro')->__('The specified Title is already used for other Policy. Policy Title must be unique.'),
    )); ?>);

    M2ePro.formData.id = '<?php echo $this->getRequest()->getParam('id'); ?>';
    M2ePro.formData.title = '<?php echo Mage::helper('M2ePro')->escapeJs(Mage::helper('M2ePro')->escapeHtml($formData['title'])); ?>';

    Event.observe(window, 'load', function() {
        AmazonTemplateProductTaxCodeObj = new AmazonTemplateProductTaxCode();

        $('product_tax_code_mode')
            .observe('change', AmazonTemplateProductTaxCodeObj.productTaxCodeModeChange)
            .simulate('change');

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

</script>

<div id="block_notice_amazon_template_product_tax_code" class="block_notices_module" title="<?php echo Mage::helper('M2ePro')->__('Product Tax Code Policy'); ?>">
    <?php
        echo Mage::helper('M2ePro')->__(
        'Product Tax Code Policy allows applying Amazon Product Tax Codes to your offers via M2E Pro. With a Product Tax Code applied the VAT-exclusive prices will be shown to B2B customers<br /><br />
         More details can be found <a href="%url%" target="_blank">here</a>.',
            Mage::helper('M2ePro/Module_Support')->getDocumentationUrl(null, null, 'product-tax-code-policies')
    ); ?>
</div>

<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" />

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

            </table>

        </div>
    </div>

</div>

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

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

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

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

                <tr>
                    <td class="label">
                        <label for="product_tax_code_attribute"><?php echo Mage::helper('M2ePro')->__('Tax Code'); ?>: <span class="required">*</span></label>
                    </td>
                    <td class="value">
                        <input type="hidden" id="product_tax_code_attribute" name="product_tax_code_attribute" value="<?php echo $formData['product_tax_code_attribute']; ?>"/>

                        <select id="product_tax_code_mode" name="product_tax_code_mode" class="required-entry M2ePro-custom-attribute-can-be-created" allowed_attribute_types="text,select">
                            <option class="empty-option"></option>
                            <option id="product_tax_code_mode_cv" value="<?php echo Ess_M2ePro_Model_Amazon_Template_ProductTaxCode::PRODUCT_TAX_CODE_MODE_VALUE; ?>" <?php if (Ess_M2ePro_Model_Amazon_Template_ProductTaxCode::PRODUCT_TAX_CODE_MODE_VALUE == $formData['product_tax_code_mode']) echo ' selected="selected"'; ?>><?php echo Mage::helper('M2ePro')->__('Custom Value'); ?></option>

                            <optgroup class="M2ePro-custom-attribute-optgroup" label="Magento Attribute" new_option_value="<?php echo Ess_M2ePro_Model_Amazon_Template_ProductTaxCode::PRODUCT_TAX_CODE_MODE_ATTRIBUTE; ?>">
                                <?php foreach($attributesByInputTypes['text_select'] as $attribute) : ?>
                                    <option attribute_code="<?php echo $attribute['code']; ?>" value="<?php echo Ess_M2ePro_Model_Amazon_Template_ProductTaxCode::PRODUCT_TAX_CODE_MODE_ATTRIBUTE; ?>" <?php if ($formData['product_tax_code_mode'] == Ess_M2ePro_Model_Amazon_Template_ProductTaxCode::PRODUCT_TAX_CODE_MODE_ATTRIBUTE && $attribute['code'] == $formData['product_tax_code_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')->__(
                                    'Apply Amazon Product Tax Codes to display VAT-exclusive prices to B2B customers.
                                    Find more info in <a href="%url%" target="_blank">this article</a>.',
                                    Mage::helper('M2ePro/Module_Support')->getDocumentationUrl(
                                        null,
                                        null,
                                        'product-tax-code-policies'
                                    )
                                ); ?></span>
                        </p>
                    </td>
                </tr>

                <tr id="product_tax_code_custom_value_tr" style="display: none;">
                    <td class="label">
                        <label for="product_tax_code_value"><?php echo Mage::helper('M2ePro')->__('Tax Code Value'); ?>: <span class="required">*</span></label>
                    </td>
                    <td class="value" style="width: auto;">
                        <input id="product_tax_code_value" name="product_tax_code_value" value="<?php echo $this->escapeHtml($formData['product_tax_code_value']); ?>" type="text" class="input-text M2ePro-required-when-visible" />
                    </td>
                </tr>

            </table>

        <div>
    </div>

</div>
