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

$page = max(1, (int)($_GET['page'] ?? 1));
$data = cust_get_customers($_GET, $page, 20);
$rows = $data['rows'];
$filters = $data['filters'];
$provinces = cust_get_provinces();

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="create.php" style="display:inline-block;background:#16a34a;color:#fff;text-decoration:none;padding:10px 16px;border-radius:10px;">إضافة زبون</a>
            <a href="index.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; ?>

    <form method="get" style="background:#fff;border:1px solid #e5e7eb;border-radius:16px;padding:16px;margin-bottom:18px;display:grid;grid-template-columns:repeat(auto-fit,minmax(180px,1fr));gap:12px;">
        <div>
            <label style="display:block;margin-bottom:6px;font-size:13px;">بحث</label>
            <input type="text" name="q" value="<?php echo cust_h($filters['q']); ?>" placeholder="اسم / هاتف / معرف انستغرام" style="width:100%;padding:10px 12px;border:1px solid #d1d5db;border-radius:10px;">
        </div>
        <div>
            <label style="display:block;margin-bottom:6px;font-size:13px;">الحالة الإدارية</label>
            <select name="status" style="width:100%;padding:10px 12px;border:1px solid #d1d5db;border-radius:10px;">
                <option value="">الكل</option>
                <option value="1" <?php echo $filters['status'] === '1' ? 'selected' : ''; ?>>نشط</option>
                <option value="0" <?php echo $filters['status'] === '0' ? 'selected' : ''; ?>>غير نشط</option>
            </select>
        </div>
        <div>
            <label style="display:block;margin-bottom:6px;font-size:13px;">تفعيل OTP</label>
            <select name="verified" style="width:100%;padding:10px 12px;border:1px solid #d1d5db;border-radius:10px;">
                <option value="">الكل</option>
                <option value="1" <?php echo $filters['verified'] === '1' ? 'selected' : ''; ?>>مفعل</option>
                <option value="0" <?php echo $filters['verified'] === '0' ? 'selected' : ''; ?>>غير مفعل</option>
            </select>
        </div>
        <div>
            <label style="display:block;margin-bottom:6px;font-size:13px;">المحافظة</label>
            <select name="province_id" style="width:100%;padding:10px 12px;border:1px solid #d1d5db;border-radius:10px;">
                <option value="">الكل</option>
                <?php foreach ($provinces as $province): ?>
                    <option value="<?php echo (int)$province['id']; ?>" <?php echo $filters['province_id'] == $province['id'] ? 'selected' : ''; ?>>
                        <?php echo cust_h($province['name']); ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="display:flex;align-items:end;gap:10px;">
            <button type="submit" style="padding:10px 14px;border:none;background:#1d4ed8;color:#fff;border-radius:10px;cursor:pointer;">فلترة</button>
            <a href="list.php" style="padding:10px 14px;background:#f3f4f6;color:#111827;border-radius:10px;text-decoration:none;">إعادة ضبط</a>
        </div>
    </form>

    <div style="background:#fff;border:1px solid #e5e7eb;border-radius:16px;overflow:auto;">
        <table style="width:100%;border-collapse:collapse;min-width:1300px;">
            <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>
                    <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;">OTP</th>
                    <th style="padding:12px;text-align:right;border-bottom:1px solid #e5e7eb;">إجراءات</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($rows)): ?>
                    <tr>
                        <td colspan="11" style="padding:18px;text-align:center;color:#6b7280;">لا توجد بيانات حالياً.</td>
                    </tr>
                <?php else: ?>
                    <?php foreach ($rows as $row): ?>
                        <tr>
                            <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo (int)$row['id']; ?></td>
                            <td style="padding:12px;border-bottom:1px solid #f1f5f9;font-weight:600;"><?php echo cust_h($row['full_name']); ?></td>
                            <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo cust_h($row['phone']); ?></td>
                            <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo cust_h($row['email'] ?: '-'); ?></td>
                            <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo cust_h($row['province_name'] ?: '-'); ?></td>
                            <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo number_format((int)$row['total_orders']); ?></td>
                            <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo cust_money($row['total_spent']); ?></td>
                            <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo number_format((int)$row['addresses_count']); ?></td>
                            <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo cust_status_badge($row['status']); ?></td>
                            <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo cust_verified_badge($row['is_verified']); ?></td>
                            <td style="padding:12px;border-bottom:1px solid #f1f5f9;white-space:nowrap;">
                                <a href="view.php?id=<?php echo (int)$row['id']; ?>" style="display:inline-block;padding:7px 10px;border-radius:8px;background:#eff6ff;color:#1d4ed8;text-decoration:none;margin:2px;">عرض</a>
                                <a href="edit.php?id=<?php echo (int)$row['id']; ?>" style="display:inline-block;padding:7px 10px;border-radius:8px;background:#ecfdf5;color:#166534;text-decoration:none;margin:2px;">تعديل</a>

                                <?php if ((int)$row['status'] === 1): ?>
                                    <?= admin_post_action_form('set_status.php', 'عدم تفعيل', '', 'customer_status', ['id' => (int)$row['id'], 'value' => 0, 'back' => $_SERVER['REQUEST_URI'] ?? '/app/admin/customers/list.php'], 'تأكيد تعطيل الزبون؟'); ?>
                                <?php else: ?>
                                    <?= admin_post_action_form('set_status.php', 'تفعيل', '', 'customer_status', ['id' => (int)$row['id'], 'value' => 1, 'back' => $_SERVER['REQUEST_URI'] ?? '/app/admin/customers/list.php'], 'تأكيد تفعيل الزبون؟'); ?>
                                <?php endif; ?>

                                <?php if ((int)$row['is_verified'] === 1): ?>
                                    <?= admin_post_action_form('set_verification.php', 'OTP غير مفعل', '', 'customer_verification', ['id' => (int)$row['id'], 'value' => 0, 'back' => $_SERVER['REQUEST_URI'] ?? '/app/admin/customers/list.php'], 'تأكيد إلغاء تفعيل OTP؟'); ?>
                                <?php else: ?>
                                    <?= admin_post_action_form('set_verification.php', 'تفعيل OTP', '', 'customer_verification', ['id' => (int)$row['id'], 'value' => 1, 'back' => $_SERVER['REQUEST_URI'] ?? '/app/admin/customers/list.php'], 'تأكيد تفعيل OTP؟'); ?>
                                    <a href="issue_otp.php?id=<?php echo (int)$row['id']; ?>" style="display:inline-block;padding:7px 10px;border-radius:8px;background:#111827;color:#fff;text-decoration:none;margin:2px;">OTP مباشر</a>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>

    <?php if ($data['pages'] > 1): ?>
        <div style="display:flex;gap:8px;flex-wrap:wrap;margin-top:16px;">
            <?php for ($i = 1; $i <= $data['pages']; $i++): ?>
                <?php $query = array_merge($_GET, array('page' => $i)); ?>
                <a href="?<?php echo http_build_query($query); ?>"
                   style="display:inline-block;padding:8px 12px;border-radius:10px;text-decoration:none;<?php echo $i === $data['page'] ? 'background:#1d4ed8;color:#fff;' : 'background:#fff;color:#111827;border:1px solid #e5e7eb;'; ?>">
                    <?php echo $i; ?>
                </a>
            <?php endfor; ?>
        </div>
    <?php endif; ?>
</div>

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