<?php
/**
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the GNU General Public License (GPL 3)
 * that is bundled with this package in the file LICENSE.txt
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Payone_Core to newer
 * versions in the future. If you wish to customize Payone_Core for your
 * needs please refer to http://www.payone.de for more information.
 *
 * @category        Payone
 * @package         design_frontend_base_default
 * @subpackage      template
 * @copyright       Copyright (c) 2015 <kontakt@fatchip.de> - www.fatchip.com, Copyright (c) 2016 <support@e3n.de> - www.e3n.de
 * @author          Robert Müller <robert.mueller@fatchip.de>, Tim Rein <tim.rein@e3n.de>
 * @license         <http://www.gnu.org/licenses/> GNU General Public License (GPL 3)
 * @link            http://www.fatchip.com,  http://www.e3n.de
 */

/** @var $this Payone_Core_Block_Payment_Method_Form_Ratepay */
$code = $this->getMethodCode();
$configId = $this->getPaymentConfig()->getId();
$paymentType = Payone_Api_Enum_RatepayType::RPS;
$ratePayShopId = $this->getMatchingRatePayShopId();
$ratePayCurrency = $this->getRatePayCurrency();
$types = $this->getTypes();
$allowedSEPACountries = htmlspecialchars($this->getAllowedSEPACountries());
$accountOwner = $this->getAccountOwner();
$configShowBankData = $this->getPaymentConfig()->getSepaDeShowBankData();
$isAllowedDirectDebit = $this->isAllowedDirectDebit();
$ratePaymentDebitAllowedType = $this->getRateDebitType();
$initialDebitType = $ratePaymentDebitAllowedType == $this::RATE_PAYMENT_DEBIT_ONLY_BANKTRANSFER ? Payone_Api_Enum_RatepayDebitType::BANK_TRANSFER : Payone_Api_Enum_RatepayDebitType::DIRECT_DEBIT;

$sDeviceIdentToken = $this->getRatePayDeviceFingerprint();
$sDeviceIdentId = $this->getRatePayDeviceFingerprintSnippetId();
?>
<fieldset class="form-list">
    <ul id="payment_form_<?php echo $code ?>" style="display:none">
        <p>
            <?php
                echo $this->__("RATEPAY_TNC");
            ?>
        </p>

        <?php $type = array_pop($types); ?>
        <?php echo $type['name']; ?>&nbsp;<span><?php echo $type['fee']; ?></span>
        <input type="hidden" id="<?php echo $code ?>_type" name="payment[payone_ratepay_type]" value="<?php echo $paymentType;?>"/>

        <?php if ($this->isDobRequired()): ?>
            <li class="fields">
                <div class="field">
                    <label for="<?php echo $code ?>_additional_fields_customer_dob_month"
                           class="required"><em>*</em><?php echo $this->__('Date of birth (DD MM YYYY)') ?>
                    </label>
                    <br class="clear"/>
                    <select id="<?php echo $code ?>_additional_fields_customer_dob_day" class="required-entry"
                            style="width: 60px;" onchange="payoneCustomerDobInput('<?php echo $code ?>')">
                        <option value="">--</option>
                        <?php for ($x = 1; $x <= 31; $x++): ?>
                            <option value="<?php echo sprintf('%02d', $x); ?>">
                                <?php echo sprintf('%02d', $x); ?>
                            </option>
                        <?php endfor; ?>
                    </select>
                    <select id="<?php echo $code ?>_additional_fields_customer_dob_month" class="required-entry"
                            style="width: 60px;" onchange="payoneCustomerDobInput('<?php echo $code ?>')">
                        <option value="">--</option>
                        <?php for ($x = 1; $x <= 12; $x++) : ?>
                            <option value="<?php echo sprintf('%02d', $x); ?>">
                                <?php echo sprintf('%02d', $x); ?>
                            </option>
                        <?php endfor; ?>
                    </select>
                    <select id="<?php echo $code ?>_additional_fields_customer_dob_year" class="required-entry"
                            style="width: 60px;" onchange="payoneCustomerDobInput('<?php echo $code ?>')">
                        <option value="">----</option>
                        <?php for ($x = date('Y'); $x > (date('Y') - 100); $x--) : ?>
                            <option value="<?php echo $x; ?>">
                                <?php echo $x; ?>
                            </option>
                        <?php endfor; ?>
                    </select>

                    <input type="hidden" id="<?php echo $code ?>_additional_fields_customer_dob_full"
                            name="payment[payone_customer_dob]" class="validate-18-years">
                </div>
            </li>
        <?php endif;?>
        <?php if ($this->isB2BMode()) { ?>
            <div id="<?php echo $code; ?>_b2b_wrap" class="field">
                <div class="input-box">
                    <label
                            for="<?php echo $code ?>_trade_registry_number"><?php echo $this->__('Company trade registry number') ?>
                        <span class="required">*</span>
                    </label>
                    <br class="clear"/>
                    <input type="text" id="<?php echo $code ?>_trade_registry_number"
                           name="payment[payone_trade_registry_number]"
                           value="" title="" class="required-entry input-text no-submit"/>
                </div>
                <div class="input-box">
                    <label
                            for="<?php echo $code ?>_vat_id"><?php echo $this->__('VAT ID number') ?>
                        <span class="required">*</span>
                    </label>
                    <br class="clear"/>
                    <input type="text" id="<?php echo $code ?>_vat_id"
                           name="payment[payone_vat_id]"
                           value="" title="" class="required-entry input-text no-submit"/>
                </div>
            </div>
        <?php } ?>
        <?php if ($this->isTelephoneRequired()) : ?>
            <li class="fields">
                <div class="field">
                    <label for="<?php echo $code ?>_additional_fields_customer_telephone" class="required">
                        <em>*</em><?php echo $this->__('Telephone') ?>
                    </label>
                    <br class="clear"/>
                    <input type="text" name="payment[payone_customer_telephone]"
                           id="<?php echo $code ?>_additional_fields_customer_telephone"
                           class="input-text required-entry"/>
                </div>
            </li>
        <?php endif;?>

        <div id="<?php echo $code ?>_debit_details">
            <?php if ($ratePaymentDebitAllowedType != $this::RATE_PAYMENT_DEBIT_ONLY_BANKTRANSFER) : ?>
                <li>
                    <div id="ratepay-Header">
                        <div>
                            <p>
                                <a href="#" onclick="toggleRatepayDirectDebitOverlay('<?php echo $code ?>');return false;">
                                    <?php echo $this->__('Read the consent to the SEPA mandate'); ?>
                                </a>
                            </p>
                        </div>
                        <br class="ratepay-ClearFix" />
                        <div id="<?php echo $code ?>_overlay" style="display:none;" class="ratepay-overlay">
                            <a href="#" onclick="toggleRatepayDirectDebitOverlay('<?php echo $code ?>');return false;"
                               style="float:right;font-weight:bold;"><?php echo $this->__('X'); ?></a>
                            <div class="ratepay-FullWidth">
                                <img class="ratepay-LogoImg" src="<?php echo $this->getSkinUrl('images/payone/ratepay_logo_brand_web_rgb_red.png'); ?>" width="150" alt="Ratepay"><br>
                                <img class="ratepay-LogoImg" src="<?php echo $this->getSkinUrl('images/payone/' . $this->__('RATEPAY_TEXT_LOGO')); ?>" width="150" alt="<?php echo $this->__('Direct Debit'); ?>">
                            </div>
                            <br class="ratepay-ClearFix" />
                            <p><?php echo $this->__('Ratepay GmbH, Franklinstraße 28-29, 10587 Berlin'); ?></p>
                            <p><?php echo $this->__('Creditors ID: ') . 'DE39RPY00000568463'; ?></p>
                            <p><?php echo $this->__('Mandate reference: (will be sent after the purchase)'); ?></p><br><br>
                            <?php echo $this->getRatepayDirectDebitAcceptanceText(); ?>
                        </div>
                    </div>
                </li>

                <p>
                    <?php echo $this->__('Please enter your bank details:'); ?>
                </p>

                <li class="fields">
                    <div class="field">
                        <label for="<?php echo $code ?>_account_owner">
                            <?php echo $this->__('Account holder');?>
                        </label>
                        <br class="clear"/>
                        <p><?php echo $accountOwner; ?></p>
                        <input type="hidden" name="payment[payone_account_owner]"
                               id="<?php echo $code ?>_account_owner"
                               value="<?php echo $accountOwner; ?>"/>
                    </div>
                </li>

                <li class="fields">
                    <div class="field">
                        <label for="<?php echo $code ?>_sepa_iban_xxx">
                            <?php echo $this->__('IBAN') ?>
                            <span class="required">*</span>
                        </label>
                        <br class="clear"/>
                        <input type="text" id="<?php echo $code ?>_sepa_iban_xxx"
                               title="<?php echo $this->__('IBAN') ?>"
                               class="validate-sepa-iban required-entry input-text"
                               value="<?php echo $this->strToXXX($this->getSavedCustomerData('payone_sepa_iban'));?>"
                               autocomplete="off" maxlength="34"
                               onchange="inputToUppaerCaseAndNumbers(this); copyDebitPaymentSepaIban('<?php echo $code ?>'); checkIbanSEPACode('<?php echo $code ?>','<?php echo $allowedSEPACountries; ?>');"
                               oninput="inputToUppaerCaseAndNumbers(this); copyDebitPaymentSepaIban('<?php echo $code ?>'); checkIbanSEPACode('<?php echo $code ?>', '<?php echo $allowedSEPACountries; ?>');toggleBicField(this, '<?php echo $code ?>')"/>
                        <input type="hidden" id="<?php echo $code ?>_sepa_iban" name="payment[payone_sepa_iban]"
                               value="<?php echo $this->getSavedCustomerData('payone_sepa_iban');?>">
                    </div>
                </li>

                <li class="fields" id="<?php echo $code ?>_section_sepa_bic">
                    <div class="field">
                        <label for="<?php echo $code ?>_sepa_bic">
                            <?php echo $this->__('BIC') ?>
                        </label>
                        <br class="clear"/>
                        <input type="text" id="<?php echo $code ?>_sepa_bic" name="payment[payone_sepa_bic]"
                               title="<?php echo $this->__('BIC') ?>"
                               class="validate-sepa-bic input-text"
                               value="<?php echo $this->getSavedCustomerData('payone_sepa_bic');?>"
                               autocomplete="off" maxlength="11"
                               onchange="inputToUppaerCaseAndNumbers(this); blockPaymentMethodInputs('<?php echo $code ?>', <?php echo $configShowBankData; ?>);"
                               oninput="inputToUppaerCaseAndNumbers(this); blockPaymentMethodInputs('<?php echo $code ?>', <?php echo $configShowBankData; ?>);"/>
                    </div>
                </li>
            <?php endif; ?>
        </div>

        <li <?php if ($ratePaymentDebitAllowedType != $this::RATE_PAYMENT_DEBIT_BOTH) { echo 'style="display: none"'; } ?>>
            <p id="method-switch-section" onclick="switchRatePaymentMethod('<?php echo $code ?>')">
                <span id="method-switch-checkbox"></span>
                <a id="<?php echo $code ?>_method_switch_invoice" style="cursor: pointer;">
                    <?php echo $this->__('I would like to make the installment payment myself and not pay by direct debit'); ?>
                </a>
                <input id="<?php echo $code ?>_debit_type" name="payment[payone_ratepay_debit_type]" value="<?php echo $initialDebitType; ?>" type="hidden" />
            </p>
        </li>

        <li style="display:none;">
            <input type="hidden" name="payment[payone_config_payment_method_id]"  id="<?php echo $code ?>_config_id" value="<?php echo $configId; ?>"/>
            <input type="hidden" id="ratePayShopId" name="payment[payone_ratepay_shop_id]" value="<?php echo $ratePayShopId; ?>"/>
            <input type="hidden" id="ratePayCurrency" name="payment[payone_ratepay_currency]" value="<?php echo $ratePayCurrency; ?>"/>
            <input type="hidden" name="payment[payone_isb2b]" value="<?php echo (int)$this->isB2BMode(); ?>"/>
            <input type="hidden" name="payment[payone_country]" value="<?php echo $this->getCountry(); ?>"/>
        </li>

        <li>
            <div id="cover">
                <div id="ajaxLoaderId">
                    <img src="<?php echo Mage::getDesign()->getSkinUrl('/images/payone/ajaxloader.gif') ?>">
                </div>
            </div>
            <div id="ratepay-main-cont" style="<?php echo (count($this->getTypes()) == 1 && $paymentType == 'RPS') ? 'display: block' : 'display: none'; ?>">
                <div id="ratepay-Header">
                    <div class="ratepay-FullWidth">
                        <h2 class="ratepay-H2"><?php echo $this->__('Calculate here your own rate'); ?></h2>
                     </div>
                    <br class="ratepay-ClearFix" />
                </div>
                <div class="container">
                    <div class="row">
                        <div class="col-md-8">
                            <?php echo $this->__('lang_cash_payment_price_part_one'); ?>:
                            <span><b><?php echo Mage::helper('checkout')->formatPrice($this->getAmount(), true, true); ?></b></span>
                            <input type="hidden" id="amount" value="<?php echo $this->getAmount() ?>">
                            <?php echo $this->__('lang_cash_payment_price_part_two'); ?>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-8">
                            <input id="calculationValidationFlag" class="required-entry" type="hidden" />
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-4" style="z-index: 1;">
                            <div class="panel panel-default">
                                <div class="panel-heading text-center" id="firstInput">
                                    <h2><?php echo $this->__('lang_choose_runtime'); ?></h2>
                                    <?php echo $this->__('lang_payment_text_runtime'); ?> <b><?php echo $this->__('lang_runtime_duration_unit'); ?></b>
                                </div>
                                <div class="panel-body">
                                    <div class="btn-group btn-group-justified" role="group" aria-label="...">
                                        <div class="btn-group btn-group-sm" role="group">
                                            <input class="btn btn-default <?php echo $code ?>-btn-runtime" data-bind="3" value="3" readonly="readonly"/>
                                        </div>
                                        <div class="btn-group btn-group-sm" role="group">
                                            <input class="btn btn-default <?php echo $code ?>-btn-runtime" data-bind="6" value="6" readonly="readonly"/>
                                        </div>
                                        <div class="btn-group btn-group-sm" role="group">
                                            <input class="btn btn-default <?php echo $code ?>-btn-runtime" data-bind="12" value="12" readonly="readonly"/>
                                        </div>
                                        <div class="btn-group btn-group-sm" role="group">
                                            <input class="btn btn-default <?php echo $code ?>-btn-runtime" data-bind="24" value="24" readonly="readonly"/>
                                        </div>
                                        <div class="btn-group btn-group-sm" role="group">
                                            <input class="btn btn-default <?php echo $code ?>-btn-runtime" data-bind="36" value="36" readonly="readonly"/>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-4" style="z-index: 1;">
                            <div class="panel panel-default">
                                <div class="panel-heading text-center" id="secondInput">
                                    <h2><?php echo $this->__('lang_insert_wishrate'); ?></h2>
                                    <?php echo $this->__('lang_payment_text_wishrate'); ?>
                                </div>
                                <div class="panel-body">
                                    <div class="input-group input-group-sm">
                                        <span class="input-group-addon">€</span>
                                        <input type="text" id="<?php echo $code ?>-rate" class="form-control" aria-label="Amount" data-com.agilebits.onepassword.user-edited="yes">
                                        <span class="input-group-btn">
                                            <input id="<?php echo $code ?>-btn-rate" class="btn btn-default <?php echo $code ?>-btn-rate" value="<?php echo $this->__('lang_calculate_rate'); ?>" readonly="readonly"/>
                                        </span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-8">
                            <div id="<?php echo $code ?>_ResultContainer">
                            </div>
                        </div>
                    </div>
                </div>
                <br class="ratepay-ClearFix" />
            </div>
        </li>
    </ul>
</fieldset>

<script type="text/javascript">
    //    Add validation messages translation
    Translator.add('You have to be at least 18 years old to use this payment type!', '<?php echo $this->__('You have to be at least 18 years old to use this payment type!'); ?>');

    checkRequirementFields('<?php echo $paymentType;?>', -1);
    attachCalcButtonsListeners(
        '<?php echo $code ?>',
        '<?php echo $this->getUrl("payone_core/ratepay/runtime", array('_secure' => true)); ?>',
        '<?php echo $this->getUrl("payone_core/ratepay/rate", array('_secure' => true)); ?>'
    );

    di = {t: '<?= $sDeviceIdentToken ?>', v: '<?= $sDeviceIdentId ?>', l: 'Checkout'};

    var methodSelector = $('p_method_<?php echo $code ?>');
    if (methodSelector && methodSelector.checked) {
        fetchDFP('<?= $sDeviceIdentId ?>');
    }

    $('dt_method_<?php echo $code ?>').onclick = function() {
        fetchDFP('<?= $sDeviceIdentId ?>');
    }
</script>