<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

ob_start();
if (session_status() === PHP_SESSION_NONE) { 
    session_start(); 
}

// Core framework configurations
require_once('../titancore/titanconfig.php');

if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit;
}

// Inject standard layout shell & enforce charset compliance
require_once('../titancore/titanheader.php');
mysqli_set_charset($conn, "utf8mb4");

// Dynamic contextual payload mapping
$uid_fetch = $_SESSION['user_id'];
$user_query = $conn->query("SELECT * FROM users WHERE id='$uid_fetch'");
$u_row = $user_query->fetch_assoc() ?? [];
$u_name = $u_row['user_name'] ?? $_SESSION['user_name'] ?? 'User';
$u_mobile = $u_row['phone'] ?? '0000000000';
$wallet_bal = $u_row['wallet'] ?? 0.00;

// Resolve platform identity details
$site_name = "TitanApi"; 
$settings_query = $conn->query("SELECT site_name FROM settings LIMIT 1");
if ($settings_query && $settings_query->num_rows > 0) {
    $set_row = $settings_query->fetch_assoc();
    if (!empty($set_row['site_name'])) {
        $site_name = htmlspecialchars($set_row['site_name'], ENT_QUOTES, 'UTF-8');
    }
}
?>

<div class="container-fluid px-4 py-4">
    <div class="row mb-4">
        <div class="col-12">
            <div class="card bg-white shadow-sm border border-light-subtle">
                <div class="row gx-0 align-items-center p-3">
                    <div class="col-auto d-flex align-items-center">
                        <div class="p-3 bg-success-subtle rounded-circle me-3 d-flex align-items-center justify-content-center" style="width: 50px; height: 50px;">
                            <span class="fas fa-wallet text-success fs-4"></span>
                        </div>
                        <div>
                            <h6 class="text-muted small uppercase mb-0">Current Balance</h6>
                            <h4 class="text-success fw-bold mb-0">₹<?php echo number_format($wallet_bal, 2); ?></h4>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="row g-4">
        <div class="col-lg-6">
            <div class="card shadow-sm border-0 h-100 border-top border-4 border-primary">
                <div class="card-header bg-white py-3 border-bottom text-center">
                    <h5 class="mb-0 fw-bold"><i class="fas fa-bolt me-2 text-warning"></i>Instant Wallet TopUp</h5>
                </div>
                <div class="card-body bg-body-tertiary p-4">
                    <form method="POST" action="akpaytm/txnProcess.php" autocomplete="off">
                        
                        <input type="hidden" name="phone" value="<?= htmlspecialchars($u_mobile, ENT_QUOTES, 'UTF-8') ?>">
                        <input type="hidden" name="id" value="<?= (int)$uid_fetch ?>">

                        <div class="mb-4">
                            <label class="form-label fs-6 fw-bold text-dark">Enter Amount (₹)</label>
                            <div class="input-group input-group-lg">
                                <span class="input-group-text bg-white text-dark fw-bold">₹</span>
                                <input type="number" 
                                       class="form-control shadow-sm fw-bold text-primary" 
                                       placeholder="e.g. 500" 
                                       name="amount" 
                                       min="50" 
                                       max="10000" 
                                       required>
                            </div>
                            <div class="form-text text-muted small mt-2">Accepted platform thresholds: ₹50.00 up to ₹10,000.00 maximum.</div>
                        </div>

                        <div class="d-grid">
                            <button type="submit" name="akupi" class="btn btn-primary btn-lg fw-bold shadow-sm">
                                <i class="fas fa-qrcode me-2"></i>Generate QR & Pay
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <div class="col-lg-6">
            <div class="card shadow-sm border-0 h-100 border-top border-4 border-info">
                <div class="card-header bg-white py-3 border-bottom">
                    <h6 class="mb-0 fw-bold text-dark"><i class="fas fa-history me-2 text-info"></i>Recent Transactions</h6>
                </div>
                <div class="card-body p-0 bg-white">
                    <div class="table-responsive">
                        <table class="table table-striped table-hover align-middle mb-0" style="font-size: 0.9rem;">
                            <thead class="table-light text-secondary">
                                <tr>
                                    <th class="ps-3 py-3">Date</th>
                                    <th>Amount</th>
                                    <th>Txn ID / UTR</th>
                                    <th class="text-end pe-3">Status</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $req_hist = $conn->query("SELECT * FROM wallethistory WHERE userid='$u_mobile' ORDER BY id DESC LIMIT 6");
                                if ($req_hist && $req_hist->num_rows > 0) {
                                    while ($row = $req_hist->fetch_assoc()) {
                                        $dbStatus = trim($row['status'] ?? '0');
                                        
                                        // Standard Integer Mapping Table Rules
                                        if ($dbStatus === '1') {
                                            $displayStatus = 'SUCCESS';
                                            $badgeClass    = 'bg-success';
                                        } elseif ($dbStatus === '0') {
                                            $displayStatus = 'PENDING';
                                            $badgeClass    = 'bg-warning text-dark';
                                        } else {
                                            $displayStatus = 'FAILED';
                                            $badgeClass    = 'bg-danger';
                                        }
                                ?>
                                <tr>
                                    <td class="ps-3 py-3 text-muted">
                                        <?php echo isset($row['request_date']) ? date('d M, Y', strtotime($row['request_date'])) : date('d M, Y'); ?>
                                    </td>
                                    <td class="fw-bold text-success">₹<?php echo number_format($row['amount'], 2); ?></td>
                                    <td class="font-monospace text-secondary"><?php echo htmlspecialchars($row['txn_id'] ?? 'N/A', ENT_QUOTES, 'UTF-8'); ?></td>
                                    <td class="text-end pe-3">
                                        <span class="badge <?php echo $badgeClass; ?> px-2.5 py-2 uppercase"><?php echo $displayStatus; ?></span>
                                    </td>
                                </tr>
                                <?php 
                                    } 
                                } else { 
                                ?>
                                <tr>
                                    <td colspan="4" class="text-center py-5 text-muted fst-italic">No recent top-ups logged inside your profile history.</td>
                                </tr>
                                <?php } ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php 
require_once('../titancore/TitanFooter.php'); 
ob_end_flush();
?>