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

class Loginuser extends BaseController{
    private $dbHandle;

    public function index(){
        $this->dbHandle   = \Config\Database::connect();
        $dataToReturn = [];
        
        $email = $this->request->getVar('email');
		$password = $this->request->getVar('password');
		
		$R = $this->dbHandle->table('staff')->getWhere([
		    'email'=>$email,
		    'password'=>$password
		], 1, 0);
		
		$A = $R->getResultArray();
        if(sizeof($A)>0){
            $dataToReturn['status'] = 0;
            $dataToReturn['load'] = $A;
        }else{
            $dataToReturn['status'] = 1;
        }
        
		return json_encode($dataToReturn, JSON_HEX_APOS | JSON_HEX_QUOT);
    }
    
    public function vendor(){
        $this->dbHandle   = \Config\Database::connect();
        $dataToReturn = [];
        
        $email = $this->request->getVar('email');
		$password = $this->request->getVar('password');
		
		$R = $this->dbHandle->table('vendors')->getWhere([
		    'companyemail'=>$email,
		    'password'=>$password
		], 1, 0);
		
		$A = $R->getResultArray();
        if(sizeof($A)>0){
            $dataToReturn['status'] = 0;
            $dataToReturn['load'] = $A;
        }else{
            $dataToReturn['status'] = 1;
        }
        
		return json_encode($dataToReturn, JSON_HEX_APOS | JSON_HEX_QUOT);
    }
    
    public function setvendorpass(){
        $this->dbHandle   = \Config\Database::connect();
        $dataToReturn = [];
        
        $companyid = $this->request->getVar('companyid');
		$password = $this->request->getVar('password');
		
		$this->dbHandle->query("update `vendors` set `password`='{$password}' where `companyid`='{$companyid}'");
        $E = $this->dbHandle->error();
        
        $dataToReturn['status'] = $E['code'];
        
		return json_encode($dataToReturn, JSON_HEX_APOS | JSON_HEX_QUOT);
    }
}