<?php
$loader = $this->registry->get('load');
$loader->controller('base/base');

class ControllerAccountWishList extends ControllerBaseBase
{
	protected $referencePathFile = 'account/wishlist';
    private $error = array();
	private $productQtd;

	public function index()
	{
		if (!$this->customer->isLogged()) {
			$this->session->data['redirect'] = $this->url->link('account/wishlist', '', 'SSL');

			$this->redirect($this->url->link('account/register', '', 'SSL'));
		}

		$this->language->load('account/wishlist');

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

		$this->load->type('tipocomum');

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

		if (!isset($this->session->data['wishlist'])) {
			$this->session->data['wishlist'] = array();
		}

		if (isset($this->request->get['remove'])) {
			$key = array_search($this->request->get['remove'], $this->session->data['wishlist']);

			if ($key !== false) {
				unset($this->session->data['wishlist'][$key]);
			}

			$this->session->data['success'] = $this->language->get('text_remove');

			$this->redirect($this->url->link('account/wishlist'));
		}

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

		$this->data['breadcrumbs'] = array();

		$this->data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_home'),
			'href' => $this->url->link('common/home', '', 'NONSSL', 'false'),
			'separator' => false
		);

		$this->data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_account'),
			'href' => $this->url->link('account/account', '', 'SSL'),
			'separator' => $this->language->get('text_separator')
		);

		$this->data['breadcrumbs'][] = array(
			'text' => $this->language->get('heading_title'),
			'href' => $this->url->link('account/wishlist'),
			'separator' => $this->language->get('text_separator')
		);

		$this->breadcrumbs->setbreadcumbs($this->data['breadcrumbs']);

		$this->data['heading_title'] = $this->language->get('heading_title');
		$this->data['text_empty'] = $this->language->get('text_empty');
		$this->data['column_model'] = $this->language->get('column_model');
		$this->data['button_continue'] = $this->language->get('button_continue');
		$this->data['button_cart'] = $this->language->get('button_cart');
		$this->data['button_remove'] = $this->language->get('button_remove');
		$this->data['text_price'] = $this->language->get('text_price');
		$this->data['text_buy'] = $this->language->get('text_buy');
		$this->data['text_delete'] = $this->language->get('text_delete');	
		$this->data['text_empty_list'] = $this->language->get('text_empty_list');
		$this->data['text_delete_list'] = $this->language->get('text_delete_list');	
		$this->data['text_confirm'] = $this->language->get('text_confirm');
		$this->data['text_cancel'] = $this->language->get('text_cancel');
		$this->data['view_more_kit_products'] = $this->language->get('view_more_kit_products');
		$this->data['text_button_empty_list'] = $this->language->get('text_button_empty_list');

		$this->data['home'] = $this->url->link('common/home', '', 'NONSSL', 'false');
	
		$this->data['text_empty_wishlist'] = $this->language->get('text_empty_wishlist');
			
		$this->data['back'] = $this->url->link('account/account', '', 'SSL');
		
		//adicionar para que se caso o clique em comprar e o produto for grade redirecionar para a tela do produto
		$this->data['layout_id'] = Type_IdLayout::Tp_Idcategory;

		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];

			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$this->data['products'] = array();

		foreach ($this->session->data['wishlist'] as $key => $product_id) {
			$product_info = $this->model_catalog_product->getProduct($product_id);

			if ($product_info) {

				$this->productQtd = ($this->config->get('config_fator_multiplicador')) ? $product_info['fator_multiplicador'] : $product_info['qty'];

				if ($product_info['image']) {
					$watermark_config_date_number = $this->dmodel_tool_image->getWatermarkCacheNumber();
					$image_version_number = '?version=' . preg_replace("/[^0-9]/", "", $product_info['data_atualizacao_replicacao']) . $watermark_config_date_number;

					$image = $this->dmodel_tool_image->resizeWithWatermark($product_info['image'], Type_MiniatureMedium::Tp_width, Type_MiniatureMedium::Tp_height) . $image_version_number;
				} else {
					$image = $this->dmodel_tool_image->resize('data/sem-imagem.png', Type_MiniatureMedium::Tp_width, Type_MiniatureMedium::Tp_height);
				}

				if ($product_info['qty'] <= 0) {
					$stock = $this->language->get('text_soldout');
					$stock_css = 'background-warning text-warning border-warning';
				} else {
					$stock = $this->language->get('text_instock');
					$stock_css = 'background-success text-success border-success';
				}

				if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
					$price = $this->currency->format($product_info['price']);
				} else {
					$price = false;
				}

				if ((float) $product_info['special']) {
					$special = $this->currency->format($product_info['special']);
				} else {
					$special = false;
				}

				$this->data['futuraquantidadeatacado_status'] = $this->config->get('futuraquantidadeatacado_status');
				$this->dataVue['futuraquantidadeatacado_status'] = $this->config->get('futuraquantidadeatacado_status');


				//KIT
				$this->load->dmodel('catalog/product');
				$itens_kit = $this->dmodel_catalog_product->getProductKitItens($product_info['product_kit_id']);
				//KIT

				//GRID
				if($product_info['isProductGrid'] === '1'){
					$gradeId = $this->model_catalog_product->getIDProductGrid($product_id);
				}else{
					$gradeId = "0";
				}

				$this->data['products'][] = array(
					'product_id' => $product_info['product_id'],
					'thumb' => $image,
					'name' => $product_info['name'],
					'model' => $product_info['model'],
					'stock' => $stock,
					'stock_css' => $stock_css,
					'price' => $price,
					'special' => $special,
					'href' => $this->url->link('product/product', 'product_id=' . $product_info['product_id']),
					'remove' => $this->url->link('account/wishlist', 'remove=' . $product_info['product_id']),
					'notification_value' => isset($this->cart->getProducts()[$product_info['product_id'] . '::']) ? $this->cart->getProducts()[$product_info['product_id'] . '::']['quantity'] : false,
					'itens_kit' => $itens_kit,
				);

				$this->dataVue['products'][] = array(
					'product_id' => $product_info['product_id'],
					'thumb' => $image,
					'name' => $product_info['name'],
					'model' => $product_info['model'],
					'stock' => $stock,
					'stock_css' => $stock_css,
					'price' => $price,
					'special' => $special,
					'href' => $this->url->link('product/product', 'product_id=' . $product_info['product_id']),
					'remove' => $this->url->link('account/wishlist', 'remove=' . $product_info['product_id']),
					'notification_value' => isset($this->cart->getProducts()[$product_info['product_id'] . '::']) ? $this->cart->getProducts()[$product_info['product_id'] . '::']['quantity'] : false,
					'itens_kit' => $itens_kit,
					'itens_quantity' => $this->productQtd,
					'fator_multiplicador' => $product_info['fator_multiplicador'],
					'product_grid' => intval($product_info['isProductGrid']),
					'grade_id' => $gradeId,
				);
			} else {
				unset($this->session->data['wishlist'][$key]);
			}
		}

		if ($this->config->get('wg24_seo_fer_facebook_status') == 'enable') {
			if (!$this->config->get('wg24_seo_fer_facebook_pixel') == '') {
				$this->data['facebook_pixel'] = html_entity_decode("<script> fbq('track', 'AddToWishlist'); </script>", ENT_QUOTES, 'UTF-8');
			}
		}

		$this->data['continue'] = $this->url->link('account/account', '', 'SSL');

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/account/wishlist.twig')) {
			$this->template = $this->config->get('config_template') . '/template/account/wishlist.twig';
		} else {
			$this->template = 'default/template/account/wishlist.twig';
		}

		$this->children = array(
			'common/breadcrumbs',
			'common/column_left',
			'common/column_right',
			'common/content_top',
			'common/content_bottom',
			'common/footer',
			'common/header'
		);

		if (!$this->ftrgeralregistry->isApp()) {
			array_push($this->children, 'module/account');
		}

		$this->document->addScript([
			'catalog/view/theme/vista/public/js/account/wishlist.js',
			'public/js/menuAccount.js',
		]);


		$this->response->setOutput($this->render('account/wishlist'));
	}

	//NEW FUNCTION
	public function removeItem(){
		$filters = $this->request->post['filters'];
		$removeAll = $this->request->post['removeAll'];
		$product = array_search($filters, $this->session->data['wishlist']);

		if($removeAll){
			unset($this->session->data['wishlist']);

			if (!isset($this->session->data['wishlist'])) {
				return $this->response->setOutputJson('removeAll');	
			} else {
				return $this->response->setOutputJson('removeAll_Fail');
			}
		} else{
			if ($product !== false) {
				unset($this->session->data['wishlist'][$product]);
		
				if (!isset($this->session->data['wishlist'][$product])) {
					return $this->response->setOutputJson('removeItem');	
				} else {
					return $this->response->setOutputJson('removeItem_fail');
				}
			} else {
				return $this->response->setOutputJson(false);
			}
		}			
	}

		//NEW FUNCTION
		public function addQuantity(){	
			$this->load->model('catalog/product');
			$filters = $this->request->post['filters'];
			$product_info = $this->model_catalog_product->getProduct($filters);
			$notification_value = isset($this->cart->getProducts()[$product_info['product_id'] . '::']) ? $this->cart->getProducts()[$product_info['product_id'] . '::']['quantity'] : false;
			return $this->response->setOutputJson($notification_value);
		}

	public function add()
	{
		$this->language->load('account/wishlist');
		$this->load->helper('ftr_geral');

		$json = array();

		if (!isset($this->session->data['wishlist'])) {
			$this->session->data['wishlist'] = array();
		}

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

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

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

		if ($product_info) {
			if (!in_array($this->request->post['product_id'], $this->session->data['wishlist'])) {
				$this->session->data['wishlist'][] = $this->request->post['product_id'];
			}

			if ($this->customer->isLogged()) {
				$json['success'] = sprintf($this->language->get('text_success'), $this->url->link('product/product', 'product_id=' . $this->request->post['product_id']), $product_info['name'], $this->url->link('account/wishlist'));
			} else {
				$json['success'] = sprintf($this->language->get('text_login'), $this->url->link('account/register', '', 'SSL'), $this->url->link('account/register', '', 'SSL'), $this->url->link('product/product', 'product_id=' . $this->request->post['product_id']), $product_info['name'], $this->url->link('account/wishlist'));
			}

			$json['total'] = sprintf($this->language->get('text_wishlist'), (isset($this->session->data['wishlist']) ? count($this->session->data['wishlist']) : 0));

			$json['total_qtd'] = (isset($this->session->data['wishlist']) ? count($this->session->data['wishlist']) : 0);
		}

		$this->response->setOutput(json_encode($json));
	}

	public function toggle()
	{
		$this->language->load('account/wishlist');

		if (!isset($this->session->data['wishlist'])) {
			$this->session->data['wishlist'] = array();
		}

		$json = array();

		if (isset($this->request->post['product_id'])) {
			$this->load->model('catalog/product');

			$product_info = $this->model_catalog_product->getProduct($this->request->post['product_id']);

			if ($product_info) {
				if ($this->customer->isLogged()) {
					$key = array_search($this->request->post['product_id'], $this->session->data['wishlist']);

					if ($key !== false) {
						unset($this->session->data['wishlist'][$key]);

						$json['success'] = sprintf($this->language->get('text_success_remove'), $this->url->link('product/product', 'product_id=' . $this->request->post['product_id']), $product_info['name'], $this->url->link('account/wishlist'));
						$json['toggle'] = true;
					} else if (isset($this->request->post['product_id']) && !in_array($this->request->post['product_id'], $this->session->data['wishlist'])) {
						$this->session->data['wishlist'][] = $this->request->post['product_id'];

						$json['success'] = sprintf($this->language->get('text_success'), $this->url->link('product/product', 'product_id=' . $this->request->post['product_id']), $product_info['name'], $this->url->link('account/wishlist'));
						$json['toggle'] = true;
					} else {
						$json['toggle'] = false;
					}

				} else {
					$json['success'] = sprintf($this->language->get('text_login'), $this->url->link('account/register', '', 'SSL'), $this->url->link('account/register', '', 'SSL'), $this->url->link('product/product', 'product_id=' . $this->request->post['product_id']), $product_info['name'], $this->url->link('account/wishlist'));
				}

				$json['total'] = sprintf($this->language->get('text_wishlist'), (isset($this->session->data['wishlist']) ? count($this->session->data['wishlist']) : 0));

				$json['total_qtd'] = (isset($this->session->data['wishlist']) ? count($this->session->data['wishlist']) : 0);
			}

			$this->response->setOutput(json_encode($json));
		}
	}
}