<?php

$newsRecord = $newsRecord ?? null;
$formErrors = $formErrors ?? [];
$fieldValue = static function (string $field, mixed $default = '') use ($newsRecord) {
    if (isset($_SESSION['_old']) && array_key_exists($field, $_SESSION['_old'])) {
        return old($field, $default);
    }

    return $newsRecord[$field] ?? $default;
};
?>
<div class="row g-3">
    <div class="col-md-8">
        <label class="form-label" for="title">Title</label>
        <input class="form-control <?php echo validation_error($formErrors, 'title') ? 'is-invalid' : ''; ?>" id="title" name="title" type="text" maxlength="200" value="<?php echo e((string) $fieldValue('title')); ?>" required>
        <?php if (validation_error($formErrors, 'title')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'title')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-2">
        <label class="form-label" for="category">Category</label>
        <select class="form-select <?php echo validation_error($formErrors, 'category') ? 'is-invalid' : ''; ?>" id="category" name="category">
            <?php foreach (news_category_options() as $category): ?>
                <option value="<?php echo e($category); ?>" <?php echo (string) $fieldValue('category', 'general') === $category ? 'selected' : ''; ?>><?php echo e(news_category_label($category)); ?></option>
            <?php endforeach; ?>
        </select>
        <?php if (validation_error($formErrors, 'category')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'category')); ?></div><?php endif; ?>
    </div>
    <div class="col-md-2">
        <label class="form-label" for="status">Status</label>
        <?php if (can_publish_news()): ?>
            <select class="form-select <?php echo validation_error($formErrors, 'status') ? 'is-invalid' : ''; ?>" id="status" name="status">
                <?php foreach (news_status_options() as $status): ?>
                    <option value="<?php echo e($status); ?>" <?php echo (string) $fieldValue('status', 'draft') === $status ? 'selected' : ''; ?>><?php echo e(ucfirst($status)); ?></option>
                <?php endforeach; ?>
            </select>
        <?php else: ?>
            <input class="form-control" type="text" value="Draft" readonly>
            <input type="hidden" name="status" value="draft">
        <?php endif; ?>
        <?php if (validation_error($formErrors, 'status')): ?><div class="invalid-feedback d-block"><?php echo e((string) validation_error($formErrors, 'status')); ?></div><?php endif; ?>
    </div>
    <div class="col-12">
        <label class="form-label" for="body">Body</label>
        <textarea class="form-control <?php echo validation_error($formErrors, 'body') ? 'is-invalid' : ''; ?>" id="body" name="body" rows="10" required><?php echo e((string) $fieldValue('body')); ?></textarea>
        <?php if (validation_error($formErrors, 'body')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'body')); ?></div><?php endif; ?>
    </div>
    <div class="col-12">
        <label class="form-label" for="featured_image">Featured image</label>
        <input class="form-control <?php echo validation_error($formErrors, 'featured_image') ? 'is-invalid' : ''; ?>" id="featured_image" name="featured_image" type="file" accept="image/*">
        <?php if (!empty($newsRecord['featured_image'])): ?><div class="form-text">Current image is on file. Upload a new one to replace it.</div><?php endif; ?>
        <?php if (validation_error($formErrors, 'featured_image')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'featured_image')); ?></div><?php endif; ?>
    </div>
</div>