<?php
/**
 * Webkul Software.
 *
 * @category  Webkul
 * @package   Webkul_Marketplace
 * @author    Webkul
 * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */
$helper = $this->helper(\Webkul\Marketplace\Helper\Data::class);
$captchenable = $helper->getCaptchaEnable();
$partner = $block->getProfileDetail();
$sellerId = $partner->getSellerId();
?>
<div class="wk-mp-seller-profile-box wk-mp-collection-header-bottom-txt">
    <a class="askque"><?= $block->escapeHtml(__('Contact')) ?></a>
    <div class="ask-que">
        <div id="wk-mp-ask-data">
            <div class="modals-wrapper">
                <aside tabindex="0" data-type="popup" data-role="modal" class="modal-popup modal-slide _inner-scroll wk-mp-model-popup">
                    <div tabindex="0" data-role="focusable-start"></div>
                    <div data-role="focusable-scope" class="modal-inner-wrap">
                        <header class="modal-header">
                            <h4 class="modal-title"><?= $block->escapeHtml(__('Contact Seller')) ?></h4>
                            <button type="button" data-role="closeBtn" class="action-close wk-close">
                                <span><?= $block->escapeHtml(__('Close')) ?></span>
                            </button>
                            <span  class="wk-clear" ></span>
                        </header>
                        <?php
                        if ($helper->isCustomerLoggedIn()) {
                            $readonly = "readonly='readonly'";
                            $customer = $helper->getCustomerData();
                            $buyerName = $customer->getName();
                                $buyerEmail = $customer->getEmail();
                        } else {
                            $readonly = '';
                            $buyerName = '';
                            $buyerEmail = '';
                        }
                        ?>
                        <form id="ask-form" method="post" action="#" class="fieldset">
                            <div class="modal-body form-list field required">
                                <label class="label"><?= $block->escapeHtml(__('Your Name')) ?> :</label>
                                <input type="text" name="name" class="queryemail wk-contact_input_fields required-entry" value="<?= $block->escapeHtml($buyerName)?>" <?= /* @noEscape */ $readonly?>/>
                                <label class="label"><?= $block->escapeHtml(__('Your Email')) ?> :</label>
                                <input type="text" name="email" class="queryemail required-entry validate-email wk-contact_input_fields" value="<?= $block->escapeHtml($buyerEmail)?>" <?= /* @noEscape */ $readonly?>/>
                                <label class="label"><?= $block->escapeHtml(__('Subject')) ?> :</label>
                                <input type="text" name="subject" class="wk-contact_input_fields required-entry"/>
                                <label class="label"><?= $block->escapeHtml(__('Your Query')) ?> :</label>
                                <textarea  name="ask" class="wk-full-width queryquestion wk-contact_input_fields required-entry"></textarea>
                                <input type="hidden" name="seller-id" value="<?= $block->escapeHtml($sellerId);?>"/>
                                <?php if ($captchenable) { ?>
                                    <label class="label"><?= $block->escapeHtml(__('Please enter desired result')) ?></label>
                                    <div>
                                        <span>
                                            <label for="wk-mp-captcha"><span id="wk-mp-captchalable1"><?= $block->escapeHtml(rand(1, 20))?></span> + <span id="wk-mp-captchalable2"><?= $block->escapeHtml(rand(1, 20))?></span> =</label>
                                        </span>
                                        <input type="text" class="required-entry wk-contact_input_fields" name="wk-mp-captcha" id="wk-mp-captcha" />
                                    </div>
                                    <?php
                                }?>
                            </div>
                            <div class="modal-footer">
                                <span class="error"></span>
                                <span class="errormail"></span>
                                <input type="reset" value="<?= $block->escapeHtml(__('Reset')) ?>" id="resetbtn" class="wk-btn wk-btn_default"/>
                                <input type="submit" value="<?= $block->escapeHtml(__('Submit')) ?>" id="askbtn" class="wk-btn wk-btn-primary clickask"/>
                                <span class="wk-clear"></span>
                            </div>
                        </form>
                    </div>
                    <div tabindex="0" data-role="focusable-end"></div>
                </aside>
            </div>
        </div>
    </div>
</div>
<script>
    require([
        "jquery",
        "mage/mage"
    ], function($) {
        var askDataForm = $('#ask-form');
        askDataForm.mage('validation', {});

        $('body').append($('#wk-mp-ask-data'));
        $('.askque').click(function() {
            $('#ask-form input,#ask-form textarea').removeClass('mage-error');
            $('.page-wrapper').css('opacity','0.4');
            $('.wk-mp-model-popup').addClass('_show');
            $('#wk-mp-ask-data').show();
        });
        $('.wk-close').click(function() {
            $('.page-wrapper').css('opacity','1');
            $('#resetbtn').trigger('click');
            $('#wk-mp-ask-data').hide();
            $('#ask-form .validation-failed').each(function() {
                $(this).removeClass('validation-failed');
            });
            $('#ask-form .validation-advice').each(function() {
                $(this).remove();
            });
        });
        $('#askbtn').click(function() {
            if (askDataForm.valid()!=false) {
                var thisthis = $(this);
                if (thisthis.hasClass("clickask")) {
                    <?php if ($captchenable) { ?>
                        var total = parseInt($('#wk-mp-captchalable1').text()) + parseInt($('#wk-mp-captchalable2').text());
                        var wk_mp_captcha = $('#wk-mp-captcha').val();
                        if (total != wk_mp_captcha) {
                            $('#wk-mp-captchalable1').text(Math.floor((Math.random()*10)+1));
                            $('#wk-mp-captchalable2').text(Math.floor((Math.random()*100)+1));
                            $('#wk-mp-captcha').val('');
                            $('#wk-mp-captcha').addClass('mage-error');
                            $(this).addClass('mage-error');
                            $('#ask_form .errormail').text("<?= $block->escapeHtml(__(' Wrong verification number. ')) ?>").slideDown('slow').delay(2000).slideUp('slow');
                        } else {
                            thisthis.removeClass('clickask');
                            $('#wk-mp-ask-data').addClass('mail-procss');
                            $.ajax({
                                url:'<?= $block->escapeUrl($block->getUrl("marketplace/seller/sendmail", ["_secure" => $this->getRequest()->isSecure()])) ?>',
                                data:$('#ask-form').serialize(),
                                type:'post',
                                dataType:'json',
                                success:function(d) {
                                    thisthis.addClass('clickask');
                                    $('#wk-mp-ask-data').removeClass('mail-procss')
                                    alert("<?= $block->escapeHtml(__('Your mail has been sent.')) ?>..");
                                    $('.wk-close,#resetbtn').trigger('click');
                                }
                            });
                        }
                    <?php } else { ?>
                        thisthis.removeClass('clickask');
                            $('#wk-mp-ask-data').addClass('mail-procss');
                            $.ajax({
                                url:'<?= $block->escapeUrl($block->getUrl("marketplace/seller/sendmail", ["_secure" => $this->getRequest()->isSecure()])) ?>',
                                data:$('#ask-form').serialize(),
                                type:'post',
                                dataType:'json',
                                success:function(d) {
                                    thisthis.addClass('clickask');
                                    $('#wk-mp-ask-data').removeClass('mail-procss')
                                    alert("<?= $block->escapeHtml(__('Your mail has been sent.')) ?>..");
                                    $('.wk-close,#resetbtn').trigger('click');
                                }
                            });
                    <?php } ?>
                }
                return false;
            }
        });
    });
</script>
