<?php
session_start();
include("connection.php");

$email    = $_SESSION['email'] ?? '';
$username = $_SESSION['username'] ?? '';

if (isset($_POST['submit_kyc'])) {

    $fname    = mysqli_real_escape_string($myConn, $_POST['fname']);
    $lname    = mysqli_real_escape_string($myConn, $_POST['lname']);
    $gender   = mysqli_real_escape_string($myConn, $_POST['gender']);
    $address  = mysqli_real_escape_string($myConn, $_POST['address']);
    $city     = mysqli_real_escape_string($myConn, $_POST['city']);
    $country  = mysqli_real_escape_string($myConn, $_POST['country']);
    $doc_type = mysqli_real_escape_string($myConn, $_POST['doc_type']);
    $status   = "Pending";
    $date     = date("Y-m-d H:i:s");

    $uploadDir = "app/uploads/";

    $idfront = time().'_front_'.$_FILES['idfront']['name'];
    $idback  = time().'_back_'.$_FILES['idback']['name'];

    move_uploaded_file($_FILES['idfront']['tmp_name'], $uploadDir.$idfront);
    move_uploaded_file($_FILES['idback']['tmp_name'], $uploadDir.$idback);

    $query = "INSERT INTO kyc 
    (fname,lname,email,username,gender,address,city,country,doc_type,idfront,idback,status,date)
    VALUES
    ('$fname','$lname','$email','$username','$gender','$address','$city','$country','$doc_type','$idfront','$idback','$status','$date')";

    if(mysqli_query($myConn,$query)){
        $success = "KYC submitted successfully. Awaiting approval.";
    } else {
        $error = "Submission failed.";
    }
}
?>
<!DOCTYPE html>
<html>
<head>
<title>KYC Verification</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-5">
<div class="card shadow">
<div class="card-header bg-primary text-white"><h4>KYC Verification</h4></div>
<div class="card-body">
<?php if(isset($success)) echo "<div class='alert alert-success'>$success</div>"; ?>
<?php if(isset($error)) echo "<div class='alert alert-danger'>$error</div>"; ?>

<form method="POST" enctype="multipart/form-data">
<input class="form-control mb-2" name="fname" placeholder="First Name" required>
<input class="form-control mb-2" name="lname" placeholder="Last Name" required>
<select class="form-control mb-2" name="gender" required>
<option value="">Gender</option><option>Male</option><option>Female</option>
</select>
<input class="form-control mb-2" name="address" placeholder="Address" required>
<input class="form-control mb-2" name="city" placeholder="City" required>
<input class="form-control mb-2" name="country" placeholder="Country" required>
<select class="form-control mb-2" name="doc_type" required>
<option value="">Document Type</option>
<option>Passport</option><option>Driver License</option><option>National ID</option>
</select>
<input type="file" class="form-control mb-2" name="idfront" required>
<input type="file" class="form-control mb-2" name="idback" required>
<button class="btn btn-primary w-100" name="submit_kyc">Submit KYC</button>
</form>
</div></div></div>
</body>
</html>
