
<?php
	$helper = $this->helper("Magetop\Agency\Helper\Data");
	$queryString = $helper->getQueryString();
	$isAgency = $helper->isAgency();
	$collection = $this->getAllProducts();
	$_imagehelper = $this->helper('Magento\Catalog\Helper\Image');
	$editAction = $this->getUrl('agency/assignproduct/edit');
	$deleteAction = $this->getUrl('agency/assignproduct/delete');
	$url = $this->getUrl('agency/assignproduct/massdelete');
?>
<?php if($isAgency == 1): ?>
	<?php if($collection->getSize()): ?>
		<button title="<?php echo __('Delete'); ?>" class="wk-ap-del action primary">
			<span><?php echo __("Delete"); ?></span>
		</button>
		<fieldset class="fieldset wk-ap-fieldset">
			<form method="post" action="<?php echo $url; ?>" id="assignproduct_delete_form">
				<table class="data table wk-table-product-list">
					<thead>
						<th class="wk-check-first-td">
							<span>
								<input type="checkbox" name="mpselecctall" value="all" id="mpselecctall"/>
							</span>
						</th>
						<th><?php echo __("Image"); ?></th>
						<th><?php echo __("Name"); ?></th>
						<th><?php echo __("Description"); ?></th>
						<th><?php echo __("Price"); ?></th>
						<th><?php echo __("Status"); ?></th>
						<th><?php echo __("Action"); ?></th>
					</thead>
					<?php foreach ($collection as $product): ?>
						<?php
							$_product = $helper->getProduct($product->getProductId());
							$name = $_product->getName();
							$price = $this->helper('Magento\Framework\Pricing\Helper\Data')->currency($product->getPrice(), true, false);
							$condition = $product->getCondition();
							if($condition == 1) {
								$condition = "New";
							} else {
								$condition = "Used";
							}
							$status = $product->getStatus();
							if($status == 1) {
								$status = "Approved";
							} else {
								$status = "Unapproved";
							}
							$fullDescription = $block->escapeHtml($helper->getDescription($product->getId()));
							if(strlen($fullDescription) > 20) {
								$description = substr($fullDescription, 0, 19)."...";
							} else {
								$description = $fullDescription;
							}
							$image = $_product->getImage();
							if ($image == "") {
								$image = "noimage.jpg";
								$image = $helper->getImageUrl($image);
							} else {
								$image = $this->helper('Magento\Cms\Helper\Wysiwyg\Images')->getBaseUrl(). 'catalog/product'. $_product->getImage();
							}
						?>
						<tr>
							<td class="col"><input class="wk-ap-del-chkbox" type="checkbox" name="ag_delete[]" value="<?php echo $product->getId(); ?>"></td>
							<td class="col"><img class="wk-ap-img2" src="<?php echo $image; ?>" /></td>
							<td data-title="<?php echo __("Name"); ?>" class="col"><a class="wk-ap-pn" href="<?php echo $_product->getProductUrl(); ?>" target="_blank"><?php echo $name; ?></a></td>
							<td data-title="<?php echo __("Description"); ?>" class="col" title="<?php echo $fullDescription; ?>"><?php echo $description; ?></td>
							<td data-title="<?php echo __("Price"); ?>" class="col"><?php echo $price; ?></td>
							<td data-title="<?php echo __("Status"); ?>" class="col"><?php echo __($status); ?></td>
							<td class="col">
								<span class="wk-ap-edit-item" data-id="<?php echo $product->getId(); ?>"></span>
								<span class="wk-ap-delete-item" data-id="<?php echo $product->getId(); ?>"></span>
							</td>
						</tr>
					<?php endforeach; ?>
				</table>
			</form>
		</fieldset>
	<?php else: ?>
		<div>
			<?php echo __("No Product Found"); ?>
		</div>
	<?php endif ?>
	<?php if ($block->getPagerHtml()): ?>
		<div class="order-products-toolbar toolbar bottom"><?php echo $block->getPagerHtml(); ?></div>
	<?php endif ?>
<?php else: ?>
	<h2 class="-msg">
		<?php echo __("To Become Agency Please Contact to Admin."); ?>
	</h2>
<?php endif; ?>
<?php
	$data = [];
	$data["editTitle"] = __('Do you want to edit?');
	$data["editAction"] = $editAction;
	$data["deleteTitle"] = __('Do you want to delete product?');
	$data["deleteAction"] = $deleteAction;
	$data["msg"] = __('Please select products to delete.');
	$data = json_encode($data);
?>
<script type="text/x-magento-init">
	{
		"body": {
			"Magetop_Agency/js/list": <?php echo $data ?>
		}
	}
</script>
