<?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'] ?? $_POST['id'] ?? 0);
if ($id < 1) {
    sett_redirect('list.php?msg=' . urlencode('معرّف الإعداد غير صحيح'));
}

$row = sett_get_setting($id);
if (!$row) {
    sett_redirect('list.php?msg=' . urlencode('الإعداد غير موجود'));
}

$form = $row;
$error = '';
$groups = sett_get_groups();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $form = sett_fill_input($_POST, $row);
    $save = sett_save_setting($form, $id);

    if ($save['ok']) {
        sett_redirect('view.php?id=' . $id . '&msg=' . urlencode($save['message']));
    }

    $error = $save['message'];
}

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

<div class="content-wrapper" style="padding:20px;max-width:900px;">
    <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="view.php?id=<?php echo $id; ?>" style="display:inline-block;background:#f3f4f6;color:#111827;text-decoration:none;padding:10px 16px;border-radius:10px;">رجوع</a>
            <a href="list.php" style="display:inline-block;background:#1d4ed8;color:#fff;text-decoration:none;padding:10px 16px;border-radius:10px;">القائمة</a>
        </div>
    </div>

    <?php if ($error !== ''): ?>
        <div style="margin-bottom:16px;padding:12px 14px;border-radius:12px;background:#fee2e2;color:#991b1b;border:1px solid #fecaca;">
            <?php echo sett_h($error); ?>
        </div>
    <?php endif; ?>

    <form method="post" style="background:#fff;border:1px solid #e5e7eb;border-radius:16px;padding:20px;display:grid;gap:16px;">
        <input type="hidden" name="id" value="<?php echo $id; ?>">

        <div>
            <label style="display:block;margin-bottom:6px;font-size:13px;">مفتاح الإعداد</label>
            <input type="text" name="setting_key" required value="<?php echo sett_h($form['setting_key']); ?>" 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>
            <div style="display:grid;grid-template-columns:1fr 1fr;gap:10px;">
                <select name="setting_group" style="width:100%;padding:10px 12px;border:1px solid #d1d5db;border-radius:10px;">
                    <?php foreach ($groups as $group): ?>
                        <option value="<?php echo sett_h($group); ?>" <?php echo ($form['setting_group'] === $group ? 'selected' : ''); ?>><?php echo sett_h($group); ?></option>
                    <?php endforeach; ?>
                </select>
                <input type="text" name="setting_group_custom" value="" placeholder="أو اكتب مجموعة جديدة" style="width:100%;padding:10px 12px;border:1px solid #d1d5db;border-radius:10px;">
            </div>
        </div>

        <div>
            <label style="display:block;margin-bottom:6px;font-size:13px;">قيمة الإعداد</label>
            <textarea name="setting_value" rows="10" style="width:100%;padding:10px 12px;border:1px solid #d1d5db;border-radius:10px;resize:vertical;"><?php echo sett_h($form['setting_value']); ?></textarea>
        </div>

        <div style="display:flex;justify-content:flex-end;gap:10px;flex-wrap:wrap;">
            <a href="view.php?id=<?php echo $id; ?>" style="display:inline-block;text-decoration:none;background:#f3f4f6;color:#111827;padding:10px 16px;border-radius:10px;">إلغاء</a>
            <button type="submit" style="background:#16a34a;color:#fff;border:none;padding:10px 16px;border-radius:10px;cursor:pointer;">حفظ التعديلات</button>
        </div>
    </form>
</div>

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