<?php

namespace App\Controllers;

use CodeIgniter\API\ResponseTrait;
use CodeIgniter\HTTP\ResponseInterface;

class BkoReports extends BaseController
{
    use ResponseTrait;
    // protected $db;

    var $request;

    function __construct()
    {
        $this->request = \Config\Services::request();
    }

    public function startedApplication() :ResponseInterface
    {
        //SELECT uid,loan_amount,payment_month,sales_agent,gender,marital_status,email,address,state,country,status,added,updated FROM applications;
        $query = $this->db->query("SELECT c.firstname, c.lastname, a.uid,a.loan_amount,
                                   a.payment_month,a.sales_agent,a.gender,
                                   a.marital_status,a.email,a.address,
                                   a.state,a.country,a.status,a.added,
                                   a.updated 
                                   FROM applications a 
                                   LEFT JOIN customers c ON c.uid = a.customer_uid  WHERE a.status = 1 ");
        $row   = $query->getResult('array');
        $data = [
            'call_return' => '100',
            'records' =>  $row
        ];
        return $this->respond($data, 200);
    }

    public function pendingApplication() :ResponseInterface
    {
        $pendingQuery  = $this->applicationQuery(1);
        //SELECT uid,loan_amount,payment_month,sales_agent,gender,marital_status,email,address,state,country,status,added,updated FROM applications;
        $query = $this->db->query("$pendingQuery ");
        $row   = $query->getResult('array');
        $data = [
            'call_return' => '100',
            'records' =>  $row
        ];
        return $this->respond($data, 200);
    }

    private function applicationQuery($status): string{
        return "
                SELECT c.firstname, c.lastname, a.uid,a.loan_amount,
                a.payment_month,a.sales_agent,a.gender,
                a.marital_status,a.email,a.address,
                a.state,a.country,a.status,a.added,
                a.updated,
                e.name AS employer_name ,
                e.uid AS employer_uid 
                FROM applications a 
                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 a.status = $status 
        ";
   }

    public function readyApplication() :ResponseInterface
    {
        $readyQuery  = $this->applicationQuery(2);
        //SELECT uid,loan_amount,payment_month,sales_agent,gender,marital_status,email,address,state,country,status,added,updated FROM applications;
        $query = $this->db->query("$readyQuery");
        $row   = $query->getResult('array');
        $data = [
            'call_return' => '100',
            'records' =>  $row
        ];
        return $this->respond($data, 200);
    }

    public function approvedApplication() :ResponseInterface
    {
        $approvedQuery  = $this->applicationQuery(5);
        //SELECT uid,loan_amount,payment_month,sales_agent,gender,marital_status,email,address,state,country,status,added,updated FROM applications;
        $query = $this->db->query($approvedQuery);
        $row   = $query->getResult('array');
        $data = [
            'call_return' => '100',
            'records' =>  $row
        ];
        return $this->respond($data, 200);
    }

    public function rejectedApplication() :ResponseInterface
    {
        $rejectedQuery  = $this->applicationQuery(7);
        //SELECT uid,loan_amount,payment_month,sales_agent,gender,marital_status,email,address,state,country,status,added,updated FROM applications;
        $query = $this->db->query($rejectedQuery);
        $row   = $query->getResult('array');
        $data = [
            'call_return' => '100',
            'records' =>  $row
        ];
        return $this->respond($data, 200);
    }

public function employersList()  :ResponseInterface {
    $query = $this->db->query("SELECT uid,name FROM employer_sector");
    $employer_sector   = $query->getResult('array');

    $query = $this->db->query("SELECT uid,name FROM salary_sources");
    $salary_sources   = $query->getResult('array');

    $query = $this->db->query("SELECT * FROM employers");
    $row   = $query->getResult('array');
    $data = [
        'call_return' => '100',
        'salary_sources' => $salary_sources,
        'employer_sector' => $employer_sector,
        'records' =>  $row
    ];
    return $this->respond($data, 200);
}

    public function employerAdd():ResponseInterface{
        $data = $this->request->getPost();
        $insert_data = $data;
        $result = $this-> insert_db('employers', $insert_data);
        $result_data = [
            'call_return' => '100',
            'result' =>$result
        ];
        return $this->respond($result_data, 200);
    }

    public function signatoryList()  :ResponseInterface {

        $query = $this->db->query("SELECT e.name AS employer_name,  es.* FROM employers_signatory es LEFT JOIN employers e ON e.id =  es.employer_id");
        $row   = $query->getResult('array');
        $data = [
            'call_return' => '100',
            'records' =>  $row
        ];
        return $this->respond($data, 200);
    }
    public function signatoryAdd():ResponseInterface{
        $data = $this->request->getPost();
$loc = 100;
        $employer_uid = $data["employer_uid"];
        if ( $employer_uid !=''){
            $query = $this->db->query("SELECT id FROM employers WHERE uid = '".$employer_uid."' ");
            $loc = $employer_uid;
            $row   = $query->getResult('array');
            if ($row && $row[0]['id']> 0){
                $loc = 300;
                $insert_data['employer_id'] =$row[0]['id'];
                $insert_data['name'] = $data['name'];
                $insert_data['email'] = $data['email'];
                $insert_data['title'] = $data['title'];
                $insert_data['phone'] = $data['phone'];
                $result = $this-> insert_db('employers_signatory', $insert_data);
            }
            else
            {
                $result = ['fail'=>'Not Added'];
            }
        }

     //   $insert_data = $data;

        $result_data = [
            'call_return' => '100',
            'loc' => $loc,
            'result' =>$result
        ];
        return $this->respond($result_data, 200);
    }

    public function listUsers():ResponseInterface{

        $query = $this->db->query("SELECT id,uid,username, email,firstname,lastname,status, added,updated FROM bko_users");
        $row   = $query->getResult('array');
        $data = [
            'call_return' => '100',
            'records' =>  $row
        ];
        return $this->respond($data, 200);
    }
}