<?php

class ControllerAccountInterestsCategories extends Controller {
  private $error = array();
  
  public function index($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['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['interests_categories'] = $this->config->get('fields_register_brazil_status_interests_subcategory');
    
    $this->app();
    
    if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/account/interests_categories.tpl')) {
      $this->template = $this->config->get('config_template') . '/template/account/interests_categories.tpl';
    } else {
      $this->template = 'default/template/account/interests_categories.tpl';
    }
    
    $this->response->setOutput($this->render('account/interests_categories'));
  }
  
  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());
  }
}
?>
