<?php $pageTitle = Lang::get('condition_options') . ' - ' . Lang::get('app_name'); ?>
<?php require APP_ROOT . '/views/layouts/header.php'; ?>

<div class="mb-6">
    <div class="flex items-center justify-between">
        <h1 class="text-2xl font-bold"><?= Lang::get('condition_options') ?></h1>
        <a href="<?= APP_URL ?>/admin" class="text-gray-500 dark:text-gray-400 hover:text-primary-600 dark:hover:text-primary-400 text-sm transition-colors">
            &larr; <?= Lang::get('back') ?>
        </a>
    </div>
</div>

<!-- Add new option -->
<div class="bg-white dark:bg-gray-800 rounded-xl border border-gray-200 dark:border-gray-700 p-6 mb-6">
    <h2 class="text-lg font-semibold mb-4"><?= Lang::get('add_option') ?></h2>
    <form method="POST" action="<?= APP_URL ?>/admin/condition-options" class="flex items-end gap-4">
        <input type="hidden" name="csrf_token" value="<?= Session::getCsrfToken() ?>">
        <div class="flex-1">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"><?= Lang::get('label') ?></label>
            <input type="text" name="label" required class="w-full px-3 py-2 rounded-lg border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100 focus:ring-2 focus:ring-primary-500 focus:border-primary-500 outline-none transition-colors text-sm" placeholder="UNC">
        </div>
        <div class="flex-1">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"><?= Lang::get('description') ?></label>
            <input type="text" name="description" class="w-full px-3 py-2 rounded-lg border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100 focus:ring-2 focus:ring-primary-500 focus:border-primary-500 outline-none transition-colors text-sm" placeholder="Uncirculated">
        </div>
        <div class="w-32">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"><?= Lang::get('type') ?></label>
            <select name="type" class="w-full px-3 py-2 rounded-lg border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100 focus:ring-2 focus:ring-primary-500 focus:border-primary-500 outline-none transition-colors text-sm">
                <option value="both"><?= Lang::get('condition_type_both') ?></option>
                <option value="coin"><?= Lang::get('condition_type_coin') ?></option>
                <option value="banknote"><?= Lang::get('condition_type_banknote') ?></option>
            </select>
        </div>
        <div class="w-24">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"><?= Lang::get('sort_order') ?></label>
            <input type="number" name="sort_order" value="0" class="w-full px-3 py-2 rounded-lg border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100 focus:ring-2 focus:ring-primary-500 focus:border-primary-500 outline-none transition-colors text-sm">
        </div>
        <button type="submit" class="btn-gradient btn-gradient-blue btn-gradient-sm"><?= Lang::get('add') ?></button>
    </form>
</div>

<!-- Existing options -->
<div class="bg-white dark:bg-gray-800 rounded-xl border border-gray-200 dark:border-gray-700 overflow-hidden">
    <table class="w-full text-sm">
        <thead class="bg-gray-50 dark:bg-gray-700/50">
            <tr>
                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider"><?= Lang::get('label') ?></th>
                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider"><?= Lang::get('description') ?></th>
                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider"><?= Lang::get('type') ?></th>
                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider"><?= Lang::get('sort_order') ?></th>
                <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider"><?= Lang::get('actions') ?></th>
            </tr>
        </thead>
        <tbody class="divide-y divide-gray-200 dark:divide-gray-700">
            <?php foreach ($options as $opt): ?>
            <tr class="hover:bg-gray-50 dark:hover:bg-gray-700/30 transition-colors">
                <form method="POST" action="<?= APP_URL ?>/admin/condition-options/<?= $opt['id'] ?>">
                    <input type="hidden" name="csrf_token" value="<?= Session::getCsrfToken() ?>">
                    <td class="px-6 py-3">
                        <input type="text" name="label" value="<?= htmlspecialchars($opt['label']) ?>" required class="w-full px-2 py-1 rounded border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100 text-sm">
                    </td>
                    <td class="px-6 py-3">
                        <input type="text" name="description" value="<?= htmlspecialchars($opt['description'] ?? '') ?>" class="w-full px-2 py-1 rounded border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100 text-sm">
                    </td>
                    <td class="px-6 py-3">
                        <select name="type" class="w-32 px-2 py-1 rounded border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100 text-sm">
                            <option value="both" <?= ($opt['type'] ?? 'both') === 'both' ? 'selected' : '' ?>><?= Lang::get('condition_type_both') ?></option>
                            <option value="coin" <?= ($opt['type'] ?? 'both') === 'coin' ? 'selected' : '' ?>><?= Lang::get('condition_type_coin') ?></option>
                            <option value="banknote" <?= ($opt['type'] ?? 'both') === 'banknote' ? 'selected' : '' ?>><?= Lang::get('condition_type_banknote') ?></option>
                        </select>
                    </td>
                    <td class="px-6 py-3">
                        <input type="number" name="sort_order" value="<?= $opt['sort_order'] ?>" class="w-20 px-2 py-1 rounded border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100 text-sm">
                    </td>
                    <td class="px-6 py-3 text-right">
                        <div class="flex items-center justify-end gap-2">
                            <button type="submit" class="btn-gradient btn-gradient-blue btn-gradient-sm"><?= Lang::get('save') ?></button>
                            <a href="javascript:void(0)" onclick="confirmDelete('<?= Lang::get('confirm_delete') ?>', function(){ window.location.href='<?= APP_URL ?>/admin/condition-options/<?= $opt['id'] ?>/delete'; })" class="btn-gradient btn-gradient-red btn-gradient-sm"><?= Lang::get('delete') ?></a>
                        </div>
                    </td>
                </form>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>

<?php require APP_ROOT . '/views/layouts/footer.php'; ?>
