<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cek Nota Penjualan</title>
    <link href="https://mienyinyir.com/struk/assets/img/logo.png' ?>" rel="shortcut icon" type="image/x-icon">
    <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 p-5 md:p-10">

    <div class="max-w-4xl mx-auto bg-white p-6 rounded-lg shadow-md">
        <h2 class="text-2xl font-bold mb-6 text-gray-800">Riwayat Nota Penjualan</h2>

        <form method="GET" action="" class="mb-8 flex gap-2">
            <div class="flex-1">
                <label class="block text-sm font-medium text-gray-700 mb-1">Masukkan Nomor HP Anda:</label>
                <input type="text" name="no_hp" required 
                       placeholder="Contoh: 08123456789"
                       value="<?php echo isset($_GET['no_hp']) ? htmlspecialchars($_GET['no_hp']) : ''; ?>"
                       class="w-full border border-gray-300 rounded-lg px-4 py-2 focus:ring-2 focus:ring-blue-500 outline-none">
            </div>
            <button type="submit" class="mt-6 bg-blue-600 hover:bg-blue-700 text-white font-semibold px-6 py-2 rounded-lg transition">
                Cari Nota
            </button>
        </form>

        <?php
        if (isset($_GET['no_hp']) && !empty($_GET['no_hp'])) {
            // Koneksi Database (Sesuaikan dengan data Anda)
            include "koneksi.php";

            if (!$conn) {
                die("<p class='text-red-500'>Koneksi gagal: " . mysqli_connect_error() . "</p>");
            }

            $no_hp = mysqli_real_escape_string($conn, $_GET['no_hp']);

            // Query Data
            $query = "SELECT no_faktur_penjualan, tgl_penjualan, netto,no_sha
                      FROM tabel_penjualan 
                      WHERE notelp = '$no_hp' and tgl_penjualan >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)
                      ORDER BY tgl_penjualan DESC";
            
            $result = mysqli_query($conn, $query);

            if (mysqli_num_rows($result) > 0) {
                echo '<div class="overflow-x-auto">
                        <table class="w-full text-left border-collapse">
                            <thead>
                                <tr class="bg-gray-200 text-gray-700 uppercase text-sm">
                                    <th class="p-3 border">Tanggal</th>
                                    <th class="p-3 border">Total (Netto)</th>
                                    <th class="p-3 border text-center">Aksi</th>
                                </tr>
                            </thead>
                            <tbody>';
                
                while ($row = mysqli_fetch_assoc($result)) {
                    echo "<tr class='hover:bg-gray-50 border-b text-gray-600'>
                            <td class='p-3 border'>".date('d M Y', strtotime($row['tgl_penjualan']))."</td>
                            <td class='p-3 border font-semibold'>Rp ".number_format($row['netto'], 0, ',', '.')."</td>
                            <td class='p-3 border text-center'>
                                <a href='https://mienyinyir.com/struk/mobile/menu/lihat_invoice/".$row['no_sha']."'
                                    class='bg-green-500 hover:bg-green-600 text-white text-xs py-1 px-3 rounded inline-block'>
                                    Lihat
                                </a>

                            </td>
                        </tr>";
                }
                
                echo '    </tbody>
                        </table>
                      </div>';
            } else {
                echo "<p class='text-orange-500 bg-orange-50 p-4 rounded-lg'>Data tidak ditemukan untuk nomor HP tersebut.</p>";
            }
            mysqli_close($conn);
        }
        ?>
    </div>

</body>
</html>