<?php

require_once '../../vendor/autoload.php';
require_once '../../include/DatabaseConn.php';
require_once '../../include/Constants.php';
require_once '../../models/Invoice.php';

$db = new DatabaseConn();
$link = $db->connect();

$std = new stdClass();

$id = filter_input(INPUT_GET, "id", FILTER_VALIDATE_INT);
$email = filter_input(INPUT_GET, "email", FILTER_VALIDATE_EMAIL);
$capital = filter_input(INPUT_GET, "capital", FILTER_VALIDATE_FLOAT);
$profit = filter_input(INPUT_GET, "profit", FILTER_VALIDATE_FLOAT);

header("content-type: application/json");

if (is_null($id)) {
    $error = 'Token missing in request body';
    $std->error = $error;
    echo (json_encode($std));
    die();
} else if (is_null($email)) {
    $error = 'Token missing in request body';
    $std->error = $error;
    echo (json_encode($std));
    die();
} else if (is_null($capital)) {
    $error = 'Enter a valid capital';
    $std->error = $error;
    echo (json_encode($std));
    die();
} else if (is_null($profit)) {
    $error = 'Enter a valid profit';
    $std->error = $error;
    echo (json_encode($std));
    die();
} else {
    $status = GROWING;
    $time = time();
    mysqli_query($link, "update invoice set amount = $capital, `cReturns` = $profit WHERE id = $id and `user` = '$email' ");
    if (mysqli_affected_rows($link) == 1) {
        $std->msg = PAID;
        echo (json_encode($std));
        die();
    } else {
        $error = 'Already updated';
        $std->error = $error;
        echo (json_encode($std));
        die();
    }
}

