<?php

class ControllerUsertrocasenha extends Controller {

    private $error = array();

    public function index() {
        $this->load->language('user/trocasenha');

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

        $this->load->model('user/trocasenha');

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validate())) {

            $this->session->data['success'] = $this->language->get('text_success');
            $this->redirect($this->url->link('index', 'token=' . $this->session->data['token'], 'SSL'));
        }

        $this->data['token'] = $this->session->data['token'];
        $this->data['cancel'] = $this->url->link('index', 'token=' . $this->session->data['token'], 'SSL');

        // declaração das variaveis, pegando o nome expresso na linguagem
        $this->data['button_save']         = $this->language->get('button_save');
        $this->data['button_cancel']       = $this->language->get('button_cancel');
        $this->data['heading_title']       = $this->language->get('heading_title');
        $this->data['text_senha_atual']    = $this->language->get('text_senha_atual');
        $this->data['text_senha_nova']     = $this->language->get('text_senha_nova');
        $this->data['text_senha_confirma'] = $this->language->get('text_senha_confirma');
        $this->data['text_dados']          = $this->language->get('text_dados');
        //ate aqui
        
        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['breadcrumbs'] = array();
        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_dados'),
            'href' => $this->url->link('user/trocarsenha', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => ' :: '
        );

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link('user/trocasenha', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => ' :: '
        );

        $this->data['action'] = $this->url->link('user/trocasenha', 'token=' . $this->session->data['token'], 'SSL');
        $this->data['cancel'] = $this->url->link('user/trocasenha', 'token=' . $this->session->data['token'], 'SSL');

        // chama a view
        $this->template = 'user/trocasenha.tpl';
        $this->children = array(
            'common/header',
            'common/footer',
        );

        $this->response->setOutput($this->render());
    }

    private function validate() {
        if (!$this->user->hasPermission('modify', 'user/trocasenha')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }

        if (!$this->error) {
            return true;
        } else {
            return false;
        }
    }

    public function verifica() {


        $this->load->language('user/trocasenha');
        $this->load->model('user/trocasenha');

        $json = array();

        if ($this->request->server['REQUEST_METHOD'] == 'POST') {
             
            if (empty($this->request->post['senha_anterior'])) {
            $json['error'] = $this->language->get('error_senha_anterior');
            }
            if (empty($this->request->post['senha_nova'])) {
                $json['error'] = $this->language->get('error_senha_nova');
            }
            if (empty($this->request->post['senha_nova_confirm'])) {
                $json['error'] = $this->language->get('error_senha_nova_confirm');
            }
           
            $this->data['senha_anterior'] = $this->request->post['senha_anterior'];
            $this->data['senha_crip'] = $this->model_user_trocasenha->verifyPassword($this->data['senha_anterior']);
            $this->data['usuario_id'] = $this->user->getId();
            $this->data['senha_nova'] = ($this->request->post['senha_nova']);
            $this->data['senha_nova_confirm'] = $this->request->post['senha_nova_confirm'];
            $this->data['senhabanco'] = $this->model_user_trocasenha->getPassword($this->user->getId());

            if (!$this->data['senha_crip']) {
                $json['error'] = $this->language->get('erro_senha');
            }

            if ($this->data['senha_nova'] != $this->data['senha_nova_confirm']) {
                $json['error'] = $this->language->get('erro_senha_confirm');
            }
           
            if (!isset($json['error'])) {

                $this->model_user_trocasenha->editPassword($this->data['usuario_id'], $this->data['senha_nova']);
                $json['success'] = $this->language->get('text_success');
                $this->user->logout();
                }else{
                    
                    $this->error['warning']= $json['error'];
                    
                }
        }
          $this->response->setOutput(json_encode($json));
    }

  }
?>



