<?php
require_once '../includes/auth.php';
require_once 'functions.php';

$id = (int)($_GET['id'] ?? 0);
$order = order_get_by_id($conn, $id);
if (!$order) { die('الطلب غير موجود'); }
$pricingBreakdown = [];
if (!empty($order['pricing_breakdown'])) {
    $pricingBreakdown = json_decode((string)$order['pricing_breakdown'], true) ?: [];
}
$pageTitle = 'عرض الطلب';
require_once '../includes/header.php';
require_once '../includes/sidebar.php';
?>
<style>
.box{background:#fff;border-radius:20px;padding:20px;box-shadow:0 12px 32px rgba(25,68,107,.08);margin-bottom:18px}.grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(220px,1fr));gap:14px}.label{color:#6c8397;font-size:13px}.value{font-weight:800;color:#183b56}.table{width:100%;border-collapse:collapse}.table th,.table td{padding:12px;border-bottom:1px solid #edf3f7;text-align:right}.badge{padding:5px 10px;border-radius:999px;background:#eef5f9;color:#183b56;font-weight:700;font-size:12px}.badge.info{background:#e8f3ff;color:#195f9d}.badge.success{background:#e8f8ef;color:#1e7c49}.badge.warn{background:#fff1eb;color:#aa5121}.badge.soft{background:#f4f0ff;color:#6f49bf}.badge.neutral{background:#eef5f9;color:#183b56}.btnx{display:inline-block;background:#96b8cc;color:#fff;text-decoration:none;padding:11px 16px;border-radius:12px;font-weight:800;margin-left:8px}.addon-chip{display:inline-flex;padding:8px 12px;border-radius:999px;background:#eff6ff;color:#1d4ed8;font-weight:700;margin:0 0 8px 8px}
</style>
<div class="content-wrapper">
    <div class="page-header">
        <h1>الطلب <?= e(order_display_serial($order)) ?></h1>
        <div>
            <a class="btnx" href="update.php?id=<?= (int)$order['id'] ?>">تعديل</a>
            <a class="btnx" target="_blank" href="print.php?id=<?= (int)$order['id'] ?>">طباعة</a>
            <a class="btnx" target="_blank" href="compact.php?id=<?= (int)$order['id'] ?>">موبايل</a>
            <a class="btnx" href="<?= e(base_admin_url('settings/order_invoices.php')) ?>">إعدادات الفاتورة</a>
        </div>
    </div>

    <div class="box">
        <div class="grid">
            <div><div class="label">اسم الزبون</div><div class="value"><?= e($order['customer_name']) ?></div></div>
            <div><div class="label">الهاتف</div><div class="value"><?= e($order['customer_phone']) ?></div></div>
            <div><div class="label">نوع الطلب</div><div class="value"><?= e(order_type_options()[$order['order_type']] ?? $order['order_type']) ?></div></div>
            <div><div class="label">حالة الطلب</div><div class="value"><span class="badge <?= e(order_status_badge_variant($order['order_status'])) ?>"><?= e(order_status_label($order['order_status'])) ?></span></div></div>
            <div><div class="label">طريقة الدفع</div><div class="value"><?= e($order['payment_method_name'] ?? '-') ?></div></div>
            <div><div class="label">حالة الدفع</div><div class="value"><span class="badge neutral"><?= e(payment_status_options()[$order['payment_status']] ?? $order['payment_status']) ?></span></div></div>
            <div><div class="label">المحافظة</div><div class="value"><?= e($order['province'] ?? '-') ?></div></div>
            <div><div class="label">المدينة / المنطقة</div><div class="value"><?= e(trim(($order['city'] ?? '') . ' ' . ($order['area'] ?? ''))) ?: '-' ?></div></div>
            <?php if (($order['order_type'] ?? '') === 'pickup' && !empty($order['pickup_branch'])): ?><div><div class="label">فرع الاستلام</div><div class="value"><?= e($order['pickup_branch']) ?></div></div><?php endif; ?>
            <?php if (($order['order_type'] ?? '') === 'preorder' && !empty($order['preorder_expected_at'])): ?><div><div class="label">الموعد المطلوب</div><div class="value"><?= e($order['preorder_expected_at']) ?></div></div><?php endif; ?>
            <div style="grid-column:1/-1"><div class="label">العنوان الكامل</div><div class="value"><?= nl2br(e($order['full_address'])) ?></div></div>
        </div>
    </div>

    <div class="box">
        <h3>تفاصيل المنتجات</h3>
        <table class="table">
            <thead><tr><th>الصورة</th><th>المنتج</th><th>السعر</th><th>الكمية</th><th>الخصم</th><th>المجموع</th></tr></thead>
            <tbody>
            <?php foreach ($order['items'] as $item): ?>
                <tr>
                    <td><?php if ($item['product_image']): ?><img src="../uploads/<?= e($item['product_image']) ?>" alt="" style="width:50px;height:50px;object-fit:cover;border-radius:10px"><?php endif; ?></td>
                    <td><?= e($item['product_name']) ?><?php if (!empty($item['applied_offer_title'])): ?><div class="label"><?= e($item['applied_offer_title']) ?></div><?php endif; ?></td>
                    <td><?php if (isset($item['original_unit_price']) && (float)$item['original_unit_price'] > (float)$item['unit_price']): ?><div><s><?= number_format((float)$item['original_unit_price']) ?></s></div><?php endif; ?><div><?= number_format((float)$item['unit_price']) ?></div></td>
                    <td><?= (int)$item['quantity'] ?></td>
                    <td><?= number_format((float)($item['line_discount_value'] ?? 0)) ?></td>
                    <td><?= number_format((float)$item['line_total']) ?></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>

    <div class="box">
        <h3>الإضافات المدفوعة</h3>
        <?php if (!empty($order['addons'])): ?>
            <?php foreach ($order['addons'] as $addon): ?>
                <span class="addon-chip"><?= e($addon['addon_title']) ?> - <?= number_format((float)$addon['addon_fee']) ?> د.ع</span>
            <?php endforeach; ?>
        <?php else: ?>
            <div class="label">لا توجد إضافات على هذا الطلب.</div>
        <?php endif; ?>
    </div>

    <div class="box">
        <div class="grid">
            <div><div class="label">المجموع الأصلي</div><div class="value"><?= number_format((float)($order['original_subtotal'] ?? $order['subtotal'])) ?></div></div>
            <div><div class="label">خصم العروض</div><div class="value"><?= number_format((float)($order['offers_discount_value'] ?? 0) + (float)($order['threshold_discount_value'] ?? 0)) ?></div></div>
            <div><div class="label">خصم البروموكود/النقاط</div><div class="value"><?= number_format((float)($order['promo_discount_value'] ?? $order['discount_value']) + (float)($order['loyalty_discount_value'] ?? 0)) ?></div></div>
            <div><div class="label">أجور التوصيل</div><div class="value"><?= number_format((float)$order['delivery_fee']) ?></div></div>
            <div><div class="label">الإضافات المدفوعة</div><div class="value"><?= number_format((float)($order['addons_total'] ?? 0)) ?></div></div>
            <div><div class="label">الإجمالي النهائي</div><div class="value"><?= number_format((float)$order['total_amount']) ?></div></div>
            <div><div class="label">البروموكود</div><div class="value"><?= e($order['promo_code_text'] ?? '-') ?></div></div>
            <div><div class="label">خصم المخزون</div><div class="value"><?= (int)$order['stock_deducted'] ? 'نعم' : 'لا' ?></div></div>
            <?php if (!empty($pricingBreakdown['threshold_titles'])): ?><div style="grid-column:1/-1"><div class="label">عروض الحد الأدنى المطبقة</div><div class="value"><?= e(implode(' - ', (array)$pricingBreakdown['threshold_titles'])) ?></div></div><?php endif; ?>
            <?php if (!empty($pricingBreakdown['free_delivery_title'])): ?><div><div class="label">سبب الشحن المجاني</div><div class="value"><?= e((string)$pricingBreakdown['free_delivery_title']) ?></div></div><?php endif; ?>
        </div>
    </div>

    <div class="box">
        <h3>سجل الحالات</h3>
        <table class="table">
            <thead><tr><th>من</th><th>إلى</th><th>بواسطة</th><th>ملاحظات</th><th>التاريخ</th></tr></thead>
            <tbody>
            <?php if (!$order['logs']): ?><tr><td colspan="5">لا يوجد سجل.</td></tr><?php endif; ?>
            <?php foreach ($order['logs'] as $log): ?>
                <tr>
                    <td><?= e($log['old_status'] ? order_status_label($log['old_status']) : '-') ?></td>
                    <td><?= e(order_status_label($log['new_status'])) ?></td>
                    <td><?= e($log['admin_name'] ?: '-') ?></td>
                    <td><?= e($log['notes'] ?: '-') ?></td>
                    <td><?= e($log['created_at']) ?></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php require_once '../includes/footer.php'; ?>
