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

// @codingStandardsIgnoreFile

/** @var $this Ess_M2ePro_Block_Adminhtml_Amazon_Account_Edit_Tabs_ListingOther */

/** @var Ess_M2ePro_Model_Account $account */
$account = Mage::helper('M2ePro/Data_Global')->getValue('model_account');

$formData = $account ? $account->toArray() : array();

if (isset($formData['other_listings_mapping_settings'])) {
    $formData['other_listings_mapping_settings'] = (array)json_decode($formData['other_listings_mapping_settings'],true);
}

$defaults = Mage::getModel('M2ePro/Amazon_Account_Builder')->getDefaultData();

$formData = array_merge($defaults, $formData);
$isEdit = !!$this->getRequest()->getParam('id');

?>

<script type="text/javascript">

    // Data for current tab
    //-----------------------------
    M2ePro.translator.add(<?php echo json_encode(array(
        'If Yes is chosen, you must select at least one Attribute for Product Linking.' =>
            Mage::helper('M2ePro')->__('If Yes is chosen, you must select at least one Attribute for Product Linking.')
    )); ?>);
    //-----------------------------

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

            $('other_listings_synchronization')
                .observe('change', AmazonAccountObj.other_listings_synchronization_change)
                .simulate('change');
            $('other_listings_mapping_mode')
                .observe('change', AmazonAccountObj.other_listings_mapping_mode_change)
                .simulate('change');

            $('mapping_general_id_mode')
                .observe('change', AmazonAccountObj.mapping_general_id_mode_change)
                .simulate('change');
            $('mapping_sku_mode')
                .observe('change', AmazonAccountObj.mapping_sku_mode_change)
                .simulate('change');
            $('mapping_title_mode')
                .observe('change', AmazonAccountObj.mapping_title_mode_change)
                .simulate('change');

            $('amazonAccountEditTabs_listingOther').removeClassName('changed');
        }, 350);
    });

</script>

<div id="block_notice_amazon_accounts_other_listings" class="block_notices_module" title="<?php echo Mage::helper('M2ePro')->__('Unmanaged Listings'); ?>">
    <?php echo Mage::helper('M2ePro')->__('
        The Unmanaged Listings include the Items which are listed on Amazon without using M2E Pro Extension.<br/><br/>

        To allow the Unmanaged Listing importing for the current Amazon Account, enable the Import Unmanaged Listings option. The imported Unmanaged Items can be found under the <i>%navigation%</i>.<br/><br/>

        The Unmanaged Items can be automatically linked to the related Magento Product by ASIN/ISBN, SKU or Title value after you enable the Product Linking option.<br/><br/>

        <strong>Note:</strong> Automatic linking of the Unmanaged Items is performed only during the initial Unmanaged Listing synchronization. Afterward, you can link and move the Unmanaged Items manually under the <i>%navigation%</i>.<br/><br/>

        The detailed information can be found <a href="%url%" target="_blank">here</a>.',

        Mage::helper('M2ePro/View_Amazon')->getPageNavigationPath('listings', 'Unmanaged'),
        Mage::helper('M2ePro/Module_Support')->getDocumentationUrl(null, null, 'unmanaged-listings')
    ); ?>
</div>

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

    <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="other_listings_synchronization"><?php echo Mage::helper('M2ePro')->__('Import Unmanaged Listings'); ?>:</label>
                    </td>
                    <td class="value" style="width: auto;">
                        <select id="other_listings_synchronization" name="other_listings_synchronization">
                            <option value="1" <?php if ($formData['other_listings_synchronization'] == 1) echo ' selected="selected"'; ?>><?php echo Mage::helper('M2ePro')->__('Yes'); ?></option>
                            <option value="0" <?php if ($formData['other_listings_synchronization'] == 0) echo ' selected="selected"'; ?>><?php echo Mage::helper('M2ePro')->__('No'); ?></option>
                        </select>
                        <p class="note">
                            <span><?php echo Mage::helper('M2ePro')->__('Allows importing Unmanaged Listings.'); ?></span>
                        </p>
                    </td>
                </tr>

                <tbody id="other_listings_store_view_tr">
                    <tr id="marketplaces_related_store_id_container" <?php if (!$isEdit) echo ' style="display:none;"'; ?>>
                        <td class="label">
                            <label><?php echo Mage::helper('M2ePro')->__('Related Store View'); ?>:</label>
                        </td>
                        <td class="value" style="width: auto;">
                            <?php
                            $tempStoreBlock = $this->getLayout()->createBlock(
                                'M2ePro/adminhtml_storeSwitcher',
                                '',
                                array(
                                    'id'=>'related_store_id',
                                    'name' => 'related_store_id',
                                    'selected' => $formData['related_store_id']
                                )
                            );
                            echo $tempStoreBlock->toHtml();

                            ?>
                            <p class="note">
                                <span><?php echo Mage::helper('M2ePro')->__('Store View, which will be associated with chosen Marketplace of the current Account.'); ?></span>
                            </p>
                        </td>
                    </tr>
                </tbody>

                <tr id="other_listings_mapping_mode_tr">
                    <td class="label">
                        <label for="other_listings_mapping_mode"><?php echo Mage::helper('M2ePro')->__('Product Linking'); ?>:</label>
                    </td>
                    <td class="value" style="width: auto;">
                        <select id="other_listings_mapping_mode" name="other_listings_mapping_mode" class="M2ePro-require-select-attribute">
                            <option value="1" <?php if ($formData['other_listings_mapping_mode'] == 1) echo ' selected="selected"'; ?>><?php echo Mage::helper('M2ePro')->__('Yes'); ?></option>
                            <option value="0" <?php if ($formData['other_listings_mapping_mode'] == 0) echo ' selected="selected"'; ?>><?php echo Mage::helper('M2ePro')->__('No'); ?></option>
                        </select>
                        <p class="note">
                            <span><?php echo Mage::helper('M2ePro')->__('Links Amazon Product with Magento Product.'); ?></span>
                        </p>
                    </td>
                </tr>

            </table>

        </div>
    </div>

</div>

<div class="entry-edit" id="magento_block_amazon_accounts_other_listings_product_mapping" <?php if ($formData['other_listings_mapping_mode'] == 0) echo 'style="display:none"'; ?>>

    <div class="entry-edit-head">
        <h4 class="icon-head head-edit-form fieldset-legend"><?php echo Mage::helper('M2ePro')->__('Attributes Of Linking Amazon Items To Magento Products'); ?></h4>
    </div>

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

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

                <?php $mappingSettings = $formData['other_listings_mapping_settings']; ?>

                <tr class="amazon-listings-mapping-settings">
                    <td class="label">
                        <label for="mapping_sku_mode"><?php echo Mage::helper('M2ePro')->__('SKU') ?>:</label>
                    </td>
                    <td class="value">
                        <?php $tempAttributeValue = isset($mappingSettings['sku']['attribute']) ? $mappingSettings['sku']['attribute'] : ''; ?>
                        <input type="hidden" name="mapping_sku_attribute" id="mapping_sku_attribute" value="<?php echo $tempAttributeValue; ?>"/>

                        <select id="mapping_sku_mode" name="mapping_sku_mode" class="attribute-mode-select M2ePro-custom-attribute-can-be-created" allowed_attribute_types="text,textarea,select">
                            <option value="<?php echo Ess_M2ePro_Model_Amazon_Account::OTHER_LISTINGS_MAPPING_SKU_MODE_NONE ?>" <?php if (isset($mappingSettings['sku']['mode']) && $mappingSettings['sku']['mode'] == Ess_M2ePro_Model_Amazon_Account::OTHER_LISTINGS_MAPPING_SKU_MODE_NONE) echo 'selected="selected"' ?>><?php echo Mage::helper('M2ePro')->__('None') ?></option>
                            <option value="<?php echo Ess_M2ePro_Model_Amazon_Account::OTHER_LISTINGS_MAPPING_SKU_MODE_DEFAULT ?>" <?php if (isset($mappingSettings['sku']['mode']) && $mappingSettings['sku']['mode'] == Ess_M2ePro_Model_Amazon_Account::OTHER_LISTINGS_MAPPING_SKU_MODE_DEFAULT) echo 'selected="selected"' ?>><?php echo Mage::helper('M2ePro')->__('Product SKU') ?></option>
                            <option value="<?php echo Ess_M2ePro_Model_Amazon_Account::OTHER_LISTINGS_MAPPING_SKU_MODE_PRODUCT_ID ?>" <?php if (isset($mappingSettings['sku']['mode']) && $mappingSettings['sku']['mode'] == Ess_M2ePro_Model_Amazon_Account::OTHER_LISTINGS_MAPPING_SKU_MODE_PRODUCT_ID) echo 'selected="selected"' ?>><?php echo Mage::helper('M2ePro')->__('Product ID') ?></option>

                            <optgroup class="M2ePro-custom-attribute-optgroup" label="Magento Attributes">
                                <?php foreach ($this->attributes as $attribute) { ?>
                                    <option attribute_code="<?php echo $attribute['code'] ?>" value="<?php echo Ess_M2ePro_Model_Amazon_Account::OTHER_LISTINGS_MAPPING_SKU_MODE_CUSTOM_ATTRIBUTE?>" <?php if ($tempAttributeValue == $attribute['code'] && $mappingSettings['sku']['mode'] == Ess_M2ePro_Model_Amazon_Account::OTHER_LISTINGS_MAPPING_SKU_MODE_CUSTOM_ATTRIBUTE) echo 'selected="selected"' ?>>
                                        <?php echo Mage::helper('M2ePro')->escapeHtml($attribute['label']); ?>
                                    </option>
                                <?php } ?>
                            </optgroup>
                        </select>
                    </td>
                    <td id="mapping_sku_priority_td" class="value" <?php if (!isset($mappingSettings['sku']['mode']) || $mappingSettings['sku']['mode'] == Ess_M2ePro_Model_Amazon_Account::OTHER_LISTINGS_MAPPING_SKU_MODE_NONE) echo 'style="display: none;"' ?>>
                        <?php echo Mage::helper('M2ePro')->__('Priority') ?>: <input name="mapping_sku_priority" type="text" value="<?php echo isset($mappingSettings['sku']['priority']) ? (int)$mappingSettings['sku']['priority'] : Ess_M2ePro_Model_Amazon_Account::OTHER_LISTINGS_MAPPING_SKU_DEFAULT_PRIORITY; ?>" class="input-text" style="width: 30px;" />
                    </td>
                </tr>

                <tr class="amazon-listings-mapping-settings">
                    <td class="value" colspan="2" style="width: auto;">
                        <hr style="border: 1px solid silver; border-bottom: none;"/>
                    </td>
                </tr>

                <tr class="amazon-listings-mapping-settings">
                    <td class="label">
                        <label for="mapping_general_id_mode"><?php echo Mage::helper('M2ePro')->__('ASIN / ISBN') ?>:</label>
                    </td>
                    <td class="value">
                        <?php $tempAttributeValue = isset($mappingSettings['general_id']['attribute']) ? $mappingSettings['general_id']['attribute'] : ''; ?>
                        <input type="hidden" name="mapping_general_id_attribute" id="mapping_general_id_attribute" value="<?php echo $tempAttributeValue; ?>"/>

                        <select id="mapping_general_id_mode" name="mapping_general_id_mode" class="attribute-mode-select M2ePro-custom-attribute-can-be-created" allowed_attribute_types="text,textarea,select">
                            <option value="<?php echo Ess_M2ePro_Model_Amazon_Account::OTHER_LISTINGS_MAPPING_GENERAL_ID_MODE_NONE ?>" <?php if (isset($mappingSettings['general_id']['mode']) && $mappingSettings['general_id']['mode'] == Ess_M2ePro_Model_Amazon_Account::OTHER_LISTINGS_MAPPING_GENERAL_ID_MODE_NONE) echo 'selected="selected"' ?>><?php echo Mage::helper('M2ePro')->__('None') ?></option>

                            <optgroup class="M2ePro-custom-attribute-optgroup" label="Magento Attribute">
                                <?php foreach ($this->attributes as $attribute) { ?>
                                    <option attribute_code="<?php echo $attribute['code'] ?>" value="<?php echo Ess_M2ePro_Model_Amazon_Account::OTHER_LISTINGS_MAPPING_GENERAL_ID_MODE_CUSTOM_ATTRIBUTE?>" <?php if ($tempAttributeValue == $attribute['code']) echo 'selected="selected"' ?>>
                                        <?php echo Mage::helper('M2ePro')->escapeHtml($attribute['label']); ?>
                                    </option>
                                <?php } ?>
                            </optgroup>
                        </select>
                    </td>
                    <td id="mapping_general_id_priority_td" class="value" <?php if (!isset($mappingSettings['general_id']['mode']) || $mappingSettings['general_id']['mode'] == Ess_M2ePro_Model_Amazon_Account::OTHER_LISTINGS_MAPPING_GENERAL_ID_MODE_NONE) echo 'style="display: none;"' ?>>
                        <?php echo Mage::helper('M2ePro')->__('Priority') ?>: <input name="mapping_general_id_priority" type="text" value="<?php echo isset($mappingSettings['general_id']['priority']) ? (int)$mappingSettings['general_id']['priority'] : Ess_M2ePro_Model_Amazon_Account::OTHER_LISTINGS_MAPPING_GENERAL_ID_DEFAULT_PRIORITY; ?>" class="input-text" style="width: 30px;" />
                    </td>
                </tr>

                <tr class="amazon-listings-mapping-settings">
                    <td class="value" colspan="2" style="width: auto;">
                        <hr style="border: 1px solid silver; border-bottom: none;"/>
                    </td>
                </tr>

                <tr class="amazon-listings-mapping-settings">
                    <td class="label">
                        <label for="mapping_title_mode"><?php echo Mage::helper('M2ePro')->__('Title') ?>:</label>
                    </td>
                    <td class="value">
                        <?php $tempAttributeValue = isset($mappingSettings['title']['attribute']) ? $mappingSettings['title']['attribute'] : ''; ?>
                        <input type="hidden" name="mapping_title_attribute" id="mapping_title_attribute" value="<?php echo $tempAttributeValue; ?>"/>

                        <select id="mapping_title_mode" name="mapping_title_mode" class="attribute-mode-select M2ePro-custom-attribute-can-be-created" allowed_attribute_types="text,textarea,select">
                            <option value="<?php echo Ess_M2ePro_Model_Amazon_Account::OTHER_LISTINGS_MAPPING_TITLE_MODE_NONE ?>" <?php if (isset($mappingSettings['title']['mode']) && $mappingSettings['title']['mode'] == Ess_M2ePro_Model_Amazon_Account::OTHER_LISTINGS_MAPPING_TITLE_MODE_NONE) echo 'selected="selected"' ?>><?php echo Mage::helper('M2ePro')->__('None') ?></option>
                            <option value="<?php echo Ess_M2ePro_Model_Amazon_Account::OTHER_LISTINGS_MAPPING_TITLE_MODE_DEFAULT ?>" <?php if (isset($mappingSettings['title']['mode']) && $mappingSettings['title']['mode'] == Ess_M2ePro_Model_Amazon_Account::OTHER_LISTINGS_MAPPING_TITLE_MODE_DEFAULT) echo 'selected="selected"' ?>><?php echo Mage::helper('M2ePro')->__('Product Name') ?></option>

                            <optgroup class="M2ePro-custom-attribute-optgroup" label="Magento Attribute">
                                    <?php foreach ($this->attributes as $attribute) { ?>
                                        <option attribute_code="<?php echo $attribute['code'] ?>" value="<?php echo Ess_M2ePro_Model_Amazon_Account::OTHER_LISTINGS_MAPPING_TITLE_MODE_CUSTOM_ATTRIBUTE?>" <?php if ($tempAttributeValue == $attribute['code']) echo 'selected="selected"' ?>>
                                            <?php echo Mage::helper('M2ePro')->escapeHtml($attribute['label']); ?>
                                        </option>
                                    <?php } ?>
                            </optgroup>
                        </select>
                    </td>
                    <td id="mapping_title_priority_td" class="value" <?php if (!isset($mappingSettings['title']['mode']) || $mappingSettings['title']['mode'] == Ess_M2ePro_Model_Amazon_Account::OTHER_LISTINGS_MAPPING_TITLE_MODE_NONE) echo 'style="display: none;"' ?>>
                        <?php echo Mage::helper('M2ePro')->__('Priority') ?>: <input name="mapping_title_priority" type="text" value="<?php echo isset($mappingSettings['title']['priority']) ? (int)$mappingSettings['title']['priority'] : Ess_M2ePro_Model_Amazon_Account::OTHER_LISTINGS_MAPPING_TITLE_DEFAULT_PRIORITY; ?>" class="input-text" style="width: 30px;" />
                    </td>
                </tr>

            </table>

        </div>
    </div>

</div>
