<?php
/**
 * Quote list 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
 */
?>
<head>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<?php
$quoteHelper = $block->getHelper();
$quoteData = $block->getQuotesCollection();
$quote = null;
$baseCurrency = $quoteHelper->getBaseCurrencyCode();
$storeCurrency = $quoteHelper->getCurrentCurrency();
$generateUrl = $block->getUrl(
    'quotesystem/sidebar/generateQuote/',
    [
        '_secure' => $block->getIsSecure()
    ]
);
?>

<div class="table-wrapper mt-quote-buyer-list"> 
    <?php if (!$quoteData->getSize()): ?>
        <div class="fieldset mt_fieldset">
            <div class="mt_emptymsg">
                <?= $escaper->escapeHtml(__('No Cart Quotes Available')) ?>
            </div>
        </div>
    <?php else: ?> 
        <div class="actions" >
            <div class="primary">
                <button
                    id="top-cart-btn-checkout"
                    type="button"
                    class="action primary checkout"
                >
                    <a href = "<?= $escaper->escapeHtml(__($generateUrl)) ?>" style="color: white;">
                        <?= $escaper->escapeHtml(__('Generate Quote'))?>
                    </a>
                </button>
            </div>
        </div>
            <table cellspacing="0" class="data mt_quotelist_table table table-order-items history">
                <thead>
                    <tr> 
                        <th scope="col"> 
                            <?= $escaper->escapeHtml(__('Product Name'))?>
                        </th>
                        <th scope="col"> 
                            <?= $escaper->escapeHtml(__('Quantity'))?>
                        </th>
                        <th scope="col"> 
                                <?= $escaper->escapeHtml(__('Quote Price'))?>
                        </th>
                        <th scope="col">
                            <?= $escaper->escapeHtml(__('Status'))?>
                        </th>
                        <th scope="col">
                        <span><?= $escaper->escapeHtml(__('Date')) ?></span>
                        </th>
                        <th scope="col">
                            <?= $escaper->escapeHtml(__('Action'))?>
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <?php $i = 0;
                    foreach ($block->getQuotesCollection() as $quote):
                        $currency = $quote->getQuoteCurrency() ? $quote->getQuoteCurrency() : $baseCurrency;
                        ?>
                        <tr>
                            <input type="hidden" id="productId_<?= $escaper->escapeHtml($quote->getEntityId())?>" 
                            value="<?= $escaper->escapeHtml($quote->getProductId())?>"/>
                            <input type="hidden" class="id" 
                            value="<?= $escaper->escapeHtml($quote->getEntityId()); ?>"/>
                            <td data-th="Product Name" class="col body productName">
                                <span class="label">
                                    <?php $product = $quoteHelper->getProduct($quote->getProductId());
                                    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;?>
                                </span>
                            </td>
                            <td data-th="Product Quantity" class="col productQty">
                                <span class="label qty">
                                <input type="number" id="quoteQty_<?= $escaper->escapeHtml($quote->getEntityId()) ?>" 
                                value="<?= $escaper->escapeHtml($quote->getQuoteQty()); ?>" style="width: 7rem;"/>
                                </span>
                            </td>
                            <td data-th="Product Price" class="col productPrice">
                                <span class="label">
                                    <input type="number"
                                    id="quotePrice_<?= $escaper->escapeHtml($quote->getEntityId()) ?>" 
                                    class="mt_qs_quote_price"
                                    value="<?= $escaper->escapeHtml(number_format($quote->getQuotePrice(), 2)); ?>" 
                                    style="width: 7rem;"/>
                                </span>
                            </td>
                            <td data-th="Status" class="col mtstatus">
                                <span class="label pro_status">
                                    <?php
                                        $statusDecline = \Magetop\Quotesystem\Model\Quotes::STATUS_DECLINE;
                                        $statusUnapproved = \Magetop\Quotesystem\Model\Quotes::STATUS_UNAPPROVED;
                                        $statusApproved = \Magetop\Quotesystem\Model\Quotes::STATUS_APPROVED;
                                    ?>
                                    <?php if (!$product->getSku()) { ?>
                                        <span class="label mt_prod_del">
                                            <?= $escaper->escapeHtml(__("Product doesn't exist anymore"))?>
                                        </span>
                                    <?php } elseif ($quote->getStatus() == $statusDecline) { ?>
                                        <span class="label mt_qs_status_decline">
                                            <?= $escaper->escapeHtml(__('Declined'))?>
                                        </span>
                                    <?php } elseif ($product->getStatus() == "2") { ?>
                                        <span class="label mt_not_available">
                                            <?= $escaper->escapeHtml(__('Not Available'))?>
                                        </span>
                                    <?php } elseif ($quote->getStatus() == $statusUnapproved) { ?>
                                        <span class="label mt_qs_status_pending">
                                            <?= $escaper->escapeHtml(__('Pending'))?>
                                        </span>
                                    <?php } else { ?>
                                        <span class="label mt_qs_status_sold">
                                            <?= $escaper->escapeHtml(__('Sold'))?>
                                        </span>
                                    <?php } ?>
                                </span>
                            </td>
                            <td data-th="Date" class="col mtdate">
                                <span class="label">
                                <?= $escaper->escapeHtml($block->formatDate($quote->getCreatedAt())); ?>
                                </span>
                            </td>
                            <td data-th="Action" class="col mtactions">
                                <span class="label mt_action">
                                <?php if ($block->quoteStatusIsNotSold($quote['status'])) { ?>
                                    <span><i class="fa fa-trash quote_delete" aria-hidden="true"
                                    title="<?= $escaper->escapeHtml(__('Delete'))?>" 
                                    data-type="<?= $escaper->escapeHtml($quote->getEntityId());?>"></i></span>
                                    | <span><i class="fa fa-save quote_update" aria-hidden="true"
                                    title="<?= $escaper->escapeHtml(__('Update'))?>" 
                                    data-type="<?= $escaper->escapeHtml($quote->getEntityId());?>"></i></span>
                                    <?php
                                } ?>
                                </span>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table> 
    <?php endif; ?>
</div>
<div class="toolbar bottom">
    <?= $block->getPagerHtml(); ?>
</div>
<div class="buttons-set">
    <p class="back-link">
        <a href="<?= $escaper->escapeUrl($block->getUrl(
            'quotesystem/buyerquote/index/',
            ['_secure' => $block->getRequest()->isSecure()]
        )) ?>"
        class="left">&laquo; <?= $escaper->escapeHtml(__('Back')) ?></a>
    </p>
</div>
<script>
 require(['jquery','mage/translate','Magento_Ui/js/modal/confirm'], function($, $t, confirm) {
    $('.quote_delete').click(
        function () {
            var quoteId = $(this).attr("data-type");
            <?php
                    $deleteUrl = $block->getUrl(
                        'quotesystem/buyerquote/removeQuoteItem/',
                        [
                            'id'=>0,
                            '_secure' => $block->getIsSecure()
                        ]
                    );
                    $deleteUrl = substr($deleteUrl, 0, -2);
                    ?>
            var dicision = confirm(
                {
                    content: $t('Are you sure you would like to remove this item from the quote cart?'),
                    actions: {
                        confirm: function () {
                            var $url= '<?= $escaper->escapeHtml(__($deleteUrl)) ?>' + quoteId
                            window.location = $url;
                        },
                    }
                }
            );
        }
    );
    $('.quote_update').click(
        function () {
            var quoteId = $(this).attr("data-type");
            var quoteQty = $('#quoteQty_' + quoteId).val();
            var quotePrice = $('#quotePrice_' + quoteId).val();
            var productId = $('#productId_' + quoteId).val();
            <?php
                    $updateUrl = $block->getUrl(
                        'quotesystem/buyerquote/updateQuoteItem/',
                        [
                            'quote_id'=>0,
                            '_secure' => $block->getIsSecure()
                        ]
                    );
                    $updateUrl = substr($updateUrl, 0, -2);
                    ?>
            var dicision = confirm(
                {
                    content: $t('Are you sure you would like to update this item?'),
                    actions: {
                        confirm: function () {
                            var $url= '<?= $escaper->escapeHtml(__($updateUrl)) ?>' + quoteId + '/quote_qty/' + 
                            quoteQty + '/quote_price/' + quotePrice
                            window.location = $url;
                        },
                    }
                }
            );
        }
    );
});
</script>
