<?php
session_start();
include 'koneksi.php';

if(!isset($_SESSION['admin'])){
    header("Location: index.php");
    exit;
}

$id = $_GET['id'];

if(isset($_POST['tolak'])){
    
    $alasan = mysqli_real_escape_string($conn, $_POST['alasan']);

    // ambil data transaksi
    $data = mysqli_fetch_assoc(mysqli_query($conn, "
        SELECT username, tanggal 
        FROM orders 
        WHERE id='$id'
    "));

    $username = $data['username'];
    $tanggal  = $data['tanggal'];

    // 🔥 update semua dalam transaksi
    mysqli_query($conn, "
        UPDATE orders 
        SET status='Ditolak', alasan='$alasan'
        WHERE username='$username' 
        AND tanggal='$tanggal'
    ");

    header("Location: pesanan.php");
    exit;
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Tolak Pesanan</title>
    <style>
        body{font-family:Arial;background:#f4f6f9;}
        .box{
            width:400px;
            margin:100px auto;
            background:white;
            padding:20px;
            border-radius:10px;
            text-align:center;
        }
        textarea{
            width:100%;
            height:100px;
            padding:10px;
        }
        .btn{
            margin-top:10px;
            padding:10px;
            background:#dc3545;
            color:white;
            border:none;
            border-radius:5px;
        }
    </style>
</head>
<body>

<div class="box">
    <h3>❌ Alasan Penolakan</h3>

    <form method="POST">
        <textarea name="alasan" required placeholder="Masukkan alasan penolakan..."></textarea>
        <br>
        <button type="submit" name="tolak" class="btn">Tolak Pesanan</button>
    </form>
</div>

</body>
</html>