<?php

namespace App\Controllers;

use CodeIgniter\HTTP\ResponseInterface;

class Employment extends BaseController
{
    public function login() :ResponseInterface
    {
        $data = $this->request->getPost();
        $otp_ready = false;
        $verify_uid = '';
        $call_return = 0;
        $status_message = '';

        $row =[];
        if ( $data['application_uid'] !='' && $data['username'] && $data['password']){
            $sqlLogin =    "SELECT * FROM employer_checks WHERE application_uid='".$data['application_uid']."' AND username='".$data['username']."' AND password=md5('".$data['password']."') ";
            $query = $this->db->query(  $sqlLogin );
            $row   = $query->getResult('array');
            if ( $row[0]){
                $verify_uid =  $row[0]['uid'];
                $otp_ready = true;
                $call_return = 100;
            }
            else
            {
                $status_message = "Invalid Username or Password";
            }
        }else{
                $status_message = "Missing Required Parameters";
                $call_return =-1;
        }

        $data = [
            'call_return' => $call_return,
            'verify_uid'=> $verify_uid,
            'status_message'=>$status_message,
            'otp_ready' => $otp_ready,
          /*  'row' => $row,
            'records' =>  $data*/
        ];
        return $this->respond($data, 200);
    }

    public function verifyOTP() :ResponseInterface
    {
        $data = $this->request->getPost();
        $row =[];
        $session_valid = false;
        if ($data['verify_uid'] !='' && $data['otp'] !=''){

            $sqlVer = " SELECT a.loan_amount,c.firstname,c.lastname,e.name AS employer_name,
                       '1000000055' AS deposit_account_no,a.uid AS aplication_uid
                    FROM employer_checks ec 
                    LEFT JOIN applications a  ON ec.application_uid::text = a.uid::text 
                    LEFT JOIN customers c ON c.uid::text=a.customer_uid::text 
                    LEFT JOIN employers e ON e.uid::text = a.employer_uid::text  
                    WHERE ec.uid = '".$data['verify_uid']."' ";

            $query = $this->db->query(  $sqlVer );
            $rowV   = $query->getResult('array');
            if ( $rowV[0]){
                $row =  $rowV[0];
                $session_valid = true;
            }
        }

        $data = [
            'call_return' => '100',
            'session_valid' => $session_valid,
            'records' =>  $row
        ];
        return $this->respond($data, 200);
    }

    public function verifyEmployment() :ResponseInterface{

        $data = $this->request->getPost();

        $updateVerifyQr    = "UPDATE employer_checks SET 
                            education = '".$data["education"]."', 
                            grade= '".$data["grade"]."',
                            applicant_date= '".$data["applicant_date"]."',
                            ippis_number= '".$data["ippis_number"]."',
                            employers_name= '".$data["employers_name"]."',
                            designation= '".$data["designation"]."' 
                            WHERE uid =  '".$data["verify_uid"]."' ";

        log_message('error', "updateVerifyQr  -----   updateVerifyQr  -->" . $updateVerifyQr);
        $this->db->query($updateVerifyQr);

        $sqlU =    "SELECT * FROM employer_checks WHERE uid= '".$data['verify_uid']."'";
        $query = $this->db->query(  $sqlU );
        $row   = $query->getResult('array');
        if ( $row[0]){
            $out =  $row[0];
            $sqlApplication = "UPDATE applications SET status = 4 
                                WHERE status = 2 
                                AND uid= '".$out["application_uid"]."' ";
            $this->db->query($sqlApplication);
        }




        $row =[];
        $data = [
            'call_return' => '100',
            'records' =>  $row
        ];
        return $this->respond($data, 200);
    }

    public function loadApplication() :ResponseInterface
    {

        /*
            education
            grade
            applicant_date
            ippis_number
            employers_name
            designation


            education = '.$data["education"].',
            grade= '.$data["grade"].',
            applicant_date= '.$data["applicant_date"].',
            ippis_number= '.$data["ippis_number"].',
            employers_name= '.$data["employers_name"].',
            designation= '.$data["designation"].',

                       $sigCountUpdateQr = "UPDATE employers SET signatory_count = $sig_count WHERE uid = '$employer_uid' ";
                log_message('error', "updateSignatoryCount     sigCountUpdateQr  -->" . $sigCountUpdateQr);
                $this->db->query($sigCountUpdateQr);
         */

        $row =[];
        $data = [
            'call_return' => '100',
            'records' =>  $row
        ];
        return $this->respond($data, 200);
    }

}