<?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\Product\Productlist */

$helper = $this->helper(\Webkul\Marketplace\Helper\Data::class);
$paramData = $this->getRequest()->getParams();
$filter = '';
$filterStatus = '';
$filterDateFrom = '';
$filterDateTo = '';
if (isset($paramData['s'])) {
    $filter = $paramData['s'] != '' ? $paramData['s'] : '';
}
if (isset($paramData['status'])) {
    $filterStatus = $paramData['status'] != '' ? $paramData['status'] : '';
}
if (isset($paramData['from_date'])) {
    $filterDateFrom = $paramData['from_date'] != '' ? $paramData['from_date'] : '';
}
if (isset($paramData['to_date'])) {
    $filterDateTo = $paramData['to_date'] != '' ? $paramData['to_date'] : '';
}
$_products_coll = $block->getAllProducts();
if ($helper->getIsProductApproval() || $helper->getIsProductEditApproval()) {
    $enabledStatusText = __('Approved');
    $disabledStatusText = __('Pending');
} else {
    $enabledStatusText = __('Enabled');
    $disabledStatusText = __('Disabled');
}
?>
<div class="wk-mp-design">
    <fieldset class="fieldset info wk-mp-fieldset">
        <legend class="legend">
            <span><?= $block->escapeHtml(__('Product List')) ?></span>
        </legend>
        <div class="grid">
            <div class="hor-scroll">
                <form action="<?= $block->escapeUrl($block->getUrl('marketplace/product/productlist', ['_secure' => $this->getRequest()->isSecure()]))?>" method="get" id="form-productlist-filter"  name="formProductlistFilter" data-mage-init='{"validation":{}}' autocomplete="off">
                    <table cellspacing="0" class="border wk-mp-list-table">
                        <thead>
                            <tr id="wk-mp-tr-heading">
                                <th><span><?= $block->escapeHtml(__('Product Name')) ?></span></th>
                                <th><span><?= $block->escapeHtml(__('Date')) ?></span></th>
                                <th><span><?= $block->escapeHtml(__('Product Status')) ?></span></th>
                                <th><span>&nbsp;</span></th>
                            </tr>
                        </thead>
                        <tbody class="wk-mp-body" id="colender-check">
                            <tr>
                                <td>
                                    <input type="text" class="input-text" name="s" placeholder='<?= $block->escapeHtml(__('Search by product name')) ?>' value="<?= $block->escapeHtml($filter)?>"/>
                                </td>
                                <td>
                                    <span class="wk-mp-td-span">
                                        <?= $block->escapeHtml(__('From: ')) ?>
                                        <input name="from_date" id="special-from-date" class="input-text" value="<?= $block->escapeHtml($filterDateFrom)?>" />
                                    </span>
                                    <span class="wk-mp-td-span">
                                        <?= $block->escapeHtml(__('To: ')) ?>
                                        <input name="to_date" id="special-to-date" class="input-text" value="<?= $block->escapeHtml($filterDateTo)?>" />
                                    </span>
                                </td>
                                <td>
                                    <select name="status" class="input-text">
                                        <option value=""><?= $block->escapeHtml(__('All')) ?></option>
                                        <option value="1" <?php if ($filterStatus == 1) { echo 'selected="selected"'; }?>>
                                            <?= /* @noEscape */ $enabledStatusText ?>
                                        </option>
                                        <option value="2" <?php if ($filterStatus == 2) { echo 'selected="selected"'; }?>>
                                            <?= /* @noEscape */ $disabledStatusText ?>
                                        </option>
                                    </select>
                                </td>
                                <td>
                                    <button class="button" title="Save" type="submit">
                                        <span><span><span><?= $block->escapeHtml(__('Submit')) ?></span></span></span>
                                    </button>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </form>
                <div data-bind="scope: 'product-list-component'">
                <?php if ($_products_coll && count($_products_coll)): ?>
                    <form action="<?= $block->escapeUrl($block->getUrl('marketplace/product/massDelete', ['_secure' => $this->getRequest()->isSecure()])) ?>" method="post" id="form-productlist-massdelete" name="formProductlistMassdelete" data-mage-init='{"validation":{}}'>
                        <?= $block->getBlockHtml('formkey')?>
                        <button class="button" title="<?= $block->escapeHtml(__('Delete Products')) ?>" type="submit" style="float: left;padding: 5px 5px 5px 0;"  id="mass-delete-butn">
                            <span><span><?= $block->escapeHtml(__('Delete Products')) ?></span></span>
                        </button>
                            <!-- ko template: getTemplate() --><!-- /ko -->
                        <table cellspacing="0" class="border wk-mp-list-table wk-mp-list-container-table">
                            <thead>
                                <tr class="wk-content">
                                    <th class="wk-check-first-td">
                                        <span>
                                            <input type="checkbox" name="mpselecctall" value="all" id="mpselecctall"/>
                                        </span>
                                    </th>
                                    <th class="wk-first-td">
                                        <span class="label name">
                                            <?= $block->escapeHtml(__('Product'))?>
                                        </span>
                                    </th>
                                    <th>
                                        <span class="label pro-status">
                                            <?= $block->escapeHtml(__('Type'))?>
                                        </span>
                                    </th>
                                    <th>
                                        <span class="label pro-status">
                                            <?= $block->escapeHtml(__('Status'))?>
                                        </span>
                                    </th>
                                    <th class="wk-list-th-display">
                                        <span class="label qty">
                                            <?= $block->escapeHtml(__('Qty. Confirmed'))?>
                                        </span>
                                    </th>
                                    <th class="wk-list-th-display">
                                        <span class="label qty">
                                            <?= $block->escapeHtml(__('Qty. Pending'))?>
                                        </span>
                                    </th>
                                    <th class="wk-list-th-display">
                                        <span class="label qty">
                                            <?= $block->escapeHtml(__('Qty. Sold'))?>
                                        </span>
                                    </th>
                                    <th class="wk-list-th-display1">
                                        <span class="label">
                                            <?= $block->escapeHtml(__('Earned Amount'))?>
                                        </span>
                                    </th>
                                    <th>
                                        <span class="label">
                                            <?= $block->escapeHtml(__('Action'))?>
                                        </span>
                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $i = 0;
                                foreach ($block->getAllProducts() as $products) {
                                    $product = $block->getProductData($products->getMageproductId());
                                    $i++;
                                    $image_url = $block->imageHelperObj()->init($product, 'product_page_image_small')
                                                ->setImageFile($product->getImage())
                                                ->getUrl();
                                    $salesdetail=$block->getSalesdetail($products->getMageproductId());
                                    $isApprovedBefore = $products['is_approved'];
                                    ?>
                                    <tr class="wk-row-view <?= $class = ($i == count($block->getAllProducts()))? 'wk-last_tr':''?>">
                                        <td class="wk-check-first-td">
                                            <span>
                                                <input type="checkbox" name="product_mass_delete[]" class="mpcheckbox" value="<?= $block->escapeHtml($products->getMageproductId()); ?>"/>
                                            </span>
                                        </td>
                                        <td class="wk-first_td">
                                            <input type="hidden" class="hidden-id" value="<?= $block->escapeHtml($products->getMageproductId()); ?>" />
                                            <div class="label name" title="<?= $block->escapeHtml($product->getName()); ?>">
                                                <div class="wk-pro-divide1">
                                                    <img src="<?= $block->escapeUrl($image_url)?>" class="image" />
                                                </div>
                                                <div class="wk-pro-divide2">
                                                    <div style="width:100%;">
                                                        <?php if ($product->getStatus()==1 && $product->getVisibility()!=1) { ?>
                                                            <a href="<?= $block->escapeUrl($product->getProductUrl())?>" target="blank">
                                                        <?php }?>
                                                            <?= $block->escapeHtml($product->getName());?>
                                                        <?php if ($product->getStatus()==1 && $product->getVisibility()!=1) { ?>
                                                            </a>
                                                        <?php }?>
                                                    </div>
                                                    <div style="width:100%;float:left;">
                                                        <?php
                                                        if ($product->getPrice()*1) {
                                                            /* @noEscape */ echo $block->getFormatedPrice($product->getPrice(), $helper->getCurrencySymbol());
                                                        } else {
                                                            /* @noEscape */ echo $helper->getCurrencySymbol().'0.00';
                                                        }
                                                        ?>
                                                    </div>
                                                </div>
                                            </div>
                                        </td>
                                        <td>
                                            <span class="label pro-status">
                                                <?= $block->escapeHtml($product->getTypeId())?>
                                            </span>
                                        </td>
                                        <?php
                                        if (($product->getStatus()==2 && !$isApprovedBefore)) {    ?>
                                            <td>
                                                <span class="label pro-status">
                                                    <?= $block->escapeHtml($disabledStatusText) ?>
                                                </span>
                                            </td>
                                            <td class="wk-list-th-display">
                                                <span class="label qty">
                                                    <?= $block->escapeHtml(__('Pending'))?>
                                                </span>
                                            </td>
                                            <td class="wk-list-th-display">
                                                <span class="label qty">
                                                    <?= $block->escapeHtml(__('Pending'))?>
                                                </span>
                                            </td>
                                            <td class="wk-list-th-display">
                                                <span class="label qty">
                                                    <?= $block->escapeHtml(__('Pending'))?>
                                                </span>
                                            </td>
                                            <td class="wk-list-th-display1">
                                                <span class="label qty">
                                                    <?= $block->escapeHtml(__('Pending'))?>
                                                </span>
                                            </td>
                                        <?php } else { ?>
                                            <?php if ($product->getStatus() == 2) { ?>
                                                <td>
                                                    <span class="label pro-status">
                                                        <?= $block->escapeHtml($disabledStatusText) ?>
                                                    </span>
                                                </td>
                                            <?php } else { ?>
                                                <td>
                                                    <span class="label pro-status">
                                                        <?= $block->escapeHtml($enabledStatusText) ?>
                                                    </span>
                                                </td>
                                            <?php } ?>
                                            <td class="wk-list-th-display">
                                                <span class="label qty">
                                                    <?= /* @noEscape */ $salesdetail['quantitysoldconfirmed']; ?>
                                                </span>
                                            </td>
                                            <td class="wk-list-th-display">
                                                <span class="label qty">
                                                    <?= /* @noEscape */ $salesdetail['quantitysoldpending']; ?>
                                                </span>
                                            </td>
                                            <td class="wk-list-th-display1">
                                                <span class="label qty">
                                                    <a href="<?= $block->escapeUrl($block->getUrl('marketplace/order/salesdetail/', ['id'=>$product->getId(), '_secure' => $this->getRequest()->isSecure()])); ?>"><?= /* @noEscape */ $salesdetail['quantitysold']; ?></a>
                                                </span>
                                            </td>
                                            <td class="wk-list-th-display">
                                                <span class="label price">
                                                    <?= /* @noEscape */ $block->getFormatedPrice($salesdetail['amountearned'], $helper->getCurrencySymbol()); ?>
                                                </span>
                                            </td>
                                            <?php
                                        }?>
                                        <td>
                                            <span class="label wk-action">
                                                <img src="<?= $block->escapeUrl($this->getViewFileUrl('Webkul_Marketplace::images/icon-edit.png')); ?>" data-url="<?= $block->escapeUrl($block->getUrl('marketplace/product/edit', ['id'=>$product->getId(), '_secure' => $this->getRequest()->isSecure()]))?>" alt="<?= $block->escapeHtml(__('Edit'))?>" title="<?= $block->escapeHtml(__('Edit'))?>" class="mp-edit"/>
                                            </span>
                                        </td>
                                    </tr>
                                    <?php
                                }?>
                            </tbody>
                        </table>
                    </form>
                    </div>
                <?php else: ?>
                    <div class="wk-emptymsg">
                        <?= $block->escapeHtml(__('No Product Available')) ?>
                    </div>
                <?php endif ?>
            </div>
        </div>
        <?php if ($block->getPagerHtml()): ?>
            <div class="order-products-toolbar toolbar bottom"><?= $block->getPagerHtml(); ?></div>
        <?php endif ?>
    </fieldset>
    <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>
</div>
<script type="text/x-magento-init">
    {
        "*": {
            "Webkul_Marketplace/js/product/product-date-range": {}
        }
    }
</script>
<script type="text/x-magento-init">
        {
            "*": {
                "Magento_Ui/js/core/app": {
                    "components": {
                        "product-list-component": {
                            "component": "sellerProductList",
                            "template" : "Webkul_Marketplace/product-list"
                        }
                    }
                }
            }
        }
</script>
