<?php

namespace App\Controllers;

class AirFlow extends BaseController
{

    public function flowNewAccount(){

        $raw_json = file_get_contents('php://input');
        $in = json_decode($raw_json, true);
        $out =[];
        $this->processLoadData();
        log_message('critical', "AirFlow::flowNewAccount ********* ALL   ".serialize($in) );
        return  []; //json_encode( $final_out );
    }

    public function flowAppReminder(){

        $raw_json = file_get_contents('php://input');
        $in = json_decode($raw_json, true);
        $out =[];
        // $this->processLoadData();
        log_message('critical', "AirFlow::flowAppReminder ********* ALL   ".serialize($in) );
        return  []; //json_encode( $final_out );
    }

    public function flowDailyReports(){

        $raw_json = file_get_contents('php://input');
        $in = json_decode($raw_json, true);
        $out =[];
        // $this->processLoadData();
        log_message('critical', "AirFlow::flowDailyReports ********* ALL   ".serialize($in) );
        return  []; //json_encode( $final_out );
    }

    public function flowGeneralAlerts(){

        $raw_json = file_get_contents('php://input');
        $in = json_decode($raw_json, true);
        $out =[];
        // $this->processLoadData();
        log_message('critical', "AirFlow::flowGeneralAlerts ********* ALL   ".serialize($in) );
        return  []; //json_encode( $final_out );
    }

    public function flowNewEmployers(){

        $raw_json = file_get_contents('php://input');
        $in = json_decode($raw_json, true);
        $out =[];
        // $this->processLoadData();
        log_message('critical', "AirFlow::flowNewEmployers ********* ALL   ".serialize($in) );
        return  []; //json_encode( $final_out );
    }

    public function flowPaymentReminder(){

        $raw_json = file_get_contents('php://input');
        $in = json_decode($raw_json, true);
        $out =[];
        // $this->processLoadData();
        log_message('critical', "AirFlow::flowPaymentReminder ********* ALL   ".serialize($in) );
        return  []; //json_encode( $final_out );
    }

    public function flowVerifyEmpoyer(){

        $raw_json = file_get_contents('php://input');
        $in = json_decode($raw_json, true);
        $out =[];
        // $this->processLoadData();
        log_message('critical', "AirFlow::flowVerifyEmpoyer ********* ALL   ".serialize($in) );
        return  []; //json_encode( $final_out );
    }


    private function processLoadData(){
        $mysql  ="SELECT uid,customer_uid,loan_detail 
                  FROM applications 
                  WHERE pc = 0 limit 2";

        $query = $this->db->query($mysql);
      //  $row   = $query->getRowArray();
        while ($row = $query->getUnbufferedRow()) {

           // log_message('critical', "AirFlow::processLoadData ********* ALL 0000  ". $row->customer_uid );
                  $this->parseLoadDetail($row->uid,$row->customer_uid,$row->loan_detail);

//            echo $row->customer_uid;
//            echo $row->name;
//            echo $row->body;
        }

//        foreach ($row as $rw){
//            log_message('critical', "AirFlow::processLoadData ********* =========================in " );
//            log_message('critical', "AirFlow::flowNewAccount ********* ALL        ".serialize($rw) );
//           // log_message('critical', "AirFlow::processLoadData ********* ALL       ". $rw["customer_uid"] );
//            log_message('critical', "AirFlow::processLoadData ********* ALL 0000  ". $rw[0]["customer_uid"] );
//            log_message('critical', "AirFlow::processLoadData ********* ALL 0000  ". $rw->customer_uid );
//            log_message('critical', "AirFlow::processLoadData ********* =========================out" );
//
//        }

        return 0;
    }

    private function parseLoadDetail($uid,$customer_uid,$loan_detail){
        $loan_detail_array = json_decode( $loan_detail );
     //   var_dump( $loan_detail_array  );
        //$loan_detail_array =  unserialize($loan_detail->loan_reference);
        log_message('critical', "AirFlow::processLoadData*parseLoadDetail ***customer_uid     *** ". $loan_detail_array->customer_uid );
        log_message('critical', "AirFlow::processLoadData*parseLoadDetail ***loan_reference   *** ". $loan_detail_array->loan_reference );
       // log_message('critical', "AirFlow::processLoadData*parseLoadDetail ***loan_reference*** ". $loan_detail_array["loan_reference"] );

        while ($lr = json_decode($loan_detail_array->loan_reference )  ){
            var_dump($lr);
        }

        return 0;
    }

}