<?php
/**
 * Webkul Software.
 *
 * @category  Webkul
 * @package   Webkul_Agorae
 * @author    Webkul
 * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */

// @codingStandardsIgnoreFile

/**
 * Ask question form at product view page
 *
 * @var $block \Webkul\Agorae\Block\Sellerblock
 */
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$moduleManager = $objectManager->create(\Magento\Framework\Module\Manager::class);
if ($moduleManager->isOutputEnabled('Webkul_Marketplace')) {
$helper = $this->helper('Webkul\Marketplace\Helper\Data');

$agoraeHelper = $this->helper('Webkul\Agorae\Helper\Data');

$isMpSellerCommunicationModule = $agoraeHelper->isModuleEnabled('Webkul_MpSellerBuyerCommunication');
$isMpSellerCommunicationOutput = $agoraeHelper->isOutputEnabled('Webkul_MpSellerBuyerCommunication');

if ($isMpSellerCommunicationModule && $isMpSellerCommunicationOutput) {
    echo $block->setTemplate(
        'Webkul_MpSellerBuyerCommunication::wk_block.phtml'
    )->toHtml();
}else {
    $_product=$block->getProduct();
    $seller_id = '';
    $marketplaceProduct = $helper->getSellerProductDataByProductId($_product['entity_id']);
    foreach ($marketplaceProduct as $value) {
        $seller_id = $value['seller_id'];
    }
    if ($seller_id!="") {
        $captchenable = $helper->getCaptchaEnable();
        $rowsocial = $helper->getSellerDataBySellerId($seller_id);
        $shoptitle = '';
        $shop_url = '';
        foreach ($rowsocial as $value) {
            $shoptitle = $value['shop_title'];
            $shop_url = $value['shop_url'];
            if(!$shoptitle){
                $shoptitle = $value->getShopUrl();
            }
        }
        $feeds=$helper->getFeedTotal($seller_id);
        ?>
        <div class="wk-seller-block wk-block no-display" id="mp-wk-block">
            <div class="">
                <div class="ask_que">
                    <a id="askque"><?php echo __('Ask Question') ?></a>
                </div>
                <?php echo $block->getChildHtml(); ?>
            </div>
        </div>
        <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"><?php echo __('Contact Seller') ?></h4>
                                <button type="button" data-role="closeBtn" class="action-close wk-close">
                                    <span>Close</span>
                                </button>
                                <span  class="wk-clear" ></span>
                            </header>
                            <?php
                            if ($block->Session->isLoggedIn()) {
                                $buyer_name = $block->Session->getCustomer()->getName();
                                $buyer_email = $block->Session->getCustomer()->getEmail();
                                $readonly = "readonly='readonly'";
                            }else {
                                $buyer_email = '';
                                $buyer_name = '';
                                $readonly = '';
                            }
                            ?>
                            <form id="ask-form" method="post" action="#" class="fieldset">
                                <div class="modal-body form-list field required">
                                    <label class="label"><?php echo __('Your Name') ?> :</label>
                                    <input type="text" name="name" class="queryemail wk-contact_input_fields required-entry" value="<?php echo $buyer_name ?>" <?php echo $readonly ?>/>
                                    <label class="label"><?php echo __('Your Email') ?> :</label>
                                    <input type="text" name="email" class="queryemail required-entry validate-email wk-contact_input_fields" value="<?php echo $buyer_email ?>" <?php echo $readonly ?>/>
                                    <label class="label"><?php echo __('Subject') ?> :</label>
                                    <input type="text" name="subject" class="wk-contact_input_fields required-entry"/>
                                    <label class="label"><?php echo __('Your Query') ?> :</label>
                                    <textarea  name="ask" class="queryquestion wk-contact_input_fields required-entry" style="width:100%;"></textarea>
                                    <input type="hidden" name="seller-id" value="<?php echo $seller_id; ?>"/>
                                    <input type="hidden" name="product-id" value="<?php echo $_product['entity_id']; ?>" />
                                    <?php
                                    if($captchenable){?>
                                        <div>
                                            <span>
                                                <label for="wk-mp-captcha"><span id="wk-mp-captchalable1"><?php echo rand(1,20) ?></span> + <span id="wk-mp-captchalable2"><?php echo 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="seccessmail"></span>
                                    <span class="error"></span>
                                    <span class="errormail"></span>
                                    <input type="reset" value="<?php echo __('Reset') ?>" id="resetbtn" class="wk-btn wk-btn_default"/>
                                    <input type="submit" value="<?php echo __('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>
        <script>
            require([
                "jquery",
                'Magento_Ui/js/modal/alert',
                'mage/translate',
                "mage/mage"
            ], function($, alert, __){
                var askDataForm = $('#ask-form');
                askDataForm.mage('validation', {});

                $('.product-social-links').after($('#mp-wk-block'));
                $('#mp-wk-block').show();

                $('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("<?php echo __(' Wrong varification number. ') ?>").slideDown('slow').delay(2000).slideUp('slow');
                                }else{
                                    thisthis.removeClass('clickask');
                                    $('#wk-mp-ask-data').addClass('mail-procss');
                                    $.ajax({
                                        url:'<?php echo $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({
                                                content : __(' Your mail has been sent '),
                                                actions: {
                                                            always: function () {
                                                                $('.wk-close,#resetbtn').trigger('click');
                                                                $('.modals-overlay').remove();
                                                                $('#wk-mp-ask-data').parent().removeClass('_has-modal');
                                                            }
                                                        }
                                            });
                                        }
                                    });
                                }
                            <?php
                            }else { ?>
                                thisthis.removeClass('clickask');
                                    $('#wk-mp-ask-data').addClass('mail-procss');
                                    $.ajax({
                                        url:'<?php echo $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({
                                                content : __(' Your mail has been sent '),
                                                actions: {
                                                            always: function () {
                                                                $('.wk-close,#resetbtn').trigger('click');
                                                                $('.modals-overlay').remove();
                                                                $('#wk-mp-ask-data').parent().removeClass('_has-modal');
                                                            }
                                                        }
                                            });
                                        }
                                    });
                                <?php
                            } ?>
                        }
                        return false;
                    }
                });
            });
        </script>
    <?php
    }
}
} ?>
