<?php
require_once('../../vendor/autoload.php');
require_once('../init.php');

function error_res($status, $response_code, $message)
{
    $response = new stdClass;
    $response->status = $status;
    $response->response_code = $response_code;
    $response->message = $message;
    return json_encode($response);
}
if (isset($_SESSION['user_id'])) {
    extract($_POST);

    $user = User::find_by_id(isset($user_id) ? $user_id : $_SESSION['user_id']);
    if ($user) {

        $jamb_service = Jamb::find_by_id($id);
        $service_type = Service::find_by_id($jamb_service->service_id);
        $user_s = User::find_by_id($jamb_service->user_id);

        $wallet = Wallet::find_by_query("SELECT * FROM wallet WHERE user_id = $jamb_service->user_id");
        $wallet = array_shift($wallet);
        if ($type == 'done') {
            $jamb_service->status = 1;
            $in_transaction = new InTransaction;
            $in_transaction->user_id = $user_s->id;
            $in_transaction->wallet_id = $wallet->id;
            $in_transaction->search_query = $jamb_service->profile_code;
            $in_transaction->amount = $service_type->service_fee;
            $in_transaction->remaining_balance = 00000000;
            $in_transaction->service = $service_type->service_name;
            $in_transaction->date = $date;
            $in_transaction->status = 1;
            $in_transaction->save();

        } else {

            $jamb_service->status = 2;

            $wallet->balance += $service_type->service_fee;
            $wallet->save();

            $payment = new Payment;
            $payment->user_id = $user->id;
            $payment->amount = ($service_type->service_fee);
            $payment->reference = "Refunded";
            $payment->status = "Verified";
            $payment->date_created = $date;
            $payment->method = "Refund - JAMB Service";
            $payment->save();

        }

        if ($jamb_service->save()) {

            echo error_res(false, "00", "Status changed Successful");
            exit;
        } else {
            echo error_res(false, "03", "Status change Failed!");
            exit;
        }
    }

} else {
    echo error_res(false, "03", "Contact Administrator!");
    exit;
}
