<?php

defined('BASEPATH') OR exit('No direct script access allowed');

// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH . '/libraries/REST_Controller.php';

/**
 * This is an example of a few basic user interaction methods you could use
 * all done with a hardcoded array
 *
 * @package         CodeIgniter
 * @subpackage      Rest Server
 * @category        Controller
 * @author          Hector Luna
 * @license         MIT
 * @link            https://github.com/chriskacerguis/codeigniter-restserver
 */
class Cava extends REST_Controller {

	function __construct() {
		header('Access-Control-Allow-Origin: *');
		header("Access-Control-Allow-Headers: Content-Type, Cache-Control");
		header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT");
		$method = $_SERVER['REQUEST_METHOD'];
		if($method == "OPTIONS") {
				die();
		}

		parent::__construct();
		$this->load->model('Md_cava');
	}


	public function login_post() {

		$post = $this->post();
		$cleanPost = $this->security->xss_clean($post);

		if( isset($cleanPost['username']) && isset($cleanPost['password']) ) {

			$username = $cleanPost['username'];
			$password = $cleanPost['password'];
			$test = $this->Md_cava->getLogin( $username, $password );

			if( $test['code'] == 1 ) {
				$data = $test['data'];
				$this->set_response(['status' => TRUE, 'codigo' => 0, 'message' => 'Acceso correcto', 'data' => $data], REST_Controller::HTTP_OK);
			} else {
				$this->set_response(['status' => FALSE, 'codigo' => 1, 'message' => 'Has ingresado un usuario o contraseña incorrecto', 'data' => array()], REST_Controller::HTTP_OK);
			}

		} else {
			$this->set_response(['status' => FALSE, 'codigo' => 1, 'message' => 'Parameter Missing', 'data' => ''], REST_Controller::HTTP_OK);
		}

	}

	public function putdata_post() {

		$post = $this->post();
		$cleanPost = $this->security->xss_clean($post);

		if( isset($cleanPost['data']) &&  isset($cleanPost['token'])) {
			$data = $cleanPost['data'];

			$data = json_decode($data, true);

			unset($data['clients']);
			unset($data['inventory']);
			unset($data['bodega']);
			unset($data['pricerange']);
			unset($data['html']);
			$data = json_encode($data);

			
			$token = $cleanPost['token'];
			$test = $this->Md_cava->putData( $data, $token);

			if( $test['code'] == 1 ) {
				$this->set_response(['status' => TRUE, 'codigo' => 0, 'message' => 'Información actualizada', 'data' => $test], REST_Controller::HTTP_OK);
			} else {
				$this->set_response(['status' => FALSE, 'codigo' => 1, 'message' => 'Wrong Token', 'data' => $test], REST_Controller::HTTP_OK);
			}
			

		} else {
			$this->set_response(['status' => FALSE, 'codigo' => 1, 'message' => 'Parameter Missing', 'data' => array()], REST_Controller::HTTP_OK);
		}

	}

	public function data_get() {

		if($this->get('token')) {
			
			$test = $this->Md_cava->getdata( $this->get('token') );

			if( $test['code'] == 1 ) {
	
				$this->set_response(['status' => TRUE, 'codigo' => 0, 'message' => 'Acceso correcto', 'data' => $test], REST_Controller::HTTP_OK);
			} else {
				$this->set_response(['status' => FALSE, 'codigo' => 1, 'message' => 'Wrong Token', 'data' =>$test], REST_Controller::HTTP_OK);
			}

		
		} else {
			$this->set_response(['status' => FALSE, 'codigo' => 1, 'message' => 'Missing Token', 'data' => array()], REST_Controller::HTTP_OK);
		}

	}

	
	public function fix_get() {

		$test = $this->Md_cava->fixdata( $this->get('factura') );

		$this->set_response(['status' => TRUE, 'codigo' => 0, 'message' => 'Acceso correcto', 'data' => $test], REST_Controller::HTTP_OK);
	}


}
