<link rel='stylesheet' href='https://fonts.googleapis.com/css?family=Roboto'>
<body>
<style>
    body {
    background: #222D32;
    font-family: 'Roboto', sans-serif;
    background-color: #1f1f1f;
			color: white;
			display: flex;
			align-items: center;
			justify-content: center;
			height: 100vh;
			margin: 0;
}
.hello {
    text-align:center;
    color:black;
    font-weight: bold;
    font-size:40px;
    display: flex;
    justify-content: center;
    align-items: center;
    border: 10px solid black;
    background-color: white;
    padding: 20px;
    border-radius: 10px;    
    text-align: center;
    
   
}

     .message-container {
		background-color: white;
		padding: 20px;
		border-radius: 10px;
		text-align: center;
    	}


</style>

<script>
		setTimeout(function() {
			window.location.href = "/?a=kyc";
		}, 5000);
	</script>

<div class="message-container">

<?php
// Connect to the database
require_once('db.php');

// Check if a submission with the same email address already exists
$email = $_POST['email'];
$sql = "SELECT id FROM submissions WHERE email='$email'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    // A submission with the same email address already exists
    echo '<div class="hello">
    You have already submitted your KYC documents. Please wait for approval or contact support for assistance.
    </div>';
} else {
    // Insert the submission into the database
    $name = $_POST['name'];
    $document1 = $_FILES['document1']['name'];
    $document2 = $_FILES['document2']['name'];

    $sql = "INSERT INTO submissions (name, email, document1, document2) VALUES ('$name', '$email', '$document1', '$document2')";
    if ($conn->query($sql) === TRUE) {
        echo '<div class="hello">
        Your ID has been received and is pending approval.
        </div>';
    } else {
        echo '<div class="hello">
        Error submitting KYC documents: ' . $conn->error . '
        </div>';
    }

    // Upload the documents to the server
    $target_dir = "uploads/";
    $target_file1 = $target_dir . basename($document1);
    $target_file2 = $target_dir . basename($document2);

    if(move_uploaded_file($_FILES['document1']['tmp_name'], $target_file1)) {
        echo '<div class="hello">
        The file '. basename( $_FILES['document1']['name']). ' has been uploaded.
        </div><br></br>';

    } else {
        echo '<div class="hello">
        Sorry, there was an error uploading your file.
        </div>';
    }

    if($document2) {
        if(move_uploaded_file($_FILES['document2']['tmp_name'], $target_file2)) {
            echo '<div class="hello">   
            The file '. basename( $_FILES['document2']['name']). ' has been uploaded.
            </div>';

        } else {
            echo '<div class="hello">
            Sorry, there was an error uploading your file.
            </div>';
        }
    }
}

$conn->close();
?>
</div>