<?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['configurations']['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'] = '';
    }

    if(!$this->ftrgeralregistry->isApp()){
      $this->interests();
    }else{
      $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);
  }

  public function interests($arg_interests = ''){
    $this->load->model('account/customer');
    
    $interests = false;
    
    if ($arg_interests) {
      foreach ($arg_interests as $category_id => $value) {     
        if (isset($value['status']) && $value['status']) {
          $interests[] = (int)$category_id;
        } else {
          if (is_array($value)) {
            foreach ($value as $subcategory_id => $value) {
              if ($subcategory_id != 'status') {
                $interests[] = (int)$subcategory_id;
              }
            }
          }
        }
      }

      $results_interests = $arg_interests;
    } else {
      $results_interests = $this->model_account_customer->getInterests();
      
      foreach ($results_interests as $key => $value) {
        if (isset($value['category_id'])) {
          $interests[] = $value['category_id'];
        } else if (isset($value['subcategory_id'])) {
          $interests[] = $value['subcategory_id'];
        }
      }
    }
    
    $this->load->model('catalog/category');
    
    $this->data['categories'] = array();
    
    $categories_1 = $this->model_catalog_category->getCategories(0);
    
    foreach ($categories_1 as $category_1) {
      
      $level_2_data = array();
      
      $categories_2 = $this->model_catalog_category->getCategories($category_1['category_id']);
      
      foreach ($categories_2 as $category_2) {
        $level_3_data = array();
        
        $categories_3 = $this->model_catalog_category->getCategories($category_2['category_id']);
        
        foreach ($categories_3 as $category_3) {
          $data = array(
            'filter_category_id'  => $category_3['category_id'],
            'filter_sub_category' => true
          );		
          
          $level_3_data[] = array(
            'name'    => $category_3['name'],
            'id'      => $category_3['category_id'],
            'checked' => ($interests) ? in_array($category_3['category_id'], $interests) : false
          );
          
        }
        
        $level_2_data[] = array(
          'name'     => $category_2['name'],
          'children' => $level_3_data,
          'id'       => $category_2['category_id'],
          'checked'  => ($interests) ? in_array($category_2['category_id'], $interests) : false
        );
      }
      
      // Level 1
      $this->data['configurations']['categories'][] = array(
        'name'     =>  $category_1['name'],
        'children' => $level_2_data,
        'id'       => $category_1['category_id'],
        'checked'  => ($interests) ? in_array($category_1['category_id'], $interests) : false
      );
    }
    
    $this->data['configurations']['interests_categories'] = $this->config->get('fields_register_brazil_status_interests_subcategory');

    $this->app();
  }

  private function app(){
    $this->document->addScriptApp(array(
      'catalog/view/theme/appmobile/js/jquery.group-checkbox-control.js'
    ));
    
    $this->data['scriptsApp'] = json_encode($this->document->getScriptsApp());
  }
}
?>
