<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require_once '../../includes/config.php';
require_once '../../includes/db.php';
require_once '../../includes/functions.php';
require_once '../includes/admin-functions.php';

// Provera da li je korisnik prijavljen kao admin
if (!is_admin()) {
    header('Location: /admin/login.php');
    exit;
}

// Provera ID-ja porudžbine
$order_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
if ($order_id <= 0) {
    header('Location: /admin/orders/');
    exit;
}

// Proverite da li porudžbina postoji
$order = get_row("SELECT * FROM orders WHERE id = ?", [$order_id]);
if (!$order) {
    header('Location: /admin/orders/');
    exit;
}

// Provera da li je zahtev za potvrdu brisanja
$confirm = isset($_GET['confirm']) && $_GET['confirm'] == 1;

// Ako je potvrđeno brisanje
if ($confirm) {
    // Prvo obrišite stavke porudžbine
    execute_query("DELETE FROM order_items WHERE order_id = ?", [$order_id]);
    
    // Zatim obrišite samu porudžbinu
    $deleted = execute_query("DELETE FROM orders WHERE id = ?", [$order_id]);
    
    if ($deleted) {
        // Uspešno brisanje, preusmeravanje na listu porudžbina
        header('Location: /admin/orders/?deleted=1');
        exit;
    } else {
        // Greška pri brisanju
        $error = "Došlo je do greške prilikom brisanja porudžbine.";
    }
}

// Naslov stranice
$page_title = 'Brisanje porudžbine #' . $order['order_number'];

// Uključivanje header-a
require_once '../includes/header.php';
?>

<div class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-6">
                <h1 class="m-0">Brisanje porudžbine</h1>
            </div>
            <div class="col-sm-6">
                <ol class="breadcrumb float-sm-right">
                    <li class="breadcrumb-item"><a href="/admin/">Kontrolna tabla</a></li>
                    <li class="breadcrumb-item"><a href="/admin/orders/">Porudžbine</a></li>
                    <li class="breadcrumb-item"><a href="/admin/orders/view.php?id=<?php echo $order_id; ?>">Porudžbina #<?php echo $order['order_number']; ?></a></li>
                    <li class="breadcrumb-item active">Brisanje</li>
                </ol>
            </div>
        </div>
    </div>
</div>

<section class="content">
    <div class="container-fluid">
        <?php if (isset($error)): ?>
            <div class="alert alert-danger">
                <?php echo $error; ?>
            </div>
        <?php endif; ?>
        
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">Potvrda brisanja porudžbine #<?php echo $order['order_number']; ?></h3>
            </div>
            <div class="card-body">
                <div class="alert alert-warning">
                    <h5><i class="icon fas fa-exclamation-triangle"></i> Upozorenje!</h5>
                    <p>Da li ste sigurni da želite da obrišete ovu porudžbinu?</p>
                    <p>Ova akcija će trajno ukloniti porudžbinu i sve njene stavke iz baze podataka. Ova akcija se ne može poništiti.</p>
                </div>
                
                <div class="row">
                    <div class="col-md-6">
                        <h4>Podaci o porudžbini</h4>
                        <table class="table">
                            <tr>
                                <th>Broj porudžbine:</th>
                                <td><?php echo $order['order_number']; ?></td>
                            </tr>
                            <tr>
                                <th>Datum:</th>
                                <td><?php echo date('d.m.Y. H:i', strtotime($order['created_at'])); ?></td>
                            </tr>
                            <tr>
                                <th>Klijent:</th>
                                <td><?php echo htmlspecialchars($order['name']); ?></td>
                            </tr>
                            <tr>
                                <th>Ukupno:</th>
                                <td><?php echo format_price($order['total']); ?></td>
                            </tr>
                            <tr>
                                <th>Status:</th>
                                <td>
                                    <span class="badge badge-<?php echo get_order_status_class($order['status']); ?>">
                                        <?php echo get_order_status_name($order['status']); ?>
                                    </span>
                                </td>
                            </tr>
                        </table>
                    </div>
                    
                    <div class="col-md-6">
                        <h4>Stavke porudžbine</h4>
                        <?php 
                        // Dohvatanje stavki porudžbine
                        $order_items = get_rows("
                            SELECT oi.*, p.name as product_name 
                            FROM order_items oi
                            LEFT JOIN products p ON oi.product_id = p.id
                            WHERE oi.order_id = ?
                            ORDER BY oi.id ASC
                        ", [$order_id]);
                        ?>
                        
                        <?php if (empty($order_items)): ?>
                            <p>Nema stavki u porudžbini.</p>
                        <?php else: ?>
                            <table class="table table-sm">
                                <thead>
                                    <tr>
                                        <th>Proizvod</th>
                                        <th class="text-right">Količina</th>
                                        <th class="text-right">Cena</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($order_items as $item): ?>
                                        <tr>
                                            <td><?php echo htmlspecialchars($item['product_name']); ?></td>
                                            <td class="text-right"><?php echo $item['quantity']; ?></td>
                                            <td class="text-right"><?php echo format_price($item['price'] * $item['quantity']); ?></td>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
            <div class="card-footer">
                <a href="/admin/orders/delete.php?id=<?php echo $order_id; ?>&confirm=1" class="btn btn-danger">
                    <i class="fas fa-trash"></i> Da, obriši porudžbinu
                </a>
                <a href="/admin/orders/view.php?id=<?php echo $order_id; ?>" class="btn btn-default ml-2">
                    <i class="fas fa-arrow-left"></i> Odustani
                </a>
            </div>
        </div>
    </div>
</section>

<?php require_once '../includes/footer.php'; ?>