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

// @codingStandardsIgnoreFile

/** @var Ess_M2ePro_Block_Adminhtml_Ebay_Template_ReturnPolicy_Edit_Form_Data $this */

$formData = $this->getFormData();
$default = $this->getDefault();

$formData = array_merge($default, $formData);
$marketplaceData = $this->getMarketplaceData();

?>

<script type="text/javascript">

    var init = function() {
        EbayTemplateReturnObj = new EbayTemplateReturn();

        M2ePro.php.setConstants(<?php echo Mage::helper('M2ePro')->getClassConstantAsJson('Ess_M2ePro_Model_Ebay_Template_ReturnPolicy'); ?>, 'Ess_M2ePro_Model_Ebay_Template_ReturnPolicy');

        $('return_accepted')
            .observe('change', EbayTemplateReturnObj.acceptedChange)
            .simulate('change');

        $('return_international_accepted')
            .observe('change', EbayTemplateReturnObj.internationalAcceptedChange)
            .simulate('change');
    };

    <?php if ($this->getRequest()->isXmlHttpRequest()): ?>
        init();
    <?php else: ?>
        Event.observe(window, 'load', init);
    <?php endif; ?>

</script>

<input type="hidden" name="return_policy[id]" value="<?php echo (!$this->isCustom() && isset($formData['id'])) ? (int)$formData['id'] : ''; ?>" />
<input type="hidden" name="return_policy[title]" value="<?php echo Mage::helper('M2ePro')->escapeHtml($this->getTitle()); ?>" />
<input type="hidden" name="return_policy[marketplace_id]" value="<?php echo (int)$marketplaceData['id']; ?>" />
<input type="hidden" name="return_policy[is_custom_template]" value="<?php echo $this->isCustom() ? 1 : 0; ?>" />

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

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

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

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

                <tr class="return" <?php if (empty($marketplaceData['info']['returns_accepted'])): echo 'style="display: none;"'; endif; ?>>
                    <td class="label">
                        <label for="return_accepted"><?php echo Mage::helper('M2ePro')->__('Return Policy'); ?>: <span class="required">*</span></label>
                    </td>
                    <td class="value">
                        <select id="return_accepted" name="return_policy[accepted]" class="required-entry">
                            <?php if (!empty($marketplaceData['info']['returns_accepted'])): ?>
                                <?php foreach ($marketplaceData['info']['returns_accepted'] as $item): ?>
                                    <?php $selected = $formData['accepted'] == $item['ebay_id']; ?>
                                    <option value="<?php echo $item['ebay_id'] ?>" <?php if ($selected): ?>selected="selected"<?php endif; ?>><?php echo Mage::helper('M2ePro')->escapeHtml($item['title']) ?></option>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </select>
                    </td>
                </tr>

                <tr id="return_option_tr" class="return return-accepted" <?php if (empty($marketplaceData['info']['refund'])):  echo 'style="display: none;"'; endif; ?>>
                    <td class="label">
                        <label for="return_option"><?php echo Mage::helper('M2ePro')->__('Refund Will Be Given As'); ?>:</label>
                    </td>
                    <td class="value">
                        <select id="return_option" name="return_policy[option]">
                            <?php if (!empty($marketplaceData['info']['refund'])): ?>
                                <?php foreach ($marketplaceData['info']['refund'] as $item): ?>
                                    <?php $selected = $formData['option'] == $item['ebay_id']; ?>
                                    <option value="<?php echo $item['ebay_id'] ?>" <?php if ($selected): ?>selected="selected"<?php endif; ?>><?php echo Mage::helper('M2ePro')->escapeHtml($item['title']) ?></option>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </select>
                    </td>
                </tr>

                <tr id="return_within_tr" class="return return-accepted" <?php if (empty($marketplaceData['info']['returns_within'])): echo 'style="display: none;"'; endif; ?>>
                    <td class="label">
                        <label for="return_within"><?php echo Mage::helper('M2ePro')->__('Item Must Be Returned Within'); ?>:</label>
                    </td>
                    <td class="value">
                        <select id="return_within" name="return_policy[within]">
                            <?php if (!empty($marketplaceData['info']['returns_within'])): ?>
                                <?php $daysDefaultValue = $this->getDefaultDaysValueForReturnPolicy($marketplaceData['info']['returns_within'])?>
                                <?php foreach ($marketplaceData['info']['returns_within'] as $item): ?>
                                    <?php
                                        if (empty($formData['within']) && $item['ebay_id'] == $daysDefaultValue) {
                                            $selected = true;
                                        } else {
                                            $selected = $formData['within'] == $item['ebay_id'];
                                        }
                                    ?>
                                    <option value="<?php echo $item['ebay_id'] ?>" <?php if ($selected): ?>selected="selected"<?php endif; ?>><?php echo Mage::helper('M2ePro')->escapeHtml($item['title']) ?></option>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </select>
                    </td>
                </tr>

                <tr id="return_shipping_cost_tr" class="return return-accepted" <?php if (empty($marketplaceData['info']['shipping_cost_paid_by'])): echo 'style="display: none;"'; endif; ?>>
                    <td class="label">
                        <label for="return_shipping_cost"><?php echo Mage::helper('M2ePro')->__('Return Shipping Will Be Paid By'); ?>:</label>
                    </td>
                    <td class="value">
                        <select id="return_shipping_cost" name="return_policy[shipping_cost]">
                            <?php if (!empty($marketplaceData['info']['shipping_cost_paid_by'])): ?>
                                <?php foreach ($marketplaceData['info']['shipping_cost_paid_by'] as $item): ?>
                                    <?php $selected = $formData['shipping_cost'] == $item['ebay_id']; ?>
                                    <option value="<?php echo $item['ebay_id'] ?>" <?php if ($selected): ?>selected="selected"<?php endif; ?>><?php echo Mage::helper('M2ePro')->escapeHtml($item['title']) ?></option>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </select>
                    </td>
                </tr>

            </table>

        </div>
    </div>

</div>

<div class="entry-edit return-accepted" id="magento_block_ebay_template_return_form_data_policy_international">

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

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

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

                <tr class="return" <?php if (empty($marketplaceData['info']['international_returns_accepted'])): echo 'style="display: none;"'; endif; ?>>
                    <td class="label">
                        <label for="return_international_accepted"><?php echo Mage::helper('M2ePro')->__('Return Policy'); ?>: <span class="required">*</span></label>
                    </td>
                    <td class="value">
                        <select id="return_international_accepted" name="return_policy[international_accepted]" class="required-entry">
                            <?php if (!empty($marketplaceData['info']['international_returns_accepted'])): ?>
                                <?php foreach ($marketplaceData['info']['international_returns_accepted'] as $item): ?>
                                    <?php $selected = $formData['international_accepted'] == $item['ebay_id']; ?>
                                    <option value="<?php echo $item['ebay_id'] ?>" <?php if ($selected): ?>selected="selected"<?php endif; ?>><?php echo Mage::helper('M2ePro')->escapeHtml($item['title']) ?></option>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </select>
                    </td>
                </tr>

                <tr id="return_international_option_tr" class="return return-international-accepted" <?php if (empty($marketplaceData['info']['international_refund'])):  echo 'style="display: none;"'; endif; ?>>
                    <td class="label">
                        <label for="return_international_option"><?php echo Mage::helper('M2ePro')->__('Refund Will Be Given As'); ?>:</label>
                    </td>
                    <td class="value">
                        <select id="return_international_option" name="return_policy[international_option]">
                            <?php if (!empty($marketplaceData['info']['international_refund'])): ?>
                                <?php foreach ($marketplaceData['info']['international_refund'] as $item): ?>
                                    <?php $selected = $formData['international_option'] == $item['ebay_id']; ?>
                                    <option value="<?php echo $item['ebay_id'] ?>" <?php if ($selected): ?>selected="selected"<?php endif; ?>><?php echo Mage::helper('M2ePro')->escapeHtml($item['title']) ?></option>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </select>
                    </td>
                </tr>

                <tr id="return_international_within_tr" class="return return-international-accepted" <?php if (empty($marketplaceData['info']['international_returns_within'])): echo 'style="display: none;"'; endif; ?>>
                    <td class="label">
                        <label for="return_international_within"><?php echo Mage::helper('M2ePro')->__('Item Must Be Returned Within'); ?>:</label>
                    </td>
                    <td class="value">
                        <select id="return_international_within" name="return_policy[international_within]">
                            <?php if (!empty($marketplaceData['info']['international_returns_within'])): ?>
                                <?php $daysDefaultValue = $this->getDefaultDaysValueForReturnPolicy($marketplaceData['info']['international_returns_within'])?>
                                <?php foreach ($marketplaceData['info']['international_returns_within'] as $item): ?>
                                    <?php
                                        if (empty($formData['international_within']) && $item['ebay_id'] == $daysDefaultValue) {
                                            $selected = true;
                                        } else {
                                            $selected = $formData['international_within'] == $item['ebay_id'];
                                        }
                                    ?>
                                    <option value="<?php echo $item['ebay_id'] ?>" <?php if ($selected): ?>selected="selected"<?php endif; ?>><?php echo Mage::helper('M2ePro')->escapeHtml($item['title']) ?></option>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </select>
                    </td>
                </tr>

                <tr id="return_international_shipping_cost_tr" class="return return-international-accepted" <?php if (empty($marketplaceData['info']['international_shipping_cost_paid_by'])): echo 'style="display: none;"'; endif; ?>>
                    <td class="label">
                        <label for="return_international_shipping_cost"><?php echo Mage::helper('M2ePro')->__('Return Shipping Will Be Paid By'); ?>:</label>
                    </td>
                    <td class="value">
                        <select id="return_international_shipping_cost" name="return_policy[international_shipping_cost]">
                            <?php if (!empty($marketplaceData['info']['international_shipping_cost_paid_by'])): ?>
                                <?php foreach ($marketplaceData['info']['international_shipping_cost_paid_by'] as $item): ?>
                                    <?php $selected = $formData['international_shipping_cost'] == $item['ebay_id']; ?>
                                    <option value="<?php echo $item['ebay_id'] ?>" <?php if ($selected): ?>selected="selected"<?php endif; ?>><?php echo Mage::helper('M2ePro')->escapeHtml($item['title']) ?></option>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </select>
                    </td>
                </tr>

            </table>

        </div>
    </div>

</div>

<?php if ($this->canShowGeneralBlock()): ?>
<div class="entry-edit return-accepted" id="magento_block_ebay_template_return_form_data_policy_general">

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

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

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

                <tr id="return_description_tr">
                    <td class="label">
                        <label for="return_description"><?php echo Mage::helper('M2ePro')->__('Description'); ?>:</label>
                    </td>
                    <td class="value">
                        <textarea id="return_description" name="return_policy[description]" class="input-text" style="width: 600px; height: 250px;"><?php echo Mage::helper('M2ePro')->escapeHtml($formData['description']); ?></textarea>
                    </td>
                </tr>

            </table>

        </div>
    </div>

</div>
<?php endif; ?>
