<?php
session_start();
require_once 'PHPMailer/PHPMailer.php';
require_once 'PHPMailer/SMTP.php';
require_once 'PHPMailer/Exception.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

include 'config/koneksi.php';
$query = "SELECT * FROM identitas"; 
  $hasil = mysqli_query($konek, $query);  
  $d     = mysqli_fetch_array($hasil);
  
  
  
   $querytemplate = "SELECT folder FROM templates WHERE aktif='Y'"; 
  $pilihtemplate = mysqli_query($konek, $querytemplate);  
  $f              = mysqli_fetch_array($pilihtemplate);
  
  include "$f[folder]/template.php"; 
  
  
  
  
// Ambil data domain/berita berdasarkan GET id
if(isset($_GET['id'])){
    $id = intval($_GET['id']); // amanin input
    $query = mysqli_query($konek, "SELECT * FROM berita WHERE id_berita='$id'");
    
    
   
    
    
    if(mysqli_num_rows($query) == 0){
        echo "<script>alert('Domain not found!'); window.location='index.php';</script>";
        exit;
    }
    $r = mysqli_fetch_assoc($query);
     $susu = $r['username'];

if($susu == 'admin'){
    $emailku = $d['email']; // email admin
} else {
    $emailku = $r['username']; // username user biasa
}
    
}

// Fungsi kirim email pemesanan
function kirimEmailPemesanan($to, $nama, $wa, $domain, $awal, $harga, $catatan, $admin_email) {
    $mail = new PHPMailer(true);
    try {
        $mail->isSMTP();
        $mail->Host       = 'mail.jasabuatwebmrrobot.com';    // ganti sesuai server
        $mail->SMTPAuth   = true;
        $mail->Username   = 'info@jasabuatwebmrrobot.com';   // email pengirim
        $mail->Password   = '@deLia2017';       // password email
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
        $mail->Port       = 465;

        $mail->setFrom('info@jasabuatwebmrrobot.com', 'Lelangdi.com');
        $mail->addAddress($to, $nama);           // ke user
        $mail->addBCC($admin_email);             // notifikasi ke admin

        $mail->isHTML(true);
        $mail->Subject = "Domain Order Confirmation";
        $mail->Body = "
            <h3>Halo $nama,</h3>
            <p>Thank you for participating in the domain auction at <strong>Lelangdi.com</strong>.</p>
            <ul>
                <li><strong>Email:</strong> $email</li>
                <li><strong>WhatsApp:</strong> $wa</li>
                <li><strong>Domain:</strong> $domain</li>
                <li><strong>Maximum Price:</strong> $awal</li>
                <li><strong>Bid Price:</strong> $harga</li>
                <li><strong>Note:</strong> $catatan</li>
            </ul>
            <p>our team will contact you shortly.</p>
        ";

        $mail->send();
        return true;
    } catch (Exception $e) {
        error_log("Failed to send email: {$mail->ErrorInfo}");
        return false;
    }
}

// Proses submit form
if($_SERVER['REQUEST_METHOD'] === 'POST'){
    $nama    = htmlspecialchars($_POST['nama']);
    $email   = htmlspecialchars($_POST['email']);
    $wa      = htmlspecialchars($_POST['wa']);
    $domain  = htmlspecialchars($_POST['domain']);
     $awal  = htmlspecialchars($_POST['awal']);
    $harga   = htmlspecialchars($_POST['harga']);
    $catatan = htmlspecialchars($_POST['catatan']);

    $admin_email = $emailku;

    if(kirimEmailPemesanan($email, $nama, $wa, $domain, $awal, $harga,$catatan, $admin_email)){
        echo "<script>alert('bid successful! Please check your email.'); window.location='index.php';</script>";
    } else {
        echo "<script>alert('Failed to send email. Please try again.'); window.history.back();</script>";
    }
}
?>

<style>
   
  .navbar {
    display:none;

}
footer{
     display:none;
}
</style>

<!-- Form Pemesanan Domain -->
<div class="container my-5">
  <div class="row justify-content-center">
    <div class="col-lg-8">
      <div class="card shadow-lg rounded-4 border-0">
        <div class="card-body p-5">
          <h2 class="fw-bold text-center mb-4">Domain Order Form</h2>
          <p class="text-muted text-center mb-4">Please fill in the following information to place your domain order at <span class="fw-bold">LELANGdi.com</span>.</p>

          <form method="POST">
         
            
            <div class="mb-3">
              <label for="nama" class="form-label">Full Name</label>
              <input type="text" name="nama" id="nama" class="form-control form-control-lg" placeholder="Enter your full name" required>
            </div>

            <div class="mb-3">
              <label for="email" class="form-label">Email</label>
              <input type="email" name="email" id="email" class="form-control form-control-lg" placeholder="Enter an active email address" required>
            </div>

            <div class="mb-3">
              <label for="wa" class="form-label">WhatsApp</label>
              <input type="text" name="wa" id="wa" class="form-control form-control-lg" placeholder="Example: 628123456789" required>
            </div>
            
            <div class="mb-3">
              <label for="domain" class="form-label">Domain</label>
              <input type="text" name="domain" value="<?php echo htmlspecialchars($r['judul']); ?>" class="form-control form-control-lg" readonly>
            </div>
            
            
            <div class="mb-3">
              <label for="domain" class="form-label">Maximum Price</label>
              <input type="text" name="awal" value="$<?php echo htmlspecialchars($r['a2']); ?>" class="form-control form-control-lg" readonly>
            </div>
            
            

            <div class="mb-3">
              <label for="harga" class="form-label">Bid Price</label>
              <input type="text" name="harga" id="harga" class="form-control form-control-lg" required>
            </div>

            <div class="mb-4">
              <label for="catatan" class="form-label">Additional Notes (Opsional)</label>
              <textarea name="catatan" id="catatan" rows="3" class="form-control form-control-lg" placeholder="Enter additional notes if applicable."></textarea>
            </div>

            <button type="submit" class="btn btn-warning btn-lg w-100 fw-bold shadow-sm">Bidding</button>
          </form>

        </div>
      </div>
    </div>
  </div>
</div>
