<?php

namespace App\Controllers;

use CodeIgniter\API\ResponseTrait;

class Loan extends BaseController
{
    use ResponseTrait;

    var $request;

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

    public function loanApply(){
/*
 	uid  uuid,
	loan_amount INT DEFAULT 0,
	payment_month INT DEFAULT 0,
	sales_agent VARCHAR(50),
	gender VARCHAR(20),
	marital_status VARCHAR(20),
	email VARCHAR(100),
	address VARCHAR(150),
	state VARCHAR(50),
	country VARCHAR(3),
	loan_detail TEXT,
 */
       // header('Access-Control-Allow-Origin: "*" ');
        //header("Access-Control-Allow-Headers: Origin, X-API-KEY, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method, Access-Control-Allow-Headers, Authorization, observe, enctype, Content-Length, X-Csrf-Token");
//        log_message('critical', "0003");
//        header("Access-Control-Allow-Methods: GET, PUT, POST, DELETE, PATCH, OPTIONS");
//        header("Access-Control-Allow-Credentials: true");
//        header("Access-Control-Max-Age: 3600");
//        header('content-type: application/json; charset=utf-8');

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

        try {
            $json_data     =json_encode($data);
            $serialize_data=serialize($data);
            /*
             *employment
loan_reference
disbursement
             */
        //    $data2     =json_decode($data);

            //log_message('error', "JSON      -->" . $json_data);
            log_message('error', "BEFORE THE LOG      -->");

            log_message('error', "UID2---=>" . $data["customer_uid"]);

            log_message('error', "UID2-employment--=>" . serialize( $data["employment"]));
            log_message('error', "UID2-loan_reference--=>" . serialize( $data["loan_reference"]));
            log_message('error', "UID2-disbursement--=>" . serialize( $data["disbursement"]));

            log_message('error', "UID3-employment--=>" . serialize( $data->employment));
            log_message('error', "UID3-loan_reference--=>" . serialize( $data->loan_reference));
            log_message('error', "UID3-disbursement--=>" . serialize( $data->disbursement));

        //    log_message('error', "UID3---=>" . $data2->customer_uid);
        //    log_message('error', "UID4---=>" . $data2["customer_uid"]);


            log_message('error', "---------------------------------------------------------------->");
            log_message('error', "SERIALIZE -->" . $serialize_data);
            log_message('error', "---------------------------------------------------------------->");
        } catch (Exception $e) {
            echo 'Caught exception: ',  $e->getMessage(), "\n";
        }



        $addedData =[];
        if (
            $data['customer_uid'] != '' &&
            $data['loan_amount'] != '' &&
            $data['payment_month'] != '' &&
            $data['sales_agent'] != '' &&
            $data['marital_status'] != '' &&
            $data['email'] != '' &&
            $data['address'] != '' &&
            $data['state'] != '' &&
            $data['country'] != ''
        ) {
            $udata = [
                'customer_uid' => $data['customer_uid'],
                'loan_amount' => $data['loan_amount'],
                'payment_month' => $data['payment_month'],
                'sales_agent' => $data['sales_agent'],
                'marital_status' => $data['marital_status'],
                'email' => $data['email'],
                'address' => $data['address'],
                'state' => $data['state'],
                'country' => $data['country'],
                'loan_detail' => json_encode($data)
            ];

            $addedData = $this->insert_db('applications', $udata);

        }
        return $this->respond($addedData, 200);
    }
    public function loanlist(){

        return $this->respond([], 200);
    }
}