<?php
class ControllerModuleMercadolivreAnswers extends Controller {

	private $error = array();  

	public function index(){

    $this->document->addScript(array(
      'view/javascript/answers.js'
    ));		

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

    $this->data['answers'] = array();
    $answers = $this->model_catalog_answers->getAnswers();

    foreach ($answers as $key => $answer) {
      $this->data['answers'][] = array(
         'answers_id' => $answer['answers_id'],
         'answer'     => $answer['answer']
      );
		}
		
    $this->template = 'module/mercadolivre/answers.twig';

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

	}
 
	public function insert() {

		$json = array();

		if(isset($this->request->post['answer'])){
			$answer = $this->request->post['answer'] ;
		}else{
			$json['error'] = 'Nenhuma resposta encontrada';
		}

		if(!$json){

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

			$data['answer'] = $answer;
			$this->model_catalog_answers->addAnswers($data);			
			$json['success'] = true;
		}

		$this->response->setOutputJson($json);

	}

	public function delete() {

		$json = array();

		if(isset($this->request->post['answers_id'])){
			$answers_id = $this->request->post['answers_id'] ;
		}else{
			$json['error'] = 'Nenhuma resposta encontrada';
		}

		if(!$json){

			$this->load->model('catalog/answers');
			$this->model_catalog_answers->deleteAnswers($answers_id);			
			$json['success'] = true;

		}

		$this->response->setOutputJson($json);

	}

	
}
