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

/** @var $this Ess_M2ePro_Block_Adminhtml_Amazon_Account_Edit_Tabs_General */

?>

<div id="block_notice_amazon_accounts_general" class="block_notices_module" title="<?php echo Mage::helper('M2ePro')->__('General'); ?>">
    <?php echo Mage::helper('M2ePro')->__(
        'Fill in the Title, choose the Marketplace you want to work with and click the <i class="underline">Get Access Data</i> link.
        <br/>You will be redirected to the Amazon Website.<br/><br/>
    <b>Note:</b> To be eligible to sell on Amazon Sellers must have at least one of the following: a non-individual <i>Selling on Amazon Account</i>,
    an <i>Amazon WebStore Account</i>, a <i>Checkout by Amazon Account</i>, or an <i>Amazon Product Ads Account</i>.<br/>
    If you are an individual Seller you have to upgrade to a Pro Merchant Seller Account from the Amazon Services Selling on Amazon Page.
        <br/>
        <br/>Sign-in and complete steps of getting access for each Marketplace:
        <ul class="list">
            <li>Select - <i>\'I want to use an application to access my Amazon Seller Account with MWS.\'</i></li>
            <li>Fill in Application Name and Application\'s Developer Account Number, which you can find in the Marketplaces Section on the current Page.</li>
            <li>Accept the Amazon MWS License Agreement.</li>
            <li>Copy generated "Merchant ID" / "MWS Auth Token" and paste it in the corresponding fields of the current Page.</li>
        </ul>
        Continue working with Unmanaged Listings and Orders Settings Tabs.<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, 'amazon-account-activation')
    ); ?>
</div>

<?php
    /** @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);

    $accountTitle = Mage::helper('M2ePro/Data_Session')->getValue('account_title', true);
    $merchantId = Mage::helper('M2ePro/Data_Session')->getValue('merchant_id', true);

    $authMarketplace = Mage::helper('M2ePro/Data_Session')->getValue('marketplace', true);

    $licenseMessage = (string)Mage::helper('M2ePro/Data_Global')->getValue('license_message');
?>

<script type="text/javascript">

    // General for all tabs
    //-----------------------------
    M2ePro.php.setConstants(
        <?php echo Mage::helper('M2ePro')->getClassConstantAsJson('Ess_M2ePro_Model_Amazon_Account'); ?>,
        'Ess_M2ePro_Model_Amazon_Account'
    );
    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_account')) ?>);

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

        'adminhtml_amazon_account/beforeGetToken' => $this->getUrl('M2ePro/adminhtml_amazon_account/beforeGetToken', array('wizard' => (bool)$this->getRequest()->getParam('wizard', false))),
        'adminhtml_amazon_account/check' => $this->getUrl('M2ePro/adminhtml_amazon_account/check', array('id' => $this->getRequest()->getParam('id')))
    )); ?>);

    M2ePro.translator.add(<?php echo json_encode(array(
        'Unlink Repricing Tool' => Mage::helper('M2ePro')->__('Unlink From Repricing Tool'),
        'Please enter correct value.' => Mage::helper('M2ePro')->__('Please enter correct value.'),
        'Coefficient is not valid.' => Mage::helper('M2ePro')->__('Coefficient is not valid.'),
        'on_delete_account_message' => Mage::helper('M2ePro')->__(
            <<<HTML
<p>You are about to delete your eBay/Amazon/Walmart seller account from M2E Pro. This will remove the
account-related Listings and Products from the extension and disconnect the synchronization.
Your listings on the channel will <b>not</b> be affected.</p>
<p>Please confirm if you would like to delete the account.</p>
<p>Note: once the account is no longer connected to your M2E Pro, please remember to delete it from
<a href="%url%">M2E Accounts</a></p>
HTML
            ,
            Mage::helper('M2ePro/Module_Support')->getAccountsUrl()
        )
    )); ?>);

    M2ePro.formData.id = '<?php echo $this->getRequest()->getParam('id'); ?>';

    Event.observe(window, 'load', function() {
        <?php echo $licenseMessage; ?>

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

        var urlHash = location.hash.substr(1);
        if (urlHash != '') {
            setTimeout(function() {
                amazonAccountEditTabsJsTabs.tabs.each(function(tab){
                    if (tab.name == urlHash) {
                        amazonAccountEditTabsJsTabs.showTabContent(tab);
                    }
                });
                location.hash = '';
            }, 100);
        }
    });
    //-----------------------------

</script>

<script type="M2ePro/template" id="on_delete_account_template"><?php echo $this->getChildHtml('confirm_popup'); ?></script>

<script type="text/javascript">

    // Data for current tab
    //-----------------------------
    M2ePro.translator.add(<?php echo json_encode(array(
        'The specified Title is already used for other Account. Account Title must be unique.' => Mage::helper('M2ePro')->__('The specified Title is already used for other Account. Account Title must be unique.'),
    )); ?>);

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

</script>

<div class="entry-edit" id="magento_block_amazon_accounts_general_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 !empty($accountTitle) ? $accountTitle : $this->escapeHtml($formData['title']); ?>" type="text" class="input-text required-entry M2ePro-account-title" />
                        <p class="note">
                            <span><?php echo Mage::helper('M2ePro')->__('Title or Identifier of Amazon Account for your internal use.'); ?></span>
                        </p>
                    </td>
                </tr>

            </table>

        </div>
    </div>

</div>

<div class="entry-edit" id="magento_block_amazon_accounts_marketplaces_access" collapseable="no">

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

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

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

                <tr>
                    <td class="label"><?php echo Mage::helper('M2ePro')->__('Marketplace') ?>:</td>
                    <td class="value">
                        <?php echo $this->marketplace->getTitle(); ?>
                    </td>
                </tr>

                <input type="hidden" id="marketplace_id" name="marketplace_id" value="<?php echo $formData['marketplace_id']; ?>" />
                <input type="hidden" id="merchant_id" name="merchant_id" value="<?php echo $formData['merchant_id']; ?>" />

                <tr id="marketplaces_merchant_id_container">
                    <td class="label"><?php echo Mage::helper('M2ePro')->__('Merchant ID') ?>:</td>
                    <td class="value">
                        <?php echo $formData['merchant_id']; ?>
                        <p class="note">
                            <span><?php echo Mage::helper('M2ePro')->__('Your Amazon Seller ID.'); ?></span>
                        </p>
                    </td>
                </tr>

                <tr>
                    <td class='label'></td>
                    <td id="update_access_data" class='value'>
                        <?php echo $this->getChildHtml('update_access_data'); ?>
                    </td>
                </tr>

                <tr>
                    <td class='label'></td>
                    <td id="check_token_validity" class='value'>
                        <?php echo $this->getChildHtml('check_token_validity'); ?>
                    </td>
                </tr>

            </table>

        </div>
    </div>

</div>
