<?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;
}

// Dohvatanje podataka o porudžbini
$order = get_row("SELECT * FROM orders WHERE id = ?", [$order_id]);
if (!$order) {
    die("Porudžbina nije pronađena.");
}

// Dohvatanje stavki porudžbine
$order_items = get_rows("
    SELECT oi.*, p.name as product_name, p.sku 
    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]);

// Dohvatanje podataka o prodavnici
$shop_info = [
    'name' => 'AGRAS D.O.O.',
    'address' => 'Oskara Daviča 2',
    'city' => 'Kragujevac',
    'zip' => '34000',
    'phone' => '065/8-065-065',
    'email' => 'info@hugong-aparati.rs',
    'website' => 'www.hugong-aparati.rs',
    'pib' => '113904929',
    'mb' => '21942626'
];

// Format datuma
$date_format = 'd.m.Y.';
?>
<!DOCTYPE html>
<html lang="sr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Porudžbina #<?php echo $order['order_number']; ?> - Štampa</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            font-size: 12pt;
            line-height: 1.5;
            margin: 0;
            padding: 0;
        }
        .print-container {
            max-width: 800px;
            margin: 20px auto;
            padding: 20px;
            border: 1px solid #ddd;
        }
        .print-header {
            border-bottom: 2px solid #333;
            padding-bottom: 10px;
            margin-bottom: 20px;
        }
        .print-header h1 {
            margin: 0 0 10px 0;
            font-size: 18pt;
        }
        .company-info {
            float: left;
            width: 60%;
        }
        .order-info {
            float: right;
            width: 35%;
            text-align: right;
        }
        .clearfix::after {
            content: "";
            clear: both;
            display: table;
        }
        .customer-info, .shipping-info {
            margin-bottom: 20px;
        }
        .section-title {
            font-weight: bold;
            margin-bottom: 5px;
            font-size: 14pt;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        th, td {
            padding: 8px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }
        th {
            background-color: #f2f2f2;
            font-weight: bold;
        }
        .text-right {
            text-align: right;
        }
        .totals {
            width: 300px;
            float: right;
            margin-bottom: 20px;
        }
        .totals table {
            width: 100%;
        }
        .totals th {
            text-align: right;
        }
        .footer {
            border-top: 1px solid #ddd;
            padding-top: 10px;
            margin-top: 30px;
            font-size: 10pt;
            color: #666;
            text-align: center;
        }
        @media print {
            .no-print {
                display: none;
            }
            .print-container {
                border: none;
                margin: 0;
                padding: 0;
            }
            body {
                font-size: 10pt;
            }
        }
    </style>
</head>
<body>
    <div class="print-container">
        <!-- Dugme za štampanje (ne prikazuje se pri štampanju) -->
        <div class="no-print" style="text-align: right; margin-bottom: 20px;">
            <button onclick="window.print();" style="padding: 8px 16px; background-color: #4CAF50; color: white; border: none; cursor: pointer;">Štampaj</button>
            <button onclick="window.close();" style="padding: 8px 16px; background-color: #f44336; color: white; border: none; cursor: pointer; margin-left: 10px;">Zatvori</button>
        </div>
        
        <!-- Zaglavlje -->
        <div class="print-header clearfix">
            <div class="company-info">
                <h1><?php echo $shop_info['name']; ?></h1>
                <p>
                    <?php echo $shop_info['address']; ?><br>
                    <?php echo $shop_info['zip']; ?> <?php echo $shop_info['city']; ?><br>
                    Tel: <?php echo $shop_info['phone']; ?><br>
                    Email: <?php echo $shop_info['email']; ?><br>
                    Web: <?php echo $shop_info['website']; ?><br>
                    PIB: <?php echo $shop_info['pib']; ?> | MB: <?php echo $shop_info['mb']; ?>
                </p>
            </div>
            <div class="order-info">
                <h2>Porudžbina #<?php echo $order['order_number']; ?></h2>
                <p>
                    Datum: <?php echo date($date_format, strtotime($order['created_at'])); ?><br>
                    Status: <?php echo get_order_status_name($order['status']); ?><br>
                    Način plaćanja: <?php echo get_payment_method_name($order['payment_method']); ?><br>
Način dostave: <?php echo get_shipping_method_name($order['shipping_method']); ?>
                </p>
            </div>
        </div>
        
        <div class="clearfix">
            <!-- Podaci o kupcu -->
            <div class="customer-info" style="float: left; width: 48%;">
                <div class="section-title">Podaci o kupcu</div>
                <p>
                    <?php echo htmlspecialchars($order['name']); ?><br>
                    <?php if (!empty($order['email'])): ?>
                    Email: <?php echo $order['email']; ?><br>
                    <?php endif; ?>
                    <?php if (!empty($order['phone'])): ?>
                    Telefon: <?php echo $order['phone']; ?><br>
                    <?php endif; ?>
                </p>
            </div>
            
            <!-- Adresa za dostavu -->
            <div class="shipping-info" style="float: right; width: 48%;">
                <div class="section-title">Adresa za dostavu</div>
                <p>
                    <?php echo htmlspecialchars($order['name']); ?><br>
                    <?php echo htmlspecialchars($order['address']); ?><br>
                    <?php echo $order['zip']; ?> <?php echo htmlspecialchars($order['city']); ?><br>
                    Srbija
                </p>
            </div>
        </div>
        
        <div class="clearfix"></div>
        
        <!-- Napomena ako postoji -->
        <?php if (!empty($order['notes'])): ?>
        <div class="notes" style="margin-bottom: 20px;">
            <div class="section-title">Napomena</div>
            <p><?php echo nl2br(htmlspecialchars($order['notes'])); ?></p>
        </div>
        <?php endif; ?>
        
        <!-- Tabela stavki -->
        <div class="section-title">Stavke porudžbine</div>
        <table>
            <thead>
                <tr>
                    <th style="width: 40px;">RB</th>
                    <th>Proizvod</th>
                    <th>Šifra</th>
                    <th class="text-right">Cena</th>
                    <th class="text-right" style="width: 80px;">Količina</th>
                    <th class="text-right">Ukupno</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($order_items)): ?>
                    <tr>
                        <td colspan="6" style="text-align: center;">Nema stavki u porudžbini</td>
                    </tr>
                <?php else: ?>
                    <?php $subtotal = 0; ?>
                    <?php foreach ($order_items as $index => $item): ?>
                        <?php $item_total = $item['price'] * $item['quantity']; ?>
                        <?php $subtotal += $item_total; ?>
                        <tr>
                            <td><?php echo $index + 1; ?></td>
                            <td><?php echo htmlspecialchars($item['product_name']); ?></td>
                            <td><?php echo $item['sku']; ?></td>
                            <td class="text-right"><?php echo format_price($item['price']); ?></td>
                            <td class="text-right"><?php echo $item['quantity']; ?></td>
                            <td class="text-right"><?php echo format_price($item_total); ?></td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
        
        <!-- Ukupno -->
        <div class="totals">
            <table>
                <tr>
                    <th>Ukupno:</th>
                    <td class="text-right"><?php echo format_price($order['total']); ?></td>
                </tr>
            </table>
        </div>
        
        <div class="clearfix"></div>
        
        <!-- Potpisi -->
        <div style="margin-top: 60px;">
            <div style="float: left; width: 45%; text-align: center;">
                _______________________________<br>
                Prodavac
            </div>
            <div style="float: right; width: 45%; text-align: center;">
                _______________________________<br>
                Kupac
            </div>
        </div>
        
        <div class="clearfix"></div>
        
        <!-- Footer -->
        <div class="footer">
            <p>
                Dokument kreiran: <?php echo date($date_format . ' H:i:s'); ?><br>
                <?php echo $shop_info['name']; ?> | <?php echo $shop_info['address']; ?>, <?php echo $shop_info['zip']; ?> <?php echo $shop_info['city']; ?> | PIB: <?php echo $shop_info['pib']; ?>
            </p>
        </div>
    </div>
</body>
</html>