<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

include('../includes/config.php');


if(!isset($_POST['id'])){
    die("Akses langsung tidak diperbolehkan. <a href='index_admin.php'>Kembali ke Dashboard</a>");
}

$id = intval($_POST['id']);


$data = mysqli_fetch_assoc(mysqli_query($conn,"SELECT * FROM pesanan WHERE id='$id'"));


$status_text = '';
$show_animation = false;


if(isset($_POST['approve'])){
    $no_resi = $_POST['no_resi'];

   
    $foto_kirim = '';
    if(isset($_FILES['foto_kirim']) && $_FILES['foto_kirim']['error'] == 0){
        $folder = '../uploads/pengiriman/';
        if(!is_dir($folder)) mkdir($folder, 0777, true);

        $nama_file = time().'_'.basename($_FILES['foto_kirim']['name']);
        $target_file = $folder.$nama_file;

        if(move_uploaded_file($_FILES['foto_kirim']['tmp_name'], $target_file)){
            $foto_kirim = 'uploads/pengiriman/'.$nama_file;
        }
    }

   
    $update = mysqli_query($conn, "UPDATE pesanan SET status='Sedang dikirim', no_resi='$no_resi', foto_kirim='$foto_kirim' WHERE id='$id'");

    if($update){
        $status_text = "Pesanan Sedang Dikirim!";
        $show_animation = true;
    } else {
        die("Gagal update status pesanan. " . mysqli_error($conn));
    }

   
    $data = mysqli_fetch_assoc(mysqli_query($conn,"SELECT * FROM pesanan WHERE id='$id'"));
}


if(isset($_POST['reject'])){
    $alasan = mysqli_real_escape_string($conn, $_POST['alasan']);
    $update = mysqli_query($conn, "UPDATE pesanan SET status='Ditolak', komentar='$alasan' WHERE id='$id'");
    if($update){
        $status_text = "Pesanan Ditolak!";
        $show_animation = false;
    } else {
        die("Gagal update status pesanan. " . mysqli_error($conn));
    }

    $data = mysqli_fetch_assoc(mysqli_query($conn,"SELECT * FROM pesanan WHERE id='$id'"));
}


header("Refresh: 5; url=index_admin.php");
?>
<!DOCTYPE html>
<html>
<head>
<title>Pengiriman</title>
<style>
body{font-family:'Poppins';background:#f5f5f5;text-align:center;padding:50px;}
.delivery-box{width:350px;height:350px;margin:auto;background:#fff;border-radius:10px;box-shadow:0 4px 8px rgba(0,0,0,0.1);padding:20px;position:relative;}
.truck{width:60px;height:35px;background:#0288d1;position:absolute;top:150px;left:0;border-radius:5px;animation:drive 5s linear forwards;}
@keyframes drive {
  0% {left:0;}
  100% {left:260px;}
}
</style>
</head>
<body>

<div class="delivery-box">
<h2>No Pesanan: <?= isset($data['no_pesanan']) ? $data['no_pesanan'] : '-' ?></h2>
<p>Status: <?= isset($data['status']) ? $data['status'] : '-' ?></p>

<?php if(isset($data['no_resi']) && $data['no_resi']) { ?>
<p>No Resi: <?= $data['no_resi'] ?></p>
<?php } ?>

<?php if($show_animation) { ?>
<div class="truck"></div>
<p><?= $status_text ?> <br>Anda akan diarahkan ke dashboard admin dalam 5 detik...</p>
<?php } else { ?>
<p><?= $status_text ?> <br>Anda akan diarahkan ke dashboard admin dalam 5 detik...</p>
<?php } ?>

<p>Jika tidak diarahkan otomatis, <a href="index_admin.php">klik di sini</a>.</p>
</div>

</body>
</html>
