<?php
namespace Opencart\Catalog\Controller\Product;
use \Opencart\System\Helper as Helper;
class Product extends \Opencart\System\Engine\Controller {
  public function index(): void {
    $this->load->language('product/product');

    $data['breadcrumbs'] = [];

    $data['breadcrumbs'][] = [
      'text' => $this->language->get('text_home'),
      'href' => $this->url->link('common/home', 'language=' . $this->config->get('config_language'))
    ];

    $this->load->model('catalog/category');

    if (isset($this->request->get['path'])) {
      $path = '';

      $parts = explode('_', (string)$this->request->get['path']);

      $category_id = (int)array_pop($parts);

      foreach ($parts as $path_id) {
        if (!$path) {
          $path = $path_id;
        } else {
          $path .= '_' . $path_id;
        }

        $category_info = $this->model_catalog_category->getCategory($path_id);

        if ($category_info) {
          $data['breadcrumbs'][] = [
            'text' => $category_info['name'],
            'href' => $this->url->link('product/category', 'language=' . $this->config->get('config_language') . '&path=' . $path)
          ];
        }
      }

      // Set the last category breadcrumb
      $category_info = $this->model_catalog_category->getCategory($category_id);

      if ($category_info) {
        $url = '';

        if (isset($this->request->get['sort'])) {
          $url .= '&sort=' . $this->request->get['sort'];
        }

        if (isset($this->request->get['order'])) {
          $url .= '&order=' . $this->request->get['order'];
        }

        if (isset($this->request->get['page'])) {
          $url .= '&page=' . $this->request->get['page'];
        }

        if (isset($this->request->get['limit'])) {
          $url .= '&limit=' . $this->request->get['limit'];
        }

        $data['breadcrumbs'][] = [
          'text' => $category_info['name'],
          'href' => $this->url->link('product/category', 'language=' . $this->config->get('config_language') . '&path=' . $this->request->get['path'] . $url)
        ];
      }
    }

    $this->load->model('catalog/manufacturer');

    if (isset($this->request->get['manufacturer_id'])) {
      $data['breadcrumbs'][] = [
        'text' => $this->language->get('text_brand'),
        'href' => $this->url->link('product/manufacturer', 'language=' . $this->config->get('config_language'))
      ];

      $url = '';

      if (isset($this->request->get['sort'])) {
        $url .= '&sort=' . $this->request->get['sort'];
      }

      if (isset($this->request->get['order'])) {
        $url .= '&order=' . $this->request->get['order'];
      }

      if (isset($this->request->get['page'])) {
        $url .= '&page=' . $this->request->get['page'];
      }

      if (isset($this->request->get['limit'])) {
        $url .= '&limit=' . $this->request->get['limit'];
      }

      $manufacturer_info = $this->model_catalog_manufacturer->getManufacturer($this->request->get['manufacturer_id']);

      if ($manufacturer_info) {
        $data['breadcrumbs'][] = [
          'text' => $manufacturer_info['name'],
          'href' => $this->url->link('product/manufacturer|info', 'language=' . $this->config->get('config_language') . '&manufacturer_id=' . $this->request->get['manufacturer_id'] . $url)
        ];
      }
    }

    if (isset($this->request->get['search']) || isset($this->request->get['tag'])) {
      $url = '';

      if (isset($this->request->get['search'])) {
        $url .= '&search=' . $this->request->get['search'];
      }

      if (isset($this->request->get['tag'])) {
        $url .= '&tag=' . $this->request->get['tag'];
      }

      if (isset($this->request->get['description'])) {
        $url .= '&description=' . $this->request->get['description'];
      }

      if (isset($this->request->get['category_id'])) {
        $url .= '&category_id=' . $this->request->get['category_id'];
      }

      if (isset($this->request->get['sub_category'])) {
        $url .= '&sub_category=' . $this->request->get['sub_category'];
      }

      if (isset($this->request->get['sort'])) {
        $url .= '&sort=' . $this->request->get['sort'];
      }

      if (isset($this->request->get['order'])) {
        $url .= '&order=' . $this->request->get['order'];
      }

      if (isset($this->request->get['page'])) {
        $url .= '&page=' . $this->request->get['page'];
      }

      if (isset($this->request->get['limit'])) {
        $url .= '&limit=' . $this->request->get['limit'];
      }

      $data['breadcrumbs'][] = [
        'text' => $this->language->get('text_search'),
        'href' => $this->url->link('product/search', 'language=' . $this->config->get('config_language') . $url)
      ];
    }

    if (isset($this->request->get['product_id'])) {
      $product_id = (int)$this->request->get['product_id'];
    } else {
      $product_id = 0;
    }

    $this->load->model('catalog/product');

    $product_info = $this->model_catalog_product->getProduct($product_id);

    if ($product_info) {
      $url = '';

      if (isset($this->request->get['path'])) {
        $url .= '&path=' . $this->request->get['path'];
      }

      if (isset($this->request->get['filter'])) {
        $url .= '&filter=' . $this->request->get['filter'];
      }

      if (isset($this->request->get['manufacturer_id'])) {
        $url .= '&manufacturer_id=' . $this->request->get['manufacturer_id'];
      }

      if (isset($this->request->get['search'])) {
        $url .= '&search=' . $this->request->get['search'];
      }

      if (isset($this->request->get['tag'])) {
        $url .= '&tag=' . $this->request->get['tag'];
      }

      if (isset($this->request->get['description'])) {
        $url .= '&description=' . $this->request->get['description'];
      }

      if (isset($this->request->get['category_id'])) {
        $url .= '&category_id=' . $this->request->get['category_id'];
      }

      if (isset($this->request->get['sub_category'])) {
        $url .= '&sub_category=' . $this->request->get['sub_category'];
      }

      if (isset($this->request->get['sort'])) {
        $url .= '&sort=' . $this->request->get['sort'];
      }

      if (isset($this->request->get['order'])) {
        $url .= '&order=' . $this->request->get['order'];
      }

      if (isset($this->request->get['page'])) {
        $url .= '&page=' . $this->request->get['page'];
      }

      if (isset($this->request->get['limit'])) {
        $url .= '&limit=' . $this->request->get['limit'];
      }

      $data['breadcrumbs'][] = [
        'text' => $product_info['name'],
        'href' => $this->url->link('product/product', 'language=' . $this->config->get('config_language') . $url . '&product_id=' . $this->request->get['product_id'])
      ];

      $this->document->setTitle($product_info['meta_title']);
      $this->document->setDescription($product_info['meta_description']);
      $this->document->setKeywords($product_info['meta_keyword']);
      $this->document->addLink($this->url->link('product/product', '&product_id=' . $this->request->get['product_id']), 'canonical');

      $this->document->addScript('catalog/view/javascript/jquery/magnific/jquery.magnific-popup.min.js');
      $this->document->addStyle('catalog/view/javascript/jquery/magnific/magnific-popup.css');

      $data['product_name'] = $product_info['name'];
      // Locked schema-safe value
      $data['schema_name'] = $product_info['name'];

      $data['text_minimum'] = sprintf($this->language->get('text_minimum'), $product_info['minimum']);
      $data['text_login'] = sprintf($this->language->get('text_login'), $this->url->link('account/login', 'language=' . $this->config->get('config_language')), $this->url->link('account/register', 'language=' . $this->config->get('config_language')));
      $data['text_reviews'] = sprintf($this->language->get('text_reviews'), (int)$product_info['reviews']);

      $data['tab_review'] = sprintf($this->language->get('tab_review'), $product_info['reviews']);

      $data['error_upload_size'] = sprintf($this->language->get('error_upload_size'), $this->config->get('config_file_max_size'));

      $data['config_file_max_size'] = ((int)$this->config->get('config_file_max_size') * 1024 * 1024);

      $data['product_id'] = (int)$this->request->get['product_id'];
      $data['manufacturer'] = $product_info['manufacturer'];
      $data['manufacturers'] = $this->url->link('product/manufacturer|info', 'language=' . $this->config->get('config_language') . '&manufacturer_id=' . $product_info['manufacturer_id']);
      $data['model'] = $product_info['model'];
      $data['reward'] = $product_info['reward'];
      $data['points'] = $product_info['points'];
      
      
	  if(strpos($product_info['description'], '&lt;br /&gt;')) {
			$data['description'] = html_entity_decode($product_info['description'], ENT_QUOTES, 'UTF-8');
		}
		else {
			$data['description'] = html_entity_decode(nl2br($product_info['description']), ENT_QUOTES, 'UTF-8');
      // Locked schema-safe value
      $data['schema_description'] = strip_tags(html_entity_decode($product_info['description'], ENT_QUOTES, 'UTF-8'));
		}
		
		if(strpos($product_info['description_upper'], '&lt;br /&gt;')) {
	  		$data['aboutproduct'] = html_entity_decode($product_info['description_upper'], ENT_QUOTES, 'UTF-8');
		}
		else {
	  		$data['aboutproduct'] = html_entity_decode(nl2br($product_info['description_upper']), ENT_QUOTES, 'UTF-8');
		}
	  
	  
	  //$data['description'] = html_entity_decode(nl2br($product_info['description']), ENT_QUOTES, 'UTF-8');
      // Locked schema-safe value
      $data['schema_description'] = strip_tags(html_entity_decode($product_info['description'], ENT_QUOTES, 'UTF-8'));
	  //$data['aboutproduct'] = html_entity_decode(nl2br($product_info['description_upper']), ENT_QUOTES, 'UTF-8');

      if ($product_info['quantity'] <= 0) {
        $data['stock'] = $product_info['stock_status'];
      } elseif ($this->config->get('config_stock_display')) {
        $data['stock'] = $product_info['quantity'];
      } else {
        $data['stock'] = $this->language->get('text_instock');
      }

      $data['rating'] = (int)$product_info['rating'];
      $data['review_status'] = (int)$this->config->get('config_review_status');

      $data['review'] = $this->load->controller('product/review');

      $data['add_to_wishlist'] = $this->url->link('account/wishlist|add', 'language=' . $this->config->get('config_language'));
      $data['add_to_compare'] = $this->url->link('product/compare|add', 'language=' . $this->config->get('config_language'));
      $data['upload'] = $this->url->link('tool/upload', 'language=' . $this->config->get('config_language'));

      $data['language'] = $this->config->get('config_language');

      $this->load->model('tool/image');

      if (is_file(DIR_IMAGE . html_entity_decode($product_info['image'], ENT_QUOTES, 'UTF-8'))) {
        $data['popup'] = $this->model_tool_image->resize(html_entity_decode($product_info['image'], ENT_QUOTES, 'UTF-8'), $this->config->get('config_image_popup_width'), $this->config->get('config_image_popup_height'));
      } else {
        $data['popup'] = '';
      }

      if (is_file(DIR_IMAGE . html_entity_decode($product_info['image'], ENT_QUOTES, 'UTF-8'))) {
        // $data['thumb'] = $this->model_tool_image->resize(html_entity_decode($product_info['image'], ENT_QUOTES, 'UTF-8'), $this->config->get('config_image_thumb_width'), $this->config->get('config_image_thumb_height'));
        $data['thumb'] = $this->model_tool_image->resize(html_entity_decode($product_info['image'], ENT_QUOTES, 'UTF-8'), 610, 775);
      } else {
        $data['thumb'] = '';
      }

      $data['images'] = [];

      $results = $this->model_catalog_product->getImages($this->request->get['product_id']);

      foreach ($results as $result) {
        if (is_file(DIR_IMAGE . html_entity_decode($result['image'], ENT_QUOTES, 'UTF-8'))) {
          $data['images'][] = [
            'popup' => $this->model_tool_image->resize(html_entity_decode($result['image'], ENT_QUOTES, 'UTF-8'), $this->config->get('config_image_popup_width'), $this->config->get('config_image_popup_height')),
            'thumb' => $this->model_tool_image->resize(html_entity_decode($result['image'], ENT_QUOTES, 'UTF-8'), $this->config->get('config_image_additional_width'), $this->config->get('config_image_additional_height'))
          ];
        }
      }

      if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
        $data['price'] = $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
      } else {
        $data['price'] = false;
      }

      if ((float)$product_info['special']) {
        $data['special'] = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
      } else {
        $data['special'] = false;
      }

      if ($this->config->get('config_tax')) {
        $data['tax'] = $this->currency->format((float)$product_info['special'] ? $product_info['special'] : $product_info['price'], $this->session->data['currency']);
      } else {
        $data['tax'] = false;
      }

      $discounts = $this->model_catalog_product->getDiscounts($this->request->get['product_id']);

      $data['discounts'] = [];

      if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
        foreach ($discounts as $discount) {
          $data['discounts'][] = [
            'quantity' => $discount['quantity'],
            'price'    => $this->currency->format($this->tax->calculate($discount['price'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency'])
          ];
        }
      }

      $data['options'] = [];

      // Check if product is variant
      if ($product_info['master_id']) {
        $product_id = (int)$product_info['master_id'];
      } else {
        $product_id = (int)$this->request->get['product_id'];
      }

      $product_options = $this->model_catalog_product->getOptions($product_id);

      foreach ($product_options as $option) {
        if ((int)$this->request->get['product_id'] && !isset($product_info['override']['variant'][$option['product_option_id']])) {
          $product_option_value_data = [];

          foreach ($option['product_option_value'] as $option_value) {
            if (!$option_value['subtract'] || ($option_value['quantity'] > 0)) {
              if ((($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) && (float)$option_value['price']) {
                $price = $this->currency->format($this->tax->calculate($option_value['price'], $product_info['tax_class_id'], $this->config->get('config_tax') ? 'P' : false), $this->session->data['currency']);
              } else {
                $price = false;
              }

              if (is_file(DIR_IMAGE . html_entity_decode($option_value['image'], ENT_QUOTES, 'UTF-8'))) {
                $image = $this->model_tool_image->resize(html_entity_decode($option_value['image'], ENT_QUOTES, 'UTF-8'), 50, 50);
              } else {
                $image = '';
              }

              $product_option_value_data[] = [
                'product_option_value_id' => $option_value['product_option_value_id'],
                'option_value_id'         => $option_value['option_value_id'],
                'name'                    => $option_value['name'],
                'image'                   => $image,
                'price'                   => $price,
                'price_prefix'            => $option_value['price_prefix']
              ];
            }
          }

          $data['options'][] = [
            'product_option_id'    => $option['product_option_id'],
            'product_option_value' => $product_option_value_data,
            'option_id'            => $option['option_id'],
            'name'                 => $option['name'],
            'type'                 => $option['type'],
            'value'                => $option['value'],
            'required'             => $option['required']
          ];
        }
      }

      // Subscriptions
      $data['subscription_plans']  = [];

      $results = $this->model_catalog_product->getSubscriptions($this->request->get['product_id']);

      foreach ($results as $result) {
        $description = '';

        $trial_price = $this->currency->format($this->tax->calculate($result['trial_price'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
        $trial_cycle = $result['trial_cycle'];
        $trial_frequency = $this->language->get('text_' . $result['trial_frequency']);
        $trial_duration = $result['trial_duration'];

        if ($result['trial_status']) {
          $description .= sprintf($this->language->get('text_subscription_trial'), $trial_price, $trial_cycle, $trial_frequency, $trial_duration);
        }

        $price = $this->currency->format($this->tax->calculate($result['price'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
        $cycle = $result['cycle'];
        $frequency = $this->language->get('text_' . $result['frequency']);
        $duration = $result['duration'];

        if ($duration) {
          $description .= sprintf($this->language->get('text_subscription_duration'), $price, $cycle, $frequency, $duration);
        } else {
          $description .= sprintf($this->language->get('text_subscription_cancel'), $price, $cycle, $frequency);
        }

        $data['subscription_plans'][] = [
          'subscription_plan_id' => $result['subscription_plan_id'],
          'name'                 => $result['name'],
          'description'          => $description
        ];
      }

      if ($product_info['minimum']) {
        $data['minimum'] = $product_info['minimum'];
      } else {
        $data['minimum'] = 1;
      }

      $data['share'] = $this->url->link('product/product', 'language=' . $this->config->get('config_language') . '&product_id=' . (int)$this->request->get['product_id']);

      $data['attribute_groups'] = $this->model_catalog_product->getAttributes($this->request->get['product_id']);

      $data['products'] = [];

      $results = $this->model_catalog_product->getRelated($this->request->get['product_id']);

      foreach ($results as $result) {
        // Webibazaar Custom 15-12-2022
        $images = [];
        if (is_file(DIR_IMAGE . html_entity_decode($result['image'], ENT_QUOTES, 'UTF-8'))) {
          $image = $this->model_tool_image->resize(html_entity_decode($result['image'], ENT_QUOTES, 'UTF-8'), $this->config->get('config_image_related_width'), $this->config->get('config_image_related_height'));
          $images[] = [
            'additional' => $this->model_tool_image->resize(html_entity_decode($result['image'], ENT_QUOTES, 'UTF-8'), $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height')),
            'thumb' => $this->model_tool_image->resize(html_entity_decode($result['image'], ENT_QUOTES, 'UTF-8'), $this->config->get('config_image_additional_width'), $this->config->get('config_image_additional_height'))
          ];          
        } else {
          $image = $this->model_tool_image->resize('placeholder.png', $this->config->get('config_image_related_width'), $this->config->get('config_image_related_height'));
		  $images[] = [
            'additional' => $this->model_tool_image->resize('placeholder.png', $this->config->get('config_image_related_width'), $this->config->get('config_image_related_height')),
            'thumb' => $this->model_tool_image->resize('placeholder.png', $this->config->get('config_image_additional_width'), $this->config->get('config_image_additional_height'))
          ]; 
        }
        $image_results = $this->model_catalog_product->getImages($result['product_id']);
        
		
		if(!empty($image_results)){
			foreach ($image_results as $image_result) {
			  if (is_file(DIR_IMAGE . html_entity_decode($image_result['image'], ENT_QUOTES, 'UTF-8'))) {
				$images[] = [
				  'additional' => $this->model_tool_image->resize(html_entity_decode($image_result['image'], ENT_QUOTES, 'UTF-8'),  $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height')),
				  'thumb' => $this->model_tool_image->resize(html_entity_decode($image_result['image'], ENT_QUOTES, 'UTF-8'), $this->config->get('config_image_additional_width'), $this->config->get('config_image_additional_height'))
				];
				break;
			  }
			}
		}else{
			if (is_file(DIR_IMAGE . html_entity_decode($result['image'], ENT_QUOTES, 'UTF-8'))) {
				$image = $this->model_tool_image->resize(html_entity_decode($result['image'], ENT_QUOTES, 'UTF-8'), $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height'));
				$images[] = [
					'additional' => $this->model_tool_image->resize(html_entity_decode($result['image'], ENT_QUOTES, 'UTF-8'), $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height')),
					'thumb' => $this->model_tool_image->resize(html_entity_decode($result['image'], ENT_QUOTES, 'UTF-8'), $this->config->get('config_image_additional_width'), $this->config->get('config_image_additional_height'))
				];

			} else {
				$image = $this->model_tool_image->resize('placeholder.png', $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height'));
				$images[] = [
					'additional' => $this->model_tool_image->resize('placeholder.png',$this->config->get('config_image_product_width'), $this->config->get('config_image_product_height')),
					'thumb' => $this->model_tool_image->resize('placeholder.png', $this->config->get('config_image_additional_width'), $this->config->get('config_image_additional_height'))
				];
			}	
		}
        // Webibazaar Custom end
        if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
          $price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
        } else {
          $price = false;
        }
        if ((float)$result['special']) {
          $special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
          // Webibazaar Custom Code 15-12-2022
          $after_tex_special = $this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax'));
          $after_tex_price = $this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax'));
          $discount = round((($after_tex_price - $after_tex_special) * 100) / $after_tex_price);
          $save = $this->currency->format($after_tex_price - $after_tex_special, $this->session->data['currency']);
          // End
        } else {
          $special = false;
          $discount = false; // Webibazaar Custom Code 15-12-2022
          $save = false; // Webibazaar Custom Code 15-12-2022
        }

        if ($this->config->get('config_tax')) {
          $tax = $this->currency->format((float)$result['special'] ? $result['special'] : $result['price'], $this->session->data['currency']);
        } else {
          $tax = false;
        }
// option data start  01-07-23
$product_options = $this->model_catalog_product->getOptions((int)$result['product_id']);
//print_r($product_options);
$option_data = [];
foreach ($product_options as $option) {
	if ((int)$result['product_id'] && !isset($result['override']['variant'][$option['product_option_id']])) {
		$product_option_value_data = [];
		//echo $result['product_id']."Hello"."<br>";
		foreach ($option['product_option_value'] as $option_value) {
			if (!$option_value['subtract'] || ($option_value['quantity'] > 0)) {
				if ((($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) && (float)$option_value['price']) {
				$price = $this->currency->format($this->tax->calculate($option_value['price'], $result['tax_class_id'], $this->config->get('config_tax') ? 'P' : false), $this->session->data['currency']);
				} else {
					$price = false;
				}

				if (is_file(DIR_IMAGE . html_entity_decode($option_value['image'], ENT_QUOTES, 'UTF-8'))) {
					$image = $this->model_tool_image->resize(html_entity_decode($option_value['image'], ENT_QUOTES, 'UTF-8'), 50, 50);
				} else {
					$image = '';
				}

				$product_option_value_data[] = [
					'product_option_value_id' => $option_value['product_option_value_id'],
					'option_value_id'         => $option_value['option_value_id'],
					'name'                    => $option_value['name'],
					'image'                   => $image,
					'price'                   => $price,
					'price_prefix'            => $option_value['price_prefix']
				];
			}
		}
		$option_data[] = [
			'product_option_id'    => $option['product_option_id'],
			'product_option_value' => $product_option_value_data,
			'option_id'            => $option['option_id'],
			'name'                 => $option['name'],
			'type'                 => $option['type'],
			'value'                => $option['value'],
			'required'             => $option['required']
		];
	}
}
// option data end 1-7-23
        $product_data = [
          'tab_review'  => $result['reviews'],
          'product_id'  => $result['product_id'],
          'thumb'       => $image,
          'images'      => $images,
          'name'        => $result['name'],
          'description' => Helper\Utf8\substr(trim(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8'))), 0, $this->config->get('config_product_description_length')) . '..',
          'price'       => $price,
          'special'     => $special,
          'discount'    => $discount, // Webibazaar Custom Code 15-12-2022
          'save'        => $save, // Webibazaar Custom Code 15-12-2022
          'tax'         => $tax,
          'minimum'     => $result['minimum'] > 0 ? $result['minimum'] : 1,
          'rating'      => $result['rating'],
          'options'	  => $option_data,
          'href'        => $this->url->link('product/product', 'language=' . $this->config->get('config_language') . '&product_id=' . $result['product_id'])
        ];
        unset($images,$option_data);
        $data['products'][] = $this->load->controller('product/thumb', $product_data);
      }
	  
	  
	  //Gear and Accessories Products
	  $data['gaproducts'] = [];

      $results = $this->model_catalog_product->getGA($this->request->get['product_id']);

      foreach ($results as $result) {
        // Webibazaar Custom 15-12-2022
        $images = [];
        if (is_file(DIR_IMAGE . html_entity_decode($result['image'], ENT_QUOTES, 'UTF-8'))) {
          $image = $this->model_tool_image->resize(html_entity_decode($result['image'], ENT_QUOTES, 'UTF-8'), $this->config->get('config_image_related_width'), $this->config->get('config_image_related_height'));
          $images[] = [
            'additional' => $this->model_tool_image->resize(html_entity_decode($result['image'], ENT_QUOTES, 'UTF-8'), $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height')),
            'thumb' => $this->model_tool_image->resize(html_entity_decode($result['image'], ENT_QUOTES, 'UTF-8'), $this->config->get('config_image_additional_width'), $this->config->get('config_image_additional_height'))
          ];          
        } else {
          $image = $this->model_tool_image->resize('placeholder.png', $this->config->get('config_image_related_width'), $this->config->get('config_image_related_height'));
		  $images[] = [
            'additional' => $this->model_tool_image->resize('placeholder.png', $this->config->get('config_image_related_width'), $this->config->get('config_image_related_height')),
            'thumb' => $this->model_tool_image->resize('placeholder.png', $this->config->get('config_image_additional_width'), $this->config->get('config_image_additional_height'))
          ]; 
        }
        $image_results = $this->model_catalog_product->getImages($result['product_id']);
        
		
		if(!empty($image_results)){
			foreach ($image_results as $image_result) {
			  if (is_file(DIR_IMAGE . html_entity_decode($image_result['image'], ENT_QUOTES, 'UTF-8'))) {
				$images[] = [
				  'additional' => $this->model_tool_image->resize(html_entity_decode($image_result['image'], ENT_QUOTES, 'UTF-8'),  $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height')),
				  'thumb' => $this->model_tool_image->resize(html_entity_decode($image_result['image'], ENT_QUOTES, 'UTF-8'), $this->config->get('config_image_additional_width'), $this->config->get('config_image_additional_height'))
				];
				break;
			  }
			}
		}else{
			if (is_file(DIR_IMAGE . html_entity_decode($result['image'], ENT_QUOTES, 'UTF-8'))) {
				$image = $this->model_tool_image->resize(html_entity_decode($result['image'], ENT_QUOTES, 'UTF-8'), $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height'));
				$images[] = [
					'additional' => $this->model_tool_image->resize(html_entity_decode($result['image'], ENT_QUOTES, 'UTF-8'), $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height')),
					'thumb' => $this->model_tool_image->resize(html_entity_decode($result['image'], ENT_QUOTES, 'UTF-8'), $this->config->get('config_image_additional_width'), $this->config->get('config_image_additional_height'))
				];

			} else {
				$image = $this->model_tool_image->resize('placeholder.png', $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height'));
				$images[] = [
					'additional' => $this->model_tool_image->resize('placeholder.png',$this->config->get('config_image_product_width'), $this->config->get('config_image_product_height')),
					'thumb' => $this->model_tool_image->resize('placeholder.png', $this->config->get('config_image_additional_width'), $this->config->get('config_image_additional_height'))
				];
			}	
		}
        // Webibazaar Custom end
        if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
          $price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
        } else {
          $price = false;
        }
        if ((float)$result['special']) {
          $special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
          // Webibazaar Custom Code 15-12-2022
          $after_tex_special = $this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax'));
          $after_tex_price = $this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax'));
          $discount = round((($after_tex_price - $after_tex_special) * 100) / $after_tex_price);
          $save = $this->currency->format($after_tex_price - $after_tex_special, $this->session->data['currency']);
          // End
        } else {
          $special = false;
          $discount = false; // Webibazaar Custom Code 15-12-2022
          $save = false; // Webibazaar Custom Code 15-12-2022
        }

        if ($this->config->get('config_tax')) {
          $tax = $this->currency->format((float)$result['special'] ? $result['special'] : $result['price'], $this->session->data['currency']);
        } else {
          $tax = false;
        }
// option data start  01-07-23
$product_options = $this->model_catalog_product->getOptions((int)$result['product_id']);
//print_r($product_options);
$option_data = [];
foreach ($product_options as $option) {
	if ((int)$result['product_id'] && !isset($result['override']['variant'][$option['product_option_id']])) {
		$product_option_value_data = [];
		//echo $result['product_id']."Hello"."<br>";
		foreach ($option['product_option_value'] as $option_value) {
			if (!$option_value['subtract'] || ($option_value['quantity'] > 0)) {
				if ((($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) && (float)$option_value['price']) {
				$price = $this->currency->format($this->tax->calculate($option_value['price'], $result['tax_class_id'], $this->config->get('config_tax') ? 'P' : false), $this->session->data['currency']);
				} else {
					$price = false;
				}

				if (is_file(DIR_IMAGE . html_entity_decode($option_value['image'], ENT_QUOTES, 'UTF-8'))) {
					$image = $this->model_tool_image->resize(html_entity_decode($option_value['image'], ENT_QUOTES, 'UTF-8'), 50, 50);
				} else {
					$image = '';
				}

				$product_option_value_data[] = [
					'product_option_value_id' => $option_value['product_option_value_id'],
					'option_value_id'         => $option_value['option_value_id'],
					'name'                    => $option_value['name'],
					'image'                   => $image,
					'price'                   => $price,
					'price_prefix'            => $option_value['price_prefix']
				];
			}
		}
		$option_data[] = [
			'product_option_id'    => $option['product_option_id'],
			'product_option_value' => $product_option_value_data,
			'option_id'            => $option['option_id'],
			'name'                 => $option['name'],
			'type'                 => $option['type'],
			'value'                => $option['value'],
			'required'             => $option['required']
		];
	}
}
// option data end 1-7-23
        $product_data = [
          'tab_review'  => $result['reviews'],
          'product_id'  => $result['product_id'],
          'thumb'       => $image,
          'images'      => $images,
          'name'        => $result['name'],
          'description' => Helper\Utf8\substr(trim(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8'))), 0, $this->config->get('config_product_description_length')) . '..',
          'price'       => $price,
          'special'     => $special,
          'discount'    => $discount, // Webibazaar Custom Code 15-12-2022
          'save'        => $save, // Webibazaar Custom Code 15-12-2022
          'tax'         => $tax,
          'minimum'     => $result['minimum'] > 0 ? $result['minimum'] : 1,
          'rating'      => $result['rating'],
          'options'	  => $option_data,
          'href'        => $this->url->link('product/product', 'language=' . $this->config->get('config_language') . '&product_id=' . $result['product_id'])
        ];
        unset($images,$option_data);
        $data['gaproducts'][] = $this->load->controller('product/thumb', $product_data);
      }
	  

      $data['tags'] = [];

      if ($product_info['tag']) {
        $tags = explode(',', $product_info['tag']);

        foreach ($tags as $tag) {
          $data['tags'][] = [
            'tag'  => trim($tag),
            'href' => $this->url->link('product/search', 'language=' . $this->config->get('config_language') . '&tag=' . trim($tag))
          ];
        }
      }

      if ($this->config->get('config_product_report_status')) {
        $this->model_catalog_product->addReport($this->request->get['product_id'], $this->request->server['REMOTE_ADDR']);
      }

      $data['column_left'] = $this->load->controller('common/column_left');
      $data['column_right'] = $this->load->controller('common/column_right');
      $data['content_top'] = $this->load->controller('common/content_top');
      $data['content_bottom'] = $this->load->controller('common/content_bottom');
      $data['footer'] = $this->load->controller('common/footer');
      $data['header'] = $this->load->controller('common/header');

      $this->response->setOutput($this->load->view('product/product', $data));
    } else {
      $url = '';

      if (isset($this->request->get['path'])) {
        $url .= '&path=' . $this->request->get['path'];
      }

      if (isset($this->request->get['filter'])) {
        $url .= '&filter=' . $this->request->get['filter'];
      }

      if (isset($this->request->get['manufacturer_id'])) {
        $url .= '&manufacturer_id=' . $this->request->get['manufacturer_id'];
      }

      if (isset($this->request->get['search'])) {
        $url .= '&search=' . $this->request->get['search'];
      }

      if (isset($this->request->get['tag'])) {
        $url .= '&tag=' . $this->request->get['tag'];
      }

      if (isset($this->request->get['description'])) {
        $url .= '&description=' . $this->request->get['description'];
      }

      if (isset($this->request->get['category_id'])) {
        $url .= '&category_id=' . $this->request->get['category_id'];
      }

      if (isset($this->request->get['sub_category'])) {
        $url .= '&sub_category=' . $this->request->get['sub_category'];
      }

      if (isset($this->request->get['sort'])) {
        $url .= '&sort=' . $this->request->get['sort'];
      }

      if (isset($this->request->get['order'])) {
        $url .= '&order=' . $this->request->get['order'];
      }

      if (isset($this->request->get['page'])) {
        $url .= '&page=' . $this->request->get['page'];
      }

      if (isset($this->request->get['limit'])) {
        $url .= '&limit=' . $this->request->get['limit'];
      }

      $data['breadcrumbs'][] = [
        'text' => $this->language->get('text_error'),
        'href' => $this->url->link('product/product', 'language=' . $this->config->get('config_language') . $url . '&product_id=' . $product_id)
      ];

      $this->document->setTitle($this->language->get('text_error'));

      $data['continue'] = $this->url->link('common/home', 'language=' . $this->config->get('config_language'));

      $data['column_left'] = $this->load->controller('common/column_left');
      $data['column_right'] = $this->load->controller('common/column_right');
      $data['content_top'] = $this->load->controller('common/content_top');
      $data['content_bottom'] = $this->load->controller('common/content_bottom');
      $data['footer'] = $this->load->controller('common/footer');
      $data['header'] = $this->load->controller('common/header');

      $this->response->addHeader($this->request->server['SERVER_PROTOCOL'] . ' 404 Not Found');
      $this->response->setOutput($this->load->view('error/not_found', $data));
    }
  }
}
