<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Allow-Headers: Content-Type");

include "/../config.php";

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $client_index = isset($_POST['client_index']) ? intval($_POST['client_index']) : 0;
    $document_number = isset($_POST['document_number']) ? $conn->real_escape_string($_POST['document_number']) : '';
    $address = isset($_POST['address']) ? $conn->real_escape_string($_POST['address']) : '';
    $selfie_id_document = isset($_POST['selfie_id_document']) ? $conn->real_escape_string($_POST['selfie_id_document']) : '';
    $front_side = isset($_POST['front_side']) ? $conn->real_escape_string($_POST['front_side']) : '';
    $back_side = isset($_POST['back_side']) ? $conn->real_escape_string($_POST['back_side']) : '';
    $proof_of_address = isset($_POST['proof_of_address']) ? $conn->real_escape_string($_POST['proof_of_address']) : '';

    if ($client_index && $document_number && $address) {
        $sql = "INSERT INTO admin_kyc 
                (client_index, document_number, address, selfie_id_document, front_side, back_side, proof_of_address) 
                VALUES 
                ('$client_index', '$document_number', '$address', '$selfie_id_document', '$front_side', '$back_side', '$proof_of_address')";

        if ($conn->query($sql) === TRUE) {
            echo json_encode(["status" => "success", "message" => "KYC data inserted successfully"]);
        } else {
            echo json_encode(["status" => "error", "message" => "DB Error: " . $conn->error]);
        }
    } else {
        echo json_encode(["status" => "error", "message" => "Missing required fields"]);
    }
} else {
    echo json_encode(["status" => "error", "message" => "Invalid request method"]);
}
?>
