<?php
/**
 * Webkul Software.
 *
 * @category  Webkul
 * @package   Webkul_Marketplace
 * @author    Webkul
 * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */
/** @var $block \Webkul\Marketplace\Block\Transaction\History */

$paramData = $this->getRequest()->getParams();
$filterDateTo = '';
$filterDateFrom = '';
$trId = '';
if (isset($paramData['tr_id'])) {
    $trId = $paramData['tr_id'] != ""?$paramData['tr_id']:"";
}
if (isset($paramData['from_date'])) {
    $filterDateFrom = $paramData['from_date'] != ""?$paramData['from_date']:"";
}
if (isset($paramData['to_date'])) {
    $filterDateTo = $paramData['to_date'] != ""?$paramData['to_date']:"";
} ?>
<div class="wk-mp-design">
    <fieldset class="fieldset info wk-mp-fieldset">
        <div class="grid">
            <div class="hor-scroll">
                <form action="<?= $block->escapeUrl($block->getCurrentUrl());?>" method="get">
                    <table cellspacing="0" class="border wk-mp-list-table">
                        <thead>
                            <tr id="wk-mp-tr-heading">
                                <th><span><?= $block->escapeHtml(__('Transaction Id')) ?></span></th>
                                <th><span><?= $block->escapeHtml(__('Date')) ?></span></th>
                                <th><span>&nbsp;</span></th>
                            </tr>
                        </thead>
                        <tbody class="wk-mp-body">
                            <tr>
                                <td>
                                    <input type="text" class="input-text" name="tr_id" placeholder='<?= $block->escapeHtml(__('Search by Transaction Id')) ?>' value="<?= $block->escapeHtml($trId)?>"/>
                                </td>
                                <td>
                                    <span class="wk-mp-td-span">
                                        <input name="from_date" id="special-from-date" class="input-text" value="<?= $block->escapeHtmlAttr($filterDateFrom)?>" type="text" placeholder='<?= $block->escapeHtml(__('Date From')) ?>'/>
                                    </span>
                                    <span class="wk-mp-td-span">
                                        <input name="to_date" id="special-to-date" class="input-text" value="<?= $block->escapeHtmlAttr($filterDateTo)?>" type="text" placeholder='<?= $block->escapeHtml(__('Date To')) ?>'/>
                                    </span>
                                </td>
                                <td class="wk-mp-td-last">
                                    <button class="button" title="Save" id="save-btn" type="submit">
                                        <span><span><span><?= $block->escapeHtml(__('Submit')) ?></span></span></span>
                                    </button>
                                    <?php
                                    if ($filterDateFrom) {
                                        $filterDateFromPost = $block->getFormateDate($filterDateFrom);
                                    } else {
                                        $filterDateFromPost = '';
                                    }
                                    if ($filterDateTo) {
                                        $filterDateToPost = $block->getFormateDate($filterDateTo);
                                    } else {
                                        $filterDateToPost = '';
                                    }
                                    ?>
                                    <a href="<?= $block->escapeUrl($block->getUrl('marketplace/transaction/downloadCsv', ['from_date'=>$filterDateFromPost,'to_date'=>$filterDateToPost, '_secure' => $this->getRequest()->isSecure()]));?>">
                                        <button class="button" title="Save" type="button">
                                            <span><span><span><?= $block->escapeHtml(__('Download .CSV')) ?></span></span></span>
                                        </button>
                                    </a>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </form>
                <?php
                if (!$block->getAllTransaction() || !$block->getAllTransaction()->getSize()) { ?>
                    <div class="fieldset wk-mp-fieldset">
                        <div class="wk-emptymsg">
                            <?= $block->escapeHtml(__('No Transaction Available')) ?>
                        </div>
                    </div>
                    <?php
                } else { ?>
                    <div class="table-wrapper">
                        <table cellspacing="0" class="border wk-mp-list-table wk-mp-list-container-table table">
                            <thead>
                                <tr>
                                    <th class="wk-first-td">
                                        <span class="label">
                                            <?= $block->escapeHtml(__('Date'))?>
                                        </span>
                                    </th>
                                    <th>
                                        <span class="label">
                                            <?= $block->escapeHtml(__('Transaction Id'))?>
                                        </span>
                                    </th>
                                    <th>
                                        <span class="label name">
                                            <?= $block->escapeHtml(__('Comment Message'))?>
                                        </span>
                                    </th>
                                    <th>
                                        <span class="label">
                                            <?= $block->escapeHtml(__('Transaction Amount'))?>
                                        </span>
                                    </th>
                                    <th>
                                        <span class="label">
                                            <?= $block->escapeHtml(__('Action'))?>
                                        </span>
                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $i = 0;
                                foreach ($block->getAllTransaction() as $transactioncoll) {
                                    $i++;
                                    ?>
                                    <tr>
                                        <td data-th="<?= $block->escapeHtml(__('Date'))?>" class="wk-first-td">
                                            <span class="label">
                                                <?= /* @noEscape */ $block->formatDate($transactioncoll->getCreatedAt())  ; ?>
                                            </span>
                                        </td>
                                        <td data-th="<?= $block->escapeHtml(__('Transaction Id'))?>">
                                            <span class="label">
                                                <?= /* @noEscape */ $transactioncoll->getTransactionId(); ?>
                                            </span>
                                        </td>
                                        <td data-th="<?= $block->escapeHtml(__('Comment Message'))?>">
                                            <?php
                                            if ($transactioncoll->getCustomNote()) {
                                                $customnote = $transactioncoll->getCustomNote();
                                            } else {
                                                $customnote = __('None');
                                            }
                                            ?>
                                            <span class="label name">
                                                <?= "<span class='bol'>". $customnote. "</span>" ;
                                                ?>
                                            </span>
                                        </td>
                                        <td data-th="<?= $block->escapeHtml(__('Transaction Amount'))?>">
                                            <span class="label price">
                                                <?= /* @noEscape */ $block->getFormatedPrice($transactioncoll->getTransactionAmount());
                                                ?>
                                            </span>
                                        </td>
                                        <td data-th="<?= $block->escapeHtml(__('Action'))?>">
                                            <span class="label">
                                                <a href="<?= $block->escapeUrl($block->getUrl('marketplace/transaction/view', ['id'=>$transactioncoll->getId(), '_secure' => $this->getRequest()->isSecure()]))?>"><?= $block->escapeHtml(__('View'));?></a>
                                            </span>
                                        </td>
                                    </tr>
                                    <?php
                                }?>
                            </tbody>
                        </table>
                    </div>
                    <?php
                }?>
            </div>
        </div>
        <?php if ($block->getPagerHtml()): ?>
            <div class="order-products-toolbar toolbar bottom"><?= $block->getPagerHtml(); ?></div>
        <?php endif ?>
    </fieldset>
</div>
<div class="buttons-set">
    <p class="back-link">
        <a href="javascript:;" onclick="javascript: window.history.back();" class="left">&laquo; <?= $block->escapeHtml(__('Back')) ?></a>
    </p>
</div>

<script>
    require([
        "jquery",
        "mage/mage",
        'mage/calendar'
    ], function($){
        $(".wk-mp-body").dateRange({
            'dateFormat':'mm/dd/yy',
            'from': {
                'id': 'special-from-date'
            },
            'to': {
                'id': 'special-to-date'
            }
        });
    });
</script>
