<?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 Patsy 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_facebook');
		$this->load->model('Md_patsy');
	}

	public function messages_post() {

		$post = $this->input->raw_input_stream;
		
		$data = $this->Md_patsy->putMessages( $post );

		$this->set_response(['status' => TRUE, 'codigo' => 1, 'message' => 'Patsy', 'data' => $post], REST_Controller::HTTP_OK);
	}

	public function testjson_post() {

		$post = $this->input->raw_input_stream;
		
		$cleanPost = json_decode($post);

		$data = $this->Md_patsy->testJson( $cleanPost );
		$this->set_response(['status' => TRUE, 'codigo' => 1, 'message' => 'WhatsApp', 'data' => $data], REST_Controller::HTTP_OK);
		
	}


	public function last_get() {

		$compras = 0;

		if($this->get('token') && $this->get('token') == "PancitoPatsito") {
			$otherdb = $this->load->database('patsy', TRUE);
			$otherdb->select('tl_compras.id as id');
			$otherdb->from('tl_compras');
			$otherdb->limit(1);
			$otherdb->order_by('tl_compras.id', 'DESC');
			$compras = $otherdb->get()->result_array();
		}
		
		$this->set_response(['status' => TRUE, 'codigo' => 0, 'message' => '', 'data' => $compras], REST_Controller::HTTP_OK);
	}

	public function sync_post()
	{
		$post = $this->input->raw_input_stream;
		
		$data = $this->Md_patsy->guardarComprasSync($post);

		$this->set_response([
			'status'  => TRUE,
			'codigo'  => 1,
			'message' => 'Patsy',
			'data'    => $data
		], REST_Controller::HTTP_OK);
	}


	public function ventas_get() {

		if($this->get('id')) $data = $this->Md_patsy->getVenta($this->get('id'));
		else $data = $this->Md_patsy->getVentas();
		
		$this->set_response(['status' => TRUE, 'codigo' => 0, 'message' => '', 'data' => $data], REST_Controller::HTTP_OK);
	}

	public function ventaslog_get() {

		function checkIsAValidDate($myDateString){
			return (bool)strtotime($myDateString);
		}

		if($this->get('start_date') && $this->get('end_date')) {

			$start = $this->get('start_date');
			$end = $this->get('end_date');

			if($this->get('status')) $status = $this->get('status');
			else $status = FALSE;

			

			if(checkIsAValidDate($start) && checkIsAValidDate($end)){
				$data = $this->Md_patsy->getVentaslog($this->get('start_date'), $this->get('end_date'), $status);
			}
		}
		
		if($data) $this->set_response(['status' => TRUE, 'codigo' => 0, 'message' => 'Ventas', 'data' => $data], REST_Controller::HTTP_OK);
		else $this->set_response(['status' => TRUE, 'codigo' => 1, 'message' => 'No hay resultados', 'data' => array()], REST_Controller::HTTP_OK);

	}

	public function delete_post() {

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

		if( isset($cleanPost['id']) && isset($cleanPost['product_id']) && !empty($cleanPost['id']) && !empty($cleanPost['product_id'])) {

			$id = $cleanPost['id'];
			$product_id = $cleanPost['product_id'];
			$test = $this->Md_patsy->deleteProduct( $id, $product_id );

			if( $test ) {
				$this->set_response(['status' => TRUE, 'codigo' => 0, 'message' => 'Mensaje enviado al clinete', 'data' => array()], REST_Controller::HTTP_OK);
			} else {
				$this->set_response(['status' => TRUE, 'codigo' => 1, 'message' => 'No se pudo borrar el producto', 'data' => array()], REST_Controller::HTTP_OK);
			}

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

	}

	public function status_post() {

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

		if( isset($cleanPost['id']) && isset($cleanPost['status']) ) {

			$id = $cleanPost['id'];
			$status = $cleanPost['status'];
			$test = $this->Md_patsy->changeStatus( $id, $status );

			if( $test ) {
				$this->set_response(['status' => TRUE, 'codigo' => 0, 'message' => 'Pedido actualizado', 'data' => array()], REST_Controller::HTTP_OK);
			} else {
				$this->set_response(['status' => TRUE, 'codigo' => 1, 'message' => 'No se pudo actualizar el pedido', 'data' => array()], REST_Controller::HTTP_OK);
			}

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

	}

	public function sendmsg_post() {

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

		if( isset($cleanPost['id']) && isset($cleanPost['msg']) ) {

			$id = $cleanPost['id'];
			$msg = $cleanPost['msg'];
			$test = $this->Md_patsy->snedMsg( $id, $msg );

			if( $test ) {
				$this->set_response(['status' => TRUE, 'codigo' => 0, 'message' => 'Mensaje enviado', 'data' => array()], REST_Controller::HTTP_OK);
			} else {
				$this->set_response(['status' => TRUE, 'codigo' => 1, 'message' => 'No se pudo enviar el mensaje', 'data' => array()], REST_Controller::HTTP_OK);
			}

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

	}


}
