<?php

Class Api_models extends CI_Model
{
	public $number;
	public $device_token;
	public $device_type;
	public $device_id;
	public $otp;
	public $app_type;
	public $token;
	public $ref;

	function __construct()
	{
		parent::__construct();
		$this->mysql = $this->load->database('mysql',true);
	}

	function get_login()
	{
		$query_mysql =  $this->mysql->select('*')->from('operators_new')->where([ 'number' => trim($this->number) ])->get()->first_row('array');
		if(  $query_mysql['ref']!="" )
		{
			return $this->mysql->select('ref,lindex, loginname,fullname, accesslevel,number,imei_no, otp, is_verified, lastin, lastout, custref, depot,type, logintype, costcentre , contractarea, office, emailaddress, createdate, termsagree')->from('operators_new')->where([ 'number' => trim($this->number)])->get()->first_row('array');
		}
	}

	// Update OTP
	function update_otp()
	{
		$data['otp'] = $this->otp;
		$this->mysql->where('ref', $this->ref);
		$this->mysql->update('operators', $data);
	}

	/* verify otp query */

	function verified_otp()
	{
		$get_result = $this->mysql->select('ref')->from('operators_new')->where([ 'number' => trim($this->number), 'otp' => $this->otp ])->get()->first_row('array');
		if( $get_result['ref'] )
		{
			$data['device_token'] = $this->device_token;
			$data['device_type'] = $this->device_type;
			$data['is_verified'] = '1';
			$data['lastin'] = date( 'Y-m-d H:i:s' );
			$this->mysql->where('ref', $get_result['ref'])->update('operators_new', $data);
		}

		return $get_result;
	}

	// update token of logged in user
	function update_user_token()
	{
		$data['token'] = $this->token;
		$data['lastin'] = date( 'Y-m-d H:i:s' );
		$this->mysql->where('ref', $this->ref);
		return $this->mysql->update('operators_new', $data);
	}
}