<?php
require_once __DIR__ . '/../includes/config.php';
if (file_exists(__DIR__ . '/../includes/auth.php')) {
    require_once __DIR__ . '/../includes/auth.php';
}
require_once __DIR__ . '/functions.php';

$id = (int)($_GET['id'] ?? 0);
$promo = promo_get_code($id);
if (!$promo) {
    die('كود الخصم غير موجود');
}

$usageRows = promo_get_recent_usage($id, 25);
$orderRows = promo_get_recent_orders($id, 20);

include __DIR__ . '/../includes/header.php';
if (file_exists(__DIR__ . '/../includes/sidebar.php')) {
    include __DIR__ . '/../includes/sidebar.php';
}
?>

<div class="content-wrapper" style="padding:20px;">
    <div style="display:flex;justify-content:space-between;align-items:center;gap:15px;flex-wrap:wrap;margin-bottom:18px;">
        <div>
            <h1 style="margin:0 0 8px;">تفاصيل كود الخصم</h1>
            <p style="margin:0;color:#6b7280;">مراجعة شروط الكود، الصلاحية، الاستخدام، والطلبات المرتبطة.</p>
        </div>
        <div style="display:flex;gap:10px;flex-wrap:wrap;">
            <a href="edit.php?id=<?php echo $id; ?>" style="display:inline-block;background:#16a34a;color:#fff;text-decoration:none;padding:10px 16px;border-radius:10px;">تعديل</a>
            <?php if ((int)$promo['status'] === 1): ?>
                <?= admin_post_action_form('set_status.php', 'تعطيل', '', 'promo_status', ['id' => $id, 'value' => 0, 'back' => $_SERVER['REQUEST_URI'] ?? '/app/admin/promo_codes/view.php?id=' . $id], 'تأكيد تعطيل كود الخصم؟'); ?>
            <?php else: ?>
                <?= admin_post_action_form('set_status.php', 'تفعيل', '', 'promo_status', ['id' => $id, 'value' => 1, 'back' => $_SERVER['REQUEST_URI'] ?? '/app/admin/promo_codes/view.php?id=' . $id], 'تأكيد تفعيل كود الخصم؟'); ?>
            <?php endif; ?>
            <a href="list.php" style="display:inline-block;background:#111827;color:#fff;text-decoration:none;padding:10px 16px;border-radius:10px;">رجوع</a>
        </div>
    </div>

    <?php if (!empty($_GET['msg'])): ?>
        <div style="margin-bottom:16px;padding:12px 14px;border-radius:12px;background:#ecfeff;color:#155e75;border:1px solid #a5f3fc;">
            <?php echo promo_h($_GET['msg']); ?>
        </div>
    <?php endif; ?>

    <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(250px,1fr));gap:16px;">
        <div style="background:#fff;border:1px solid #e5e7eb;border-radius:16px;padding:18px;grid-column:span 2;">
            <div style="display:flex;justify-content:space-between;align-items:start;gap:12px;flex-wrap:wrap;">
                <div>
                    <h2 style="margin:0 0 8px;"><?php echo promo_h($promo['title']); ?></h2>
                    <div style="color:#6b7280;">الكود: <code><?php echo promo_h($promo['code']); ?></code></div>
                </div>
                <div style="display:flex;gap:8px;flex-wrap:wrap;">
                    <?php echo promo_status_badge($promo['status']); ?>
                    <?php echo promo_validity_badge($promo); ?>
                </div>
            </div>

            <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(220px,1fr));gap:14px;margin-top:18px;line-height:1.9;">
                <div><strong>نوع الخصم:</strong><br><?php echo $promo['discount_type'] === 'fixed' ? 'مبلغ ثابت' : 'نسبة مئوية'; ?></div>
                <div><strong>قيمة الخصم:</strong><br><?php echo promo_h(promo_discount_text($promo['discount_type'], $promo['discount_value'])); ?></div>
                <div><strong>الحد الأدنى للطلب:</strong><br><?php echo promo_h(promo_money($promo['minimum_order_amount'])); ?></div>
                <div><strong>الحد الأعلى للخصم:</strong><br><?php echo $promo['maximum_discount_amount'] !== null ? promo_h(promo_money($promo['maximum_discount_amount'])) : '-'; ?></div>
                <div><strong>حد الاستخدام:</strong><br><?php echo $promo['usage_limit'] !== null ? number_format((int)$promo['usage_limit']) : 'غير محدود'; ?></div>
                <div><strong>مرة واحدة لكل زبون:</strong><br><?php echo (int)$promo['is_single_use_per_customer'] === 1 ? 'نعم' : 'لا'; ?></div>
                <div><strong>لأول طلب فقط:</strong><br><?php echo (int)($promo['first_order_only'] ?? 0) === 1 ? 'نعم' : 'لا'; ?></div>
                <div><strong>بداية الصلاحية:</strong><br><?php echo promo_h(promo_datetime($promo['start_at'])); ?></div>
                <div><strong>نهاية الصلاحية:</strong><br><?php echo promo_h(promo_datetime($promo['end_at'])); ?></div>
                <div><strong>أُنشئ في:</strong><br><?php echo promo_h(promo_datetime($promo['created_at'])); ?></div>
                <div><strong>آخر تحديث:</strong><br><?php echo promo_h(promo_datetime($promo['updated_at'])); ?></div>
            </div>

            <div style="margin-top:16px;padding:14px;border:1px dashed #d1d5db;border-radius:12px;background:#f9fafb;line-height:1.8;">
                <strong>ملاحظات:</strong><br>
                <?php echo nl2br(promo_h($promo['notes'] ?: '-')); ?>
            </div>
        </div>

        <div style="background:#fff;border:1px solid #e5e7eb;border-radius:16px;padding:18px;">
            <div style="color:#6b7280;font-size:13px;">عداد الاستخدام المخزن</div>
            <div style="font-size:28px;font-weight:700;margin-top:8px;"><?php echo number_format((int)$promo['used_count']); ?></div>
        </div>
        <div style="background:#fff;border:1px solid #e5e7eb;border-radius:16px;padding:18px;">
            <div style="color:#6b7280;font-size:13px;">سجلات الاستخدام الفعلية</div>
            <div style="font-size:28px;font-weight:700;margin-top:8px;"><?php echo number_format((int)$promo['actual_usage_rows']); ?></div>
        </div>
        <div style="background:#fff;border:1px solid #e5e7eb;border-radius:16px;padding:18px;">
            <div style="color:#6b7280;font-size:13px;">الزبائن المستفيدين</div>
            <div style="font-size:28px;font-weight:700;margin-top:8px;"><?php echo number_format((int)$promo['distinct_customers']); ?></div>
        </div>
        <div style="background:#fff;border:1px solid #e5e7eb;border-radius:16px;padding:18px;">
            <div style="color:#6b7280;font-size:13px;">الطلبات المرتبطة</div>
            <div style="font-size:28px;font-weight:700;margin-top:8px;"><?php echo number_format((int)$promo['orders_count']); ?></div>
        </div>
        <div style="background:#fff;border:1px solid #e5e7eb;border-radius:16px;padding:18px;">
            <div style="color:#6b7280;font-size:13px;">إجمالي الخصم الممنوح</div>
            <div style="font-size:28px;font-weight:700;margin-top:8px;"><?php echo promo_h(promo_money($promo['total_discount_given'])); ?></div>
        </div>
    </div>

    <div style="display:grid;grid-template-columns:1.2fr 1fr;gap:16px;margin-top:20px;align-items:start;">
        <div style="background:#fff;border:1px solid #e5e7eb;border-radius:16px;overflow:auto;">
            <div style="padding:16px 18px;border-bottom:1px solid #e5e7eb;font-weight:700;">آخر استخدامات الكود</div>
            <table style="width:100%;border-collapse:collapse;min-width:820px;">
                <thead>
                    <tr style="background:#f9fafb;">
                        <th style="padding:12px;text-align:right;border-bottom:1px solid #e5e7eb;">#</th>
                        <th style="padding:12px;text-align:right;border-bottom:1px solid #e5e7eb;">الزبون</th>
                        <th style="padding:12px;text-align:right;border-bottom:1px solid #e5e7eb;">الهاتف</th>
                        <th style="padding:12px;text-align:right;border-bottom:1px solid #e5e7eb;">الطلب</th>
                        <th style="padding:12px;text-align:right;border-bottom:1px solid #e5e7eb;">قيمة الخصم</th>
                        <th style="padding:12px;text-align:right;border-bottom:1px solid #e5e7eb;">الوقت</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($usageRows)): ?>
                        <tr><td colspan="6" style="padding:18px;text-align:center;color:#6b7280;">لا توجد استخدامات بعد.</td></tr>
                    <?php else: ?>
                        <?php foreach ($usageRows as $usage): ?>
                            <tr>
                                <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo (int)$usage['id']; ?></td>
                                <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo promo_h($usage['customer_name'] ?: '-'); ?></td>
                                <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo promo_h($usage['customer_phone'] ?: '-'); ?></td>
                                <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo promo_h($usage['order_number'] ?: '-'); ?></td>
                                <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo promo_h(promo_money($usage['discount_value'])); ?></td>
                                <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo promo_h(promo_datetime($usage['used_at'])); ?></td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <div style="background:#fff;border:1px solid #e5e7eb;border-radius:16px;overflow:auto;">
            <div style="padding:16px 18px;border-bottom:1px solid #e5e7eb;font-weight:700;">آخر الطلبات المرتبطة</div>
            <table style="width:100%;border-collapse:collapse;min-width:620px;">
                <thead>
                    <tr style="background:#f9fafb;">
                        <th style="padding:12px;text-align:right;border-bottom:1px solid #e5e7eb;">الطلب</th>
                        <th style="padding:12px;text-align:right;border-bottom:1px solid #e5e7eb;">الزبون</th>
                        <th style="padding:12px;text-align:right;border-bottom:1px solid #e5e7eb;">الإجمالي</th>
                        <th style="padding:12px;text-align:right;border-bottom:1px solid #e5e7eb;">الخصم</th>
                        <th style="padding:12px;text-align:right;border-bottom:1px solid #e5e7eb;">الحالة</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($orderRows)): ?>
                        <tr><td colspan="5" style="padding:18px;text-align:center;color:#6b7280;">لا توجد طلبات مرتبطة بهذا الكود.</td></tr>
                    <?php else: ?>
                        <?php foreach ($orderRows as $order): ?>
                            <tr>
                                <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo promo_h($order['order_number']); ?></td>
                                <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo promo_h($order['customer_name']); ?></td>
                                <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo promo_h(promo_money($order['total_amount'])); ?></td>
                                <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo promo_h(promo_money($order['discount_value'])); ?></td>
                                <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo promo_h($order['order_status']); ?></td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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