<?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
 * ******************************************************************
 */
defined('_JEXEC') or die('Restricted access');
JHTML::_('behavior.tooltip');
$lang=JFactory::getLanguage();
$lang->load(JPATH_SITE, 'com_virtuemart', 'en-GB', true);
JHtml::addIncludePath(JPATH_COMPONENT.'/helpers/html');
JHtml::_('bootstrap.tooltip');
JHtml::_('behavior.multiselect');
JHtml::_('behavior.modal');
JHtml::_('dropdown.init');
JHtml::_('formbehavior.chosen', 'select'); 
$user = JFactory::getUser();
$userId = $user->get('id');
$listOrder = $this->escape($this->state->get('list.ordering'));
$listDirn = $this->escape($this->state->get('list.direction'));
if (!class_exists('VmConfig')) {
    include JPATH_ADMINISTRATOR . DIRECTORY_SEPARATOR . 'components'
    . DIRECTORY_SEPARATOR . 'com_virtuemart'.DIRECTORY_SEPARATOR.
    'helpers'.DIRECTORY_SEPARATOR.'config.php';
}
$vmconf=VmConfig::loadConfig();
if (!class_exists('JHtmlMpusers')) {
    include JPATH_ADMINISTRATOR.
    '/components/com_marketplace/helpers/html/mpusers.php';
}
if (!class_exists('CurrencyDisplay')) {
    include JPATH_ADMINISTRATOR.
    '/components/com_virtuemart/helpers/currencydisplay.php';
}
if (!class_exists('MarketplaceHelper')) {
    include JPATH_ADMINISTRATOR.DIRECTORY_SEPARATOR.'components'.
    DIRECTORY_SEPARATOR.'com_marketplace'.DIRECTORY_SEPARATOR.
    'helpers'.DIRECTORY_SEPARATOR.'marketplace.php';
}
$mpHelp=new MarketplaceHelper();
$doc=JFactory::getDocument();
$input=JFactory::getApplication()->input;
$test=$input->get('wk_set');
$redirect=$input->get('wk_redirect');
if (isset($redirect)) {
    $cloned=$input->get('cloned');
    $moved=$input->get('moved');
    if (isset($cloned)) {
        JFactory::getApplication()->redirect(
            'index.php?option=com_marketplace&view=adminproducts',
            $cloned." ".JText::_('ASSIGN_PRODUCT_TO_SELLER')
        );
    } else {
        JFactory::getApplication()->redirect(
            'index.php?option=com_marketplace&view=adminproducts',
            $moved." ".JText::_('ASSIGN_PRODUCT_TO_SELLER')
        );
    }
}
if (isset($test)) {
    $pids=unserialize(urldecode($input->get('wk_pid', null, '')));

    $script="window.addEvent('domready', function () {
            SqueezeBox.initialize();
            SqueezeBox.open('index.php?option=com_marketplace&view=adminproducts&layout=assigntoseller&tmpl=component&wk_pid=".urlencode(serialize($pids))."', {
                handler: 'iframe',size: {x: 400,y: 300}
            });
            SqueezeBox.addEvent('onClose', function () {
                window.location = 'index.php?option=com_marketplace&view=adminproducts';
            });
        });
    ";
    $doc->addScriptDeclaration($script);
}
$productModel =VmModel::getModel('Product');
$doc=JFactory::getDocument();
$style="
.icon-adminproducts {
    background-image: url('components/com_marketplace/assets/images/sprite-icon.png');
    background-repeat: no-repeat;
    background-position: -39px -151px !important;
    vertical-align: middle;
    width: 20px;
    height: 25px;
    margin-right: 5px!important;
}

";
$doc->addStyleDeclaration($style);
?>
<form action="<?php
    echo JRoute::_('index.php?option=com_marketplace&view=adminproducts', false);
    ?>" method="post" name="adminForm" id="adminForm">
<?php if (!empty($this->sidebar)) : ?>
    <div id="j-sidebar-container" class="span2">
        <?php echo $this->sidebar; ?>
    </div>
    <div id="j-main-container" class="span10">
<?php else : ?>
        <div id="j-main-container">

<?php endif; ?>
<?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%" >
                <input type="checkbox" name="checkall-toggle" value="" title="<?php
                    echo JText::_('JGLOBAL_CHECK_ALL');
                ?>" onclick="Joomla.checkAll(this)" />
            </th>
             <th width="15%" >
                <?php echo JHtml::_(
                    'searchtools.sort',
                    'PRODUCT_NAME',
                    'vpe.product_name',
                    $listDirn,
                    $listOrder
                ); ?>
            </th>
            <th width="15%">
                <?php echo JText::_('PRODUCT_IMAGE'); ?>
            </th>
            <th width="15%">
                <?php echo JText::_('PRODUCT_SKU') ?>
            </th>
            <th width="10%">
                <?php echo JHtml::_(
                    'searchtools.sort', 'PRODUCT_PRICE',
                    'pp.product_price', $listDirn, $listOrder
                );
                ?>
            </th>
            <th width="12%">
                <?php echo JHtml::_(
                    'searchtools.sort', 'PRODUCT_STOCK', 'product_in_stock',
                    $listDirn, $listOrder
                );
                ?>
            </th>
            <th width="12%">
                <?php echo JText::_('JSTATUS'); ?>
            </th>
            <th width="1%">
                <?php echo JHTML::_(
                    'searchtools.sort',  'ID', 'virtuemart_product_id',
                    $listDirn, $listOrder
                ); ?>
            </th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <td colspan="9">
                <?php  echo $this->pagination->getListFooter(); ?>
            </td>
        </tr>
    </tfoot>
            <tbody>
    <?php
    if (count($this->items)) {
        $k = 0;
        for ($i=0 , $n=count($this->items); $i < $n; $i++) {
            $item = &$this->items[$i];
            if (isset($this->items[$i+1])) {
                if ($this->items[$i]->virtuemart_product_id == $this->items[$i+1]->virtuemart_product_id) {
                    continue;
                }
            }
            $checked    = JHTML::_('grid.id', $i, $item->virtuemart_product_id);
            $products = $productModel->getProduct(
                $item->virtuemart_product_id,
                $front = false,
                $withCalc = true,
                $onlyPublished = false
            );
            if ($mpHelp->checkSeller($products->created_by) != null) {
                continue;
            }
        ?>
        <tr class="<?php echo "row" . $k; ?>">
            <td>
                <?php  echo $checked; ?>
            </td>
            <td class="nowrap">
                <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 >
                <?php
                
                $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 >
                <?php echo $this->escape($item->product_sku); ?> 
            </td>
            <td >
                <?php
                    $instance=CurrencyDisplay::getInstance($products->allPrices[0]["product_currency"]);
                    $price = number_format($item->product_price, 2, '.', '');  
                    echo $price." ".$instance->getSymbol(); ?>
            </td>
            <td  >
                <?php echo $this->escape($item->product_in_stock); ?>
            </td>
            <td >
                <?php
                echo JHtml::_(
                    'jgrid.state',
                    JHtmlMpusers::adminproductStates(),
                    $item->published, $i,
                    'adminproducts.', $item->virtuemart_product_id
                );
                ?>
            </td>
            <td>
                <?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>
</div>
</form>