<?php
namespace App\Controllers;
use Config\Services;

class Registry extends BaseController{
    private $dbHandle;
    
    public function signupuser(){
        $this->dbHandle   = \Config\Database::connect();
        $dataToReturn = [];
        
        $now = date('Y-m-d H:i:s', time());
        
        //get the district
        $region = $this->request->getVar('region');
        $town = $this->request->getVar('town');
        $mmda = '';
            
        $R = $this->dbHandle->query("select mmda from `locations` where region='{$region}' and town='{$town}' order by mmda DESC limit 1");
		$A = $R->getResultArray();
        if(sizeof($A)>0){
            $mmda = $A[0]['mmda'];
        }
        
        //continue
        $data = [
            'transactdate'      => $now,
            'firstname'         => $this->request->getVar('firstname'),
            'phone'             => $this->request->getVar('phone'),
            'whatsapp'          => $this->request->getVar('whatsapp'),
            'region'            => $region,
            'district'          => $mmda,
            'town'              => $town,
            'password'          => $this->request->getVar('password')
		];
		//tests
		if (preg_match('/^(0|\+233)[25]\d{8}$/', $data['phone'])) {
		    $this->dbHandle->table('registry_users')->insert($data);
		    $dataToReturn['status'] = $this->dbHandle->error()['code'];
		    if($dataToReturn['status']!=0){
		        $dataToReturn['extra'] = "The Phone No is already being used by another account";
		    }
        }else {
            $dataToReturn['status'] = 2;
            $dataToReturn['extra'] = "Your Phone No was not recognized as valid";
        }
		
		return json_encode($dataToReturn, JSON_HEX_APOS | JSON_HEX_QUOT);
    }
    
    public function loginadmin(){
        $this->dbHandle   = \Config\Database::connect();
        $dataToReturn = [];
        
        $email = $this->request->getVar('email');
		$password = $this->request->getVar('password');
		
		$R = $this->dbHandle->table('registry_admins')->getWhere([
		    'email'=>$email,
		    'password'=>$password
		], 1, 0);
		
		$A = $R->getResultArray();
        if(sizeof($A)>0){
            $dataToReturn['status'] = 0;
            $dataToReturn['load'] = $A[0];
            $dataToReturn['extra'] = $A[0];
            
            //no resource bundle here
        }else{
            $dataToReturn['status'] = 1;
        }
        
		return json_encode($dataToReturn, JSON_HEX_APOS | JSON_HEX_QUOT);
    }
    
    public function loginuser(){
        $this->dbHandle   = \Config\Database::connect();
        $dataToReturn = [];
        
        $phone = $this->request->getVar('phone');
		$password = $this->request->getVar('password');
		
		$R = $this->dbHandle->table('registry_users')->getWhere([
		    'phone'=>$phone,
		    'password'=>$password
		], 1, 0);
		
		$A = $R->getResultArray();
        if(sizeof($A)>0){
            $dataToReturn['status'] = 0;
            $dataToReturn['load'] = $A[0];
            $dataToReturn['extra'] = [];
            
            //no resource bundle here
        }else{
            $dataToReturn['status'] = 1;
        }
        
		return json_encode($dataToReturn, JSON_HEX_APOS | JSON_HEX_QUOT);
    }
    
    public function logingeneral(){
        $this->dbHandle   = \Config\Database::connect();
        $dataToReturn = [];
        
        $email = $this->request->getVar('email');
		$password = $this->request->getVar('password');
		
		$R = $this->dbHandle->table('registry_general')->getWhere([
		    'email'=>$email,
		    'password'=>$password
		], 1, 0);
		
		$A = $R->getResultArray();
        if(sizeof($A)>0){
            $dataToReturn['status'] = 0;
            $dataToReturn['load'] = $A[0];
            $dataToReturn['extra'] = [];
            
            //get the resource bundle
            switch(strtolower($A[0]['resourcetype'])){
                case "vendor":
                $B = ($this->dbHandle->table('vendors')->getWhere([
        		    'companyid'=>$A[0]['resourceid']
        		], 1, 0))->getResultArray();
                if(sizeof($B)>0){ $dataToReturn['extra'] = $B[0]; }
                break;
            }
        }else{
            $dataToReturn['status'] = 1;
        }
        
		return json_encode($dataToReturn, JSON_HEX_APOS | JSON_HEX_QUOT);
    }

}