<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Otp_login extends REF_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model('login_details_model', '', TRUE);
    }
    public function get_log_otp_details() {
        /*** Check OTP Permission - Start ***/
        
        $user = $this->user_model->get_by_id($this->user_data['user_id']);
        $this->permissions = unserialize_str($user->permissions);
        $main_controllers = array_keys($this->permissions);
        
        /*** Check OTP Permission - End ***/
        
        $args = unserialize($_POST['ids']);
        $auto_id = $_POST['auto_id'];
        $user_id = $this->user_data['user_id'];
        $page_type_id = $_POST['page_type_id'];
        $url = $_POST['url'];
        $req_controller = $_POST['controller'];
        $bill_permissions = unserialize( $user->billing_permissions );
        
        if((in_array('otp',$main_controllers)) && OTP_SEND == TRUE && array_key_exists( $req_controller, $bill_permissions ) ) {
            $data = $this->login_details_model->get_log_otp_details($args,$auto_id,$user_id,$url,$page_type_id);
        } else {
            $this->login_details_model->insert_log_details_by_id($args,$auto_id,$user_id,$url,$page_type_id);
        }
        echo json_encode($data);
        exit(0);
    }
    
    public function update_otp_status() {
        $args = unserialize($_POST['ids']);
        $service_menu_id = $args['service_menu_id'];
        $menu_type = $args['menu_type'];
        $user_id = $this->user_data['user_id'];
        $controller = $this->login_details_model->get_controller_name($service_menu_id,$menu_type);
        $rows = $this->login_details_model->check_otp_correct($user_id,$_POST['otp_value'])->num_rows();
        $used_rows = $this->login_details_model->used_otp_rows($user_id,$_POST['otp_value'])->num_rows();
        $details = $this->login_details_model->check_otp_correct($user_id,$_POST['otp_value'])->row();
        $data = array();
        #if((!empty($details)) && (!empty($controller))) {
        if($rows > 0 ) {
            if(($controller->id == $details->service_menu_id) ) {
                $data['row'] = $details;
                $data['status'] = 1;
                $this->login_details_model->update_otp_status($details->id);
            } else {
                $data['controller'] = $this->login_details_model->get_controller_name($details->service_menu_id,$details->menu_type)->controller_name;
                $data['correct_url'] = $details->url;
                $data['correct_id'] = $details->id;
                $data['status'] = 2;
            }
        } elseif($used_rows > 0)
            $data['status'] = 3;
        
        else 
            $data['status'] = $rows;
        
        echo json_encode($data, JSON_UNESCAPED_SLASHES);
        exit(0);
    }
    
    public function update_otp_status_by_correct_id() {
        $id = $_POST['id'];
        $data = $this->login_details_model->update_otp_status($id);
        echo json_encode($data);
        exit(0);
    }
} ?>