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


class Md_aldeamo extends CI_Model {

	public function putLive($user, $account) {
		$otherdb = $this->load->database('aldeamo', TRUE);
			$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"),
			);
			$otherdb->where('id', $user[0]['id']);
			$return = $otherdb->update('chatbot_web_user', $data2);

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

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

		return $return2;
	}

	public function getLive($account, $ip, $device) {
		$otherdb = $this->load->database('aldeamo', TRUE);
		$otherdb->select('*');
		$otherdb->from('chatbot_web_user');
		$otherdb->where('ip', $ip);
		$otherdb->where('device', $device);
		$otherdb->where('account_id', $account);
		$test_usr = $otherdb->get()->result_array();

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

			return $test_usr;

		} else 	return false;


	}

	public function getCombos($id) {
		$otherdb = $this->load->database('aldeamo', TRUE);
		$otherdb->select('*');
		$otherdb->from('bots_gallery');
		$otherdb->where('bots_keys_id', $id);
		return $otherdb->get()->result_array();
	}

	public function getCombots($id) {
		$otherdb = $this->load->database('aldeamo', TRUE);
		$otherdb->select('*');
		$otherdb->from('bots_keys_buttons_img');
		$otherdb->where('bots_keys_id', $id);
		return $otherdb->get()->result_array();
	}

	public function getUser($id) {
		$otherdb = $this->load->database('aldeamo', TRUE);
		$otherdb->select('*');
		$otherdb->from('user');
		$otherdb-> where('auth_key', $id);
		return $otherdb->get()->result_array();
	}

	public function getKeys($id) {
		$otherdb = $this->load->database('aldeamo', TRUE);
		$otherdb->select('*');
		$otherdb->from('bots_keys');
		$otherdb-> where('account_id', $id);
		return $otherdb->get()->result_array();
	}

	public function getBotones($id) {
		$otherdb = $this->load->database('aldeamo', TRUE);
		$otherdb->select('*');
		$otherdb->from('bots_keys_buttons');
		$otherdb-> where('bots_keys_id', $id);
		return $otherdb->get()->result_array();
	}

	public function getBotonesEnc($id) {
		$otherdb = $this->load->database('aldeamo', TRUE);
		$otherdb->select('*');
		$otherdb->from('bots_encuestas_buttons');
		$otherdb-> where('bots_keys_id', $id);
		return $otherdb->get()->result_array();
	}

	public function getEncuestas($id) {
		$otherdb = $this->load->database('aldeamo', TRUE);
		$otherdb->select('*');
		$otherdb->from('bots_keys_encuestas');
		$otherdb-> where('bots_keys_id', $id);
		return $otherdb->get()->result_array();
	}

	public function getEncResp($id) {
		$otherdb = $this->load->database('aldeamo', TRUE);
		$otherdb->select('*');
		$otherdb->from('bots_encuestas');
		$otherdb-> where('bots_keys_id', $id);
		return $otherdb->get()->result_array();
	}

	public function getEncRespId($id) {
		$otherdb = $this->load->database('aldeamo', TRUE);
		$otherdb->select('*');
		$otherdb->from('bots_keys_encuestas');
		$otherdb-> where('id', $id);
		return $otherdb->get()->result_array();
	}

	public function getImage($id) {
		$otherdb = $this->load->database('aldeamo', TRUE);
		$otherdb->select('*');
		$otherdb->from('bots_keys_image');
		$otherdb->where('bots_keys_id', $id);
		return $otherdb->get()->result_array();
	}

	public function getCycle($ip, $device, $account) {
		$otherdb = $this->load->database('aldeamo', TRUE);
		$otherdb->select('*');
		$otherdb->from('chatbot_web_user');
		$otherdb->where('ip', $ip);
		$otherdb->where('device', $device);
		$otherdb->where('account_id', $account);
		$test_usr = $otherdb->get()->result_array();

		if( empty( $test_usr ) ) {
			$return = 0;
		} else {
			$user = $test_usr[0]['id'];

			$otherdb->select('*');
			$otherdb->from('bot_cycle');
			$otherdb->where('user_id', $user);

			$return2 = $otherdb->get()->result_array();
			if( empty( $return2 ) ) {
				$return = 0;
			} else if($return2[0]['active']){

				$data2 = array(
								'active' => 0,
				);
				$otherdb->where('user_id', $user);
				$otherdb->update('bot_cycle', $data2);

				$return = $return2[0]['key_id'];


			} else {
				$return = 0;
			}
		}

		return $return;

	}

	public function getMensajes($id) {
		$otherdb = $this->load->database('aldeamo', TRUE);
		$otherdb->select('*');
		$otherdb->from('bots_welcome');
		$otherdb->where('account_id', $id);
		return $otherdb->get()->result_array();
	}

	public function putMsg($user, $msg, $type, $accountID, $botsKeyID, $keyID, $device, $word, $typeA) {
		$otherdb = $this->load->database('aldeamo', TRUE);
		$return = FALSE;

		$otherdb->select('*');
		$otherdb->from('chatbot_web_user');
		$otherdb->where('ip', $user);
		$otherdb->where('device', $device);
		$otherdb->where('account_id', $accountID);
		$test_usr = $otherdb->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,
			);
			$otherdb->insert('chatbot_web_user', $data2);
			$new_id = $otherdb->insert_id();

			if($new_id) {
				$data3 = array(
								'account_id' => $accountID,
								'type' => 3,
								'active' => 1,
								'user_id' => $new_id,
								'fecha' => date('Y-m-d H:i:s'),
				);
				$otherdb->insert('bots_alerts', $data3);
			}
			//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' => $otherdb->escape($word),
							'live' => 0,
			);
			$return = $otherdb->insert('chatbot_web', $data);

			if($typeA == 4){
				$data = array(
								'user_id' => $new_id,
								'active' => 1,
								'key_id' => $botsKeyID,
				);
				$otherdb->insert('bot_cycle', $data);
			} else {
				$data = array(
								'user_id' => $new_id,
								'active' => 0,
								'key_id' => $botsKeyID,
				);
				$otherdb->insert('bot_cycle', $data);
			}

		} else {

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

			$otherdb->where('id', $test_usr[0]['id']);
			$return = $otherdb->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' => $otherdb->escape($word),
							'live' => $test_usr[0]['bot'],
			);
			$return = $otherdb->insert('chatbot_web', $data);

			if($typeA == 4){

				$otherdb->select('*');
				$otherdb->from('bot_cycle');
				$otherdb->where('user_id', $test_usr[0]['id']);
				$botCycle = $otherdb->get()->result_array();

				if( empty($botCycle) ) {
					$data = array(
									'user_id' => $test_usr[0]['id'],
									'active' => 1,
									'key_id' => $botsKeyID,
					);
					$otherdb->insert('bot_cycle', $data);
				} else {
					$data2 = array(
									'active' => 1,
									'key_id' => $botsKeyID,
					);
					$otherdb->where('user_id', $test_usr[0]['id']);
					$return = $otherdb->update('bot_cycle', $data2);
				}

			} else {
				$otherdb->select('*');
				$otherdb->from('bot_cycle');
				$otherdb->where('user_id', $test_usr[0]['id']);
				$botCycle = $otherdb->get()->result_array();

				if( empty($botCycle) ) {
					$data = array(
									'user_id' => $test_usr[0]['id'],
									'active' => 0,
									'key_id' => $botsKeyID,
					);
					$otherdb->insert('bot_cycle', $data);
				} else {
					$data2 = array(
									'active' => 0,
									'key_id' => $botsKeyID,
					);
					$otherdb->where('user_id', $test_usr[0]['id']);
					$return = $otherdb->update('bot_cycle', $data2);
				}
			}

		}

		return $return;
	}

	public function putMsgOld($user, $msg, $type, $accountID, $botsKeyID, $keyID, $device, $word) {
		$otherdb = $this->load->database('aldeamo', TRUE);
		$return = FALSE;
		$otherdb->select('*');
		$otherdb->from('chatbot_web_user');
		$otherdb->where('ip', $user);
		$otherdb->where('device', $device);
		$otherdb->where('account_id', $accountID);
		$test_usr = $otherdb->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,
			);
			$otherdb->insert('chatbot_web_user', $data2);
			$new_id = $otherdb->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' => $otherdb->escape($word),
							'live' => 0,
			);
			$return = $otherdb->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,
			);
			$otherdb->where('id', $test_usr[0]['id']);
			$return = $otherdb->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' => $otherdb->escape($word),
							'live' => $test_usr[0]['bot'],
			);
			$return = $otherdb->insert('chatbot_web', $data);

		}

		return $return;
	}

	public function putUser($accountID, $ip, $name, $email, $phone, $device) {
		$otherdb = $this->load->database('aldeamo', TRUE);
		$otherdb->select('*');
		$otherdb->from('chatbot_web_user');
		$otherdb->where('ip', $ip);
		$otherdb->where('device', $device);
		$otherdb->where('account_id', $accountID);
		$test_usr = $otherdb->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,
			);
			$otherdb->insert('chatbot_web_user', $data2);
			$return = $otherdb->insert_id();

			if($return) {
				$data3 = array(
								'account_id' => $accountID,
								'type' => 3,
								'active' => 1,
								'user_id' => $return,
								'fecha' => date('Y-m-d H:i:s'),
				);
				$otherdb->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,
			);
			$otherdb->where('id', $test_usr[0]['id']);

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

		}

		return $return;
	}
}
