<?php
session_start();
include('includes/config.php');

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

$id_pesanan = intval($_GET['id']);
$id_user = $_SESSION['user_id'];


$query = mysqli_query($conn, "SELECT p.*, pay.bukti_pembayaran, pay.status as status_bayar 
                              FROM pesanan p 
                              LEFT JOIN pembayaran pay ON p.id = pay.id_pesanan 
                              WHERE p.id = '$id_pesanan' AND p.id_user = '$id_user'");
$data = mysqli_fetch_assoc($query);

if(!$data){ echo "Pesanan tidak ditemukan."; exit(); }


if(isset($_POST['upload_ulang'])){
    if(isset($_FILES['bukti']) && $_FILES['bukti']['error'] === 0){
        $folder = 'uploads/bukti_pembayaran/';
        if(!is_dir($folder)) mkdir($folder, 0777, true);
        $file_name = time() . '_' . $_FILES['bukti']['name'];
        move_uploaded_file($_FILES['bukti']['tmp_name'], $folder . $file_name);
        
        
        mysqli_query($conn, "UPDATE pembayaran SET bukti_pembayaran = '$folder$file_name', status='Menunggu Verifikasi' WHERE id_pesanan = '$id_pesanan'");
        echo "<script>alert('Bukti berhasil diunggah!'); window.location.href='status_pesanan.php?id=$id_pesanan';</script>";
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Detail Status - Alfateeh Sepatuku</title>
    <style>
        body { font-family: 'Poppins', sans-serif; background: #f4f7f6; padding: 20px; }
        .card { max-width: 600px; margin: auto; background: #fff; padding: 25px; border-radius: 15px; box-shadow: 0 5px 15px rgba(0,0,0,0.1); }
        .section { margin-bottom: 20px; padding-bottom: 10px; border-bottom: 1px solid #eee; }
        .badge { padding: 5px 12px; border-radius: 5px; color: #fff; font-weight: bold; font-size: 0.8rem; }
        .bg-blue { background: #2196f3; }
        .bg-orange { background: #ff9800; }
        .bg-green { background: #4caf50; }
        .btn-upload { background: #1e88e5; color: white; padding: 10px; border: none; border-radius: 5px; cursor: pointer; width: 100%; margin-top: 10px; }
    </style>
</head>
<body>

<div class="card">
    <a href="pesanan_saya.php" style="text-decoration:none; color:#1e88e5;">← Kembali ke Pesanan Saya</a>
    
    <div class="section" style="margin-top:20px;">
        <h3>🚚 Status Pengiriman</h3>
        <p>Status: <span class="badge bg-blue"><?= $data['status'] ?></span></p>
        <p>No. Pesanan: <strong><?= $data['no_pesanan'] ?></strong></p>
    </div>

    <div class="section">
        <h3>💰 Status Pembayaran</h3>
        <p>Total: <strong>Rp <?= number_format($data['total'], 0, ',', '.') ?></strong></p>
        <p>Metode: <?= $data['metode_pembayaran'] ?></p>
        <p>Status Bayar: <span class="badge bg-orange"><?= $data['status_bayar'] ?? 'Belum Ada Data' ?></span></p>
        
        <div style="margin-top: 15px;">
            <strong>Bukti Pembayaran:</strong><br>
            <?php if($data['bukti_pembayaran']): ?>
                <img src="<?= $data['bukti_pembayaran'] ?>" style="width: 100%; max-height: 300px; object-fit: contain; margin-top: 10px; border: 1px solid #ddd;">
            <?php else: ?>
                <p style="color: red; font-style: italic;">Bukti belum diunggah.</p>
                <?php if($data['metode_pembayaran'] == 'Transfer Bank'): ?>
                    <form method="POST" enctype="multipart/form-data">
                        <input type="file" name="bukti" required>
                        <button type="submit" name="upload_ulang" class="btn-upload">Unggah Bukti Sekarang</button>
                    </form>
                <?php endif; ?>
            <?php endif; ?>
        </div>
    </div>

    <?php if(strpos(strtolower($data['status']), 'kirim') !== false): ?>
        <a href="proses_selesai.php?id=<?= $data['id'] ?>" 
           style="display:block; text-align:center; background:#4caf50; color:white; padding:15px; border-radius:10px; text-decoration:none; font-weight:bold; margin-top:20px;"
           onclick="return confirm('Sudah terima barang?')">
           KONFIRMASI BARANG DITERIMA
        </a>
    <?php endif; ?>
</div>

</body>
</html>