<?php
session_start();
if (!isset($_SESSION['username'])) { header("location:login"); exit; }
ob_start();
include 'master.php';

$id = (int)($_GET['id'] ?? 0);
if (!$id) { header("location:kyc-submissions"); exit; }

$res = $conn->query("SELECT * FROM kyc_submissions WHERE id = '$id' LIMIT 1");
$kyc = $res ? $res->fetch_assoc() : null;
if (!$kyc) { header("location:kyc-submissions"); exit; }
?>
        <div class="page-wrapper">
            <div class="page-content-wrapper">
                <div class="page-content">
                    <div class="page-breadcrumb d-none d-md-flex align-items-center mb-3">
                        <div class="breadcrumb-title pr-3">KYC</div>
                        <div class="pl-3">
                            <nav aria-label="breadcrumb">
                                <ol class="breadcrumb mb-0 p-0">
                                    <li class="breadcrumb-item"><a href="kyc-submissions">KYC Submissions</a></li>
                                    <li class="breadcrumb-item active" aria-current="page">Review #<?php echo $id; ?></li>
                                </ol>
                            </nav>
                        </div>
                    </div><br>

                    <?php
                    $sBadge = ['Pending'=>'warning','Approved'=>'success','Rejected'=>'danger'][$kyc['status']] ?? 'secondary';
                    ?>

                    <div class="row">
                        <!-- Details -->
                        <div class="col-lg-8">

                            <!-- Personal -->
                            <div class="card mb-4">
                                <div class="card-body">
                                    <h5 class="card-title">Personal Details</h5>
                                    <table class="table table-sm table-bordered">
                                        <tr><th width="35%">Full Name</th><td><?php echo htmlspecialchars($kyc['full_name']); ?></td></tr>
                                        <tr><th>Email</th><td><?php echo htmlspecialchars($kyc['user_email']); ?></td></tr>
                                        <tr><th>Phone</th><td><?php echo htmlspecialchars($kyc['phone']); ?></td></tr>
                                        <tr><th>Title</th><td><?php echo htmlspecialchars($kyc['title']); ?></td></tr>
                                        <tr><th>Gender</th><td><?php echo htmlspecialchars($kyc['gender']); ?></td></tr>
                                        <tr><th>Date of Birth</th><td><?php echo htmlspecialchars($kyc['dob']); ?></td></tr>
                                        <tr><th>Zipcode</th><td><?php echo htmlspecialchars($kyc['zipcode']); ?></td></tr>
                                    </table>
                                </div>
                            </div>

                            <!-- Employment -->
                            <div class="card mb-4">
                                <div class="card-body">
                                    <h5 class="card-title">Employment Information</h5>
                                    <table class="table table-sm table-bordered">
                                        <tr><th width="35%">SSN / NI</th><td><?php echo htmlspecialchars($kyc['ssn']); ?></td></tr>
                                        <tr><th>Account Type</th><td><?php echo htmlspecialchars($kyc['account_type']); ?></td></tr>
                                        <tr><th>Employment Type</th><td><?php echo htmlspecialchars($kyc['employment_type']); ?></td></tr>
                                        <tr><th>Annual Income</th><td><?php echo htmlspecialchars($kyc['annual_income']); ?></td></tr>
                                    </table>
                                </div>
                            </div>

                            <!-- Address -->
                            <div class="card mb-4">
                                <div class="card-body">
                                    <h5 class="card-title">Address</h5>
                                    <table class="table table-sm table-bordered">
                                        <tr><th width="35%">Address</th><td><?php echo htmlspecialchars($kyc['address']); ?></td></tr>
                                        <tr><th>City</th><td><?php echo htmlspecialchars($kyc['city']); ?></td></tr>
                                        <tr><th>State</th><td><?php echo htmlspecialchars($kyc['state']); ?></td></tr>
                                        <tr><th>Country</th><td><?php echo htmlspecialchars($kyc['country']); ?></td></tr>
                                    </table>
                                </div>
                            </div>

                            <!-- Next of Kin -->
                            <div class="card mb-4">
                                <div class="card-body">
                                    <h5 class="card-title">Next of Kin</h5>
                                    <table class="table table-sm table-bordered">
                                        <tr><th width="35%">Name</th><td><?php echo htmlspecialchars($kyc['kin_name']); ?></td></tr>
                                        <tr><th>Address</th><td><?php echo htmlspecialchars($kyc['kin_address']); ?></td></tr>
                                        <tr><th>Relationship</th><td><?php echo htmlspecialchars($kyc['kin_relationship']); ?></td></tr>
                                        <tr><th>Age</th><td><?php echo htmlspecialchars($kyc['kin_age']); ?></td></tr>
                                    </table>
                                </div>
                            </div>

                            <!-- Documents -->
                            <div class="card mb-4">
                                <div class="card-body">
                                    <h5 class="card-title">Uploaded Documents — <?php echo htmlspecialchars($kyc['document_type']); ?></h5>
                                    <div class="row mt-3">
                                        <div class="col-md-4 text-center mb-3">
                                            <p class="font-weight-bold small mb-1">Front Side</p>
                                            <a href="../uploads/kyc/<?php echo $kyc['frontimg']; ?>" target="_blank">
                                                <img src="../uploads/kyc/<?php echo $kyc['frontimg']; ?>" class="img-fluid rounded border" style="max-height:180px;" onerror="this.src='assets/images/placeholder.png'">
                                            </a>
                                        </div>
                                        <div class="col-md-4 text-center mb-3">
                                            <p class="font-weight-bold small mb-1">Back Side</p>
                                            <a href="../uploads/kyc/<?php echo $kyc['backimg']; ?>" target="_blank">
                                                <img src="../uploads/kyc/<?php echo $kyc['backimg']; ?>" class="img-fluid rounded border" style="max-height:180px;" onerror="this.src='assets/images/placeholder.png'">
                                            </a>
                                        </div>
                                        <div class="col-md-4 text-center mb-3">
                                            <p class="font-weight-bold small mb-1">Passport Photo</p>
                                            <a href="../uploads/kyc/<?php echo $kyc['photo']; ?>" target="_blank">
                                                <img src="../uploads/kyc/<?php echo $kyc['photo']; ?>" class="img-fluid rounded border" style="max-height:180px;" onerror="this.src='assets/images/placeholder.png'">
                                            </a>
                                        </div>
                                    </div>
                                </div>
                            </div>

                        </div>

                        <!-- Action Panel -->
                        <div class="col-lg-4">
                            <div class="card">
                                <div class="card-body">
                                    <h5 class="card-title">Review Decision</h5>
                                    <p class="mb-2">Current Status: <span class="badge badge-<?php echo $sBadge; ?>"><?php echo $kyc['status']; ?></span></p>
                                    <p class="text-muted small">Submitted: <?php echo $kyc['date_added']; ?></p>
                                    <hr/>
                                    <form method="POST" action="update-kyc-status">
                                        <input type="hidden" name="id" value="<?php echo $id; ?>">
                                        <div class="form-group">
                                            <label class="font-weight-bold">Status</label>
                                            <select name="status" class="form-control">
                                                <?php foreach (['Pending','Approved','Rejected'] as $s): ?>
                                                <option value="<?php echo $s; ?>" <?php echo $kyc['status'] === $s ? 'selected' : ''; ?>><?php echo $s; ?></option>
                                                <?php endforeach; ?>
                                            </select>
                                        </div>
                                        <div class="form-group">
                                            <label class="font-weight-bold">Admin Notes <small class="text-muted">(shown to user if rejected)</small></label>
                                            <textarea name="admin_notes" rows="4" class="form-control" placeholder="Optional notes..."><?php echo htmlspecialchars($kyc['admin_notes'] ?? ''); ?></textarea>
                                        </div>
                                        <button type="submit" class="btn btn-primary btn-block">
                                            <i class="bx bx-save mr-1"></i> Save Decision
                                        </button>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </div>
        <div class="overlay toggle-btn-mobile"></div>
        <a href="javaScript:;" class="back-to-top"><i class='bx bxs-up-arrow-alt'></i></a>
<?php include 'footer.php'; ?>
