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

// @codingStandardsIgnoreFile

/** @var $this Ess_M2ePro_Block_Adminhtml_Ebay_Account_Edit_Tabs_ListingOther */

$formData = Mage::helper('M2ePro/Data_Global')->getValue('temp_data')
    ? Mage::helper('M2ePro/Data_Global')->getValue('temp_data')->toArray()
    : array();

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

$helper = Mage::helper('M2ePro');

$defaults = Mage::getModel('M2ePro/Ebay_Account_Builder')->getDefaultData();
$formData = array_merge($defaults, $formData);

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

?>

<script type="text/javascript">

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

            $('other_listings_synchronization')
                .observe('change', EbayAccountObj.other_listings_synchronization_change)
                .simulate('change');
            $('other_listings_mapping_mode')
                .observe('change', EbayAccountObj.other_listings_mapping_mode_change)
                .simulate('change');
            $('mapping_sku_mode')
                .observe('change', EbayAccountObj.mapping_sku_mode_change)
                .simulate('change');
            $('mapping_title_mode')
                .observe('change', EbayAccountObj.mapping_title_mode_change)
                .simulate('change');
            $('mapping_item_id_mode')
                .observe('change', EbayAccountObj.mapping_item_id_mode_change)
                .simulate('change');

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

</script>

<div id="block_notice_ebay_accounts_other_listings" class="block_notices_module" title="<?php echo Mage::helper('M2ePro')->__('Unmanaged Listings'); ?>">
    <?php echo Mage::helper('M2ePro')->__(
        'Edit the Import, Linking and Synchronization Settings for <b>Unmanaged Listings</b> here.<br/>
        Unmanaged Listings are Items that have been Listed on eBay either directly or with a tool other than M2E Pro.<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, 'unmanaged-listing')); ?>
</div>

<div class="entry-edit" id="magento_block_ebay_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"'; endif; ?>><?php echo Mage::helper('M2ePro')->__('Yes'); ?></option>
                            <option value="0" <?php if ($formData['other_listings_synchronization'] == 0): echo ' selected="selected"'; endif; ?>><?php echo Mage::helper('M2ePro')->__('No'); ?></option>
                        </select>
                        <p class="note">
                            <span><?php echo Mage::helper('M2ePro')->__(
                                'Choose whether to import items that have been listed on eBay either directly or using
                                 a tool other than M2E Pro. M2E Pro will import only active eBay items.'
                            ); ?></span>
                        </p>
                    </td>
                </tr>

                <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')->__(
                                'Choose whether imported eBay Listings should automatically link to a Product in your Magento Inventory.'
                            ); ?></span>
                        </p>
                    </td>
                </tr>

            </table>

        </div>
    </div>

</div>

<div class="entry-edit" id="magento_block_ebay_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')->__('Linking Settings'); ?></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="ebay-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_Ebay_Account::OTHER_LISTINGS_MAPPING_SKU_MODE_NONE ?>" <?php if (isset($mappingSettings['sku']['mode']) && $mappingSettings['sku']['mode'] == Ess_M2ePro_Model_Ebay_Account::OTHER_LISTINGS_MAPPING_SKU_MODE_NONE) echo 'selected="selected"' ?>><?php echo Mage::helper('M2ePro')->__('None') ?></option>
                            <option value="<?php echo Ess_M2ePro_Model_Ebay_Account::OTHER_LISTINGS_MAPPING_SKU_MODE_DEFAULT ?>" <?php if (isset($mappingSettings['sku']['mode']) && $mappingSettings['sku']['mode'] == Ess_M2ePro_Model_Ebay_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_Ebay_Account::OTHER_LISTINGS_MAPPING_SKU_MODE_PRODUCT_ID ?>" <?php if (isset($mappingSettings['sku']['mode']) && $mappingSettings['sku']['mode'] == Ess_M2ePro_Model_Ebay_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_Ebay_Account::OTHER_LISTINGS_MAPPING_SKU_MODE_CUSTOM_ATTRIBUTE; ?>"
                                        <?php if (isset($mappingSettings['sku']['mode']) && $mappingSettings['sku']['mode'] == Ess_M2ePro_Model_Ebay_Account::OTHER_LISTINGS_MAPPING_SKU_MODE_CUSTOM_ATTRIBUTE && $tempAttributeValue == $attribute['code']) : echo 'selected="selected"'; endif; ?>>
                                        <?php echo Mage::helper('M2ePro')->escapeHtml($attribute['label']); ?>
                                    </option>
                                <?php endforeach; ?>
                            </optgroup>

                        </select>
                      </td>
                    <td id="mapping_sku_priority_td" class="value" <?php if (!isset($mappingSettings['sku']['mode']) || $mappingSettings['sku']['mode'] == Ess_M2ePro_Model_Ebay_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_Ebay_Account::OTHER_LISTINGS_MAPPING_SKU_DEFAULT_PRIORITY; ?>" class="input-text" style="width: 30px;" />
                    </td>
                </tr>

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

                <tr class="ebay-listings-mapping-settings">
                    <td class="label">
                        <label for="mapping_title_mode"><?php echo Mage::helper('M2ePro')->__('Listing 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_Ebay_Account::OTHER_LISTINGS_MAPPING_TITLE_MODE_NONE ?>" <?php if (isset($mappingSettings['title']['mode']) && $mappingSettings['title']['mode'] == Ess_M2ePro_Model_Ebay_Account::OTHER_LISTINGS_MAPPING_TITLE_MODE_NONE) echo 'selected="selected"' ?>><?php echo Mage::helper('M2ePro')->__('None') ?></option>
                            <option value="<?php echo Ess_M2ePro_Model_Ebay_Account::OTHER_LISTINGS_MAPPING_TITLE_MODE_DEFAULT ?>" <?php if (isset($mappingSettings['title']['mode']) && $mappingSettings['title']['mode'] == Ess_M2ePro_Model_Ebay_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 Attributes">
                                <?php foreach ($this->attributes as $attribute): ?>
                                    <option attribute_code="<?php echo $attribute['code']; ?>"
                                            value="<?php echo Ess_M2ePro_Model_Ebay_Account::OTHER_LISTINGS_MAPPING_TITLE_MODE_CUSTOM_ATTRIBUTE; ?>"
                                        <?php if (isset($mappingSettings['title']['mode']) && $mappingSettings['title']['mode'] == Ess_M2ePro_Model_Ebay_Account::OTHER_LISTINGS_MAPPING_TITLE_MODE_CUSTOM_ATTRIBUTE && $tempAttributeValue == $attribute['code']) : echo 'selected="selected"'; endif; ?>>
                                        <?php echo Mage::helper('M2ePro')->escapeHtml($attribute['label']); ?>
                                    </option>
                                <?php endforeach; ?>
                            </optgroup>

                        </select>
                    </td>
                    <td id="mapping_title_priority_td" class="value" <?php if (!isset($mappingSettings['title']['mode']) || $mappingSettings['title']['mode'] == Ess_M2ePro_Model_Ebay_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_Ebay_Account::OTHER_LISTINGS_MAPPING_TITLE_DEFAULT_PRIORITY; ?>" class="input-text" style="width: 30px;" />
                    </td>
                </tr>

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

                <tr class="ebay-listings-mapping-settings">
                    <td class="label">
                        <label for="mapping_item_id_mode"><?php echo Mage::helper('M2ePro')->__('eBay Item ID') ?>:</label>
                    </td>
                    <td class="value">

                        <?php $tempAttributeValue = isset($mappingSettings['item_id']['attribute']) ? $mappingSettings['item_id']['attribute'] : ''; ?>
                        <input type="hidden" name="mapping_item_id_attribute" id="mapping_item_id_attribute" value="<?php echo $tempAttributeValue; ?>"/>

                        <select id="mapping_item_id_mode" name="mapping_item_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_Ebay_Account::OTHER_LISTINGS_MAPPING_ITEM_ID_MODE_NONE ?>" <?php if (isset($mappingSettings['item_id']['mode']) && $mappingSettings['item_id']['mode'] == Ess_M2ePro_Model_Ebay_Account::OTHER_LISTINGS_MAPPING_ITEM_ID_MODE_NONE) echo 'selected="selected"' ?>><?php echo Mage::helper('M2ePro')->__('None') ?></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_Ebay_Account::OTHER_LISTINGS_MAPPING_ITEM_ID_MODE_CUSTOM_ATTRIBUTE; ?>"
                                        <?php if (isset($mappingSettings['item_id']['mode']) && $mappingSettings['item_id']['mode'] == Ess_M2ePro_Model_Ebay_Account::OTHER_LISTINGS_MAPPING_ITEM_ID_MODE_CUSTOM_ATTRIBUTE && $tempAttributeValue == $attribute['code']) : echo 'selected="selected"'; endif; ?>>
                                        <?php echo Mage::helper('M2ePro')->escapeHtml($attribute['label']); ?>
                                    </option>
                                <?php endforeach; ?>
                            </optgroup>

                        </select>
                    </td>
                    <td id="mapping_item_id_priority_td" class="value" <?php if (!isset($mappingSettings['item_id']['mode']) || $mappingSettings['item_id']['mode'] == Ess_M2ePro_Model_Ebay_Account::OTHER_LISTINGS_MAPPING_ITEM_ID_MODE_CUSTOM_ATTRIBUTE) echo 'style="display: none;"' ?>>
                        <?php echo Mage::helper('M2ePro')->__('Priority') ?>: <input name="mapping_item_id_priority" type="text" value="<?php echo isset($mappingSettings['item_id']['priority']) ? (int)$mappingSettings['item_id']['priority'] : Ess_M2ePro_Model_Ebay_Account::OTHER_LISTINGS_MAPPING_ITEM_ID_DEFAULT_PRIORITY; ?>" class="input-text" style="width: 30px;" />
                    </td>
                </tr>

            </table>

        </div>
    </div>

</div>

<?php if (!empty($this->marketplaces)) { ?>

<div class="entry-edit" id="magento_block_ebay_accounts_other_listings_related_store_views" <?php if ($formData['other_listings_synchronization'] == 0 || count($this->marketplaces) == 0) echo 'style="display:none"'; ?>>

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

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

            <div id="block_notice_ebay_accounts_related_store_view" class="block_notices_module" title="<?php echo Mage::helper('M2ePro')->__('Related Store View'); ?>">
                <?php echo Mage::helper('M2ePro')->__(
                    'Establish Connection between Marketplaces and Magento Store Views for correct data Synchronization.'
                ); ?>
            </div>

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

                <?php $storeSwitcher = $this->getLayout()->createBlock('M2ePro/adminhtml_storeSwitcher'); ?>

                <?php foreach ($this->marketplaces as $marketplace): ?>

                    <tr>
                        <td class="label">
                            <p style="padding-top: 10px;">
                                <label><?php echo Mage::helper('M2ePro')->__($marketplace['title']); ?>: </label>
                            </p>
                        </td>
                        <td class="value">
                            <?php
                                $storeSwitcher->addData(array(
                                    'id'=> 'related_store_id_'.$marketplace['id'],
                                    'selected' => $marketplace['related_store_id']
                                ));
                                echo $storeSwitcher->toHtml();
                            ?>
                        </td>
                    </tr>

                <?php endforeach; ?>

            </table>

        </div>
    </div>

</div>

<?php } ?>
