<?php
session_start();
if (!isset($_SESSION['nama_cs'])) {
  header("Location: login_cs.php");
  exit;
}

$cs = $_SESSION['nama_cs'];
<?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);
}

$id = isset($_GET['id']) ? $_GET['id'] : 0;

$query = "SELECT * FROM orders WHERE id = $id AND nama_cs = '$cs'";
$result = $conn->query($query);
if ($result->num_rows == 0) {
  echo "❌ Data tidak ditemukan atau bukan milik Anda.";
  exit;
}
$data = $result->fetch_assoc();
?>

<!DOCTYPE html>
<html>
<head>
  <title>Edit Order</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style>
    body { font-family: Arial; padding: 20px; background: #f9f9f9; }
    .container { background: white; padding: 30px; max-width: 600px; margin: auto; border-radius: 10px; box-shadow: 0 0 10px rgba(0,0,0,0.1); }
    h2 { text-align: center; color: #4CAF50; }
    label { display: block; margin-top: 15px; }
    input, textarea { width: 100%; padding: 10px; margin-top: 5px; border-radius: 5px; border: 1px solid #ccc; box-sizing: border-box; }
    button, .back-btn { margin-top: 20px; padding: 12px; background: #4CAF50; color: white; border: none; width: 100%; font-size: 16px; border-radius: 6px; text-align: center; text-decoration: none; display: block; }
    button:hover, .back-btn:hover { background: #45a049; }
    .btn-group { display: flex; flex-direction: column; gap: 10px; margin-top: 20px; }
    .btn-group a { padding: 12px; background: #4CAF50; color: white; border: none; border-radius: 6px; text-align: center; text-decoration: none; display: block; font-size: 16px; }
    .btn-group a:hover { background: #45a049; }
    @media (min-width: 500px) {
      .btn-group { flex-direction: row; justify-content: space-between; }
      .btn-group a { flex: 1; margin: 0 5px; }
    }
  </style>
</head>
<body>
  <div class="container">
    <h2>📝 Edit Order</h2>
    <form method="POST" action="update_order.php">
      <input type="hidden" name="id" value="<?= $data['id'] ?>">
      <label>Tanggal:</label>
      <input type="date" name="tanggal" value="<?= $data['tanggal'] ?>">

      <label>Nama Customer:</label>
      <input type="text" name="nama_customer" value="<?= $data['nama_customer'] ?>">

      <label>Nama Produk:</label>
      <input type="text" name="nama_produk" value="<?= $data['nama_produk'] ?>">

      <label>No. WA:</label>
      <input type="text" name="no_wa" value="<?= $data['no_wa'] ?>">

      <?php if (!empty($data['provinsi'])): ?>
      <label>Provinsi:</label>
      <input type="text" name="provinsi" value="<?= $data['provinsi'] ?>">
      <?php endif; ?>

      <?php if (!empty($data['kota'])): ?>
      <label>Kota:</label>
      <input type="text" name="kota" value="<?= $data['kota'] ?>">
      <?php endif; ?>

      <?php if (!empty($data['kecamatan'])): ?>
      <label>Kecamatan:</label>
      <input type="text" name="kecamatan" value="<?= $data['kecamatan'] ?>">
      <?php endif; ?>

      <?php if (!empty($data['alamat'])): ?>
      <label>Alamat:</label>
      <textarea name="alamat"><?= $data['alamat'] ?></textarea>
      <?php endif; ?>

      <label>Qty:</label>
      <input type="number" name="qty" value="<?= $data['qty'] ?>">

      <label>Harga Jual:</label>
      <input type="number" name="harga_jual" value="<?= $data['harga_jual'] ?>">

      <?php if (!empty($data['ongkir'])): ?>
      <label>Ongkir:</label>
      <input type="number" name="ongkir" value="<?= $data['ongkir'] ?>">
      <?php endif; ?>

      <label>Status Pengiriman:</label>
      <input type="text" name="status_pengiriman" value="<?= $data['status_pengiriman'] ?>">

      <?php if (!empty($data['no_resi'])): ?>
      <label>No Resi:</label>
      <input type="text" name="no_resi" value="<?= $data['no_resi'] ?>">
      <?php endif; ?>

      <?php if (!empty($data['status_paket'])): ?>
      <label>Status Paket:</label>
      <input type="text" name="status_paket" value="<?= $data['status_paket'] ?>">
      <?php endif; ?>

      <button type="submit">📂 Simpan Perubahan</button>
    </form>
    <div class="btn-group">
      <a href="edit_order.php">⬅️ Kembali ke Edit Order</a>
    </div>
  </div>
</body>
</html>
