<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends CI_Controller {

    public function generateOtp(){
		if(isset($_POST['mobile']) && $_POST['mobile']!==''){
			$check_existing_record = $this->model->getRowData('register',['mobile'=>$_POST['mobile']]);
            $otp=mt_rand(100000, 999999);
            if($check_existing_record){
                if($check_existing_record['is_disable'] == 0){
                    if($this->model->edit('register',['otp'=>$otp],['id'=>$check_existing_record['id']]))
                    {
                        echo json_encode(['return'=>true,'msg'=>$otp]);
                        // if($this->model->send_otp($_POST['mobile'],$otp))
                        // {
                        //     echo json_encode(['return'=>true,'msg'=>'']);
                        // }
                        
                    }
                    else
                    {
                        echo json_encode(['return'=>false,'msg'=>'Otp not generated']);
                        // if($this->model->send_otp($_POST['mobile'],$otp))
                        // {
                        //     echo json_encode(['return'=>true,'msg'=>'']);
                        // }
                    }
                }else{
                    echo json_encode(['return'=>false,'msg'=>'You are suspended!!']);
                }
                
            }
            else
            {
                echo json_encode(['return'=>false,'msg'=>'Mobile doesn`t exists!!']);
            }
		}
		else
		{
            echo json_encode(['return'=>false,'msg'=>'Mobile number not received']);
		}
	}
	public function verifyOtp(){
		if(isset($_POST['mobile']) && $_POST['mobile']!=='' && isset($_POST['otp']) && $_POST['otp']!==''){
			$countRecord = $this->model->countData('register',['mobile'=>$_POST['mobile'],'otp'=>$_POST['otp']]);
            if($countRecord !== 0)
            {
                $userData = $this->model->getRowData('register',['mobile'=>$_POST['mobile']]);
                
                echo json_encode(['return'=>true,'msg'=>'Otp verified','user_id'=>$userData['id']]);
            }
            else
            {
                echo json_encode(['return'=>false,'msg'=>'Incorrect otp']);
            }
		}
		else
		{
            echo json_encode(['return'=>false,'msg'=>'Mobile,otp are required']);
		}
	}
	public function UserLogin()
	{
        if(isset($_POST['mobile']) && $_POST['mobile'] !== '')
        {
            $userData = $this->model->getRowData('register',['mobile'=>$_POST['mobile']]);
            // if($userData['is_disable'] == 1){
            //     echo json_encode(['return'=>false,'msg'=>'You are suspended1!']);
            // }else{
                $this->session->set_userdata(array('user_mobile' => $userData['mobile'],'user_logged_in'=>TRUE,'user_id'=>$userData['id']) );
                    echo json_encode(['return'=>true,'msg'=>'Logged in Successfully','user_id'=>$userData['id']]);
                
            // }            
        }
        else
        {
            echo json_encode(['return'=>false,'msg'=>'mobile is required']);
        }
        
	}
	    
}
