<?php
ini_set("display_errors", 1);
if (!defined('BASEPATH'))
	exit('No direct script access allowed');


class Md_facil extends CI_Model {

	public function putLive($user, $account) {

			$timer = time() - strtotime($user[0]['last_active']);

			if($timer >= 600) {
				$bot = 0;
			} else $bot = $user[0]['bot'];

			$data2 = array(
							'live' => 1,
							'active' => 0,
							'bot' => $bot,
							'last_active' => date("Y-m-d H:i:s"),
			);
			$this->db->where('id', $user[0]['id']);
			$return = $this->db->update('chatbot_web_user', $data2);

			$this->db->select('*');
			$this->db->from('bots_send_msg');
			$this->db->where('account_id', $account);
			$this->db->where('chatbot_web_user_id', $user[0]['id']);
			$this->db->where('leido', 0);
			$data = $this->db->get()->result_array();
			if($data) {
				$return2 = $data;
				foreach ($data as $value) {

					$data2 = array(
									'leido' => 1,
					);
					$this->db->where('id', $value['id']);
					$this->db->update('bots_send_msg', $data2);
				}
			} else $return2 = FALSE;

		return $return2;
	}

	public function getLive($account, $ip, $device) {

		$this->db->select('*');
		$this->db->from('chatbot_web_user');
		$this->db->where('ip', $ip);
		$this->db->where('device', $device);
		$this->db->where('account_id', $account);
		$test_usr = $this->db->get()->result_array();

		if(isset($test_usr[0]['id'])) {

			return $test_usr;

		} else 	return false;


	}

	public function getCombos($id) {
		$this->db->select('*');
		$this->db->from('bots_gallery');
		$this -> db -> where('bots_keys_id', $id);
		return $this ->db->get()->result_array();
	}

	public function getUser($id) {
		$this->db->select('*');
		$this->db->from('user');
		$this -> db -> where('auth_key', $id);
		return $this ->db->get()->result_array();
	}

	public function getKeys($id) {
		$this->db->select('*');
		$this->db->from('bots_keys');
		$this -> db -> where('account_id', $id);
		return $this ->db->get()->result_array();
	}

	public function getBotones($id) {
		$this->db->select('*');
		$this->db->from('bots_keys_buttons');
		$this -> db -> where('bots_keys_id', $id);
		return $this ->db->get()->result_array();
	}

	public function getBotonesEnc($id) {
		$this->db->select('*');
		$this->db->from('bots_encuestas_buttons');
		$this -> db -> where('bots_keys_id', $id);
		return $this ->db->get()->result_array();
	}

	public function getEncuestas($id) {
		$this->db->select('*');
		$this->db->from('bots_keys_encuestas');
		$this -> db -> where('bots_keys_id', $id);
		return $this ->db->get()->result_array();
	}

	public function getEncResp($id) {
		$this->db->select('*');
		$this->db->from('bots_encuestas');
		$this -> db -> where('bots_keys_id', $id);
		return $this ->db->get()->result_array();
	}

	public function getEncRespId($id) {
		$this->db->select('*');
		$this->db->from('bots_keys_encuestas');
		$this -> db -> where('id', $id);
		return $this ->db->get()->result_array();
	}

	public function getTiendas($id) {
		$otherdb = $this->load->database('quefacil', TRUE);
		$otherdb->select('*');
		$otherdb->from('tiendas_facil');
		$otherdb->where('idbranchlastpaymt', $id);
		return $otherdb->get()->result_array();
	}

	public function getCuentas($id) {

		$otherdb = $this->load->database('quefacil', TRUE);
		$query = "SELECT id, fgivenname, flastname
		FROM cuentas_facil
		WHERE '$id' IN (REPLACE(Dui,'-',''),Dui)";

		return $otherdb->query($query)->result_array();
	}

	public function putName($tokenId,$word){

		$otherdb = $this->load->database('quefacil', TRUE);

		$otherdb->select('*');
		$otherdb->from('cuentas_token');
		$otherdb->where('token', $tokenId);
		$test_usr = $otherdb->get()->result_array();

		$data2 = array(
						'newname' => $word,
						'updated' => date("Y-m-d H:i:s"),
		);
		$otherdb->where('id', $test_usr[0]['cuentas_facil_id']);
		$return = $otherdb->update('cuentas_facil', $data2);

	}

	public function putPhone($tokenId,$word){

		$otherdb = $this->load->database('quefacil', TRUE);

		$otherdb->select('*');
		$otherdb->from('cuentas_token');
		$otherdb->where('token', $tokenId);
		$test_usr = $otherdb->get()->result_array();

		$data2 = array(
						'phone' => $word,
						'updated' => date("Y-m-d H:i:s"),
		);
		$otherdb->where('id', $test_usr[0]['cuentas_facil_id']);
		$return = $otherdb->update('cuentas_facil', $data2);

	}

	public function putEmail($tokenId,$word){

		$otherdb = $this->load->database('quefacil', TRUE);

		$otherdb->select('*');
		$otherdb->from('cuentas_token');
		$otherdb->where('token', $tokenId);
		$test_usr = $otherdb->get()->result_array();

		$data2 = array(
						'email' => $word,
						'updated' => date("Y-m-d H:i:s"),
		);
		$otherdb->where('id', $test_usr[0]['cuentas_facil_id']);
		$return = $otherdb->update('cuentas_facil', $data2);

	}

	public function putNewpay($tokenId,$word){

		$otherdb = $this->load->database('quefacil', TRUE);

		$otherdb->select('*');
		$otherdb->from('cuentas_token');
		$otherdb->where('token', $tokenId);
		$test_usr = $otherdb->get()->result_array();

		$otherdb->select('*');
		$otherdb->from('cuentas_upayment');
		$otherdb->where('cuentas_facil_id', $test_usr[0]['cuentas_facil_id']);
		$upayment = $otherdb->get()->result_array();

		if(empty($upayment)){
			$data2 = array(
							'payment' => $word,
							'cuentas_facil_id' => $test_usr[0]['cuentas_facil_id'],
							'fecha' => date("Y-m-d H:i:s"),
			);
			$return = $otherdb->insert('cuentas_upayment', $data2);
		} else{
			$data2 = array(
							'payment' => $word,
							'fecha' => date("Y-m-d H:i:s"),
			);
			$otherdb->where('id', $upayment[0]['id']);
			$return = $otherdb->update('cuentas_upayment', $data2);
		}

	}

	public function putNewdate($tokenId,$word){

		$otherdb = $this->load->database('quefacil', TRUE);

		$otherdb->select('*');
		$otherdb->from('cuentas_token');
		$otherdb->where('token', $tokenId);
		$test_usr = $otherdb->get()->result_array();

			$data2 = array(
							'newdate' => $word,
							'fecha' => date("Y-m-d H:i:s"),
			);
			$otherdb->where('cuentas_facil_id', $test_usr[0]['cuentas_facil_id']);
			$return = $otherdb->update('cuentas_upayment', $data2);


	}


	public function getCuenta($token) {

		$otherdb = $this->load->database('quefacil', TRUE);

		$otherdb->select('*');
		$otherdb->from('cuentas_token');
		$otherdb->where('token', $token);
		$test_usr = $otherdb->get()->result_array();

		$otherdb->select('*');
		$otherdb->from('cuentas_facil');
		$otherdb->where('id', $test_usr[0]['cuentas_facil_id']);
		return $otherdb->get()->result_array();
	}

	public function getPassword($token, $pas) {

		$otherdb = $this->load->database('quefacil', TRUE);
		$otherdb->select('*');
		$otherdb->from('cuentas_token');
		$otherdb->where('token', $token);
		$test_usr = $otherdb->get()->result_array();

		if( empty($test_usr) ) {
			$pass = 2;
		} else {
			$otherdb->select('*');
			$otherdb->from('cuentas_facil');
			$otherdb->where('id', $test_usr[0]['cuentas_facil_id']);
			$getpass = $otherdb->get()->result_array();
			if (password_verify($pas, $getpass[0]['password'])) {
					$pass = 1;
			} else {
					$pass = 0;
			}
		}

		return $pass;
	}

	public function putToken($id) {

		$token = md5(microtime(true).mt_Rand());
		$otherdb = $this->load->database('quefacil', TRUE);
		$otherdb->select('*');
		$otherdb->from('cuentas_token');
		$otherdb->where('cuentas_facil_id', $id);
		$test_usr = $otherdb->get()->result_array();
		$return = false;
		if( empty($test_usr) ) {
			$data2 = array(
							'cuentas_facil_id' => $id,
							'token' => $token,
			);
			$return = $otherdb->insert('cuentas_token', $data2);

		} else if(isset($test_usr[0]['id'])) {
			$data2 = array(
					'token' => $token,
			);
			$otherdb->where('id', $test_usr[0]['id']);
			$return = $otherdb->update('cuentas_token', $data2);
		}


		return $token;
	}

	public function getImage($id) {
		$this->db->select('*');
		$this->db->from('bots_keys_image');
		$this->db->where('bots_keys_id', $id);
		return $this ->db->get()->result_array();
	}

	public function getMensajes($id) {
		$this->db->select('*');
		$this->db->from('bots_welcome');
		$this->db->where('account_id', $id);
		return $this ->db->get()->result_array();
	}

	public function putMsg($user, $msg, $type, $accountID, $botsKeyID, $keyID, $device, $word) {

		$this->db->select('*');
		$this->db->from('chatbot_web_user');
		$this->db->where('ip', $user);
		$this->db->where('device', $device);
		$this->db->where('account_id', $accountID);
		$test_usr = $this->db->get()->result_array();

		if( empty($test_usr) && !empty($user) ) {

			$data2 = array(
							'ip' => $user,
							'device' => $device,
							'account_id' => $accountID,
							'time' => date('Y-m-d H:i:s'),
							'last_active' => date('Y-m-d H:i:s'),
							'check' => 0,
							'live' => 0,
							'active' => 0,
							'bot' => 0,
			);
			$this->db->insert('chatbot_web_user', $data2);
			$new_id = $this->db->insert_id();
			//true or false
			$data = array(
							'user' => $new_id,
							'msg' => $msg,
							'time' => time(),
							'type' => $type,
							'account_id' => $accountID,
							'bots_key_id' => $botsKeyID,
							'keyenc_id' => $keyID,
							'message' => $this->db->escape($word),
							'live' => 0,
			);
			$return = $this->db->insert('chatbot_web', $data);

		} else {

			$data2 = array(
							'check' => 0,
							'last_active' => date("Y-m-d H:i:s"),
			);

			$this->db->where('id', $test_usr[0]['id']);
			$return = $this->db->update('chatbot_web_user', $data2);

			$data = array(
							'user' => $test_usr[0]['id'],
							'msg' => $msg,
							'time' => time(),
							'type' => $type,
							'account_id' => $accountID,
							'bots_key_id' => $botsKeyID,
							'keyenc_id' => $keyID,
							'message' => $this->db->escape($word),
							'live' => $test_usr[0]['bot'],
			);
			$return = $this->db->insert('chatbot_web', $data);

		}

		return $return;
	}

	public function putMsgOld($user, $msg, $type, $accountID, $botsKeyID, $keyID, $device, $word) {

		$this->db->select('*');
		$this->db->from('chatbot_web_user');
		$this->db->where('ip', $user);
		$this->db->where('device', $device);
		$this->db->where('account_id', $accountID);
		$test_usr = $this->db->get()->result_array();

		if( empty($test_usr) && !empty($user) ) {

			$data2 = array(
							'ip' => $user,
							'device' => $device,
							'account_id' => $accountID,
							'time' => date('Y-m-d H:i:s'),
							'last_active' => date('Y-m-d H:i:s'),
							'check' => 0,
							'live' => 0,
							'active' => 0,
							'bot' => 0,
			);
			$this->db->insert('chatbot_web_user', $data2);
			$new_id = $this->db->insert_id();
			//true or false
			$data = array(
							'user' => $new_id,
							'msg' => $msg,
							'time' => time(),
							'type' => $type,
							'account_id' => $accountID,
							'bots_key_id' => $botsKeyID,
							'keyenc_id' => $keyID,
							'message' => $this->db->escape($word),
							'live' => 0,
			);
			$return = $this->db->insert('chatbot_web', $data);

		} else {

			$timer = time() - strtotime($test_usr[0]['last_active']);

				if($timer >= 600) {
					$bot = 0;
				} else $bot = $test_usr[0]['bot'];

			$data2 = array(
							'check' => 0,
							'last_active' => date("Y-m-d H:i:s"),
							'bot' => $bot,
			);
			$this->db->where('id', $test_usr[0]['id']);
			$return = $this->db->update('chatbot_web_user', $data2);

			$data = array(
							'user' => $test_usr[0]['id'],
							'msg' => $msg,
							'time' => time(),
							'type' => $type,
							'account_id' => $accountID,
							'bots_key_id' => $botsKeyID,
							'keyenc_id' => $keyID,
							'message' => $this->db->escape($word),
							'live' => $test_usr[0]['bot'],
			);
			$return = $this->db->insert('chatbot_web', $data);

		}

		return $return;
	}

	public function putUser($accountID, $ip, $name, $email, $phone, $device) {

		$this->db->select('*');
		$this->db->from('chatbot_web_user');
		$this->db->where('ip', $ip);
		$this->db->where('device', $device);
		$this->db->where('account_id', $accountID);
		$test_usr = $this->db->get()->result_array();
		$return = FALSE;

		if( empty($test_usr) && !empty($ip) ) {

			$data2 = array(
							'ip' => $ip,
							'device' => $device,
							'name' => $name,
							'email' => $email,
							'phone' => $phone,
							'account_id' => $accountID,
							'time' => date('Y-m-d H:i:s'),
							'last_active' => date('Y-m-d H:i:s'),
							'check' => 0,
							'live' => 0,
							'active' => 0,
			);
			$this->db->insert('chatbot_web_user', $data2);
			$return = $this->db->insert_id();

			if($return) {
				$data3 = array(
								'account_id' => $accountID,
								'type' => 3,
								'active' => 1,
								'user_id' => $return,
								'fecha' => date('Y-m-d H:i:s'),
				);
				$this->db->insert('bots_alerts', $data3);
			}


		} else if(isset($test_usr[0]['id'])) {

			if($name == '') $name = $test_usr[0]['name'];
			if($email == '') $email = $test_usr[0]['email'];
			if($phone == '') $phone = $test_usr[0]['phone'];

			$data2 = array(
							'name' => $name,
							'email' => $email,
							'phone' => $phone,
						//	'bot' => $bot,
			);
			$this->db->where('id', $test_usr[0]['id']);

			$return = $this->db->update('chatbot_web_user', $data2);
			//$return = $timer;

		}

		return $return;
	}

	public function putAlert($accountID, $type, $user) {

			$data2 = array(
							'account_id' => $accountID,
							'type' => $type,
							'active' => 1,
							'user_id' => $user,
							'fecha' => date('Y-m-d H:i:s'),
			);
			$return = $this->db->insert('bots_alerts', $data2);

		return $return;
	}
}
