<?php
class ControllerAccountStoreRating extends Controller {
  private $error = array();
	public function index() {
    $this->app();
		if (!$this->customer->isLogged()) {
			$this->session->data['redirect'] = $this->url->link('account/store_rating', '', 'SSL');

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

    $this->document->addScript(array(
      'catalog/view/theme/vista/js/jquery/jquery.validate.js',
      'catalog/view/theme/vista/js/jquery/additional-methods.js',
      'catalog/view/theme/vista/js/jquery/jquery.validate.futura-methods.js',
      'catalog/view/theme/vista/js/jquery/localization/messages_pt_BR.min.js'
    ));

		$language = $this->language->load('account/store_rating');
    $this->data = array_merge($this->data, $language);

		$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('text_store_rating'),
			'href'      => $this->url->link('account/store_rating', '', 'SSL'),
			'separator' => $this->language->get('text_separator')
		);

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

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

    $result = $this->model_catalog_store_rating->getStoreRating();
    $result = (isset($result[0])) ? $result[0] : 0;

    if (isset($this->request->post['text'])) {
      $this->data['text'] = $this->request->post['text'];
    } elseif (isset($result['text'])) {
      $this->data['text'] = $result['text'];
    } else {
      $this->data['text'] = '';
    }

    if (isset($this->request->post['rating'])) {
      $this->data['rating'] = $this->request->post['rating'];
    } elseif (isset($result['rating'])) {
      $this->data['rating'] = $result['rating'];
    } else {
      $this->data['rating'] = 1;
    }

    if (isset($this->request->post['allow'])) {
      $this->data['allow'] = $this->request->post['allow'];
    } elseif (isset($result['allow'])) {
      $this->data['allow'] = $result['allow'];
    } else {
      $this->data['allow'] = 1;
    }

    if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
      $this->model_catalog_store_rating->addStoreRating($this->request->post);
      $this->session->data['success'] = $this->language->get('msg_success');
      $this->redirect($this->url->link('account/account', '', 'SSL'));
    }

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

		$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->response->setOutput($this->render('account/store_rating'));
	}

  private function validate() {
    if (!$this->customer->isLogged( )) {
      $this->redirect( $this->url->link('account/register', '', 'SSL'));
    }

    if (trim($this->request->post['text']) == '') {
      $this->error['error_text'] = $this->language->get('msg_error_text');
    }


    if (!$this->error) {
      return true;
    } else {
      $this->error['error_warning'] = $this->language->get('msg_error_warning');
      $this->data = array_merge($this->data, $this->error);
      return false;
    }
  }

  public function app(){
    $this->document->addScriptApp(array(
      'catalog/view/theme/appmobile/public/js/mainheaderform.js',
      'catalog/view/theme/appmobile/js/page.store_rating.js'
    ));
    $this->data['scriptsApp'] = json_encode($this->document->getScriptsApp());
  }
}