<?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_General */

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

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

$temp = Mage::helper('M2ePro/Data_Session')->getValue('get_token_account_title', true);
$temp !== null && $formData['title'] = $temp;

$temp = Mage::helper('M2ePro/Data_Session')->getValue('get_token_account_mode', true);
$temp !== null && $formData['mode'] = $temp;

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

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

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

?>

<div id="block_notice_ebay_accounts_general" class="block_notices_module" title="<?php echo Mage::helper('M2ePro')->__('General'); ?>">
    <?php echo Mage::helper('M2ePro')->__(
        'This Page shows the Environment for your eBay Account and details of the authorization for M2E Pro to connect to your eBay Account.<br/><br/>
        If your token has expired or is not activated, click <b>Get Token</b>.<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, 'ebay-account-activation')
    ); ?>
</div>

<script type="text/javascript">

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

    M2ePro.url.add(<?php echo json_encode(Mage::helper('M2ePro')->getControllerActions('adminhtml_ebay_account')) ?>);

    M2ePro.url.set('adminhtml_ebay_account/beforeGetSellApiToken','<?php echo $this->getUrl('*/adminhtml_ebay_account/beforeGetSellApiToken',array('_current' => true)); ?>');

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

    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.'),
        'No Customer entry is found for specified ID.' => Mage::helper('M2ePro')->__('No Customer entry is found for specified ID.'),
        '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.'),
        'You should create at least one Response Template.' => Mage::helper('M2ePro')->__('You should create at least one Response Template.'),
        '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; ?>
        EbayAccountObj = new EbayAccount();
        editForm = new varienForm('edit_form', '<?php echo $this->getValidationUrl(); ?>');
    });
    //-----------------------------

</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(
        'You must get token.' => Mage::helper('M2ePro')->__('You must get token.')
    )); ?>);

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

</script>

<style>
    .columns .form-list td.value
    {
        width: auto;
    }
</style>

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

    <div class="entry-edit-head">
        <h4 class="icon-head head-edit-form fieldset-legend"><?php echo $helper->__('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-account-title" />
                        <p class="note">
                            <span><?php echo Mage::helper('M2ePro')->__('Title or Identifier of eBay Account for your internal use.'); ?></span>
                        </p>
                    </td>
                </tr>

            </table>

        </div>
    </div>

</div>

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

        <div class="entry-edit-head">
            <h4 class="icon-head head-edit-form fieldset-legend"><?php echo Mage::helper('M2ePro')->__('Account Details'); ?></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')->__('eBay User ID'); ?>: </label>
                        </td>
                        <td class="value" style="width: auto;">
                        <span id="account_title">
                            <a target="_blank" href="<?php echo Mage::helper('M2ePro/Component_Ebay')->getMemberUrl($formData['user_id'], $formData['mode']); ?>"><?php echo $this->escapeHtml($formData['user_id']); ?></a>
                        </span>
                        </td>
                    </tr>

                    <tr>
                        <td class="label">
                            <label for="mode"><?php echo Mage::helper('M2ePro')->__('Environment'); ?>: <span class="required">*</span></label>
                        </td>
                        <td class="value" style="width: auto;">
                            <?php
                            $modeValue = (int)$formData['mode'];
                            $displayText = ($modeValue === Ess_M2ePro_Model_Ebay_Account::MODE_PRODUCTION) ? Mage::helper('M2ePro')->__('Production (Live)') : Mage::helper('M2ePro')->__('Sandbox (Test)');
                            ?>
                            <?php echo $displayText; ?>
                            <input type="hidden" id="mode" name="mode" value="<?php echo $modeValue; ?>">
                        </td>
                    </tr>

                </table>

            </div>
        </div>

    </div>

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

        <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">
                            <label><?php echo Mage::helper('M2ePro')->__('Grant Access'); ?>:</label>
                        </td>
                        <td class="value" style="width: auto;">
                            <?php echo $this->getChildHtml('get_sell_api_token_button'); ?>
                            <p class="note note-no-tool-tip">
                            <span>
                                <?php echo Mage::helper('M2ePro')->__(
                                    'You need to finish the token process within 5 minutes.<br/> If not, just click <b>Get Token</b> and try again.'
                                ); ?>
                            </span>
                            </p>
                        </td>
                    </tr>

                </table>

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

                    <tr <?php if(empty($formData['sell_api_token_expired_date']) || strtotime($formData['sell_api_token_expired_date']) < 0): ?>style="display: none;"<?php endif; ?>>
                        <td class="label">
                            <label for="sell_api_token_expired_date"><?php echo Mage::helper('M2ePro')->__('Expiration Date'); ?>: <span class="required">*</span></label>
                        </td>
                        <td class="value" style="width: auto;">
                            <div><?php echo $formData['sell_api_token_expired_date']; ?></div>
                            <input id="sell_api_token_expired_date" name="sell_api_token_expired_date" value="<?php echo $formData['sell_api_token_expired_date']; ?>" type="hidden"/>
                        </td>
                    </tr>

                </table>

            </div>
        </div>

    </div>
