<?php
/**
 * Marketplace - Component Marketplace 
 * ******************************************************************
 *
 * PHP version 7.0
 *
 * @category   Component
 * @package    Joomla
 * @author     WebKul software private limited <support@webkul.com>
 * @copyright  2010 WebKul software private limited
 * @license    http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @version    GIT:5.2
 * @filesource http://store.webkul.com
 * @link       Technical Support:  webkul.uvdesk.com
 * ******************************************************************
 */
// no direct access
defined('_JEXEC') or die('Restricted access');
JHTML::_('behavior.tooltip');
JHtml::addIncludePath(JPATH_COMPONENT . '/helpers/html');
JHtml::_('bootstrap.tooltip');
JHtml::_('behavior.multiselect');
JHtml::_('formbehavior.chosen', 'select');
JFormHelper::addFieldPath(JPATH_COMPONENT . '/models/fields');
if (!class_exists('JHtmlMpusers')) {
    include JPATH_ADMINISTRATOR.
    '/components/com_marketplace/helpers/html/mpusers.php';
}
$user = JFactory::getUser();
$userId = $user->get('id');
$listOrder = $this->escape($this->state->get('list.ordering'));
$listDirn = $this->escape($this->state->get('list.direction'));
$canOrder = $user->authorise('core.edit.state', 'com_marketplace.product');
$archived = $this->state->get('filter.state') == 2 ? true : false;
$trashed = $this->state->get('filter.state') == -2 ? true : false;
$params = (isset($this->state->params)) ? $this->state->params : new JObject;
$saveOrder = $listOrder == 'a.ordering';
$doc=JFactory::getDocument();
$doc->addStyleDeclaration(
    '
.icon-products{
    background-image:url("components/com_marketplace/assets/images/sprite-icon.png");
    background-repeat:no-repeat;
    background-position:-39px -41px !important;
    vertical-align:middle;
    width:20px;
    height:20px;
    margin-right:0px!important;
}'
);
define("S_P", "DIRECTORY_SEPARATOR");
if (!class_exists('VmConfig')) {
    include JPATH_ADMINISTRATOR . DIRECTORY_SEPARATOR . 'components' .
    DIRECTORY_SEPARATOR . 'com_virtuemart'.DIRECTORY_SEPARATOR.'helpers'.
    DIRECTORY_SEPARATOR.'config.php';
}
$vmconf=VmConfig::loadConfig();
$productModel = VmModel::getModel('Product');
$model=$this->getModel('products');
if (!class_exists('CurrencyDisplay')) {
    include JPATH_ADMINISTRATOR.
    '/components/com_virtuemart/helpers/currencydisplay.php';
}
?>
<form action="<?php echo 'index.php?option=com_marketplace&view=products'; ?>" method="post" name="adminForm" id="adminForm">
    <div id="j-sidebar-container" class="span2">
        <?php echo $this->sidebar; ?>
    </div>
    <div id="j-main-container" class="span10">
        <?php
        echo JLayoutHelper::render('joomla.searchtools.default', array('view' => $this));
        ?>
        <table class="table table-striped table-bordered" id="articleList">
            <thead>
                <tr bgcolor="#6994C4" style="color:#FFFFFF;vertical-align:top">
                    <th width="1%" class="">
                        <?php echo JHtml::_('grid.checkall'); ?>
                    </th>
                    <th width="20%">
                    <?php
                        echo JHtml::_(
                            'searchtools.sort',
                            'CUSTOMER_NAME',
                            'u.username',
                            $listDirn,
                            $listOrder
                        );
                    ?>
                    </th>
                    <th width="15%">
                    <?php
                        echo JHtml::_(
                            'searchtools.sort',
                            'PRODUCT_NAME',
                            'z.product_name',
                            $listDirn,
                            $listOrder
                        );
                    ?>
                    </th>
                    <th width="15%">
                    <?php
                        echo JHtml::_(
                            'searchtools.sort',
                            'PRODUCT_IMAGE',
                            '',
                            $listDirn,
                            $listOrder
                        );
                    ?>
                    </th>
                    <th width="15%">
                    <?php
                        echo JHtml::_(
                            'searchtools.sort',
                            'PRODUCT_SKU',
                            'v.product_sku',
                            $listDirn,
                            $listOrder
                        );
                    ?>
                    </th>
                    <th width="15%">
                    <?php
                        echo JHtml::_(
                            'searchtools.sort',
                            'PRODUCT_PRICE',
                            'a.price',
                            $listDirn,
                            $listOrder
                        );
                    ?>
                    </th>
                    <th width="15%">
                    <?php
                        echo JHtml::_(
                            'searchtools.sort',
                            'PRODUCT_STOCK',
                            'v.product_in_stock',
                            $listDirn,
                            $listOrder
                        );
                    ?>
                    </th>
                    <th width="15%">
                    <?php
                        echo JHtml::_(
                            'searchtools.sort',
                            'JSTATUS',
                            'v.published',
                            $listDirn,
                            $listOrder
                        );
                    ?>
                    </th>
                    
                    <th width="1%" nowrap="nowrap">
                    <?php
                        echo JHTML::_(
                            'searchtools.sort',
                            'ID',
                            'v.virtuemart_product_id',
                            $listDirn,
                            $listOrder
                        );
                    ?>
                    </th>
                </tr>
            </thead>
            <tfoot>
            <tr>
                <td colspan="10">
                <?php
                    echo $this->pagination->getListFooter();
                ?>
                </td>
            </tr>
            </tfoot>
            <tbody>
                <?php
                if (count($this->items)) {
                    $k = 0;
                    $pro=VmModel::getModel("Product");
                    
                    foreach ($this->items as $i => $item) {
                        $item = &$this->items[$i];
                        $thispro=$pro->getProduct($item->virtuemart_product_id, false, true, false);
                        $instance=CurrencyDisplay::getInstance($thispro->prices["product_currency"]);
                        $checked    = JHTML::_('grid.id', $i, $item->virtuemart_product_id);
                ?>
                <tr class="row<?php echo $i % 2; ?>">
                    <td>
                        <?php echo JHtml::_('grid.id', $i, $item->virtuemart_product_id); ?>
                    </td>
                    
                    <td class="">
                            <a href="index.php?option=com_marketplace&view=marketplace&layout=edit_seller&seller_id=<?php
                                echo $item->created_by;
                            ?>">
                            <?php echo $model->getUserName($item->created_by);
                        ?></a>
                        
                    </td>
                    <td class="">
                        <a href="index.php?option=com_virtuemart&view=product&task=edit&virtuemart_product_id=<?php
                            echo $item->virtuemart_product_id;
                        ?>">
                            <?php echo $item->product_name ?>
                        </a>
                    </td>
                    <td class="">
                        <?php $products = $productModel->getProductSingle($item->virtuemart_product_id);
                        $productModel->addImages($products); ?>
                        <?php
                        if (!strpos($this->escape($products->file_url), '.')) {
                        ?>
                        <img src="<?php echo JURI::root().$vmconf->get("assets_general_path")."images/vmgeneral/".$vmconf->get("no_image_set");?>" width="50px" height="50px"/>
                        <?php
                        } else {
                        ?>
                        <img src="<?php echo JURI::Base().'../'.$this->escape($products->file_url); ?>" width="50px" height="50px"/>
                        <?php
                        }
                        ?>
                    </td>
                    <td class="">
                        <?php echo $this->escape($item->product_sku); ?>
                    </td>
                    <td class="">
                        <?php
                        
                         $price = number_format(floatval(isset($thispro->allPrices[0]['product_price'])?$thispro->allPrices[0]['product_price']:$item->price), 2, '.', '');  // Last two parameters are optional
                        echo $price." ".$instance->getSymbol(); ?>
                    </td>
                    <td class="">
                        <?php echo $this->escape($item->product_in_stock); ?>
                    </td>
                    <td class="">
                        <?php
                        echo JHtml::_(
                            'jgrid.state',
                            JHtmlMpusers::productStates(),
                            $item->published,
                            $i,
                            'products.',
                            $item->virtuemart_product_id
                        );
                        ?>
                    </td>
                    <td class="">
                        <?php echo (int) $item->virtuemart_product_id; ?>
                    </td>
                </tr>
                    <?php
                    $k = 1 - $k;
                    }
                } else {
                    echo "<tr ><td colspan='9'><div class='alert alert-info'>".
                    "<strong>Info!</strong>".JText::_('NO_RECORD_FOUND')."</div></td></tr>";
                }
                ?>
            </tbody>
        </table>
        <input type="hidden" name="task" value="" />
        <input type="hidden" name="boxchecked" value="0" />
        <?php echo JHTML::_('form.token'); ?>
    </div>
</form>
