<?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);
$customer = $id > 0 ? cust_get_customer($id) : null;
if (!$customer) {
    cust_redirect('list.php?msg=' . urlencode('الزبون غير موجود'));
}

$addresses = cust_get_customer_addresses($id);
$orders = cust_get_customer_orders($id, 10);
$latestOtp = cust_get_latest_otp($id);

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;">عرض البيانات الأساسية، العناوين، الطلبات، والحالة الإدارية مع OTP.</p>
        </div>
        <div style="display:flex;gap:10px;flex-wrap:wrap;">
            <a href="edit.php?id=<?php echo (int)$customer['id']; ?>" style="display:inline-block;background:#16a34a;color:#fff;text-decoration:none;padding:10px 16px;border-radius:10px;">تعديل</a>
            <?php if ((int)$customer['status'] === 1): ?>
                <?= admin_post_action_form('set_status.php', 'عدم تفعيل', '', 'customer_status', ['id' => (int)$customer['id'], 'value' => 0, 'back' => $_SERVER['REQUEST_URI'] ?? '/app/admin/customers/view.php?id=' . (int)$customer['id']], 'تأكيد تعطيل الزبون؟'); ?>
            <?php else: ?>
                <?= admin_post_action_form('set_status.php', 'تفعيل', '', 'customer_status', ['id' => (int)$customer['id'], 'value' => 1, 'back' => $_SERVER['REQUEST_URI'] ?? '/app/admin/customers/view.php?id=' . (int)$customer['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 cust_h($_GET['msg']); ?>
        </div>
    <?php endif; ?>

    <div style="display:grid;grid-template-columns:2fr 1fr;gap:16px;align-items:start;">
        <div style="background:#fff;border:1px solid #e5e7eb;border-radius:16px;padding:18px;">
            <h3 style="margin-top:0;">البيانات الأساسية</h3>
            <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(220px,1fr));gap:14px;line-height:1.9;">
                <div><strong>الاسم:</strong><br><?php echo cust_h($customer['full_name']); ?></div>
                <div><strong>الهاتف:</strong><br><?php echo cust_h($customer['phone']); ?></div>
                <div><strong>معرف انستغرام:</strong><br><?php echo cust_h($customer['email'] ?: '-'); ?></div>
                <div><strong>الجنس:</strong><br><?php echo cust_h(cust_gender_text($customer['gender'])); ?></div>
                <div><strong>تاريخ الميلاد:</strong><br><?php echo cust_h(cust_date($customer['birth_date'])); ?></div>
                <div><strong>المحافظة:</strong><br><?php echo cust_h($customer['province_name'] ?: '-'); ?></div>
                <div><strong>الحالة الإدارية:</strong><br><?php echo cust_status_badge($customer['status']); ?></div>
                <div><strong>تفعيل OTP:</strong><br><?php echo cust_verified_badge($customer['is_verified']); ?></div>
                <div><strong>آخر دخول:</strong><br><?php echo cust_h(cust_datetime($customer['last_login_at'])); ?></div>
                <div><strong>تاريخ الإنشاء:</strong><br><?php echo cust_h(cust_datetime($customer['created_at'])); ?></div>
            </div>
        </div>

        <div style="display:grid;gap:16px;">
            <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:26px;font-weight:700;margin-top:8px;"><?php echo number_format((int)$customer['total_orders']); ?></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:26px;font-weight:700;margin-top:8px;"><?php echo cust_money($customer['total_spent']); ?></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:26px;font-weight:700;margin-top:8px;"><?php echo number_format((int)$customer['points_balance']); ?></div>
            </div>
            <div style="background:#fff;border:1px solid #e5e7eb;border-radius:16px;padding:18px;display:grid;gap:10px;">
                <div><strong>إجراءات سريعة</strong></div>
                <?php if ((int)$customer['is_verified'] === 1): ?>
                    <?= admin_post_action_form('set_verification.php', 'إلغاء تفعيل OTP', '', 'customer_verification', ['id' => (int)$customer['id'], 'value' => 0, 'back' => $_SERVER['REQUEST_URI'] ?? '/app/admin/customers/view.php?id=' . (int)$customer['id']], 'تأكيد إلغاء تفعيل OTP؟'); ?>
                <?php else: ?>
                    <?= admin_post_action_form('set_verification.php', 'تفعيل OTP', '', 'customer_verification', ['id' => (int)$customer['id'], 'value' => 1, 'back' => $_SERVER['REQUEST_URI'] ?? '/app/admin/customers/view.php?id=' . (int)$customer['id']], 'تأكيد تفعيل OTP؟'); ?>
                    <a href="issue_otp.php?id=<?php echo (int)$customer['id']; ?>" style="display:inline-block;background:#111827;color:#fff;text-decoration:none;padding:10px 14px;border-radius:10px;text-align:center;">فتح صفحة إصدار OTP</a>
                <?php endif; ?>
            </div>
            <div style="background:#fff;border:1px solid #e5e7eb;border-radius:16px;padding:18px;display:grid;gap:10px;">
                <strong>ملاحظات الإدارة</strong>
                <div style="background:#f8fafc;border:1px dashed #cbd5e1;border-radius:12px;padding:12px;line-height:1.9;color:#334155;"><?php echo nl2br(cust_h((string)($customer['admin_notes'] ?? 'لا توجد ملاحظات داخلية حالياً.'))); ?></div>
            </div>
            <div style="background:#fff;border:1px solid #e5e7eb;border-radius:16px;padding:18px;display:grid;gap:10px;">
                <strong>سبب إيقاف الحساب الذي يظهر للزبون</strong>
                <div style="background:#fff7ed;border:1px dashed #fdba74;border-radius:12px;padding:12px;line-height:1.9;color:#9a3412;"><?php echo nl2br(cust_h((string)($customer['inactive_reason'] ?? 'لا يوجد سبب مكتوب حالياً.'))); ?></div>
            </div>
            <div style="background:#fff;border:1px solid #e5e7eb;border-radius:16px;padding:18px;display:grid;gap:10px;">
                <div style="display:flex;justify-content:space-between;align-items:center;gap:10px;">
                    <strong>آخر OTP مباشر</strong>
                    <?= cust_otp_badge($latestOtp); ?>
                </div>
                <?php if (!$latestOtp): ?>
                    <div style="color:#6b7280;font-size:13px;line-height:1.8;">حالياً ماكو رمز متولد لهذا الزبون. إذا مزود الخدمة ما وصل الرمز، تگدر تصدر رمز مباشر من هنا وتعطيه للزبون.</div>
                <?php else: ?>
                    <div style="display:grid;gap:8px;font-size:14px;line-height:1.8;">
                        <div><strong>الرمز:</strong> <span style="font-family:monospace;font-size:18px;letter-spacing:2px;"><?php echo cust_h((string)$latestOtp['code']); ?></span></div>
                        <div><strong>الغرض:</strong> <?php echo cust_h((string)$latestOtp['purpose']); ?></div>
                        <div><strong>ينتهي:</strong> <?php echo cust_h(cust_datetime((string)$latestOtp['expires_at'])); ?></div>
                        <div><strong>القناة:</strong> <?php echo cust_h((string)($latestOtp['channel'] ?: '-')); ?></div>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <div style="margin-top:18px;background:#fff;border:1px solid #e5e7eb;border-radius:16px;padding:18px;overflow:auto;">
        <h3 style="margin-top:0;">العناوين</h3>
        <table style="width:100%;border-collapse:collapse;min-width:900px;">
            <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>
                    <th style="padding:12px;text-align:right;border-bottom:1px solid #e5e7eb;">الحالة</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($addresses)): ?>
                    <tr>
                        <td colspan="7" style="padding:16px;text-align:center;color:#6b7280;">لا توجد عناوين لهذا الزبون.</td>
                    </tr>
                <?php else: ?>
                    <?php foreach ($addresses as $address): ?>
                        <tr>
                            <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo cust_h($address['title']); ?></td>
                            <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo cust_h($address['province']); ?></td>
                            <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo cust_h(trim(($address['city'] ?: '-') . ' / ' . ($address['area'] ?: '-'))); ?></td>
                            <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo cust_h($address['full_address']); ?></td>
                            <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo cust_money($address['delivery_fee']); ?></td>
                            <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo (int)$address['is_default'] === 1 ? 'نعم' : 'لا'; ?></td>
                            <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo (int)$address['status'] === 1 ? 'نشط' : 'غير نشط'; ?></td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>

    <div style="margin-top:18px;background:#fff;border:1px solid #e5e7eb;border-radius:16px;padding:18px;overflow:auto;">
        <h3 style="margin-top:0;">آخر الطلبات</h3>
        <table style="width:100%;border-collapse:collapse;min-width:850px;">
            <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>
                    <th style="padding:12px;text-align:right;border-bottom:1px solid #e5e7eb;">التاريخ</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($orders)): ?>
                    <tr>
                        <td colspan="7" style="padding:16px;text-align:center;color:#6b7280;">لا توجد طلبات لهذا الزبون.</td>
                    </tr>
                <?php else: ?>
                    <?php foreach ($orders as $order): ?>
                        <tr>
                            <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo (int)$order['id']; ?></td>
                            <td style="padding:12px;border-bottom:1px solid #f1f5f9;font-weight:600;"><?php echo cust_h($order['order_number']); ?></td>
                            <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo cust_h($order['order_type']); ?></td>
                            <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo cust_money($order['total_amount']); ?></td>
                            <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo cust_payment_status_badge($order['payment_status']); ?></td>
                            <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo cust_order_status_badge($order['order_status']); ?></td>
                            <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo cust_h(cust_datetime($order['placed_at'])); ?></td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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