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

class ControllerAccountInterests extends ControllerAccountInterestsCategories {
  private $error = array();

  public function index($arg_interests = '') {

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

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

    $this->language->load('account/interests');
    $this->document->setTitle($this->language->get('heading_title'));

    $this->load->model('account/customer');

    if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
      $this->insert();
    }

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

    $this->data['button_continue'] = $this->language->get('button_continue');
    $this->data['button_back'] = $this->language->get('button_back');
    $this->data['back'] = $this->url->link('account/account', '', 'SSL');

    $this->data['breadcrumbs'][] = array(
      'text'      => $this->language->get('text_home'),
      'href'      => $this->url->link('common/home'),
      '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/interests', '', 'SSL'),
      'separator' => $this->language->get('text_separator')
    );

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

    if (isset($this->error['warning'])) {
      $this->data['error_warning'] = $this->error['warning'];
    } else {
      $this->data['error_warning'] = '';
    }

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

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

    $this->data['interests_categories'] = $this->getChild('account/interests_categories');

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

    $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/interests.js',
        'public/js/menuAccount.js'
    ]);

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

  public function insert() {
    $this->load->model('account/customer');
    $result = 0;

    if (isset($this->request->post['categories'])) {
      $this->request->post['categories']['customer_id'] = $this->customer->getId();
      $result = $this->model_account_customer->addInterests($this->request->post['categories']);
      $this->remember(0);
    } else if (isset($this->request->get['categories'])) {
      $this->request->get['categories']['customer_id'] = $this->customer->getId();
      $result = $this->model_account_customer->addInterests($this->request->get['categories']);
      if ($result) {
        $this->remember(0);
      }
      return;
    } else {
      $result = $this->model_account_customer->removeInterests($this->customer->getId());
      if ($result) {
        $this->remember(0);
      }
    }
    if (!isset($this->request->post['modal']) && !isset($this->request->get['modal'])) {
      if ($result) {
        $this->session->data['success'] = $this->language->get('text_update');
        $this->redirect($this->url->link('account/account', '', 'SSL'));
        $this->remember(0);
      } else {
        $this->data['error'] = $this->language->get('error_insert');
      }
    }

  }

  public function remember($remember = null) {
    $this->load->model('account/customer');
    $result = false;

    if (isset($remember) && $remember !== null) {
      $result = $this->model_account_customer->setInterestsRemember($remember);
    } else if (isset($this->request->get['remember'])) {
      $result = $this->model_account_customer->setInterestsRemember($this->request->get['remember']);
    } else if (isset($this->request->post['remember'])) {
      $result = $this->model_account_customer->setInterestsRemember($this->request->post['remember']);
    }

    $this->response->setOutputJson($result);
  }
}
?>
