<?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();

    $filter = '';
    $quote = "";
    $filterSorting = '';
    $filterSortingType = 'Inc';
    $filterStatus = 0;
    $data = $block->getRequest()->getParams();
    $baseCurrency = $quoteHelper->getBaseCurrencyCode();
    $storeCurrency = $quoteHelper->getCurrentCurrency();
    if ($proName = $block->getRequest()->getParam('s')) {
        $filter = $proName != "" ? $proName : "";
    }
    if (isset($data['status'])) {
        $filterStatus = $data['status'] != "" ? $data['status'] : 0;
    }
    if (isset($data['sortingStatus'])) {
        $filterSorting = $data['sortingStatus'];
    }
    if (isset($data['sortingType'])) {
        $filterSortingType = $data['sortingType'];
    }

    ?>
 <div class="table-wrapper mt-quote-buyer-list">
    <form action="<?=
        $escaper->escapeHtml(
            $block->getUrl(
                'quotesystem/buyerquote/view/',
                ['quote_id' => $block->getQuoteId(), '_secure' => $block->getRequest()->isSecure()]
            )
        )?>" method="get" id="form-productlist-filter"  name="formProductlistFilter" data-mage-init='{"validation":{}}'>
        <table cellspacing="0" class="data table table-order-items history">
            <thead>
                <tr id="mt_mp_tr_heading">
                <th scope="col" class="col productName">
                    <span><?= $escaper->escapeHtml(__('Product Name')) ?></span>
                </th>
                <th scope="col" class="col status">
                    <span><?= $escaper->escapeHtml(__('Status')) ?></span>
                </th>
                <th>
                    <span>&nbsp;</span>
                </th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td data-th="Product Name" class="col productName">
                        <input type="text" class="input-text" name="s" 
                        placeholder='<?= /* @noEscape */__('Search by product name') ?>' 
                        value="<?= $escaper->escapeHtml($filter)?>"/>
                    </td>
                    <input type="hidden" id="sortingStatus" name="sortingStatus" 
                    value="<?= $escaper->escapeHtml($filterSorting); ?>">
                    <input type="hidden" id="sortingType" name="sortingType" 
                    value="<?= $escaper->escapeHtml($filterSortingType); ?>">
                    <td data-th="Product Status" class="col status">
                        <select name="status">
                            <option value="" <?= ($filterStatus == 0) ? 'selected="selected"' : '' ?>>
                                <?= $escaper->escapeHtml(__('All')) ?></option>
                            <option value="1" <?= ($filterStatus == 1) ? 'selected="selected"' : '' ?>>
                                <?= $escaper->escapeHtml(__('Pending')) ?>
                            </option>
                            <option value="2" <?= ($filterStatus == 2) ? 'selected="selected"' : '' ?>>
                                <?= $escaper->escapeHtml(__('Approved')) ?>
                            </option>
                            <option value="3" <?= ($filterStatus == 3) ? 'selected="selected"' : '' ?>>
                                <?= $escaper->escapeHtml(__('Declined')) ?>
                            </option>
                            <option value="4" <?= ($filterStatus == 4) ? 'selected="selected"' : '' ?>>
                                <?= $escaper->escapeHtml(__('Sold')) ?>
                            </option>
                        </select>
                    </td>
                    <td>
                        <button class="button" id="save" title="Save" type="submit">
                            <span><span><span><?= $escaper->escapeHtml(__('Submit')) ?></span></span></span>
                        </button>
                    </td>
                </tr>
            </tbody>
        </table>
    </form>
    <?php if (count($block->getQuotesCollection())==0): ?>
        <div class="fieldset mt_fieldset">
            <div class="mt_emptymsg">
                <?= $escaper->escapeHtml(__('No Quotes Available')) ?>
            </div>
        </div>
    <?php else: ?>
        <form action="<?= $escaper->escapeUrl($block->getUrl(
            'quotesystem/buyerquote/massdeletequote'
        )) ?>"
        method="post"
        id="formmassdelete"
        name="formmassdelete">
        <?= $block->getBlockHtml('formkey')?>
            <button class="button mt-btn"
                title="<?= $escaper->escapeHtml(__('Delete Quotes')) ?>"
                type="submit"
                id="mass_delete_butn"
            >
                <span><span>
                    <?= $escaper->escapeHtml(__('Delete Quotes')) ?>
                </span></span>
            </button>
            <span><span>
            <a class="quote_view" aria-hidden="true"
                                    title="<?= $escaper->escapeHtml(__('Print Quote'))?>"
                                    href = "<?= $escaper->escapeUrl($block->getUrl(
                                        'quotesystem/buyerquote/printPdf/',
                                        [
                                            'quote_id'=>$block->getQuoteId(),
                                            '_secure' => $block->getIsSecure()
                                        ]
                                    )) ?>"
                                    login-url = "<?= $escaper->escapeUrl($block->getUrl('customer/account/login')); ?>">
                <?= $escaper->escapeHtml(__('Print Quote')) ?>
            </a>
            </span></span>
            <table cellspacing="0" class="data mt_quotelist_table table table-order-items history">
                <thead>
                    <tr>
                        <th class="mt_check_first_td">
                            <span>
                                <input type="checkbox" name="quoteselecctall" value="all" id="quoteselecctall"/>
                            </span>
                        </th>
                        <th scope="col" class="mt_check_first_td">
                            <?= $escaper->escapeHtml(__('Item Id'))?>
                        </th>
                        <th scope="col" class="col productName sort">
                        <i class="<?= $escaper->escapeHtml($block->nameClass); ?>" type="productName"
                            sortingType="<?= $escaper->escapeHtml($filterSortingType); ?>"
                            name="productName" aria-hidden="true"></i>
                            <?= $escaper->escapeHtml(__('Product Name'))?>
                        </th>
                        <th scope="col" class="col productQty sort">
                        <i class="<?= $escaper->escapeHtml($block->quantityClass); ?>" type="productQuantity"
                            sortingType="<?= $escaper->escapeHtml($filterSortingType); ?>"
                            name="productQuantity" aria-hidden="true"></i>
                            <?= $escaper->escapeHtml(__('Quantity'))?>
                        </th>
                        <th scope="col" class="col productPrice sort">
                        <i class="<?= $escaper->escapeHtml($block->priceClass); ?>" type="productPrice"
                        sortingType="<?= $escaper->escapeHtml($filterSortingType); ?>"
                        name="productPrice" aria-hidden="true"></i>
                                <?= $escaper->escapeHtml(__('Quote Price'))?>
                        </th>
                        <th scope="col" class="col initialproductPrice sort">
                        <i class="<?= $escaper->escapeHtml($block->initialPriceClass); ?>" type="initialproductPrice"
                        sortingType="<?= $escaper->escapeHtml($filterSortingType); ?>"
                        name="initialproductPrice" aria-hidden="true"></i>
                                <?= $escaper->escapeHtml(__('Initial Quote Price'))?>
                        </th>
                        <th scope="col" class="col mtstatus">
                            <?= $escaper->escapeHtml(__('Status'))?>
                        </th>
                        <th scope="col" class="col mtdate">
                        <span><?= $escaper->escapeHtml(__('Date')) ?></span>
                        </th>
                        <th scope="col" class="col mtactions">
                            <?= $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" class="id" 
                            value="<?= $escaper->escapeHtml($quote->getEntityId()); ?>"/>
                            <td data-th="Select" class="col massDelete">
                                <span>
                                    <?php if ($block->quoteStatusIsNotSold($quote->getStatus())) { ?>
                                        <input type="checkbox" name="quote_mass_delete[]" class="quotecheckbox" 
                                        value="<?= $escaper->escapeHtml($quote->getEntityId()); ?>"/>
                                    <?php } ?>
                                </span>
                            </td>
                            <td data-th="Quote Id" class="col body quoteId">
                                <span class="label">
                                <?= $escaper->escapeHtml($quote->getEntityId()); ?>
                                </span>
                            </td>
                            <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">
                                <?= $escaper->escapeHtml($quote->getQuoteQty()); ?>
                                </span>
                            </td>
                            <td data-th="Product Price" class="col productPrice">
                                <span class="label">
                                    <input type="hidden"
                                    class="mt_qs_quote_price"
                                    value="<?= $escaper->escapeHtml($quote->getQuotePrice()); ?>">
                                        <?= /* @noEscape */$quoteHelper->getFormatPrice(
                                            $quoteHelper->getmtconvertCurrency(
                                                $currency,
                                                $storeCurrency,
                                                $quote->getQuotePrice()
                                            ),
                                            $storeCurrency
                                        ); ?>
                                </span>
                            </td>
                            <td data-th="Initial Product Price" class="col initialproductPrice">
                                <span class="label">
                                    <input type="hidden"
                                    class="mt_qs_initial_quote_price"
                                    value="<?= $escaper->escapeHtml($quote->getInitialQuotePrice()); ?>">
                                        <?= /* @noEscape */$quoteHelper->getFormatPrice(
                                            $quoteHelper->getmtconvertCurrency(
                                                $currency,
                                                $storeCurrency,
                                                $quote->getInitialQuotePrice()
                                            ),
                                            $storeCurrency
                                        ); ?>
                                </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 ($quote->getStockstatus() != "1") { ?>
                                        <span class="label mt_qs_status_outofstock">
                                            <?= $escaper->escapeHtml(__('Out of Stock'))?>
                                        </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 } elseif ($quote->getStatus() == $statusApproved) { ?>
                                        <button class="button btn-cart" type="button">
                                            <span><span>
                                                <span><?= $escaper->escapeHtml(__("Add To Cart")); ?></span>
                                            </span></span>
                                        </button>
                                    <?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->getStatus())) { ?>
                                    <span><i class="fa fa-trash quote_delete" aria-hidden="true"
                                    title="<?= $escaper->escapeHtml(__('Delete'))?>"
                                    login-url = "<?= $escaper->escapeUrl($block->getUrl('customer/account/login')); ?>"
                                    data-type="<?= $escaper->escapeHtml($quote->getEntityId());?>"></i></span>

                                <?php }  if ($product->getSku()) { ?>
                                    <?php if ($block->quoteStatusIsNotSold($quote->getStatus())) { ?>
                                        <span><i class="fa fa-pencil-square-o quote_edit" aria-hidden="true"
                                    data-id="<?= $escaper->escapeHtml($quote->getEntityId());?>"
                                    data-url = "<?= $escaper->escapeUrl($block->getUrl(
                                        'quotesystem/buyerquote/edit/',
                                        [
                                            'id'=>$quote->getEntityId(),
                                            '_secure' => $block->getIsSecure()
                                        ]
                                    )) ?>"
                                    title="<?= $escaper->escapeHtml(__('Edit'))?>"></i></span>
                                    <?php } else { ?>
                                        <span><a href = "<?= $escaper->escapeUrl($block->getUrl(
                                            'quotesystem/buyerquote/edit/',
                                            [
                                            'id'=>$quote->getEntityId(),
                                            '_secure' => $block->getIsSecure()
                                            ]
                                        )) ?>">View Quote</a></span>
                                  <?php  } ?>
                                <?php } ?>
                                </span>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </form>
    <?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>
<?php

if ($quote == "") {
    $deleteUrl = "";
} else {
    $deleteUrl = $block->getUrl(
        'quotesystem/buyerquote/delete/',
        [
            'id'=>$quote->getEntityId(),
            '_secure' => $block->getIsSecure()
        ]
    );
}
$formData = [
    'quoteedit'         =>   '.quote_edit',
    'massdelete'        =>  '#mass_delete_butn',
    'quotecheckbox'     =>  '.quotecheckbox',
    'massdeleteform'    =>  '#formmassdelete',
    'selectall'         =>  '#quoteselecctall',
    'quotedelete'       =>  '.quote_delete',
    'quotedeleteUrl'       => $deleteUrl,
    'quotestatus'       =>  '.pro_status button',
    'addtocarturl'      =>  $block->getUrl('quotesystem/buyerquote/addtocart', ['_secure' => $block->getIsSecure()]),
    'customerloginUrl'  =>  $block->getUrl('customer/account/login', ['_secure' => $block->getIsSecure()])
];
$serializedFormData = $block->getJsonHelper()->jsonEncode($formData);
// phpcs:disable
?>
<script type="text/x-magento-init">
    {
        "*": {
            "MtQuotesystem": <?= /*@noEscape*/ $serializedFormData; ?>
        }
    }
</script>
<?php // phpcs:enable ?>