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

/** @var \Magento\Contact\Block\ContactForm $block */

$scopeConfig = \Magento\Framework\App\ObjectManager::getInstance()
    ->get('Magento\Framework\App\Config\ScopeConfigInterface');

$emailActive = $scopeConfig->getValue('afd_email/enable/enable') == 1 && $scopeConfig->getValue('afd_email/forms/contact') == 1;
$phoneActive = $scopeConfig->getValue('afd_phone/enable/enable') == 1 && $scopeConfig->getValue('afd_phone/forms/contact') == 1;

?>
<form class="form contact"
      action="<?= $block->escapeUrl($block->getFormAction()) ?>"
      id="contact-form"
      method="post"
      data-hasrequired="<?= $block->escapeHtmlAttr(__('* Required Fields')) ?>"
      data-mage-init='{"validation":{}}'>
    <fieldset class="fieldset">
        <legend class="legend"><span><?= $block->escapeHtml(__('Write Us')) ?></span></legend>
        <br/>
        <div class="field note no-label"><?= $block->escapeHtml(__('Jot us a note and we’ll get back to you as quickly as possible.')) ?></div>
        <div class="field name required">
            <label class="label" for="name"><span><?= $block->escapeHtml(__('Name')) ?></span></label>
            <div class="control">
                <input name="name" id="name" title="<?= $block->escapeHtmlAttr(__('Name')) ?>"
                       value="<?= $block->escapeHtmlAttr($this->helper(\Magento\Contact\Helper\Data::class)->getPostValue('name') ?: $this->helper(\Magento\Contact\Helper\Data::class)->getUserName()) ?>"
                       class="input-text" type="text" data-validate="{required:true}"/>
            </div>
        </div>
        <div class="field email required">
            <label class="label" for="email"><span><?= $block->escapeHtml(__('Email')) ?></span></label>
            <div class="control">
                <input name="email" id="email" title="<?= $block->escapeHtmlAttr(__('Email')) ?>"
                       value="<?= $block->escapeHtmlAttr($this->helper(\Magento\Contact\Helper\Data::class)->getPostValue('email') ?: $this->helper(\Magento\Contact\Helper\Data::class)->getUserEmail()) ?>"
                       class="input-text" type="email" data-validate="{required:true, 'validate-email':true}"/>
            </div>
        </div>
        <div class="field telephone">
            <label class="label" for="telephone"><span><?= $block->escapeHtml(__('Phone Number')) ?></span></label>
            <div class="control">
                <input name="telephone" id="telephone" title="<?= $block->escapeHtmlAttr(__('Phone Number')) ?>"
                       value="<?= $block->escapeHtmlAttr($this->helper(\Magento\Contact\Helper\Data::class)->getPostValue('telephone')) ?>"
                       class="afd-validate-phone input-text" type="text"/>
            </div>
        </div>
        <div class="field comment required">
            <label class="label"
                   for="comment"><span><?= $block->escapeHtml(__('What’s on your mind?')) ?></span></label>
            <div class="control">
                <textarea name="comment" id="comment" title="<?= $block->escapeHtmlAttr(__('What’s on your mind?')) ?>"
                          class="input-text" cols="5" rows="3"
                          data-validate="{required:true}"><?= $block->escapeHtml($this->helper(\Magento\Contact\Helper\Data::class)->getPostValue('comment')) ?></textarea>
            </div>
        </div>
        <?= $block->getChildHtml('form.additional.info') ?>
    </fieldset>
    <div class="actions-toolbar">
        <div class="primary">
            <input type="hidden" name="hideit" id="hideit" value=""/>
            <button type="submit" title="<?= $block->escapeHtmlAttr(__('Submit')) ?>" class="action submit primary">
                <span><?= $block->escapeHtml(__('Submit')) ?></span>
            </button>
        </div>
    </div>
</form>


<style>
    input.afd-invalid {
        border-color: #ed8380;
    }
</style>

<?php if ($emailActive): ?>
    <script type="text/javascript">
        require(['jquery', 'afdPce', 'domReady!app/Afd/Pce/view/base/templates/afd'], function ($) {


            
            // Initialize AFD email validation, passing options directly
            $('#email').afd('email', { ...window.afdOptions, ...(window.afdEmailOptions || {}) });
        })
    </script>
<?php endif ?>

<?php if ($phoneActive): ?>
    <script type="text/javascript">
        require(['jquery', 'afdPce', 'domReady!app/Afd/Pce/view/base/templates/afd', 'mage/validation'], function ($) {
            // Initialize phone validation with options
            const $phone = $('#telephone')
            
            $phone.afd('phone', { ...window.afdOptions, ...(window.afdPhoneOptions || {}) })

            const $error = $phone.closest('.iti').siblings('.afd-error')
            $(document).on('afd:validateComplete', (e, {valid}) => {
                valid ? $error.hide() : $error.show()
            })

            $.validator.addMethod(
                'afd-validate-phone',
                function (value, element) {
                    return !(element.className.search('afd-invalid') > -1)
                },
                afdOptions.magentoOptions.phone.invalidMessage
            );
        })
    </script>
<?php endif ?>
