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


class Md_visa 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 getTransac() {

		$otherdb = $this->load->database('visanet', TRUE);
		$otherdb->select('*');
		$otherdb->from('clientes');
		return $otherdb->get()->result_array();
	}

	public function getFactu() {

		$otherdb = $this->load->database('visanet', TRUE);
		$otherdb->select('*');
		$otherdb->from('facturas');
		return $otherdb->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 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,
			);
			$return = $this->db->insert('chatbot_web_user', $data2);

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