<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

/**
 * @var \Magento\Authorizenet\Block\Transparent\Iframe $block
 * @see \Magento\Authorizenet\Block\Transparent\Iframe
 */
$code = $block->escapeHtml($block->getMethodCode());
$method = $block->getMethod();
$controller = $block->escapeHtml($block->getRequest()->getControllerName());
$orderUrl = $block->escapeUrl($block->getHelper('adminhtml')->getPlaceOrderAdminUrl());
$ccType = $block->getInfoData('cc_type');
$ccExpMonth = $block->getInfoData('cc_exp_month');
$ccExpYear = $block->getInfoData('cc_exp_year');
?>
<!-- IFRAME for request to our server -->
<iframe id="order-directpost-iframe" allowtransparency="true" frameborder="0" name="iframeSubmitOrder"
        style="display:none;width:100%;background-color:transparent"
        src="<?= $block->escapeUrl($block->getViewFileUrl('blank.html')) ?>">
</iframe>
<!-- IFRAME for request to Authorize.net -->
<iframe id="directpost-iframe" allowtransparency="true" frameborder="0"  name="iframeDirectPost"
        style="display:none;width:100%;background-color:transparent"
        src="<?= $block->escapeUrl($block->getViewFileUrl('blank.html')) ?>">
</iframe>
<fieldset class="admin__fieldset payment-method" id="payment_form_<?= /* @noEscape */ $code ?>"
          style="display:none;">
    <div class="admin__field _required">
        <label for="<?= /* @noEscape */ $code ?>_cc_type" class="admin__field-label">
            <span><?= $block->escapeHtml(__('Credit Card Type')) ?></span>
        </label>
        <div class="admin__field-control">
            <select id="<?= /* @noEscape */ $code ?>_cc_type" name="payment[cc_type]"
                    class="admin__control-select"
                    data-validate="{
                        'required':true,
                        'validate-cc-type-select':'#<?= /* @noEscape */ $code ?>_cc_number'
                        }">
                <option value=""><?= $block->escapeHtml(__('Please Select')) ?></option>
                <?php foreach ($block->getCcAvailableTypes() as $typeCode => $typeName): ?>
                    <option value="<?= $block->escapeHtmlAttr($typeCode) ?>"
                            <?php if ($typeCode == $ccType): ?>selected="selected"<?php endif; ?>>
                        <?= $block->escapeHtml($typeName) ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </div>
    </div>

    <div class="admin__field _required">
        <label for="<?= /* @noEscape */ $code ?>_cc_number" class="admin__field-label">
            <span><?= $block->escapeHtml(__('Credit Card Number')) ?></span>
        </label>
        <div class="admin__field-control">
            <input type="text" id="<?= /* @noEscape */ $code ?>_cc_number"
                   name="payment[cc_number]"
                   oncopy="return false;"
                   oncut="return false;"
                   onpaste="return false;"
                   data-validate="{
                       'required-number':true,
                       'validate-cc-number':'#<?= /* @noEscape */ $code ?>_cc_type',
                       'validate-cc-type':'#<?= /* @noEscape */ $code ?>_cc_type'
                   }"
                   class="admin__control-text"
                   value="<?= /* @noEscape */ $block->getInfoData('cc_number') ?>"/>
        </div>
    </div>

    <div class="admin__field _required field-date" id="<?= /* @noEscape */ $code ?>_cc_type_exp_div">
        <label for="<?= /* @noEscape */ $code ?>_expiration" class="admin__field-label">
            <span><?= $block->escapeHtml(__('Expiration Date')) ?></span>
        </label>

        <div class="admin__field-control">
            <select id="<?= /* @noEscape */ $code ?>_expiration"
                    name="payment[cc_exp_month]"
                    class="admin__control-select admin__control-select-month"
                    data-validate="{
                        'required':true,
                        'validate-cc-exp':'#<?= /* @noEscape */ $code ?>_expiration_yr'
                        }">
                <?php foreach ($block->getCcMonths() as $k => $v): ?>
                    <option value="<?= $block->escapeHtmlAttr($k) ?>"
                            <?php if ($k == $ccExpMonth): ?>selected="selected"<?php endif; ?>>
                        <?= $block->escapeHtml($v) ?>
                    </option>
                <?php endforeach; ?>
            </select>
            <select id="<?= /* @noEscape */ $code ?>_expiration_yr"
                    name="payment[cc_exp_year]"
                    class="admin__control-select admin__control-select-year"
                    data-container="<?= /* @noEscape */ $code ?>-cc-year"
                    data-validate="{required:true}">
                <?php foreach ($block->getCcYears() as $k => $v): ?>
                    <option value="<?= /* @noEscape */ $k ? $block->escapeHtmlAttr($k) : '' ?>"
                            <?php if ($k == $ccExpYear): ?>selected="selected"<?php endif; ?>>
                        <?= $block->escapeHtml($v) ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </div>
    </div>

    <?php if ($block->hasVerification()): ?>
    <div class="admin__field _required field-cvv">
        <label class="admin__field-label"
               for="<?= /* @noEscape */ $code ?>_cc_cid"
               id="<?= /* @noEscape */ $code ?>_cc_type_cvv_div">
            <span><?= $block->escapeHtml(__('Card Verification Number')) ?></span>
        </label>

        <div class="admin__field-control">
            <input type="text"
                   data-container="<?= /* @noEscape */ $code ?>-cc-cvv"
                   oncopy="return false;"
                   oncut="return false;"
                   onpaste="return false;"
                   title="<?= $block->escapeHtmlAttr(__('Card Verification Number')) ?>"
                   class="admin__control-text cvv"
                   id="<?= /* @noEscape */ $code ?>_cc_cid" name="payment[cc_cid]"
                   value="<?= /* @noEscape */ $block->getInfoData('cc_cid') ?>"
                   data-validate="{
                       'required-number':true,
                       'validate-cc-cvn':'#<?= /* @noEscape */ $code ?>_cc_type'
                   }"
                   autocomplete="off"/>
        </div>
    </div>
    <?php endif; ?>
</fieldset>
<script>
    require([
        'prototype',
        'Magento_Sales/order/create/scripts',
        'Magento_Sales/order/create/form',
        'Magento_Authorizenet/js/direct-post'
    ], function(){

        /**
         * Disable card server validation in admin
         */
        order.addExcludedPaymentMethod('<?= /* @noEscape */ $code ?>');

        directPostModel = new directPost(
            '<?= /* @noEscape */ $code ?>',
            'directpost-iframe',
            '<?= /* @noEscape */ $controller ?>',
            '<?= /* @noEscape */ $orderUrl ?>',
            '<?= $block->escapeUrl($method->getCgiUrl()) ?>',
            '<?= $block->escapeUrl($block->getUrl('*/*/save', [
                '_secure' => $block->getRequest()->isSecure()
            ]));?>');
    });
</script>
