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


class Md_bot extends CI_Model {

	  
	public function getLastmsg($userID)
    {
	  $this->db->select('*');
	  $this->db->from('bots_telegram');
	  $this->db->where('user', $userID);
	  $this->db->where('type', 1);
	  $this->db->order_by("id", "desc");
	  $this->db->limit(2);
	  return $this->db->get()->result_array();
    }

	public function findBotonesEnc($id)
	{

		$this->db->select('*');
		$this->db->from('bots_encuestas_buttons');
		$this->db->where('bots_keys_id', $id);
		$this->db->order_by("id", "desc");
		$this->db->limit(1);
		return $this->db->get()->result_array();
	}

	public function findEncResp($id)
	{

		$this->db->select('*');
		$this->db->from('bots_encuestas');
		$this->db->where('bots_keys_id', $id);
		$this->db->order_by("id", "desc");
		$this->db->limit(1);
		return $this->db->get()->result_array();
	}

	public function updateCycle($cycle, $user) {

		$data2 = array(
						'bot_cycle' => $cycle,
		);
		$this->db->where('id', $user);
		$return = $this->db->update('chatbot_web_user', $data2);
		return $return;
	}

	public function updateCycleTelegram($cycle, $user) {

		$data2 = array(
						'bot_cycle' => $cycle,
		);
		$this->db->where('id', $user);
		$return = $this->db->update('bots_telegram_user', $data2);
		return $return;
	}

	public function putLive($user, $account) {

			$data2 = array(
							'live' => 1,
							//'active' => 0,
							'live_active' => time(),
			);
			$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 putDatas($nombre, $telefono, $direccion, $tipo, $horario, $channel, $user, $account, $producto, $reclamo) {

		$data3 = array(
					'item1' => $nombre,
					'item2' => $telefono,
					'item3' => $direccion,
					'item4' => $tipo,
					'item5' => $horario,
					'item6' => $producto,
					'item7' => $reclamo,
					'lat' => 0,
					'lng' => 0,
					'user_id' => $user,
					'channel' => $channel,
					'account_id' => $account,
					'fecha' => date('Y-m-d H:i:s'),
		);
		$this->db->insert('data_abierta', $data3);

		return FALSE;
	}

	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 putAlerts($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'])) {
			$user = $test_usr[0]['id'];
		} else 	$user = '';

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

	public function getCombots($id) {
		$this->db->select('*');
		$this->db->from('bots_keys_buttons_img');
		$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 getUsername($id) {
		$this->db->select('*');
		$this->db->from('user');
		$this->db->where('id', $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 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 getCycle($ip, $device, $account) {

		$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( empty( $test_usr ) ) {
			$return = false;
		} else {
			$user = $test_usr[0]['id'];

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

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

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

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


			} else {
				$return = false;
			}
		}

		return $return;

	}

	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 putMsgWeb($user, $msg, $type, $accountID, $botsKeyID, $keyID, $device, $word, $typeA, $chatbotId, $to) {

		$return = FALSE;

		$this->db->select('*');
		$this->db->from('chatbot_web_user');
		$this->db->where('id', $chatbotId);
		$this->db->order_by("id", "desc");
		$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,
							'chatbotId' => $chatbotId,
			);
			$this->db->insert('chatbot_web_user', $data2);
			$new_id = $this->db->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'),
				);
				$this->db->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' =>  base64_encode($word),
							'live' => 0,
							'chatbotId' => $chatbotId,
							'to' => $to,
			);
			$return = $this->db->insert('chatbot_web', $data);

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

			return $new_id;

		} else {

			if($test_usr[0]['active'] == 0) {

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

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

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

			$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' => base64_encode($word),
							'live' => 1,
							'chatbotId' => $chatbotId,
							'to' => $to,
			);
			$return = $this->db->insert('chatbot_web', $data);

			if($typeA == 4){

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

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

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

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

				return $test_usr[0]['id'];
			} else {
				return $test_usr[0]['id'];
			}
		}

		//return $return;
	}

	public function putMsgWebChat($user, $msg, $type, $accountID, $botsKeyID, $keyID, $device, $word, $typeA, $chatbotId, $to) {

		$return = FALSE;

		$this->db->select('*');
		$this->db->from('chatbot_web_user');
		$this->db->where('chatbotId', $chatbotId);
		$this->db->order_by("id", "desc");
		$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,
							'chatbotId' => $chatbotId,
			);
			$this->db->insert('chatbot_web_user', $data2);
			$new_id = $this->db->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'),
				);
				$this->db->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' =>  base64_encode($word),
							'live' => 0,
							'chatbotId' => $chatbotId,
							'to' => $to,
							'isbutton' => 1,
			);
			$return = $this->db->insert('chatbot_web', $data);

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

			return $new_id;

		} else {

			if($test_usr[0]['active'] == 0) {

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

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

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

			$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' => base64_encode($word),
							'live' => 1,
							'chatbotId' => $chatbotId,
							'to' => $to,
							'isbutton' => 1,
			);
			$return = $this->db->insert('chatbot_web', $data);

			if($typeA == 4){

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

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

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

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

				return $test_usr[0]['id'];
			} else {
				return $test_usr[0]['id'];
			}
		}

		//return $return;
	}

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

		$return = FALSE;

		$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);
		$this->db->order_by("id", "desc");
		$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();

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

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

			return $new_id;

		} else {

			if($test_usr[0]['active'] == 0) {

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

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

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

			$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' => 1,
			);
			$return = $this->db->insert('chatbot_web', $data);

			if($typeA == 4){

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

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

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

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

				return $test_usr[0]['id'];
			} else {
				return $test_usr[0]['id'];
			}
		}

		//return $return;
	}
	//$this->Md_bot->putImage($token, $data['upload_data']['file_url'], $user, $auth, $usr_dev);

	public function putImageuser($image, $chatbotId, $fogataTo, $user, $type, $device, $accountID) {

		$return = array();

		$this->db->select('*');
		$this->db->from('chatbot_web_user');
		$this->db->where('id', $chatbotId);
		$this->db->order_by("id", "desc");
		$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,
							'chatbotId' => $chatbotId,
			);
			$this->db->insert('chatbot_web_user', $data2);
			$new_id = $this->db->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'),
				);
				$this->db->insert('bots_alerts', $data3);
			}
			//true or false
			$data = array(
							'user' => $new_id,
							'msg' => $image,
							'time' => time(),
							'type' => $type,
							'account_id' => $accountID,
							'bots_key_id' => 0,
							'keyenc_id' => 0,
							'message' => base64_encode($image),
							'live' => 0,
							'chatbotId' => $chatbotId,
							'to' => $fogataTo,
							'isfile' => 1
			);
			$return = $this->db->insert('chatbot_web', $data);

			$data2['id'] = $new_id;
			$data2['bot_cycle'] = "";

			return $data2;

		} else {

			if($test_usr[0]['active'] == 0) {

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

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

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

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

			$data = array(
							'user' => $test_usr[0]['id'],
							'msg' => $image,
							'time' => time(),
							'type' => $type,
							'account_id' => $accountID,
							'bots_key_id' => 0,
							'keyenc_id' => 0,
							'message' => base64_encode($image),
							'live' => 1,
							'chatbotId' => $chatbotId,
							'to' => $fogataTo,
							'isfile' => 1
			);
			$return = $this->db->insert('chatbot_web', $data);

				return $test_usr[0];
			} else {
				return $test_usr[0];
			}
		}

	}

	public function putImage($image, $user, $auth, $device, $accountID) {

		$return = array();

		$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);
		$this->db->order_by("id", "desc");
		$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();

			if($new_id) {
				$data3 = array(
								'account_id' => $accountID,
								'type' => 3,
								'active' => 1,
								'user_id' => $new_id,
								'fecha' => date('Y-m-d H:i:s'),
				);
				$this->db->insert('bots_alerts', $data3);
			}
			//true or false
			$data = array(
							'user' => $new_id,
							'msg' => $image,
							'time' => time(),
							'type' => 5,
							'account_id' => $accountID,
							'bots_key_id' => 0,
							'keyenc_id' => 0,
							'message' => $this->db->escape($image),
							'live' => 0,
			);
			$return = $this->db->insert('chatbot_web', $data);

			$data2['id'] = $new_id;
			$data2['bot_cycle'] = "";

			return $data2;

		} else {

			if($test_usr[0]['active'] == 0) {

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

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

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

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

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

				return $test_usr[0];
			} else {
				return $test_usr[0];
			}
		}

	}

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

		$return = array();

		$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);
		$this->db->order_by("id", "desc");
		$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();

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

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

			$data2['id'] = $new_id;
			$data2['bot_cycle'] = "";

			return $data2;

		} else {

			if($test_usr[0]['active'] == 0) {

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

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

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

			$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' => 1,
			);
			$return = $this->db->insert('chatbot_web', $data);

			if($typeA == 4){

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

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

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

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

				return $test_usr[0];
			} else {
				return $test_usr[0];
			}
		}

		//return $return;
	}

	public function putMsgOld($user, $msg, $type, $accountID, $botsKeyID, $keyID, $device, $word) {
		$return = FALSE;
		$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 >= 7200) {
				$bot = 0;
				$agent = '';
			} else {
				$bot = $test_usr[0]['bot'];
				$agent = $test_usr[0]['agent'];
			}

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

			$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 updateOption($option_name, $option_value, $channel, $account_id) {
		$otherdb = $this->load->database('desarrollos', TRUE);
		$otherdb->select('*');
		$otherdb->from('bots_options');
		$otherdb->where('option_name', $option_name);
		$otherdb->where('channel', $channel);
		$otherdb->where('account_id', $account_id);
		$test_usr = $otherdb->get()->result_array();
		$return = FALSE;
		if( empty($test_usr) ) {
			$search_array = array();

			if(is_array($option_value)) {
				foreach ($option_value as $key => $value) {
					$search_array[$key] = $value;
				}
			}

			$data2 = array(
				'option_value' => json_encode($search_array),
				'option_name' => $option_name,
				'channel' => $channel,
				'account_id' => $account_id,
				'date_updated' => date("Y-m-d H:i:s"),
			);
			$otherdb->insert('bots_options', $data2);
			$return = $search_array;

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

			if (!empty($test_usr[0]['option_value'])) {
				$search_array = json_decode($test_usr[0]['option_value'], TRUE);
			} else {
				$search_array = array();
			}

			if(is_array($option_value)) {
				foreach ($option_value as $key => $value) {
					$search_array[$key] = $value;
				}
			}
			$data2 = array(
				'option_value' => json_encode($search_array),
				'date_updated' => date("Y-m-d H:i:s"),
			);
			$otherdb->where('id', $test_usr[0]['id']);
			$otherdb->update('bots_options', $data2);
			$return = $search_array;
		}
		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 getTelegram($id) {
		$this->db->select('*');
		$this->db->from('telegram_accounts');
		$this->db-> where('telegram_id', $id);
		return $this->db->get()->result_array();
	}

	public function putMsgBots($user, $type, $accountID, $botsKeyID, $word, $channel, $name, $email, $phone) {
		if($channel == "telegram"){


			$this->db->select('*');
			$this->db->from('bots_telegram_user');
			$this->db->where('user_id', $user);
			$this->db->where('account_id', $accountID);
			$this->db->order_by("id", "desc");
			$test_usr = $this->db->get()->result_array();

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

				$data2 = array(
								'username' => $name,
								'email' => $email,
								'phone' => $phone,
								'account_id' => $accountID,
								'last_active' => date('Y-m-d H:i:s'),
								'checkx' => 0,
								'active' => 0,
								'bot' => 0,
								'date_created' => date('Y-m-d H:i:s'),
								'agent' => 0,
								'bot_cycle' => 0,
								'bot_step' => 0,
								'user_id' => $user,
				);
				$this->db->insert('bots_telegram_user', $data2);
				$new_id = $this->db->insert_id();
				if($new_id) {
					$data3 = array(
									'account_id' => $accountID,
									'type' => 5,
									'active' => 0,
									'user_id' => $new_id,
									'fecha' => date('Y-m-d H:i:s'),
					);
					$this->db->insert('bots_alerts', $data3);

					$data = array(
						'user' => $new_id,
						'timex' => time(),
						'type' => $type,
						'account_id' => $accountID,
						'bots_key_id' => $botsKeyID,
						'mensaje' => base64_encode($word),
					);
					$this->db->insert('bots_telegram', $data);
					$return = array(
						'id' => $new_id,
						'username' => $name,
						'email' => $email,
						'phone' => $phone,
						'account_id' => $accountID,
						'last_active' => date('Y-m-d H:i:s'),
						'checkx' => 0,
						'active' => 0,
						'bot' => 0,
						'date_created' => date('Y-m-d H:i:s'),
						'agent' => 0,
						'bot_cycle' => 0,
						'bot_step' => 0,
						'user_id' => 0,
					);
				} else {
					$return = array();
				}
				return $return;
			} else {

				if($test_usr[0]['bot'] == 0) {
				$timer = time() - strtotime($test_usr[0]['last_active']);
				if($timer >= 7200) {
					$bot = 0;
					$agent = 0;
				} else {
					if(isset($test_usr[0]['bot'])) $bot = $test_usr[0]['bot'];
					else $bot = 0;
					if(isset($test_usr[0]['agent'])) $agent = $test_usr[0]['agent'];
					else $agent = 0;
				}
				$data2 = array(
								'bot' => $bot,
								'checkx' => 0,
								'last_active' => date("Y-m-d H:i:s"),
								'agent' => $agent,
				);
				$this->db->where('id', $test_usr[0]['id']);
				$this->db->update('bots_telegram_user', $data2);
				$data = array(
								'user' => $test_usr[0]['id'],
								'timex' => time(),
								'type' => $type,
								'account_id' => $accountID,
								'bots_key_id' => $botsKeyID,
								'mensaje' => base64_encode($word),
				);
				$test = $this->db->insert('bots_telegram', $data);

					return $test_usr[0];
				} else {
					return $test_usr[0];
				}
			}
		}
	}

	public function getTelegramuser($id) {

		$this->db->select('*');
		$this->db->from('bots_telegram_user');
		$this->db->where('id', $id);
		$test_usr = $this->db->get()->result_array();

		return $test_usr;
		
	}

	public function putTelegramenc($id, $type) {

		$data2 = array(
			'step' => $type,
		);
		$this->db->where('id', $id);
		$return = $this->db->update('bots_telegram_user', $data2);
		return $return;
		
	}

	public function sendTelegram($token, $chatId, $msg) {

		//$msg=urlencode($msg);
		
		$api = "https://api.telegram.org/bot".$token."/";
		$url= $api."sendMessage?chat_id=".$chatId."&parse_mode=HTML&text=".$msg;
		file_get_contents($url);
	}

	public function sendTelegramphoto($token, $chatId, $msg) {

		//$msg=urlencode($msg);
		
		$api = "https://api.telegram.org/bot".$token."/";
		$url= $api."sendPhoto?chat_id=".$chatId."&parse_mode=HTML&photo=".$msg;
		file_get_contents($url);
	}

	public function amazonRegognition($image1, $image2) {


		$dir ="/var/www/html/uploads/sitels/";
		if (!file_exists($dir)) {
			mkdir($dir, 0755);
		}
		$avatarUrl1 = "";
		$avatarUrl2 = "";

		$extension1 = pathinfo(parse_url($image1, PHP_URL_PATH), PATHINFO_EXTENSION);
		$extension2 = pathinfo(parse_url($image2, PHP_URL_PATH), PATHINFO_EXTENSION);


		$filename1 = "tl_".time()."_1.".$extension1;
		$filename2 = "tl_".time()."_2.".$extension2;

		if($filename1) {
			file_put_contents($dir.$filename1, file_get_contents($image1));
			$avatarUrl1 = 'https://api.fogatabot.com/uploads/sitels/'.$filename1;
		}
		if($filename2) {
			file_put_contents($dir.$filename2, file_get_contents($image2));
			$avatarUrl2 = 'https://api.fogatabot.com/uploads/sitels/'.$filename2;
		}


		error_log("amazon ".$avatarUrl1."-".$avatarUrl2);

		$curl = curl_init();

		curl_setopt_array($curl, array(
		  CURLOPT_URL => "http://149.28.104.183:9002/rekognition",
		  CURLOPT_RETURNTRANSFER => true,
		  CURLOPT_ENCODING => "",
		  CURLOPT_MAXREDIRS => 10,
		  CURLOPT_TIMEOUT => 0,
		  CURLOPT_FOLLOWLOCATION => true,
		  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
		  CURLOPT_CUSTOMREQUEST => "POST",
		  CURLOPT_POSTFIELDS => array('ImageA'=> new CURLFILE($dir.$filename1),'ImageB'=> new CURLFILE($dir.$filename2),'QualityFilter' => 'AUTO','SimilarityThreshold' => '80'),
		));
		
		$response = curl_exec($curl);
		

		error_log("amazon ".$response);
		curl_close($curl);
		$response = json_decode($response);

		return $response;
	}

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

		$return = FALSE;

		$this->db->select('*');
		$this->db->from('chatbot_web_user');
		$this->db->where('id', $user);
		$this->db->order_by("id", "desc");
		$test_usr = $this->db->get()->result_array();

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

			if($test_usr[0]['active'] == 0) {

				$timer = time() - strtotime($test_usr[0]['last_active']);
	
				if($timer >= 7200) {
					$bot = 0;
					$agent = '';
				} else {
					$bot = $test_usr[0]['bot'];
					$agent = $test_usr[0]['agent'];
				}
	
				$data2 = array(
								'bot' => $bot,
								'check' => 0,
								'last_active' => date("Y-m-d H:i:s"),
								'agent' => $agent,
				);
	
				$this->db->where('id', $test_usr[0]['id']);
				$return = $this->db->update('chatbot_web_user', $data2);
	
				if(($type != 0 && $test_usr[0]['bot'] == 0) || $type == 0) {
					$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' => 1,
					);
					$return = $this->db->insert('chatbot_web', $data);
				} 

	
				if($typeA == 4){
	
					$this->db->select('*');
					$this->db->from('bot_cycle');
					$this->db->where('user_id', $test_usr[0]['id']);
					$botCycle = $this->db->get()->result_array();
	
					if( empty($botCycle) ) {
						$data = array(
										'user_id' => $test_usr[0]['id'],
										'active' => 1,
										'key_id' => $botsKeyID,
						);
						$this->db->insert('bot_cycle', $data);
					} else {
						$data2 = array(
										'active' => 1,
										'key_id' => $botsKeyID,
						);
						$this->db->where('user_id', $test_usr[0]['id']);
						$return = $this->db->update('bot_cycle', $data2);
					}
	
				} else {
					$this->db->select('*');
					$this->db->from('bot_cycle');
					$this->db->where('user_id', $test_usr[0]['id']);
					$botCycle = $this->db->get()->result_array();
	
					if( empty($botCycle) ) {
						$data = array(
										'user_id' => $test_usr[0]['id'],
										'active' => 0,
										'key_id' => $botsKeyID,
						);
						$this->db->insert('bot_cycle', $data);
					} else {
						$data2 = array(
										'active' => 0,
										'key_id' => $botsKeyID,
						);
						$this->db->where('user_id', $test_usr[0]['id']);
						$return = $this->db->update('bot_cycle', $data2);
					}
				}
	
					return $test_usr[0]['id'];
				} else {
					return $test_usr[0]['id'];
				}

		}
		//return $return;
	}

	public function putLiveSupport($user, $account) {

		$data2 = array(
						'live' => 1,
						//'active' => 0,
						'live_active' => time(),
		);
		$this->db->where('id', $user);
		$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);
		$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 getLivesupport($account, $ip, $device) {

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

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

			return $test_usr;

		} else 	return false;

	}

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

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

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

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

	public function getCycleSupport($ip) {

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

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

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

			$return2 = $this->db->get()->result_array();

			if( empty( $return2 ) ) {
				$return = false;
			} else if($return2[0]['active']){

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

				if( $return2[0]['key_id']  == 0) $return = false;
				else $return = $return2[0]['key_id'];


			} else {
				$return = false;
			}
		}

		return $return;

	}

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

			$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);
			}




		return $return;
	}
}
