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

// @codingStandardsIgnoreFile

/** @var Ess_M2ePro_Block_Adminhtml_Walmart_Marketplace_Edit_Form $this */

?>

<script type="text/javascript">

    M2ePro.url.add(<?php echo json_encode(array(
        'formSubmit' => $this->getUrl('M2ePro/adminhtml_walmart_marketplace/save'),
        'logViewUrl' => $this->getUrl('M2ePro/adminhtml_walmart_log/synchronization',array('back'=>Mage::helper('M2ePro')->makeBackUrlParam('*/adminhtml_walmart_synchronization/index'))),

        'runSynchNow' => $this->getUrl('M2ePro/adminhtml_walmart_marketplace/runSynchNow'),
        'adminhtml_walmart_marketplace/synchGetExecutingInfo' => $this->getUrl('M2ePro/adminhtml_walmart_marketplace/synchGetExecutingInfo'),

        'walmart_marketplace_withProductType/runSynchNow' => $this->getUrl(
            '*/adminhtml_walmart_marketplace_withProductType/runSynchNow'
        ),
        'walmart_marketplace_withProductType/synchGetExecutingInfo' => $this->getUrl(
            '*/adminhtml_walmart_marketplace_withProductType/synchGetExecutingInfo'
        ),
    )); ?>);

    M2ePro.translator.add(<?php echo json_encode(array(
        'You must select at least one Marketplace you will work with.' => Mage::helper('M2ePro')->__(
            'You must select at least one Marketplace you will work with.'
        ),
        'marketplace_sync_success_message' => Mage::helper('M2ePro')->__(
            'Walmart Data Update was completed.'
        ),
        'marketplace_sync_error_message' => Mage::helper('M2ePro')->__(
            'Walmart Data Update was completed with errors.'
            . ' <a target="_blank" href="%url%">View Log</a> for the details.',
            $syncLogUrl = $this->getUrl('*/adminhtml_walmart_synchronization_log/index')
        ),
        'marketplace_sync_warning_message' => Mage::helper('M2ePro')->__(
            'Warning Data Update was completed with warnings.'
            . ' <a target="_blank" href="%url%">View Log</a> for the details.',
            $syncLogUrl
        ),
    )); ?>);

    Event.observe(window, 'load', function() {
        editForm = new varienForm('<?php echo $this->getForm()->getId(); ?>', '<?php echo $this->getValidationUrl(); ?>');

        MarketplaceProgressObj = new SynchProgress(
            new ProgressBar('marketplaces_progress_bar'),
            new AreaWrapper('marketplaces_content_container')
        );
        MarketplaceObj = new WalmartMarketplace(MarketplaceProgressObj, storedStatuses);

        MarketplaceWithProductTypeProgressObj = new WalmartMarketplaceWithProductTypeSyncProgress(
            new ProgressBar('marketplaces_progress_bar'),
            new AreaWrapper('marketplaces_content_container')
        );
        WalmartMarketplaceWithProductTypeSyncObj = new WalmartMarketplaceWithProductTypeSync(
            MarketplaceWithProductTypeProgressObj,
            storedStatuses
        );
    });

</script>

<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 id="<?php echo $this->getContainerId(); ?>">
    <script>
        if (typeof storedStatuses !== 'undefined') {
            storedStatuses = storedStatuses.concat(<?php echo json_encode($this->storedStatuses)?>);
        } else {
            storedStatuses = <?php echo json_encode($this->storedStatuses)?>;
        }
    </script>

    <?php foreach($this->groups as $group) { ?>

        <div class="entry-edit" id="magento_block_walmart_marketplaces_group_<?php echo $group['id']; ?>" collapseable="no">

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

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

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

                        <?php
                            $componentName = Ess_M2ePro_Helper_Component_Walmart::NICK;
                            $componentTitle = Mage::helper('M2ePro/Component_Walmart')->getTitle();
                        ?>

                        <?php foreach($group['marketplaces'] as $marketplace) { ?>

                            <tr>
                                <td class="label">
                                    <label for="status_<?php echo $marketplace['instance']->getId(); ?>">
                                        <?php if ($marketplace['params']['locked'] ||
                                                  $marketplace['instance']->getData('developer_key') === null) { ?>
                                            <span style="color: gray;">
                                        <?php } ?>
                                            <?php echo Mage::helper('M2ePro')->__($marketplace['instance']->getData('title')); ?>:
                                        <?php if ($marketplace['params']['locked'] ||
                                                  $marketplace['instance']->getData('developer_key') === null) { ?>
                                            </span>
                                        <?php } ?>
                                    </label>
                                </td>
                                <td class="value">
                                    <?php if ($marketplace['params']['locked']) { ?>
                                        <select disabled="disabled" id="status_<?php echo $marketplace['instance']->getId(); ?>" name="status_<?php echo $marketplace['instance']->getId(); ?>" marketplace_id="<?php echo $marketplace['instance']->getId(); ?>" component_title="<?php echo $componentTitle; ?>" component_name="<?php echo $componentName; ?>" class="locked_selected marketplace_status_select">
                                            <option value="<?php echo Ess_M2ePro_Model_Marketplace::STATUS_ENABLE; ?>" selected="selected"><?php echo Mage::helper('M2ePro')->__('Enabled'); echo ' - '.Mage::helper('M2ePro')->__('Used in Account(s)'); ?></option>
                                        </select>
                                    <?php } elseif ($marketplace['instance']->getData('developer_key') === null) { ?>
                                        <select disabled="disabled" id="status_<?php echo $marketplace['instance']->getId(); ?>" name="status_<?php echo $marketplace['instance']->getId(); ?>" marketplace_id="<?php echo $marketplace['instance']->getId(); ?>" component_title="<?php echo $componentTitle; ?>" component_name="<?php echo $componentName; ?>" class="locked_selected marketplace_status_select">
                                            <option value="<?php echo Ess_M2ePro_Model_Marketplace::STATUS_DISABLE; ?>" selected="selected"><?php echo Mage::helper('M2ePro')->__('Disabled'); echo ' - '.Mage::helper('M2ePro')->__('Coming Soon'); ?></option>
                                        </select>
                                    <?php } else { ?>
                                        <select id="status_<?php echo $marketplace['instance']->getId(); ?>" name="status_<?php echo $marketplace['instance']->getId(); ?>" marketplace_id="<?php echo $marketplace['instance']->getId(); ?>" component_title="<?php echo $componentTitle; ?>" component_name="<?php echo $componentName; ?>" onchange="MarketplaceObj.changeStatus(this);" <?php if (Ess_M2ePro_Model_Marketplace::STATUS_ENABLE == $marketplace['instance']->getStatus()) echo ' class="hightlight_selected marketplace_status_select"'; else echo ' class="lacklustre_selected marketplace_status_select"'; ?>>
                                            <option value="<?php echo Ess_M2ePro_Model_Marketplace::STATUS_DISABLE; ?>"<?php if (Ess_M2ePro_Model_Marketplace::STATUS_DISABLE == $marketplace['instance']->getStatus()) echo ' selected="selected"'; ?>><?php echo Mage::helper('M2ePro')->__('Disabled'); ?></option>
                                            <option value="<?php echo Ess_M2ePro_Model_Marketplace::STATUS_ENABLE; ?>"<?php if (Ess_M2ePro_Model_Marketplace::STATUS_ENABLE == $marketplace['instance']->getStatus()) echo ' selected="selected"'; ?>><?php echo Mage::helper('M2ePro')->__('Enabled'); ?></option>
                                        </select>
                                    <?php } ?>
                                    <p class="note note-no-tool-tip">
                                        <span><?php echo Mage::helper('M2ePro')->__($marketplace['instance']->getData('url')); ?></span>
                                    </p>
                                </td>
                                <td id="run_single_button_<?php echo $marketplace['instance']->getId(); ?>" class="value" <?php if($marketplace['instance']->getStatus() == Ess_M2ePro_Model_Marketplace::STATUS_DISABLE): ?>style="display: none;"<?php endif; ?>>
                                    <?php if ($marketplace['params']['is_supported_pt']) { ?>
                                        <?php echo $this->getChildHtml('run_with_pt_single_button'); ?>
                                    <?php } else { ?>
                                        <?php echo $this->getChildHtml('run_single_button'); ?>
                                    <?php } ?>
                                </td>
                                <td id="changed_<?php echo $marketplace['instance']->getId(); ?>" class="changed">
                                    <span></span>
                                </td>
                                <td id="synch_info_wait_<?php echo $marketplace['instance']->getId(); ?>" class="value" style="display: none; color: gray;">
                                    &nbsp; <?php echo Mage::helper('M2ePro')->__('Waiting'); ?>
                                </td>
                                <td id="synch_info_process_<?php echo $marketplace['instance']->getId(); ?>" class="value" style="display: none; color: blue;">
                                    &nbsp; <?php echo Mage::helper('M2ePro')->__('Processing'); ?>
                                </td>
                                <td id="synch_info_complete_<?php echo $marketplace['instance']->getId(); ?>" class="value" style="display: none; color: green;">
                                    &nbsp; <?php echo Mage::helper('M2ePro')->__('Completed'); ?>
                                </td>
                                <td id="synch_info_error_<?php echo $marketplace['instance']->getId(); ?>" class="value" style="display: none; color: red;">
                                    &nbsp; <?php echo Mage::helper('M2ePro')->__('Error'); ?>
                                </td>
                                <td id="synch_info_skip_<?php echo $marketplace['instance']->getId(); ?>" class="value" style="display: none; color: grey;">
                                    &nbsp; <?php echo Mage::helper('M2ePro')->__('Skipped'); ?>
                                </td>
                                <td id="marketplace_title_<?php echo $marketplace['instance']->getId(); ?>" class="value" style="display: none;"><?php echo Mage::helper('M2ePro')->__($marketplace['instance']->getTitle()); ?></td>
                            </tr>

                        <?php } ?>

                    </table>

               </div>
            </div>

        </div>

    <?php } ?>

</div>

</form>
