<?php
/**
 * Quote edit at buyer end.
 *
 * @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
//edit quote at customer end.
$params = $block->getParameters();
$entityId = 0;
if (array_key_exists('id', $params)) {
    $entityId = $params["id"];
}
if ($entityId) {
    $quoteHelper = $block->getHelper();
    $showPrice = (int)$quoteHelper->getConfigShowPrice();
    $quote = $block->getQuoteData($entityId);
    $attachments = $quoteHelper->getQuoteAttachmentsArr($quote->getAttachments());
    $quoteEditStatus = false;
    if ($quote->getStatus()==\Magetop\Quotesystem\Model\Quotes::STATUS_UNAPPROVED) {
        $quoteEditStatus = true;
    }
    $quoteCurrency = $quote->getQuoteCurrency() ? $quote->getQuoteCurrency() : $quoteHelper->getBaseCurrencyCode();
    $currencySymbol = $quoteHelper->getCurrencySymbol($quoteCurrency);
    $storeCurrency = $quoteHelper->getCurrentCurrency();
    ?>
    <div class="mt_design block">
        <fieldset class="fieldset info mt-fieldset">
            <div class="grid">
                <div class="hor-scroll">
                    <input type="hidden"
                        class="len"
                        name="id"
                        id="id"
                        value="<?= $escaper->escapeHtml($entityId); ?>"
                    />
                    <?php   $product = $block->getProductData($quote->getProductId());
                            $imageUrl = $block->imageHelperObj()
                                        ->init($product, 'product_page_image_small')
                                        ->setImageFile($product->getFile())
                                        ->getUrl();
                            $minqty = $block->getMinQuoteQuatity($quote->getProductId());
                    ?>

                    <table class="data-table border mt-list-table">
                        <thead>
                            <tr class="first last">
                                <th rowspan="1">
                                    <span><?= $escaper->escapeHtml(__("Product")); ?></span>
                                </th>
                                <th rowspan="1">&nbsp;</th>
                                 
                                    <th colspan="1" class="a-right cart-price-head">
                                        <?php if ($showPrice) {?>
                                            <span title="<?= $escaper->escapeHtml(__('Price After Configuration')); ?>">
                                                <?= $escaper->escapeHtml(__("Price")); ?>
                                            </span>
                                        <?php } ?>
                                    </th>
                               
                            </tr>
                        </thead>
                        <tbody>
                            <tr class="first odd">
                                <td class="product-cart-image">
                                    <img alt="<?= $escaper->escapeHtml($product->getName()); ?>"
                                        src="<?= $escaper->escapeUrl($imageUrl) ?>"
                                    >
                                </td>
                                <td class="product-cart-info productName">
                                    <h2 class="product-name">
                                        <?php
                                        if ($quoteHelper->checkProductCanShowOrNot($product)): ?>
                                            <a href="<?= $escaper->escapeUrl($product->getProductUrl())?>"
                                             target="blank">
                                            <?= $escaper->escapeHtml($product->getName()); ?>
                                            </a>
                                        <?php else: ?>
                                            <?= $escaper->escapeHtml($quote->getProductName()); ?>
                                        <?php endif;?>
                                    </h2>
                                    <div class="product-cart-sku">
                                        <span class="label"><?= /* @noEscape */ __("SKU");?> : </span>
                                        <?= $escaper->escapeHtml($product->getSku()); ?>
                                    </div>
                                    <?php
                                        $optionAndPrice = $quoteHelper->getOptionNPrice($product, $quote);
                                        $optionAndPriceArr = explode("~|~", $optionAndPrice);
                                    ?>
                                    <?php if (isset($optionAndPriceArr)) { ?>
                                        <dl class="item-options"><?= /* @noEscape */ $optionAndPriceArr[0]; ?></dl>
                                <?php } ?>
                                </td>
                                <td class="a-right">
                                   
                                        <span class="cart-price">
                                            <span class="price">
                                                <?php if ($showPrice) {?> 
                                                    <?= /* @noEscape */ $quoteHelper->getformattedPrice(
                                                        $quote->getProductPrice()
                                                    ); ?> <?php } ?>
                                            </span>
                                        </span>
                                    
                                </td>
                            </tr>
                            <tr class="last even">
                                <td class="mt_quo_des" colspan="1">
                                    <b>
                                        <?= /* @noEscape */ __("Quote Description")." : "; ?>
                                    </b>
                                </td>
                                <td colspan="2">
                                    <?= $escaper->escapeHtml($quote->getQuoteDesc()); ?>
                                </td>
                            </tr>
                            <tr class="last even">
                                <td class="mt_quo_des" colspan="1">
                                    <b>
                                        <?= /* @noEscape */ __("Quote Attachments")." : "; ?>
                                    </b>
                                </td>
                                <td colspan="2">
                                    <?php
                                    if (count($attachments)) {
                                        foreach ($attachments as $attachmentKey => $attachmentVal) {
                                            $attachmentUrl = $quoteHelper->getMediaUrl().'mtquote\files'.$attachmentKey;
                                            ?>
                                            <div>
                                                <a href="<?= $escaper->escapeUrl($attachmentUrl)?>" 
                                                    target="blank" 
                                                    title="<?= $escaper->escapeHtml($attachmentVal)?>">
                                                    <?= $escaper->escapeHtml($attachmentVal)?>
                                                </a>
                                            </div>
                                            <?php
                                        }
                                    } else { ?>
                                         <?= $escaper->escapeHtml(__("NA"))?>
                                    <?php } ?>
                                </td>
                            </tr>
                            <tr class="last even">
                                <td class="mt_quo_des" colspan="1">
                                    <b>
                                        <?= /* @noEscape */ __("Initial Quote Price")." : "; ?>
                                    </b>
                                </td>
                                <td colspan="2">
                                    <?= $escaper->escapeHtml($currencySymbol.
                                    number_format($quote->getInitialQuotePrice(), 2));
                                    ?>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </fieldset>
    </div>
    <?php if (count($block->getQuoteConversationCollection())) { ?>
        <div class="mt-design block" id="mt-qs-conversations">
            <fieldset class="fieldset info mt-fieldset">
                <legend class="legend">
                    <span><?= /* @noEscape */ __('Conversations:') ?></span>
                </legend>
            <?php foreach ($block->getQuoteConversationCollection() as $conversation) { ?>
                <div class="mt_qs_onereply">
                <?php if ($conversation->getSender() == $block->getCustomerId()) { ?>
                        <span class='mt_qs_onereply_head'>
                <?php } else { ?>
                        <span class='mt_qs_onereply_head mt_qs_onereply_customer'>
                <?php } ?>
                        <span class="mt_qs_onereply_head_left">
                    <?php
                        $dateToFormat = $block->getFormattedTime($conversation->getCreatedAt()); ?>
                        <?= $escaper->escapeHtml($block->formatDate($dateToFormat, \IntlDateFormatter::MEDIUM, true));?>
                       </span>
                        <span class="mt_qs_onereply_head_right">
                        <?php
                        if ($conversation->getSender() == 0) { ?>
                            <?= $escaper->escapeHtml("Admin"); ?>
                        <?php } else { ?>
                            <?= $escaper->escapeHtml("Me"); ?>
                        <?php } ?>
                        </span>
                    </span>
                    <div class="mt_qs_onereply_cntnt_container">
                        <p class="mt_qs_onereply_cntnt">
                            <?= $escaper->escapeHtml($conversation->getConversation()); ?>
                        </p>
                        <?php
                        $conversationAttachments = $quoteHelper->getQuoteAttachmentsArr(
                            $conversation->getAttachments()
                        );
                        if (count($conversationAttachments)) { ?>
                            <div>
                                <b><?= /* @noEscape */ __('Uploaded files')?> : </b>
                                <?php
                                foreach ($conversationAttachments as $attachmentKey => $attachmentVal) {
                                    $attachmentUrl = $quoteHelper->getMediaUrl().'mtquote\files'.$attachmentKey;
                                    ?>
                                    <div>
                                        <a href="<?= $escaper->escapeUrl($attachmentUrl)?>" 
                                            target="blank" 
                                            title="<?= $escaper->escapeHtml($attachmentVal)?>">
                                            <?= $escaper->escapeHtml($attachmentVal)?>
                                        </a>
                                    </div>
                                    <?php
                                } ?>
                            </div>
                            <?php
                        } ?>
                    </div>
                </div>
            <?php } ?>
            <?= /* @noEscape */ $block->getPagerHtml(); ?>
            </fieldset>
        </div>
    <?php } ?>
    <div class="mt_qs_message_container">
        <form
            autocomplete="off" 
            action="<?= $escaper->escapeUrl($block->getUrl(
                'quotesystem/buyerquote/updatequote'
            )) ?>"
            method="post"
            id="form-quote"
        >
            <input
                type="hidden"
                class='input-text'
                readonly='readonly'
                value="<?= $escaper->escapeHtml($quote->getCustomerId()) ?>"
                name='customer_id'
            />
            <input
                type="hidden"
                class='input-text'
                readonly='readonly'
                value="<?= $escaper->escapeHtml($quote->getProductId()) ?>"
                name='product_id'
            />

            <input type="hidden"
                name="minquote_quantity"
                value="<?= $escaper->escapeHtml($minqty); ?>"
            />
            <div class="mt-design">
                <fieldset class="fieldset info mt-fieldset">
                    <div class="block">
                        <div class="block-title">
                            <strong><?= /* @noEscape */ __('Quote Detail') ?></strong>
                        </div>
                    </div>
                    <?= /* @noEscape */ $block->getBlockHtml('formkey')?>
                        <div class="field required">
                            <label class="label"><?= /* @noEscape */ __('Quoted Price') ."(".$currencySymbol.")" ?>:
                            </label>
                            <div class="control">
                                <?php
                                $quotePrice = $quote->getQuotePrice();
                                $price = number_format($quotePrice, 2);
                                ?>
                                <?php if ($quoteEditStatus) { ?>
                                <input
                                    disabled='disabled'
                                    type="text"
                                    class='required-entry required validate-number 
                                    validate-greater-than-zero input-text'
                                    value="<?= $escaper->escapeHtml($price); ?>"
                                    name='quote_price'
                                    id='quote_price'
                                />
                                <?php } else { ?>
                                <span>
                                    <?= $escaper->escapeHtml($price); ?>
                                </span>
                                <?php } ?>

                            </div>
                        </div>
                        <div class="field required">
                            <label class="label"><?= /* @noEscape */ __('Quoted Quantity') ?>:</label>
                            <div class="control">
                                <?php if ($quoteEditStatus) { ?>
                                    <input
                                    disabled='disabled'
                                    type="text"
                                    class='required-entry input-text required validate-number validate-greater-than-zero
                                    validate-digits-range digits-range-<?= $escaper->escapeHtml($minqty)?>-'
                                    value='<?= $escaper->escapeHtml($quote->getQuoteQty()) ?>'
                                    name='quote_qty'
                                    id='quote_qty'
                                    />
                                    <span class="mt-qs-min-qty"><?=
                                                                $escaper->escapeHtml(
                                                                    __('minimum quote quantity limit is %1', $minqty)
                                                                ); ?></span>
                                <?php } else { ?>
                                    <span><?= $escaper->escapeHtml($quote->getQuoteQty()) ?></span>
                                <?php } ?>
                            </div>
                        </div>
                        <div class="field">
                        <label class="label"></label>
                        <?php if ($quoteEditStatus) { ?>
                        <div class="mt_qs_row">
                            <input type="checkbox" id="mt_qs_update_switch" name="quote_update_switch"/>
                            <label for="mt_qs_update_switch">
                                <?= /* @noEscape */ __("Check To Update Quote"); ?>
                            </label>
                        </div>
                    <?php } ?>
                    </div>
                    <?php if ($block->quoteStatusIsNotSold($quote->getStatus())) { ?>
                        <input type="hidden" name="quote_id" value="<?= $escaper->escapeHtml($entityId);?>">
                            <div class="field required">
                                <label class="label"><?= /* @noEscape */ __("Enter Message") ?></label>
                                <div class="control">
                                    <textarea
                                    placeholder="Write your message here"
                                        id="mt_qs_msg_area"
                                        type="text"
                                        class="required-entry input-text"
                                        name="quote_message"
                                    ></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>
                            </div>
                    <?php } ?>
                </fieldset>
            </div>
        </form>
    </div>
    <legend class="legend" id="legend">
        <?php if ($block->quoteStatusIsNotSold($quote->getStatus())) { ?>
            <span></span>
            <button class="button mt-btn"
                title="<?= /* @noEscape */ __('Update') ?>"
                type="submit"
                id="save_butn"
            >
                <span><span><?= /* @noEscape */ __('Update') ?></span></span>
            </button>
        <?php } ?>
    </legend>
    <div class="buttons-set">
        <p class="back-link">
            <a href="<?= $escaper->escapeUrl($block->getUrl(
                'quotesystem/buyerquote/view/quote_id/' . $quote->getQuoteId(),
                ['_secure' => $block->getRequest()->isSecure()]
            )) ?>"
            class="left">
            &laquo; <?= /* @noEscape */ __('Back') ?>
            </a>
        </p>
    </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
    $formData = [
        'quoteForm'     =>  '#form-quote',
        'saveButton'    =>  '#save_butn',
        'switchOption'  =>  '#mt_qs_update_switch',
        'quotePrice'    =>  '#quote_price',
        'price'         =>  $quotePrice,
        'formatPrice'   =>  $price,
        'quoteQuantity' =>  '#quote_qty',
        '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()]
        )

    ];
    $serializedFormData = $block->getJsonHelper()->jsonEncode($formData);
    // phpcs:disable ?>
    <script type="text/x-magento-init">
        {
            "*": {
                "MtQuotesystem": <?= /* @noEscape */ $serializedFormData; ?>
            }
        }
    </script>
<?php } // phpcs:enable ?>
