<?php
	
namespace App\Http\Controllers\API;

use App\Http\Controllers\API\JsonController as JsonController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Account;

    
class DataController extends JsonController{
    
    public function index(Request $request) { 
        $input = $request->all();
        switch ($input['requestName']) {
        case 'banks':
            $json['banks'] = \DB::table('banks')->get();
            $mgs='List of Banks';
        break;

        case 'accountTransections':
            $accountId=$input['accountId'];
            $record=Account::find($accountId);
            if($record){
                $json['accountId'] = $record->accountId;
                $json['bankId'] =  $record->bankId;
                $json['accountHolder'] =  $record->accountHolder;
                $json['accountNo'] =  $record->accountNo;
                $json['balance'] =  $record->balance;

                $transections=\DB::select(config('global.transectionsSQL'), [$accountId]);
                $json['transections'] =  $transections;

                if(!$transections) $json['transections']=array();

                $mgs='Transection history';

            }else{
                return $this->sendError('No User account found!', ''); 
            }
        break;

        case 'addAccount':
        case 'updateAccount':
        case 'deleteAccount':
            $controller = new AccountController;
            return $controller->index($request);
        break;

        case 'addTransection':
        case 'updateTransection':
        case 'deleteTransection':
            $controller = new TransectionController;
            return $controller->index($request);
        break;

        case 'perseSMS':
            $controller = new SMSController;
            return $controller->perseSMS($request);
        break;

        default:
            return $this->sendError('Error !', 'No parameter found !'); 
        break;

        
        }
        if(isset($json)) return $this->sendResponse($json, $mgs);
        else return $this->sendError('Error !', 'Unknown error !'); 
    } 
    
    
    
}