<?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         Payone_Core_Block
 * @subpackage      Payment
 * @copyright       Copyright (c) 2018 <kontakt@fatchip.de> - www.fatchip.com
 * @author          Vincent Boulanger <vincent.boulanger@fatchip.de>
 * @license         <http://www.gnu.org/licenses/> GNU General Public License (GPL 3)
 * @link            http://www.fatchip.com
 */
?>

<?php
$_htmlId = $this->getHtmlId() ? $this->getHtmlId() : '_' . uniqid();

$_colspan = 2;
if (!$this->_addAfter) {
    $_colspan -= 1;
}
$_colspan = $_colspan > 1 ? 'colspan="' . $_colspan . '"' : '';
?>

<div class="grid" id="grid<?php echo $_htmlId ?>">
    <table cellpadding="0" cellspacing="0" class="border">
        <tbody>
            <tr class="headings" id="headings<?php echo $_htmlId ?>">
                <?php foreach ($this->_columns as $columnName => $column):?>
                    <th><?php echo $column['label'] ?></th>
                <?php endforeach;?>
                <th <?php echo $_colspan?>></th>
            </tr>
            <?php $aArrayRows = $this->getArrayRows(); ?>
            <?php if (empty($aArrayRows)) { ?>
                <?php
                    $aTime = explode(' ', microtime());
                    $sMilli = substr($aTime[0], 2, 3);
                    $sId = '_' . $aTime[1] . $sMilli . '_' . $sMilli;
                ?>
                <tr id="<?php echo $sId; ?>">
                    <td>
                        <input type="text" style="width:60px;" class="input-text required-entry" value="" name="groups[template_ratepay_direct_debit][fields][ratepay_config][value][<?php echo $sId; ?>][ratepay_shopid]">
                        <span class="required">*</span>
                    </td>
                    <td>
                        <input type="text" style="width:60px;" class="input-text required-entry" value="" name="groups[template_ratepay_direct_debit][fields][ratepay_config][value][<?php echo $sId; ?>][ratepay_currency]">
                        <span class="required">*</span>
                    </td>
                </tr>
            <?php } else { ?>
                <?php $blFirstRow = true; ?>
                <?php foreach ($aArrayRows as $_rowId => $_row): ?>
                    <?php $aRow = $_row->getData(); ?>
                    <tr id="<?php echo $aRow['_id']; ?>">
                        <td>
                            <input type="text" style="width:60px;" class="input-text required-entry" value="<?php echo $aRow['ratepay_shopid']; ?>" name="groups[template_ratepay_direct_debit][fields][ratepay_config][value][<?php echo $aRow['_id']; ?>][ratepay_shopid]">
                            <span class="required">*</span>
                            <br>
                            <?php $aShopConfig = $this->getRatePayShopConfig($aRow['ratepay_shopid'], $aRow['ratepay_currency']); ?>
                            <?php if ($aShopConfig): ?>
                                <div style="padding-top: 4px;">
                                    <table>
                                        <tr>
                                            <td>Invoice Country:</td>
                                            <td><?php echo $aShopConfig['country_code_billing']; ?></td>
                                        </tr>
                                        <tr>
                                            <td>Shipping Country:</td>
                                            <td><?php echo $aShopConfig['country_code_delivery']; ?></td>
                                        </tr>
                                        <tr>
                                            <td>Currency:</td>
                                            <td><?php echo $aShopConfig['currency']; ?></td>
                                        </tr>
                                        <tr>
                                            <td>min. Basket:</td>
                                            <td><?php echo $aShopConfig['tx_limit_invoice_min']; ?></td>
                                        </tr>
                                        <tr>
                                            <td>max. Basket:</td>
                                            <td><?php echo $aShopConfig['tx_limit_invoice_max']; ?></td>
                                        </tr>
                                    </table>
                                </div>
                            <?php else: ?>
                                <?php echo Mage::helper('payone_core')->__('The Ratepay Shop-Config for this ID and currency could not be requested.'); ?>
                            <?php endif;?>
                        </td>
                        <td>
                            <input type="text" style="width:60px;" class="input-text required-entry" value="<?php echo $aRow['ratepay_currency']; ?>" name="groups[template_ratepay_direct_debit][fields][ratepay_config][value][<?php echo $aRow['_id']; ?>][ratepay_currency]">
                            <span class="required">*</span>
                        </td>
                        <td>
                            <?php if (!$blFirstRow) : ?>
                                <button type="button" class="scalable delete" onclick="arrayRow<?php echo $_htmlId ?>.del('<?php echo $aRow['_id']; ?>')"><span><span><span>Delete</span></span></span></button>
                            <?php endif;?>
                        </td>
                        <?php $blFirstRow = false; ?>
                    </tr>
                <?php endforeach;?>
            <?php }?>
            <tr id="addRow<?php echo $_htmlId ?>">
                <td colspan="<?php echo count($this->_columns) ?>"></td>
                <td <?php echo $_colspan?>>
                    <button style="" onclick="" class="scalable add" type="button" id="addToEndBtn<?php echo $_htmlId ?>">
                        <span><span><span><?php echo $this->_addButtonLabel ?></span></span></span>
                    </button>
                </td>
            </tr>
        </tbody>
    </table>
    <input type="hidden" name="<?php echo $this->getElement()->getName() ?>[__empty]" value="" />
</div>
<div id="empty<?php echo $_htmlId ?>">
    <button style="" onclick="" class="scalable add" type="button" id="emptyAddBtn<?php echo $_htmlId ?>">
        <span><span><span><?php echo $this->_addButtonLabel ?></span></span></span>
    </button>
</div>
<?php echo Mage::helper('payone_core')->__('Enter currency in ISO 4217 form - i.e. EUR or USD'); ?>

<script type="text/javascript">
    //<![CDATA[
    // create row creator
    var arrayRow<?php echo $_htmlId ?> = {
        // define row prototypeJS template
        template : new Template(
            '<tr id="#{_id}">'
                <?php foreach ($this->_columns as $columnName => $column):?>
                    +'<td>'
                        +'<?php echo Mage::helper('core')->jsQuoteEscape($this->_renderCellTemplate($columnName)) ?>'
                    +'<\/td>'
                <?php endforeach;?>
                +'<td><button onclick="arrayRow<?php echo $_htmlId ?>.del(\'#{_id}\')" class="scalable delete" type="button"><span><span><span><?php echo Mage::helper('core')->jsQuoteEscape(Mage::helper('adminhtml')->__('Delete')) ?><\/span><\/span><\/span><\/button><\/td>'
            +'<\/tr>'
        ),
        rowsCount : 1,
        add : function(templateData, insertAfterId) {
            // generate default template data
            if ('' == templateData) {
                var d = new Date();
                var templateData = {
                    <?php foreach ($this->_columns as $columnName => $column):?>
                        <?php echo $columnName ?> : '',
                    <?php endforeach;?>
                    _id : '_' + d.getTime() + '_' + d.getMilliseconds()
                };
            }

            // insert before last row
            if ('' == insertAfterId) {
                Element.insert($('addRow<?php echo $_htmlId ?>'), {before: this.template.evaluate(templateData)});
            } else { // insert after specified row
                Element.insert($(insertAfterId), {after: this.template.evaluate(templateData)});
            }

            // Turns the shopid and currency newly created fields as required
            var inputs = $(templateData._id).getElementsByTagName('input');
            for (var i = 0, len = inputs.length; i < len; i++) {
                inputs[i].classList.add('required-entry');
            }

            this.rowsCount += 1;
        },
        del : function(rowId) {
            $(rowId).remove();
            this.rowsCount -= 1;
            if (0 == this.rowsCount) {
                this.showButtonOnly();
            }
        },
        showButtonOnly : function() {
            $('grid<?php echo $_htmlId ?>').hide();
            $('empty<?php echo $_htmlId ?>').show();
        }
    }

    // bind add action to "Add" button in last row
    Event.observe('addToEndBtn<?php echo $_htmlId ?>', 'click', arrayRow<?php echo $_htmlId ?>.add.bind(arrayRow<?php echo $_htmlId ?>, '', ''));

    arrayRow<?php echo $_htmlId; ?>.rowsCount += <?php echo count($this->getArrayRows()); ?>;

    // initialize standalone button
    $('empty<?php echo $_htmlId ?>').hide();
    Event.observe('emptyAddBtn<?php echo $_htmlId ?>', 'click', function () {
        $('grid<?php echo $_htmlId ?>').show();
        $('empty<?php echo $_htmlId ?>').hide();
        arrayRow<?php echo $_htmlId ?>.add('', '');
    });

    // toggle the grid, if element is disabled (depending on scope)
    <?php if ($this->getElement()->getDisabled()):?>
    toggleValueElements({checked:true}, $('grid<?php echo $_htmlId ?>').parentNode);
    <?php endif;?>

    Object.extend(Validation, {
        insertAdvice : function(elm, advice){
            var referenceElement = elm.parentNode.getElementsByClassName('required')[0];
            if (!referenceElement) {
                referenceElement = elm;
            }
            Element.insert(referenceElement, {'after': advice});
        }
    });
    //]]>
</script>
