<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

include_once '../config/database.php';
include_once '../objects/investor.php'; 

session_start();
date_default_timezone_set('Asia/Bangkok');

$database = new Database();
$db = $database->getConnection();

$investor = new Investor($db);

$data = json_decode(file_get_contents("php://input"));

$investor->id = $data->id;
$investor->name = $data->name;
$investor->position = $data->position; 
$investor->phone = $data->phone;
$investor->email = $data->email;
$investor->nrc = $data->nrc;
$investor->address = $data->address;
$investor->entry_by = $_SESSION['user'];
$investor->update_by = $_SESSION['user'];

if ($data->id) {
    if ($investor->update()) {
        echo json_encode(array("message" => "updated"));
    } else {
        echo json_encode(array("message" => "error"));
    }
} else {
    if ($investor->exist()) {
        echo json_encode(array("message" => "duplicate"));
    } elseif ($investor->create()) {
        echo json_encode(array("message" => "created"));
    } else {
        echo json_encode(array("message" => "error"));
    }
}
?>
