<?php 
    include 'config.php';     
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Pesanan Berhasil - My Menu</title>
    <style>
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 1rem;
        }

        .header {
            text-align: center;
            color: white;
            margin-bottom: 1.5rem;
            padding: 0.5rem 0;
        }

        .header h2 {
            font-size: clamp(1.3rem, 4vw, 2rem);
            margin-bottom: 0.5rem;
            text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
        }

        /* Success Ticket */
        .success-ticket {
            background: white;
            border-radius: 20px;
            margin-bottom: 1.5rem;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            overflow: hidden;
            animation: slideUp 0.6s ease-out;
        }

        .ticket-header {
            background: linear-gradient(135deg, #27ae60, #2ecc71);
            color: white;
            padding: 1.5rem;
            text-align: center;
        }

        .ticket-header .success-icon {
            font-size: 3rem;
            margin-bottom: 0.5rem;
            display: block;
        }

        .ticket-header h3 {
            font-size: 1.3rem;
            margin-bottom: 0.5rem;
        }

        .order-number {
            font-size: 2rem;
            font-weight: bold;
            text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
        }

        .ticket-body {
            padding: 1.5rem;
        }

        .order-info {
            display: grid;
            gap: 0.8rem;
            font-size: 0.95rem;
        }

        .info-item {
            display: flex;
            align-items: flex-start;
            gap: 0.5rem;
        }

        .info-label {
            font-weight: 600;
            color: #333;
            min-width: 80px;
        }

        .info-value {
            color: #666;
            flex: 1;
        }

        .ticket-footer {
            background: #e8f5e8;
            padding: 1rem 1.5rem;
            border-top: 1px solid #d4edda;
            text-align: center;
        }

        .ticket-footer p {
            font-size: 0.9rem;
            color: #155724;
            margin: 0;
            font-weight: 500;
        }

        .ticket-footer .highlight {
            font-weight: 700;
            color: #0f4229;
        }

        /* Action Buttons */
        .action-section {
            background: white;
            border-radius: 15px;
            padding: 1.5rem;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
            text-align: center;
        }

        .button-group {
            display: flex;
            gap: 1rem;
            margin-top: 1rem;
        }

        .btn-primary {
            background: linear-gradient(135deg, #04AA6D, #45a049);
            color: white;
            border: none;
            padding: 1rem 2rem;
            font-size: 1.1rem;
            font-weight: 600;
            border-radius: 12px;
            cursor: pointer;
            transition: all 0.3s ease;
            flex: 1;
            text-decoration: none;
            display: inline-block;
            box-shadow: 0 8px 20px rgba(4, 170, 109, 0.3);
        }

        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 12px 30px rgba(4, 170, 109, 0.4);
        }

        .btn-secondary {
            background: #6c757d;
            color: white;
            border: none;
            padding: 1rem 2rem;
            font-size: 1.1rem;
            font-weight: 600;
            border-radius: 12px;
            cursor: pointer;
            transition: all 0.3s ease;
            flex: 1;
            text-decoration: none;
            display: inline-block;
        }

        .btn-secondary:hover {
            background: #5a6268;
            transform: translateY(-2px);
        }

        /* Alerts */
        .alert {
            padding: 1rem;
            border-radius: 10px;
            margin-bottom: 1rem;
            position: relative;
            font-weight: 500;
        }

        .alert.success {
            background: linear-gradient(135deg, #27ae60, #2ecc71);
            color: white;
        }

        .alert.danger {
            background: linear-gradient(135deg, #e74c3c, #c0392b);
            color: white;
        }

        .closebtn {
            position: absolute;
            top: 50%;
            right: 1rem;
            transform: translateY(-50%);
            background: none;
            border: none;
            color: white;
            font-size: 1.2rem;
            cursor: pointer;
            font-weight: bold;
        }

        /* Instructions */
        .instructions {
            background: #fff3cd;
            border: 2px solid #ffeaa7;
            border-radius: 12px;
            padding: 1rem;
            margin-bottom: 1.5rem;
            text-align: center;
        }

        .instructions h4 {
            color: #856404;
            margin-bottom: 0.5rem;
        }

        .instructions p {
            color: #856404;
            font-size: 0.9rem;
            margin: 0;
        }

        /* Animations */
        @keyframes slideUp {
            from { 
                opacity: 0;
                transform: translateY(30px);
            }
            to { 
                opacity: 1;
                transform: translateY(0);
            }
        }

        @keyframes fadeIn {
            from { opacity: 0; }
            to { opacity: 1; }
        }

        .action-section {
            animation: fadeIn 0.8s ease-out 0.3s both;
        }

        /* Mobile optimizations */
        @media (max-width: 768px) {
            body {
                padding: 0.5rem;
            }

            .success-ticket,
            .action-section {
                margin-bottom: 1rem;
            }

            .ticket-header {
                padding: 1rem;
            }

            .ticket-body {
                padding: 1rem;
            }

            .button-group {
                flex-direction: column;
                gap: 0.8rem;
            }

            .btn-primary,
            .btn-secondary {
                padding: 1rem 1.5rem;
                font-size: 1rem;
            }
        }

        @media (max-width: 480px) {
            .order-number {
                font-size: 1.5rem;
            }

            .info-label {
                min-width: 60px;
                font-size: 0.85rem;
            }

            .info-value {
                font-size: 0.85rem;
            }

            .ticket-header .success-icon {
                font-size: 2.5rem;
            }
        }

        /* Print styles */
        @media print {
            body {
                background: white;
                padding: 0;
            }

            .action-section,
            .instructions {
                display: none;
            }

            .success-ticket {
                box-shadow: none;
                border: 2px solid #ccc;
            }
        }
    </style>
</head>
<body>
    <div class="header">
        <h2>Konfirmasi Penyimpanan</h2>
    </div>

    <?php
    if(isset($_GET["tujuan"])){
        $counter = $_GET["counter"];
        $tanggal = $_GET["tanggal"];
        $nama = $_GET["nama"];
        $alamat = $_GET["alamat"];
        $note = $_GET["note"];
        $nowa = $_GET["nowa"];
        $jumlah = $_GET["jumlah"];
        $total = $_GET["total"];
        $diskpersen = $_GET["diskpersen"];
        $diskrp = $_GET["diskrp"];
        
        // PERUBAHAN: Hanya mengecek counter, tanggal, dan nama saja
        // Alamat dan nowa tidak lagi wajib diisi
        if(empty($counter) || empty($tanggal) || empty($nama)) {
            ?>
            <div class="alert danger">
                <button class="closebtn" onclick="this.parentElement.style.display='none'">&times;</button>
                <strong>Error!</strong> Data tidak lengkap. Silakan kembali dan lengkapi field yang wajib (Nomor pesanan, Tanggal, dan Nama).
            </div>
            <?php
        } else {
            // Check if order already exists
            $query_sql_cek = "SELECT * FROM order_data WHERE order_no='$counter'";
            $result_cek = mysqli_query($conn, $query_sql_cek);
                    
            if(mysqli_num_rows($result_cek) > 0){
                ?>
                <div class="alert danger">
                    <button class="closebtn" onclick="this.parentElement.style.display='none'">&times;</button>
                    <strong>Gagal!</strong> Pesanan dengan nomor <?php echo $counter; ?> sudah pernah disimpan sebelumnya.
                </div>
                <?php
            } else {
                // Insert main order
                $query_sql = "INSERT INTO order_data (order_no, order_datetime, order_nama, order_alamat, order_nowa, order_jumlah, order_total, order_disk_persen, order_disk_rupiah, order_note, order_kirim) 
                            VALUES ('$counter','$tanggal','$nama','$alamat','$nowa','$jumlah','$total','$diskpersen','$diskrp','$note','0')";

                if (mysqli_query($conn, $query_sql)) { 
                    ?>
                    <div class="alert success">
                        <button class="closebtn" onclick="this.parentElement.style.display='none'">&times;</button>
                        <strong>Berhasil!</strong> Pesanan Anda telah berhasil disimpan dan akan diproses segera.
                    </div>
                    <?php 
                    
                    // Process inventory updates
                    $query_temp = "SELECT * FROM produk_temp WHERE temp_master = '$counter'";
                    $result_temp = mysqli_query($conn, $query_temp);
                    
                    while ($row = mysqli_fetch_array($result_temp)) {
                        // Insert to stock card
                        $query_stok = "INSERT INTO kartu_stok (stok_status, stok_tanggal, stok_produk, stok_keterangan, stok_jumlah) 
                                      VALUES ('Keluar','$tanggal','$row[temp_produk]','Penjualan ke Customer','$row[temp_jumlah]')";
                        mysqli_query($conn, $query_stok);

                        // Update product stock
                        $query_update = "UPDATE produk SET produk_stok = produk_stok - '$row[temp_jumlah]' WHERE produk_id = '$row[temp_produk]'";
                        mysqli_query($conn, $query_update);
                    }
                    
                    // PERBAIKAN: HAPUS BAGIAN INI - TIDAK MENGHAPUS DETAIL PRODUK
                    // Komentar atau hapus baris berikut ini:
                    // $query_cleanup = "DELETE FROM produk_temp WHERE temp_master = '$counter'";
                    // mysqli_query($conn, $query_cleanup);
                    
                    // CATATAN: Detail produk di tabel produk_temp akan tetap tersimpan
                    // untuk keperluan tracking, laporan, atau referensi lainnya
                    
                } else { 
                    ?>
                    <div class="alert danger">
                        <button class="closebtn" onclick="this.parentElement.style.display='none'">&times;</button>
                        <strong>Gagal!</strong> Terjadi kesalahan saat menyimpan pesanan. Error: <?php echo mysqli_error($conn); ?>
                    </div> 
                    <?php 
                }
            }
        }
    }
    ?>

    <!-- Success Ticket -->
    <?php if(isset($_GET["tujuan"]) && !empty($counter) && !empty($nama)) { ?>
    <div class="success-ticket">
        <div class="ticket-header">
            <span class="success-icon">✅</span>
            <h3>Pesanan Berhasil Disimpan!</h3>
            <div class="order-number">No. <?php echo $counter; ?></div>
        </div>
        <div class="ticket-body">
            <div class="order-info">
                <div class="info-item">
                    <span class="info-label">📅 Tanggal:</span>
                    <span class="info-value"><?php echo date('d/m/Y H:i', strtotime($tanggal)); ?></span>
                </div>
                <div class="info-item">
                    <span class="info-label">👤 Nama:</span>
                    <span class="info-value"><?php echo $nama; ?></span>
                </div>
                <?php if(!empty($alamat)) { ?>
                <div class="info-item">
                    <span class="info-label">📍 Alamat:</span>
                    <span class="info-value"><?php echo $alamat; ?></span>
                </div>
                <?php } ?>
                <?php if(!empty($nowa)) { ?>
                <div class="info-item">
                    <span class="info-label">📱 WhatsApp:</span>
                    <span class="info-value"><?php echo $nowa; ?></span>
                </div>
                <?php } ?>
                <?php if(!empty($note)) { ?>
                <div class="info-item">
                    <span class="info-label">📝 Catatan:</span>
                    <span class="info-value"><?php echo $note; ?></span>
                </div>
                <?php } ?>
                <div class="info-item">
                    <span class="info-label">💰 Total:</span>
                    <span class="info-value">Rp <?php echo number_format($total - ($diskpersen/100*$total) - $diskrp); ?></span>
                </div>
            </div>
        </div>
        <div class="ticket-footer">
            <p><span class="highlight">Simpan nomor pesanan ini</span> untuk tracking status pesanan Anda</p>
        </div>
    </div>

    <!-- Instructions -->
    <div class="instructions">
        <h4>📋 Langkah Selanjutnya</h4>
        <p>Tim kami akan menghubungi Anda melalui WhatsApp untuk konfirmasi pesanan dan koordinasi pengiriman</p>
    </div>

    <!-- Action Section -->
    <div class="action-section">
        <h3 style="margin-bottom: 1rem; color: #333;">Terima kasih atas pesanan Anda!</h3>
        <div class="button-group">
            <a href="index.html" class="btn-secondary">
                🏠 Kembali ke Beranda
            </a>
            <a href="order.php" class="btn-primary">
                🛒 Pesan Lagi
            </a>
        </div>
    </div>
    <?php } ?>

    <script>
        // Auto-hide alerts
        setTimeout(() => {
            const alerts = document.querySelectorAll('.alert');
            alerts.forEach(alert => {
                alert.style.opacity = '0';
                setTimeout(() => alert.style.display = 'none', 300);
            });
        }, 8000);

        // Print functionality
        function printTicket() {
            window.print();
        }

        // Add print button if ticket exists
        if (document.querySelector('.success-ticket')) {
            const actionSection = document.querySelector('.action-section');
            const printBtn = document.createElement('button');
            printBtn.innerHTML = '🖨️ Cetak Tiket';
            printBtn.className = 'btn-secondary';
            printBtn.style.width = '100%';
            printBtn.style.marginTop = '1rem';
            printBtn.onclick = printTicket;
            actionSection.appendChild(printBtn);
        }

        // Celebration animation
        if (document.querySelector('.success-ticket')) {
            // Simple celebration effect
            setTimeout(() => {
                const header = document.querySelector('.ticket-header');
                header.style.background = 'linear-gradient(135deg, #f39c12, #e67e22)';
                setTimeout(() => {
                    header.style.background = 'linear-gradient(135deg, #27ae60, #2ecc71)';
                }, 200);
            }, 1000);
        }
    </script>
</body>
</html>