<?php include __DIR__ . '/../layout/header.php'; ?>

<?php
function ordEditStatusBadge($status) {
    $map = [
        'pending'          => ['badge-warning', 'Bekliyor'],
        'pending_payment'  => ['badge-warning', 'Ödeme Bekliyor'],
        'payment_uploaded' => ['badge-info',    'Dekont İnceleniyor'],
        'processing'       => ['badge-info',    'Hazırlanıyor'],
        'shipped'          => ['badge-purple',   'Kargoya Verildi'],
        'delivered'        => ['badge-success',  'Teslim Edildi'],
        'completed'        => ['badge-success',  'Tamamlandı'],
        'cancelled'        => ['badge-danger',   'İptal'],
        'returned'         => ['badge-muted',    'İade'],
    ];
    return $map[$status] ?? ['badge-muted', $status ?: '?'];
}
[$statusClass, $statusText] = ordEditStatusBadge($order->status ?? '');
?>

<div class="admin-card-header" style="padding:0 0 20px;border-bottom:none;gap:12px;flex-wrap:wrap;">
    <div>
        <h2 style="font-size:18px;font-weight:800;color:var(--text-1);margin:0;">Sipariş Detayı</h2>
        <p style="font-size:12px;color:var(--text-3);margin:4px 0 0;">#<?php echo htmlspecialchars($order->id); ?> · <?php echo htmlspecialchars($order->created_at); ?></p>
    </div>
    <span class="status-badge <?php echo $statusClass; ?>"><?php echo $statusText; ?></span>
    <div class="header-spacer"></div>
    <a href="<?php echo $baseUrl; ?>/admin/orders" class="admin-btn"><i class="fa fa-arrow-left"></i> Siparişler</a>
</div>

<div class="form-grid">

    <!-- LEFT: Order details -->
    <div class="form-column">

        <!-- Order Items -->
        <div class="admin-card">
            <div class="section-title"><i class="fa fa-shopping-basket"></i> Sipariş İçeriği</div>
            <div class="admin-table-wrap" style="border:none;margin:-4px;">
                <table class="admin-table" style="min-width:340px;">
                    <thead>
                        <tr>
                            <th>Ürün</th>
                            <th style="text-align:center;width:60px;">Adet</th>
                            <th style="text-align:right;">Birim</th>
                            <th style="text-align:right;">Toplam</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($order->items as $item): ?>
                        <tr>
                            <td style="font-weight:500;color:var(--text-1);"><?php echo htmlspecialchars($item['name']); ?></td>
                            <td style="text-align:center;font-weight:700;"><?php echo $item['quantity']; ?></td>
                            <td style="text-align:right;"><?php echo \App\Services\CurrencyService::format($item['price'], $order->currency); ?></td>
                            <td style="text-align:right;font-weight:700;"><?php echo \App\Services\CurrencyService::format($item['price'] * $item['quantity'], $order->currency); ?></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <div style="margin-top:16px;padding-top:14px;border-top:1px solid var(--border);display:flex;flex-direction:column;gap:7px;">
                <div class="info-row" style="padding:4px 0;border:none;">
                    <span class="info-key">Ara Toplam</span>
                    <span class="info-val"><?php echo \App\Services\CurrencyService::format((float)($order->subtotal ?? $order->total), $order->currency); ?></span>
                </div>
                <div class="info-row" style="padding:4px 0;border:none;">
                    <span class="info-key">Kargo (<?php echo htmlspecialchars($order->shipping_method ?? 'Standart'); ?>)</span>
                    <span class="info-val"><?php echo \App\Services\CurrencyService::format((float)($order->shipping_cost ?? 0), $order->currency); ?></span>
                </div>
                <?php if (isset($order->payment_fee) && $order->payment_fee > 0): ?>
                <div class="info-row" style="padding:4px 0;border:none;">
                    <span class="info-key">İşlem Ücreti</span>
                    <span class="info-val"><?php echo \App\Services\CurrencyService::format((float)$order->payment_fee, $order->currency); ?></span>
                </div>
                <?php endif; ?>
                <div style="display:flex;justify-content:space-between;align-items:center;padding-top:10px;border-top:2px solid var(--border);">
                    <span style="font-size:14px;font-weight:700;color:var(--text-2);">Genel Toplam</span>
                    <span style="font-size:22px;font-weight:800;color:var(--primary);"><?php echo \App\Services\CurrencyService::format($order->total, $order->currency); ?></span>
                </div>
            </div>
        </div>

        <!-- Customer Info -->
        <div class="admin-card">
            <div class="section-title"><i class="fa fa-user"></i> Müşteri Bilgileri</div>

            <div class="info-row">
                <span class="info-key">Ad Soyad</span>
                <span class="info-val" style="font-weight:700;"><?php echo htmlspecialchars($order->customer_name); ?></span>
            </div>
            <div class="info-row">
                <span class="info-key">Telefon</span>
                <span class="info-val"><a href="tel:<?php echo $order->customer_phone; ?>" style="color:var(--info);"><?php echo htmlspecialchars($order->customer_phone); ?></a></span>
            </div>
            <div class="info-row">
                <span class="info-key">E-posta</span>
                <span class="info-val"><a href="mailto:<?php echo $order->customer_email; ?>" style="color:var(--info);"><?php echo htmlspecialchars($order->customer_email); ?></a></span>
            </div>
            <div class="info-row">
                <span class="info-key">Kullanıcı</span>
                <span class="info-val"><?php echo $order->user_id ? htmlspecialchars($order->user_id) : '<span style="color:var(--text-3);">Misafir</span>'; ?></span>
            </div>
            <div class="info-row">
                <span class="info-key">Ödeme Yöntemi</span>
                <span class="info-val">
                    <?php
                    if (isset($order->payment_method)) {
                        if ($order->payment_method === 'bank_transfer')     echo '<i class="fa fa-university" style="margin-right:5px;"></i>Havale / EFT';
                        elseif ($order->payment_method === 'cash_on_delivery') echo '<i class="fa fa-truck" style="margin-right:5px;"></i>Kapıda Ödeme';
                        else                                                    echo '<i class="fa fa-credit-card" style="margin-right:5px;"></i>Kredi Kartı';
                    }
                    ?>
                </span>
            </div>
            <div class="info-row">
                <span class="info-key">Teslimat Adresi</span>
                <span class="info-val" style="white-space:pre-line;line-height:1.6;"><?php echo nl2br(htmlspecialchars($order->customer_address)); ?></span>
            </div>

            <?php if (!empty($order->customer_notes)): ?>
            <div style="margin-top:12px;background:var(--warning-soft);border:1px dashed var(--warning);border-radius:var(--r-md);padding:12px 14px;">
                <div style="font-size:11px;font-weight:700;text-transform:uppercase;letter-spacing:0.5px;color:var(--warning);margin-bottom:4px;"><i class="fa fa-comment" style="margin-right:5px;"></i>Müşteri Notu</div>
                <div style="font-size:13.5px;color:var(--text-1);line-height:1.6;"><?php echo nl2br(htmlspecialchars($order->customer_notes)); ?></div>
            </div>
            <?php endif; ?>

            <?php if (!empty($order->receipt_file)): ?>
            <div style="margin-top:12px;">
                <a href="<?php echo $baseUrl; ?>/uploads/receipts/<?php echo htmlspecialchars($order->receipt_file); ?>" target="_blank" class="admin-btn">
                    <i class="fa fa-file-text"></i> Dekontu Görüntüle
                </a>
            </div>
            <?php endif; ?>
        </div>

    </div><!-- /form-column left -->

    <!-- RIGHT: Update panel -->
    <div class="form-column">

        <div class="admin-card">
            <div class="section-title"><i class="fa fa-refresh"></i> Siparişi Güncelle</div>

            <form action="<?php echo $baseUrl; ?>/admin/orders/update/<?php echo $order->id; ?>" method="POST">

                <div class="admin-form-group">
                    <label>Sipariş Durumu</label>
                    <select name="status" class="admin-select">
                        <option value="pending"          <?php echo $order->status === 'pending' ? 'selected' : ''; ?>>Bekliyor</option>
                        <option value="pending_payment"  <?php echo $order->status === 'pending_payment' ? 'selected' : ''; ?>>Ödeme Dekontu Bekliyor</option>
                        <option value="payment_uploaded" <?php echo $order->status === 'payment_uploaded' ? 'selected' : ''; ?>>Dekont İnceleniyor</option>
                        <option value="processing"       <?php echo $order->status === 'processing' ? 'selected' : ''; ?>>Hazırlanıyor</option>
                        <option value="shipped"          <?php echo $order->status === 'shipped' ? 'selected' : ''; ?>>Kargoya Verildi</option>
                        <option value="delivered"        <?php echo $order->status === 'delivered' ? 'selected' : ''; ?>>Teslim Edildi</option>
                        <option value="cancelled"        <?php echo $order->status === 'cancelled' ? 'selected' : ''; ?>>İptal / Reddedildi</option>
                        <option value="returned"         <?php echo $order->status === 'returned' ? 'selected' : ''; ?>>İade Edildi</option>
                    </select>
                </div>

                <div class="admin-form-group">
                    <label>Kargo Firması</label>
                    <input type="text" name="shipping_company" class="admin-input" placeholder="Örn: Yurtiçi Kargo"
                           value="<?php echo htmlspecialchars($order->shipping_company ?? ''); ?>">
                </div>

                <div class="admin-form-group">
                    <label>Kargo Takip Numarası</label>
                    <input type="text" name="tracking_number" class="admin-input" placeholder="Takip numarasını girin"
                           value="<?php echo htmlspecialchars($order->tracking_number ?? ''); ?>">
                </div>

                <button type="submit" class="admin-btn admin-btn-primary admin-btn-block">
                    <i class="fa fa-save"></i> Değişiklikleri Kaydet
                </button>

            </form>
        </div>

        <div class="admin-card">
            <div class="section-title"><i class="fa fa-trash" style="color:var(--danger);"></i> Tehlikeli Bölge</div>
            <p style="font-size:12.5px;color:var(--text-3);margin-bottom:14px;">Bu sipariş kalıcı olarak silinir ve geri alınamaz.</p>
            <button onclick="confirmDelete('<?php echo $baseUrl; ?>/admin/orders/delete/<?php echo $order->id; ?>')"
                    class="admin-btn admin-btn-danger admin-btn-block">
                <i class="fa fa-trash"></i> Siparişi Sil
            </button>
        </div>

    </div><!-- /form-column right -->

</div><!-- /.form-grid -->

<script>
function confirmDelete(url) {
    adminConfirm('Bu siparişi kalıcı olarak silmek istiyor musunuz? Bu işlem geri alınamaz.', function() { window.location.href = url; });
}
</script>

<?php include __DIR__ . '/../layout/footer.php'; ?>
