
<?php
	$helper = $this->helper("Magetop\Agency\Helper\Data");
	$isAgency = $helper->isAgency();
	$symbol = $helper->getCurrentCurrencySymbol();
	$imagehelper = $this->helper('Magento\Catalog\Helper\Image');
	$assignId = $helper->getProductId();
	
	$assignData = $helper->getAssignDataByAssignId($assignId);
	$product = $helper->getProductByAssignId($assignId);
	$productId = $product->getId();
	$imageType = 'category_page_list';
	$productImage = $imagehelper->init($product, $imageType)
								->constrainOnly(FALSE)
								->keepAspectRatio(TRUE)
								->keepFrame(FALSE)
								->resize(300)
								->getUrl();
	$price = $this->helper('Magento\Framework\Pricing\Helper\Data')->currency($product->getFinalPrice(), true, false);
	$image = $assignData->getImage();
	$img = $assignData->getImage();
	$flag = 1;
	if ($image == "") {
		$flag = 0;
		$image = "noimage.jpg";
	}
	$image = $helper->getImageUrl($image);
	$defaultImage = $helper->getImageUrl("noimage.jpg");
	$productType = $product->getTypeId();
	if ($productType == "configurable") {
		$attriburtes = [];
		$options = [];
		$_attributes = $product->getTypeInstance(true)
								->getConfigurableAttributesAsArray($product);
		foreach ($_attributes as $_attribute) {
			$attriburtes[$_attribute['attribute_code']] = $_attribute['store_label'];
			foreach ($_attribute['options'] as $option) {
				$options[$_attribute['attribute_code']][$option['value']] = $option['label'];
			}
		}
	}
	if ($productType == "configurable") {
        $isConfig = 1;
    } else {
        $isConfig = 0;
    }
?>
<?php
	$addImageLabel = __("Add Image");
    $baseImageLabel = __("Base Image");
    $deleteImageLabel = __("Delete Showcase");
    $blockHtml = '<div class="wk-showcase-block">';
    $blockHtml .= '<div title= "'.$deleteImageLabel.'" class="wk-delete-item">x</div>';
    $blockHtml .= '<div class="wk-default-block">'.$addImageLabel.'</div>';
    $blockHtml .= '<input type="file" name="showcase[]" accept="image/*" class="wk-showcase-img wk-no-display">';
	$blockHtml .= '<div class="wk-base-image-block"><input name="ag_base[]" type="checkbox" class="wk-base-image-input" value="0"><div class="wk-base-image-label">'.$baseImageLabel.'</div></div>';
    $blockHtml .= '</div>';
    $count = 0;
?>
<?php if($isAgency == 1): ?>
	<fieldset class="fieldset wk-ap-fieldset">
		<div class="wk-ap-product-view">
			<div class="wk-ap-product-img"><img src="<?php echo $productImage; ?>" /></div>
			<div class="wk-ap-product-description">
				<div class="wk-ap-main-product-name"><a href="<?php echo $product->getProductUrl();?>"><?php echo $product->getName(); ?></a></div>
				<div class="wk-ap-main-product-price"><?php echo $price; ?></div>
				<div class="wk-ap-main-product-description"><?php echo $block->escapeHtml($product->getDescription()); ?></div>
			</div>
		</div>
	</fieldset>
	<form method="post" action="<?php echo $this->getUrl('assignproduct/product/save'); ?>" id="assignproduct_form" enctype="multipart/form-data" data-mage-init='{"validation":{}}'>
		<input type="hidden" name="form_key" value="<?php echo $this->getFormKey(); ?>">
		<input type="hidden" name="product_id" value="<?php echo $productId; ?>">
		<input type="hidden" name="assign_id" value="<?php echo $assignId; ?>">
		<input type="hidden" name="del" value="0" id="del">
		<fieldset class="fieldset">
			<?php echo $this->getFormKeyBlockHtml(); ?>
			<legend class="legend">
				<span><?php echo __("Assign Product"); ?></span>
			</legend>
			<?php if ($productType != "configurable"): ?>
				<div class="field required">
					<label for="price" class="label">
						<span><?php echo __("Price"); echo ' ('.$symbol.')' ?></span>
					</label>
					<div class="control">
						<input value="<?php echo $assignData->getPrice(); ?>" type="text" data-validate="{required:true}" class="input-text required-entry validate-zero-or-greater" title="<?php echo __('Price'); ?>" name="price" id="price" aria-required="true">
					</div>
				</div>
			<?php endif; ?>
			<div class="field required">
				<label for="description" class="label">
					<span><?php echo __("Description"); ?></span>
				</label>
				<div class="control">
					<textarea data-validate="{required:true}" class="input-text required-entry" title="<?php echo __('Description'); ?>" name="description" id="description" aria-required="true"><?php echo $block->escapeHtml($helper->getDescription($assignData->getId())); ?></textarea>
				</div>
			</div>
			<?php if ($productType == "configurable"): ?>
				<div class="field required">
					<label for="qty" class="label">
						<span><?php echo __('Associated Products'); ?></span>
					</label>
					<div class="control">
						<table class="data table wk-associated-table">
							<thead>
								<tr>
									<th class="col"><input type="checkbox" class="wk-mass-associate"></th>
									<th class="col"><?php echo __("Product Name"); ?></th>
									<?php foreach ($attriburtes as $code => $label): ?>
										<th class="col"><?php echo $label; ?></th>
									<?php endforeach; ?>
									<th class="col"><?php echo __("Price");echo ' ('.$symbol.')'; ?></th>
								</tr>
							</thead>
							<?php
								$productTypeInstance = $product->getTypeInstance();
								$usedProducts = $productTypeInstance->getUsedProducts($product);
								foreach ($usedProducts  as $childProduct) {
									$childProduct->getName();
									foreach ($attriburtes as $code => $label) {
										$val = $childProduct[$code];
										$options[$code][$val];
									}
								}
								$associatesData = $helper->getAssociatesData($assignId);
							?>
							<tbody>
								<?php foreach ($usedProducts  as $childProduct): ?>
									<?php
										$childId = $childProduct->getId();
									?>
									<tr>
										<td class="col">
											<?php if (array_key_exists($childId, $associatesData)): ?>
												<input class="wk-associate-chkbox" checked name="products[<?php echo $childId; ?>][id]" type="checkbox" value="1">
												<input name="products[<?php echo $childId; ?>][associate_id]" type="hidden" value="<?php echo $associatesData[$childId]['id']; ?>">
											<?php else: ?>
												<input class="wk-associate-chkbox" name="products[<?php echo $childId; ?>][id]" type="checkbox" value="1">
											<?php endif; ?>
										</td>
										<td class="col"><?php echo $childProduct->getName(); ?></td>
										<?php foreach ($attriburtes as $code => $label): ?>
											<?php $val = $childProduct[$code]; ?>
											<td class="col"><?php echo $options[$code][$val]; ?></td>
										<?php endforeach; ?>
										<td class="col">
											<?php if (array_key_exists($childId, $associatesData)): ?>
												<input class="wk-associate-price required-entry validate-zero-or-greater" name="products[<?php echo $childId; ?>][price]" type="text" value="<?php echo $associatesData[$childId]['price']; ?>">
											<?php else: ?>
												<input class="wk-associate-price" name="products[<?php echo $childId; ?>][price]" type="text">
											<?php endif; ?>
										</td>
									</tr>
								<?php endforeach; ?>
							</tbody>
						</table>
					</div>
				</div>
			<?php endif; ?>
			<div class="field required">
				<div class="control">
					<button class="button wk-product-save-btn" type="submit">
						<span><span><?php echo __("Save Product"); ?></span></span>
					</button>
				</div>
			</div>
		</fieldset>
	</form>
<?php else: ?>
	<h2 class="-msg">
		<?php echo __("To Become Agency Please Contact to Admin."); ?>
	</h2>
<?php endif; ?>
<?php
	$data = [];
	$data["defaultImage"] = $defaultImage;
	$data["is_new"] = 0;
	$data["msg"] = __("Please select associated product.");
	$data["isConfig"] = $isConfig;
	$data["count"] = $count;
	$data["blockHtml"] = $blockHtml;
	$data = json_encode($data);
?>
<script type="text/x-magento-init">
	{
		"body": {
			"Magetop_Agency/js/item": <?php echo $data ?>
		}
	}
</script>