<?php
header('Access-Control-Allow-Origin: *');
header(
    'Access-Control-Allow-Methods: HEAD, GET, POST, PUT, PATCH, DELETE, OPTIONS'
);
header(
    'Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method,Access-Control-Request-Headers, Authorization'
);
header('Content-Type: application/json');

include '../../../../config/db.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $data = json_decode(file_get_contents('php://input'));

    if (
        !empty($data->userId) &&
        !empty($data->famName) &&
        !empty($data->relationship)
    ) {
        $userId = mysqli_real_escape_string($con, $data->userId);
        $famName = mysqli_real_escape_string($con, $data->famName);
        $relationship = mysqli_real_escape_string($con, $data->relationship);
        $dateCreated = date('Y-m-d H:i:s');

        $query = "INSERT INTO `Family` (`userId`, `famName`, `relationship`, `status`, `dateCreated`)
            VALUES ('$userId', '$famName', '$relationship', '1', '$dateCreated')";
        // echo $query;
        if (mysqli_query($con, $query)) {
            echo json_encode([
                'status' => 1,
                'message' => 'Data added successfully.',
            ]);
            http_response_code(200);
        } else {
            echo json_encode([
                'status' => 0,
                'message' => mysqli_error($con),
            ]);
            http_response_code(200);
        }
    } else {
        echo json_encode([
            'status' => -1,
            'message' => 'Required fields are missing',
        ]);
        http_response_code(200);
    }
} else {
    echo json_encode([
        'status' => 0,
    ]);
    die();
}
