<?php
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../includes/auth.php';
requireMember();

$userId = $_SESSION['user_id'];
$transaksiId = (int)($_GET['transaksi_id'] ?? 0);

$stmt = $pdo->prepare("SELECT * FROM transaksi WHERE id = ? AND user_id = ? LIMIT 1");
$stmt->execute([$transaksiId, $userId]);
$transaksi = $stmt->fetch();

if (!$transaksi) {
    setFlash('error', 'Transaksi tidak ditemukan.');
    header('Location: ' . url('/member/riwayat.php'));
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!validateCsrf()) { setFlash('error', 'CSRF error.'); header("Location: " . url("/member/upload_bukti.php?transaksi_id={$transaksiId}")); exit; }
    if (!isset($_FILES['bukti']) || $_FILES['bukti']['error'] !== UPLOAD_ERR_OK) { setFlash('error', 'Gagal upload.'); header("Location: " . url("/member/upload_bukti.php?transaksi_id={$transaksiId}")); exit; }

    $file = $_FILES['bukti'];
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mime = finfo_file($finfo, $file['tmp_name']);
    finfo_close($finfo);
    if (!in_array($mime, ['image/jpeg','image/png'])) { setFlash('error', 'Format harus JPG/PNG.'); header("Location: " . url("/member/upload_bukti.php?transaksi_id={$transaksiId}")); exit; }
    if ($file['size'] > 2*1024*1024) { setFlash('error', 'Max 2MB.'); header("Location: " . url("/member/upload_bukti.php?transaksi_id={$transaksiId}")); exit; }

    $filename = uniqid() . '_' . basename($file['name']);
    $dir = __DIR__ . '/../assets/uploads/';
    if (!is_dir($dir)) mkdir($dir, 0755, true);
    if (move_uploaded_file($file['tmp_name'], $dir . $filename)) {
        $pdo->prepare("UPDATE transaksi SET bukti_bayar = ? WHERE id = ? AND user_id = ?")->execute([$filename, $transaksiId, $userId]);
        setFlash('success', 'Bukti berhasil diupload! Menunggu validasi.');
        header('Location: ' . url('/member/riwayat.php'));
        exit;
    }
    setFlash('error', 'Gagal simpan file.'); header("Location: " . url("/member/upload_bukti.php?transaksi_id={$transaksiId}")); exit;
}

require_once __DIR__ . '/../includes/header.php';
?>
<div class="max-w-2xl mx-auto px-4 py-8">
    <div class="mb-8 animate-fadeUp"><h1 class="text-3xl font-black mb-2">Upload <span class="text-volt">Bukti Bayar</span></h1><p class="text-muted text-sm">Upload bukti transfer untuk menyelesaikan pembayaran</p></div>
    <div class="card mb-6 animate-fadeUp d1">
        <div class="grid grid-cols-2 gap-4">
            <div><p class="text-xs text-muted font-bold uppercase mb-1">ID Transaksi</p><p class="font-bold text-volt">#<?= $transaksiId ?></p></div>
            <div><p class="text-xs text-muted font-bold uppercase mb-1">Total</p><p class="font-bold text-xl">Rp <?= number_format($transaksi['total_harga'],0,',','.') ?></p></div>
            <div><p class="text-xs text-muted font-bold uppercase mb-1">Status</p><span class="badge-pending">Pending</span></div>
            <div><p class="text-xs text-muted font-bold uppercase mb-1">Tanggal</p><p class="text-sm"><?= date('d M Y, H:i', strtotime($transaksi['created_at'])) ?></p></div>
        </div>
    </div>
    <div class="card mb-6 border-l-4 border-l-volt animate-fadeUp d2">
        <h3 class="font-bold mb-3 text-volt">💳 Transfer ke Rekening</h3>
        <p class="text-sm"><span class="text-muted">Bank:</span> <b>BCA</b></p>
        <p class="text-sm"><span class="text-muted">No. Rek:</span> <b>1234567890</b></p>
        <p class="text-sm"><span class="text-muted">A/N:</span> <b>FITZONE GYM</b></p>
    </div>
    <div class="card animate-fadeUp d3">
        <h3 class="font-bold mb-4">📎 Upload Bukti Transfer</h3>
        <form method="POST" enctype="multipart/form-data">
            <input type="hidden" name="csrf_token" value="<?= csrfToken() ?>">
            <div class="mb-4">
                <label class="form-label" for="bukti-input">Foto Bukti Transfer</label>
                <input type="file" id="bukti-input" name="bukti" accept="image/jpeg,image/png" class="form-input" required>
                <p class="text-xs text-muted mt-1">JPG/PNG, max 2MB</p>
            </div>
            <div class="upload-preview mb-6 hidden" id="upload-preview"></div>
            <button type="submit" class="btn-volt w-full py-3">Upload Bukti Pembayaran</button>
        </form>
    </div>
</div>
<?php require_once __DIR__ . '/../includes/footer.php'; ?>
