<?php
/**
 * Mageplaza
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Mageplaza.com license that is
 * available through the world-wide-web at this URL:
 * https://www.mageplaza.com/LICENSE.txt
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category    Mageplaza
 * @package     Mageplaza_Smtp
 * @copyright   Copyright (c) Mageplaza (https://www.mageplaza.com/)
 * @license     https://www.mageplaza.com/LICENSE.txt
 */

/** @var \Mageplaza\Smtp\Block\Adminhtml\System\Config\Provider $block */
?>
<button class="actions-provider-button" data-trigger="autofill" type="button"
        id="<?= $block->escapeHtml($block->getHtmlId()) ?>_autofill">
    <span><?= /** @noEscape */ __('Load Settings') ?></span>
</button>
<div class="smtp-block-autofill" id="smtp-block-autofill" style="display: none"
     data-bind="mageInit: {
            'Magento_Ui/js/modal/modal':{
                'type': 'slide',
                'title': 'Select SMTP Provider',
                'subTitle': 'Select a Pre-defined SMTP Server, then click Load Settings.',
                'modalClass': 'smtp-autofill-modal',
                'trigger': '[data-trigger=autofill]',
                'wrapperClass': 'smtp-autofill-wrapper',
                'parentModalClass': '_has-modal-custom _has-auth-shown',
                'responsive': true,
                'responsiveClass': 'custom-slide',
                'buttons': []
            }}">
    <div class="accordion">
        <div class="entry-edit form-inline">
            <?php foreach ($block->getOptionProvider() as $id => $provider): ?>
                <div class="section-config">
                    <div class="entry-edit-head admin__collapsible-block">
                        <span id="smtp_provider_<?= /** @noEscape */ $id ?>-link" class="entry-edit-head-link"></span>
                        <a id="smtp_provider_<?= /** @noEscape */ $id ?>-head" href="#smtp_provider_<?= /** @noEscape */ /** @noEscape */ $id ?>-link"
                           onclick="Fieldset.toggleCollapse('smtp_provider_<?= /** @noEscape */ $id ?>'); return false;"><?= /** @noEscape */ $provider['label'] ?></a>
                    </div>
                    <input id="smtp_provider_<?= /** @noEscape */ $id ?>-state" type="hidden">
                    <fieldset class="config admin__collapsible-block" id="smtp_provider_<?= /** @noEscape */ $id ?>">
                        <legend><?= /** @noEscape */ $provider['label'] ?></legend>
                        <table cellspacing="0" class="form-list">
                            <colgroup class="label"></colgroup>
                            <colgroup class="value"></colgroup>
                            <colgroup class="scope-label"></colgroup>
                            <colgroup class=""></colgroup>
                            <tbody>
                            <?php foreach ($provider['info'] as $infoId => $info): ?>
                                <?php $note = isset($provider['note']) ? $provider['note'] : [] ?>
                                <?php if (is_array($info)): ?>
                                    <tr id="row_smtp_provider_<?= /** @noEscape */ $id ?>_<?= /** @noEscape */ $infoId ?>">
                                        <td colspan="4">
                                            <div class="section-config">
                                                <div class="entry-edit-head admin__collapsible-block">
                                                    <span id="smtp_provider_<?= /** @noEscape */ $id ?>_<?= /** @noEscape */ $infoId ?>-link"
                                                          class="entry-edit-head-link"></span>
                                                    <a id="smtp_provider_<?= /** @noEscape */ $id ?>_<?= /** @noEscape */ $infoId ?>-head"
                                                       href="#smtp_provider_<?= /** @noEscape */ $id ?>_<?= /** @noEscape */ $infoId ?>-link"
                                                       onclick="Fieldset.toggleCollapse('smtp_provider_<?= /** @noEscape */ $id ?>_<?= /** @noEscape */ $infoId ?>'); return false;"><?= /** @noEscape */ $info['label'] ?></a>
                                                </div>
                                                <input id="smtp_provider_<?= /** @noEscape */ $id ?>_<?= /** @noEscape */ $infoId ?>-state" type="hidden"
                                                       value="1">
                                                <fieldset class="config admin__collapsible-block"
                                                          id="smtp_provider_<?= /** @noEscape */ $id ?>_<?= /** @noEscape */ $infoId ?>">
                                                    <legend><?= /** @noEscape */ $info['label'] ?></legend>
                                                    <table cellspacing="0" class="form-list">
                                                        <colgroup class="label"></colgroup>
                                                        <colgroup class="value"></colgroup>
                                                        <colgroup class="scope-label"></colgroup>
                                                        <colgroup class=""></colgroup>
                                                        <tbody>
                                                        <?php foreach ($info['info'] as $key => $infoItem): ?>
                                                            <?php $note = isset($info['note']) ? $info['note'] : [] ?>
                                                            <tr id="row_smtp_provider_<?= /** @noEscape */ $id ?>_<?= /** @noEscape */ $infoId ?>_<?= /** @noEscape */ $key ?>">
                                                                <td class="label">
                                                                    <label for="smtp_provider_<?= /** @noEscape */ $id ?>_<?= /** @noEscape */ $infoId ?>_<?= /** @noEscape */ $key ?>">
                                                                        <span><?=  $block->escapeHtml($block->getLabel($key)) ?></span>
                                                                    </label>
                                                                </td>
                                                                <td class="value">
                                                                    <span class="control-value special"><?=  $block->escapeHtml($block->getLabel($infoItem)) ?></span>
                                                                    <?php if (isset($note[$key])): ?>
                                                                        <p class="note"><?= /** @noEscape */ $note[$key] ?></p>
                                                                    <?php endif; ?>
                                                                </td>
                                                                <td></td>
                                                            </tr>
                                                        <?php endforeach; ?>
                                                        <?php if (isset($note['global'])): ?>
                                                            <tr id="row_smtp_provider_<?= /** @noEscape */ $id ?>_<?= /** @noEscape */ $infoId ?>_<?= /** @noEscape */ $key ?>_note">
                                                                <td class="label"></td>
                                                                <td class="value">
                                                                    <p class="note"><?= /** @noEscape */ $note['global'] ?></p>
                                                                </td>
                                                                <td></td>
                                                            </tr>
                                                        <?php endif; ?>
                                                        <tr id="row_smtp_provider_<?= /** @noEscape */ $id ?>_<?= /** @noEscape */ $infoId ?>_<?= /** @noEscape */ $key ?>_apply">
                                                            <td class="label"></td>
                                                            <td class="value">
                                                                <div class="actions actions-apply-autofill">
                                                                    <button class="button-actions-apply-autofill"
                                                                            type="button"
                                                                            data-host="<?= $block->escapeHtmlAttr($info['info']['host']) ?>"
                                                                            data-protocol="<?= $block->escapeHtmlAttr($info['info']['protocol']) ?>"
                                                                            data-port="<?= $block->escapeHtmlAttr($info['info']['port']) ?>"
                                                                            id="row_smtp_provider_<?= /** @noEscape */ $id ?>_<?= /** @noEscape */ $infoId ?>_<?= /** @noEscape */ $key ?>_apply">
                                                                        <span><?= /** @noEscape */ __('Load Settings') ?></span>
                                                                    </button>
                                                                </div>
                                                            </td>
                                                            <td></td>
                                                        </tr>
                                                        </tbody>
                                                    </table>
                                                </fieldset>
                                                <script type="text/javascript">//<![CDATA[
                                                    require(['prototype'], function () {
                                                        Fieldset.applyCollapse('smtp_provider_<?= /** @noEscape */ $id ?>_<?= /** @noEscape */ $infoId ?>');
                                                    });
                                                    //]]>
                                                </script>
                                            </div>
                                        </td>
                                    </tr>
                                    <?php $provider['add_button'] = false; ?>
                                <?php else: ?>
                                    <tr id="row_smtp_provider_<?= /** @noEscape */ $id ?>_<?= /** @noEscape */ $infoId ?>">
                                        <td class="label">
                                            <label for="smtp_provider_<?= /** @noEscape */ $id ?>_<?= /** @noEscape */ $infoId ?>">
                                                <span><?=  $block->escapeHtml($block->getLabel($infoId)) ?></span>
                                            </label>
                                        </td>
                                        <td class="value">
                                            <span class="control-value special"><?=  $block->escapeHtml($block->getLabel($info)) ?></span>
                                            <?php if (isset($note[$infoId])): ?>
                                                <p class="note"><?= /** @noEscape */ $note[$infoId] ?></p>
                                            <?php endif; ?>
                                        </td>
                                        <td></td>
                                    </tr>
                                <?php endif; ?>
                            <?php endforeach; ?>
                            <?php if (!isset($provider['add_button'])): ?>
                                <?php if (isset($note['global'])): ?>
                                    <tr id="row_smtp_provider_<?= /** @noEscape */ $id ?>_<?= /** @noEscape */ $infoId ?>_note">
                                        <td class="label"></td>
                                        <td class="value">
                                            <p class="note"><?= /** @noEscape */ $note['global'] ?></p>
                                        </td>
                                        <td></td>
                                    </tr>
                                <?php endif; ?>
                                <tr id="row_smtp_provider_<?= /** @noEscape */ $id ?>_<?= /** @noEscape */ $infoId ?>_apply">
                                    <td class="label"></td>
                                    <td class="value">
                                        <div class="actions actions-apply-autofill">
                                            <button class="button-actions-apply-autofill" type="button"
                                                    data-host="<?= $block->escapeHtmlAttr($provider['info']['host']) ?>"
                                                    data-protocol="<?= $block->escapeHtmlAttr($provider['info']['protocol']) ?>"
                                                    data-port="<?= $block->escapeHtmlAttr($provider['info']['port']) ?>"
                                                    id="row_smtp_provider_<?= /** @noEscape */ $id ?>_<?= /** @noEscape */ $infoId ?>_apply">
                                                <span><?= /** @noEscape */ __('Load Settings') ?></span>
                                            </button>
                                        </div>
                                    </td>
                                    <td></td>
                                </tr>
                            <?php endif; ?>
                            </tbody>
                        </table>
                    </fieldset>
                    <script type="text/javascript">//<![CDATA[
                        require(['prototype'], function () {
                            Fieldset.applyCollapse('smtp_provider_<?= /** @noEscape */ $id ?>');
                        });
                        //]]>
                    </script>
                </div>
            <?php endforeach; ?>
        </div>
    </div>
</div>
<script type="text/javascript">
    require(['jquery'], function ($) {
        var modalEl = $('#smtp-block-autofill'),
            hostEl = $('#smtp_configuration_option_host'),
            portEl = $('#smtp_configuration_option_port'),
            protocolEl = $('#smtp_configuration_option_protocol');

        $('.button-actions-apply-autofill').on('click', function () {
            hostEl.val($(this).data('host'));
            portEl.val($(this).data('port'));
            protocolEl.val($(this).data('protocol'));

            modalEl.modal('closeModal');
        });
    });
</script>
<style type="text/css">
    #smtp_configuration_option_host {
        width: calc(100% - 135px);
        margin-right: 5px;
    }

    #smtp_configuration_option_host_autofill {
        width: 130px;
        box-sizing: border-box;
    }
</style>
