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

$filters = sett_build_filters($_GET);
$page = max(1, (int)($_GET['page'] ?? 1));
$perPage = 20;
$totalRows = sett_count_settings($filters);
$totalPages = max(1, (int)ceil($totalRows / $perPage));
if ($page > $totalPages) {
    $page = $totalPages;
}
$offset = ($page - 1) * $perPage;
$rows = sett_get_settings($filters, $perPage, $offset);
$groups = sett_get_groups();

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="index.php" style="display:inline-block;background:#f3f4f6;color:#111827;text-decoration:none;padding:10px 16px;border-radius:10px;">رجوع</a>
            <a href="create.php" style="display:inline-block;background:#16a34a;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 sett_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:2fr 1fr auto auto;gap:12px;align-items:end;">
        <div>
            <label style="display:block;margin-bottom:6px;font-size:13px;">بحث</label>
            <input type="text" name="q" value="<?php echo sett_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="group" style="width:100%;padding:10px 12px;border:1px solid #d1d5db;border-radius:10px;">
                <option value="">الكل</option>
                <?php foreach ($groups as $group): ?>
                    <option value="<?php echo sett_h($group); ?>" <?php echo ($filters['group'] === $group ? 'selected' : ''); ?>><?php echo sett_h($group); ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <button type="submit" style="background:#1d4ed8;color:#fff;border:none;padding:10px 16px;border-radius:10px;cursor:pointer;">بحث</button>
        <a href="list.php" style="display:inline-block;text-decoration:none;background:#f3f4f6;color:#111827;padding:10px 16px;border-radius:10px;text-align:center;">تصفير</a>
    </form>

    <div style="background:#fff;border:1px solid #e5e7eb;border-radius:16px;padding:0;overflow:auto;">
        <table style="width:100%;border-collapse:collapse;min-width:920px;">
            <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 ($rows): ?>
                <?php foreach ($rows as $row): ?>
                    <tr>
                        <td style="padding:12px;border-bottom:1px solid #f3f4f6;"><?php echo (int)$row['id']; ?></td>
                        <td style="padding:12px;border-bottom:1px solid #f3f4f6;font-weight:600;"><?php echo sett_h($row['setting_key']); ?></td>
                        <td style="padding:12px;border-bottom:1px solid #f3f4f6;max-width:350px;"><div style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;"><?php echo sett_h($row['setting_value'] === '' ? '(فارغ)' : $row['setting_value']); ?></div></td>
                        <td style="padding:12px;border-bottom:1px solid #f3f4f6;"><?php echo sett_group_badge($row['setting_group']); ?></td>
                        <td style="padding:12px;border-bottom:1px solid #f3f4f6;"><?php echo sett_datetime($row['updated_at']); ?></td>
                        <td style="padding:12px;border-bottom:1px solid #f3f4f6;">
                            <div style="display:flex;gap:8px;flex-wrap:wrap;">
                                <a href="view.php?id=<?php echo (int)$row['id']; ?>" style="text-decoration:none;background:#eff6ff;color:#1d4ed8;padding:7px 10px;border-radius:8px;">عرض</a>
                                <a href="edit.php?id=<?php echo (int)$row['id']; ?>" style="text-decoration:none;background:#ecfccb;color:#3f6212;padding:7px 10px;border-radius:8px;">تعديل</a>
                                <form method="post" action="delete.php" onsubmit="return confirm('متأكد تريد حذف هذا الإعداد؟');" style="margin:0;">
                                    <input type="hidden" name="id" value="<?php echo (int)$row['id']; ?>">
                                    <button type="submit" style="border:none;background:#fee2e2;color:#991b1b;padding:7px 10px;border-radius:8px;cursor:pointer;">حذف</button>
                                </form>
                            </div>
                        </td>
                    </tr>
                <?php endforeach; ?>
            <?php else: ?>
                <tr>
                    <td colspan="6" style="padding:20px;text-align:center;color:#6b7280;">ماكو نتائج مطابقة.</td>
                </tr>
            <?php endif; ?>
            </tbody>
        </table>
    </div>

    <?php if ($totalPages > 1): ?>
        <div style="display:flex;gap:8px;flex-wrap:wrap;margin-top:16px;">
            <?php for ($i = 1; $i <= $totalPages; $i++): ?>
                <?php
                $qs = $_GET;
                $qs['page'] = $i;
                $url = 'list.php?' . http_build_query($qs);
                ?>
                <a href="<?php echo sett_h($url); ?>" style="text-decoration:none;padding:8px 12px;border-radius:8px;border:1px solid <?php echo ($i === $page ? '#1d4ed8' : '#d1d5db'); ?>;background:<?php echo ($i === $page ? '#1d4ed8' : '#fff'); ?>;color:<?php echo ($i === $page ? '#fff' : '#111827'); ?>;"><?php echo $i; ?></a>
            <?php endfor; ?>
        </div>
    <?php endif; ?>
</div>

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