<?php
/**
 * Quote product .phtml
 *
 * @category  Magetop Extensions
 * @package   Magetop_Quotesystem
 * @author    Magetop Developer
 * @copyright Copyright (c) Magetop (https://www.magetop.com)
 * @license   https://www.magetop.com/license.html
 */

?>
<?php
// use to allow customer to quote on a product
$product = $block->getProduct();
$status = $product->getQuoteStatus();
$qty = $block->getProductQty();
$quoteHelper = $block->getHelper();
$stockStatus = $quoteHelper->isAllowAddOutOfStockProduct();
if ($product->getQuantityAndStockStatus()['is_in_stock'] == 1) {
    $stockStatus = 1;
}

$currencySymbol = $quoteHelper->getCurrentCurrencySymbol();
$showCart = (int)$quoteHelper->getConfigAddToCart();
$showPrice = (int)$quoteHelper->getConfigShowPrice();

if (($status == 1) && $stockStatus == 1) {
    $minqty = 0;
    if ($product->getMinQuoteQty()):
        $minqty = $product->getMinQuoteQty();
    else:
        $minqty = $quoteHelper->getConfigMinQty();
    endif; ?>
    <button
        type="button"
        title="<?= /* @noEscape */ __("Add to Quote");?>"
        class="mt_qs_quote_button button btn-cart action primary">
        <span><span><?= /* @noEscape */ __("Add to Quote"); ?></span></span>
    </button>
    <?php
    $finalPrice = 0;
    if ($product->getSpecialPrice() != "") {
        $finalPrice = $product->getSpecialPrice();
    } else {
        $finalPrice = $product->getPrice();
    } ?>
    <div class="ask-que popover_background">
        <div id="mt-qs-ask-data">
            <!-- <div class="modals-wrapper"> -->
                <div tabindex="0"
                    data-type="popup"
                    data-role="modal"
                    class="modal-popup modal-slide _inner-scroll mt-mp-model-popup">
                    <div tabindex="0" data-role="focusable-start"></div>
                    <div data-role="focusable-scope" class="modal-inner-wrap mt-qs-inner-wrap">
                        <header class="modal-header">
                            <h4 class="modal-title"><?= /* @noEscape */ __('Enter Quote Details') ?></h4>
                            <!-- <span  class="mt-close" >x</span> -->
                            <button type="button"
                                data-role="closeBtn"
                                class="popover_close action-close mt-close"
                            >
                                <span>Close</span>
                            </button>
                            <span  class="mt-clear" ></span>
                        </header>
                        <div class="modal-body form-list field required">
                        <fieldset class="fieldset info mt-mp-fieldset">
                            <input type="hidden"
                                name="original_price"
                                value="<?= $escaper->escapeHtml($finalPrice); ?>"
                            />
                            <input type="hidden"
                                name="product_name"
                                value="<?= $escaper->escapeHtml($product->getName()); ?>"
                            />
                            <input type="hidden"
                                name="current_url"
                                value="<?= $escaper->escapeUrl($block->getCurrentUrl()); ?>"
                            />
                            <input type="hidden"
                                name="product_quantity"
                                value="<?= $escaper->escapeHtml($qty); ?>"
                            />
                            <div class="field required">
                                <label class="label"><?= /* @noEscape */ __("Quantity"); ?> :</label>
                                <div class="control">
                                    <input type="text"
                                        class="queryquestion mt-contact_input_fields 
                                        required-entry validate-digits validate-number 
                                        validate-greater-than-zero validate-digits-range 
                                        digits-range-<?= $escaper->escapeHtml($minqty)?>- quote_qty"
                                        id="quote_qty"
                                        name="quote_qty" value=""
                                        data-validate="{required:true,
                                            'validate-number':true,
                                            'validate-digits':true,
                                            'validate-greater-than-zero':true}"
                                    />
                                    <span class="mt-qs-min-qty">
                                        <?php if ($minqty) { ?>
                                            <?= /* @noEscape */ __("minimum quote quantity limit is %1", $minqty); ?> 
                                        <?php }?>
                                    </span>
                                </div>
                            </div>
                            <div class="field required">
                                <label class="label">
                                    <?= /* @noEscape */ __("Price per Item")."( ".$currencySymbol." )"; ?> :
                                </label>
                                <div class="control">
                                    <input type="text"
                                        class="queryquestion mt-contact_input_fields 
                                        required-entry validate-greater-than-zero validate-number quote_price"
                                        id="quote_price"
                                        name="quote_price"
                                        data-validate="{required:true,
                                            'validate-number':true,
                                            'validate-greater-than-zero':true}"
                                    />
                                </div>
                            </div>
                            <div class="field required">
                                <label class="label"><?= /* @noEscape */ __("Description"); ?> :</label>
                                <div class="control">
                                    <textarea
                                        class="queryquestion mt-contact_input_fields required-entry quote_description"
                                        id='quote_description'
                                        name="quote_description"
                                        rows="10"
                                        data-validate="{required:true}"
                                    ></textarea>
                                </div>
                            </div>
                            <div class="mt-file-field-container">
                                <label class="label mt-file-label mt-quote-primary-btn">
                                    <?= /* @noEscape */ __('Add Attachment(s)') ?>
                                    <input type="file" name="files" class="mt-file-field" id="mt-file-field" 
                                    data-url="<?=
                                              $escaper->escapeUrl(
                                                  $block->getUrl(
                                                      'quotesystem/buyerquote/fileUpload',
                                                      ['_secure' => $block->getRequest()->isSecure()]
                                                  )
                                              ) ?>" multiple="multiple"/>
                                </label>
                                <span id="allowed-file-type" class="allowed-file-type">
                                    <?= $escaper->escapeHtml(
                                        "Allowed file type(s) *".$block->getAllowedFileTypes()."*"
                                    );?>
                                </span>
                            </div>
                        </fieldset>
                        </div>
                        <div class="modal-footer mt_footer">
                            <span class="error"></span>
                            <span class="errormail"></span>
                            <input type="reset"
                                value="<?= /* @noEscape */ __('Reset') ?>"
                                id="resetbtn"
                                class="mt-btn mt-btn_default mt_reset"
                            />
                            <input type="button"
                                value="<?= /* @noEscape */ __('Submit') ?>"
                                id="askbtn"
                                class="submit_button mt-btn mt-btn-primary clickask"
                            />
                            <span class="mt-clear"></span>
                        </div>
                    </div>
                    <div tabindex="0" data-role="focusable-end"></div>
                </div>
            <!-- </div> -->
        </div>
    </div>
    <script id="mt-file-field-template" type="text/x-magento-template">
        <div class="mt-uploaded-file">
            <span class="mt-uploaded-file-style mt-quote-primary-btn"><%- data.extension %></span>
            <span class="mt-uploaded-file-name"><%- data.name %></span>
            <input type="hidden" name="attachments[]" value="<%- data.file %>" class="mt-uploaded-file-value"/>
            <span class="mt-uploaded-file-del"></span>
        </div>
    </script>
    <script id="mt-file-field-uploader-template" type="text/x-magento-template">
        <div id="<%- data.id %>" class="mt-uploaded-file-row">
            <img src="<?= /* @noEscape */ $block->getViewFileUrl('Magetop_Quotesystem/images/ajax-loader-big.gif')?>"/>
        </div>
    </script>
<?php } ?>
<a class="proceed-to-checkout button primary" style="display:none">
    <?= /* @noEscape */ __('Login') ?>
</a>
<?php
if ($product->getQuantityAndStockStatus()['is_in_stock'] == false) {
    $stockStatus = 0;
}
$formData = [
    'quoteButtonHtml'       =>  '.mt_qs_quote_button',
    'popoverbackgroundhtml' =>  '.popover_background',
    'productAddToCartForm'  =>  '#product_addtocart_form',
    'saveQuoteUrl'          =>  $block->getUrl(
        'quotesystem/buyerquote/savequote',
        ['_secure' => $block->getIsSecure()]
    ),
    'quote_description'     =>  '#quote_description',
    'quote_price'           =>  '#quote_price',
    'quote_qty'             =>  '#quote_qty',
    'redirectUrl'              =>  $block->getUrl('quotesystem/buyerquote/redirectAfterLogin/'),
    'addToCartAction'       =>  '.box-tocart .actions',
    'popOverclose'          =>  '.popover_close',
    'submitButton'          =>  '.submit_button',
    'paypal'                =>  '.paypal',
    'reset'                 =>  '#resetbtn',
    'loginurl'              =>  $block->getUrl('customer/account/'),
    'maxWidth'              =>  \Magento\Framework\File\Uploader::MAX_IMAGE_WIDTH,
    'maxHeight'             =>  \Magento\Framework\File\Uploader::MAX_IMAGE_HEIGHT,
    'fileDeleteUrl'         =>  $block->getUrl(
        'quotesystem/buyerquote/fileDelete',
        ['_secure' => $block->getRequest()->isSecure()]
    ),
    'stockStatus'          => $stockStatus,
    'showCart'          => $showCart,
    'showPrice'         => $showPrice
];
$serializedFormData = $block->getJsonHelper()->jsonEncode($formData);
// phpcs:disable ?>
<script type="text/x-magento-init">
    {
        "*": {
            "MtQuotesystem": <?= /* @noEscape */ $serializedFormData; ?>
        }
    }
</script>
<?php // phpcs:enable ?>
