<?php
/**
 * Created by PhpStorm.
 * User: freuddebian
 * Date: 16/10/18
 * Time: 18:34
 */

require_once ('../database/Requester.php');

$tag="";

$postdata = file_get_contents("php://input");
$request=null;
if(isset($postdata)) {
    $request = json_decode($postdata);

}

if($postdata!=null && isset($request->tag)) {

    switch ($request->tag){
        case "credit_demands":
            getCreditDemand($request);
            break;
        case "ask_credit":
            askCreditToParrain($request);
            break;
        case 'credit_demands_of_agent':
            getCreditDemandeAgent($request);
            break;
        case 'update_ask_credit':
            treatDemand($request);
            break;
        default:
            echo json_encode($request);
    }
}
function treatDemand($request)
{
    if(isset($request->phone)){
        if (isset($request->id)) {
            $q = new Requester($request->id,$request->lang);
            echo $q->treatDemand($request->phone);
        } else {
        echo json_encode(['error' => 'id missing']);
        }
    }else{
        echo json_encode(['error' => 'phone missing']);
    }
}
function getCreditDemandeAgent($request){
    if(isset($request->id)){
        $q=new Requester($request->id,$request->lang);
        echo $q->getAgentSendDemande();
    }else{
        echo json_encode(['error'=>'id missing']);
    }
}

 function saveCreditDemande($request){

}
function askCreditToParrain($request){
    $montant = $request->montant;
    $phone=$request->phone;
    $code=$request->code;
    $requester=new Requester($phone,$request->lang);
    echo $requester->storeCreditAsk($phone,$montant,$code);
   }
function getCreditDemand($request){
        if(isset($request->id)){
            $q=new Requester($request->id);
            echo $q->getAgentReceiveDemande();
        }else{
            echo json_encode(['error'=>'id missing']);
        }

}
