<?php
<?php
$host = "localhost";
$user = "odmr9632_userdb";
$pass = "@Sukabumi2018";
$db   = "odmr9632_order_db";

$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
  die("Koneksi gagal: " . $conn->connect_error);
}
?>
if ($conn->connect_error) {
  die("Koneksi gagal: " . $conn->connect_error);
}

// Ambil data dari form
$tanggal = $_POST['tanggal'] ?? '';
$opsi_produk = $_POST['opsi_produk'] ?? '';
$nama_cs = $_POST['nama_cs'] ?? '';
$nama_customer = $_POST['nama_customer'] ?? '';
$nama_produk = $_POST['nama_produk'] ?? '';
$no_wa = $_POST['no_wa'] ?? '';
$provinsi = $_POST['provinsi'] ?? '';
$kota = $_POST['kota'] ?? '';
$kecamatan = $_POST['kecamatan'] ?? '';
$alamat = $_POST['alamat'] ?? '';
$qty = $_POST['qty'] ?? 0;
$harga_jual = $_POST['harga_jual'] ?? 0;
$ongkir = $_POST['ongkir'] ?? 0;
$fee_cod = $_POST['fee_cod'] ?? 0;
$status_pengiriman = $_POST['status_pengiriman_hidden'] ?? '';
$no_resi = $_POST['no_resi'] ?? '';
$status_paket = $_POST['status_paket'] ?? '';

// Gunakan prepared statement
$stmt = $conn->prepare("INSERT INTO orders (
  tanggal, opsi_produk, nama_cs, nama_customer, nama_produk, no_wa,
  provinsi, kota, kecamatan, alamat, qty, harga_jual,
  ongkir, fee_cod, status_pengiriman, no_resi, status_paket
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

$stmt->bind_param("ssssssssssiiiisss",
  $tanggal, $opsi_produk, $nama_cs, $nama_customer, $nama_produk, $no_wa,
  $provinsi, $kota, $kecamatan, $alamat, $qty, $harga_jual,
  $ongkir, $fee_cod, $status_pengiriman, $no_resi, $status_paket
);

if ($stmt->execute()) {
  echo <<<HTML
<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style>
    body {
      font-family: "Segoe UI", sans-serif;
      background: #f0f0f0;
      text-align: center;
      padding-top: 100px;
    }
    .box {
      background: white;
      padding: 30px;
      border-radius: 10px;
      max-width: 400px;
      margin: auto;
      box-shadow: 0 0 10px rgba(0,0,0,0.1);
    }
    .box h2 { color: #4CAF50; }
    .btn {
      display: inline-block;
      margin-top: 20px;
      padding: 10px 20px;
      background: #4CAF50;
      color: white;
      text-decoration: none;
      border-radius: 5px;
      font-size: 16px;
      transition: background 0.3s;
    }
    .btn:hover { background: #45a049; }
    .spinner {
      display: inline-block;
      width: 18px;
      height: 18px;
      border: 3px solid #fff;
      border-radius: 50%;
      border-top: 3px solid transparent;
      animation: spin 0.8s linear infinite;
      vertical-align: middle;
      margin-left: 10px;
    }
    @keyframes spin {
      from { transform: rotate(0deg); }
      to { transform: rotate(360deg); }
    }
  </style>
  <script>
    function showLoading(btn) {
      btn.innerHTML = "Memuat <span class='spinner'></span>";
      btn.disabled = true;
    }
  </script>
</head>
<body>
  <div class="box">
    <h2>✅ Order Berhasil Disimpan!</h2>
    <p>Data telah masuk ke sistem dengan baik.</p>
    <a href="form_order.php" class="btn" onclick="showLoading(this)">Kembali ke Form</a>
  </div>
</body>
</html>
HTML;
} else {
  echo "❌ Gagal menyimpan: " . $stmt->error;
}

$stmt->close();
$conn->close();
?>
