<?php
$trow = $track ?? null;
function track_val(?array $row, string $key, string $default = ''): string {
    if (isset($row[$key])) {
        return (string) $row[$key];
    }
    return old($key, $default);
}
?>

<div class="row g-3">
    <div class="col-md-6">
        <label class="form-label"><?= e(trans('track_name_ar')) ?></label>
        <input type="text" name="name_ar" class="form-control rounded-3" value="<?= e(track_val($trow, 'name_ar')) ?>" required>
    </div>
    <div class="col-md-6">
        <label class="form-label"><?= e(trans('track_name_en')) ?></label>
        <input type="text" name="name_en" class="form-control rounded-3" value="<?= e(track_val($trow, 'name_en')) ?>" required>
    </div>

    <div class="col-md-6">
        <label class="form-label"><?= e(trans('track_description_ar')) ?></label>
        <textarea name="description_ar" class="form-control rounded-3" rows="5"><?= e(track_val($trow, 'description_ar')) ?></textarea>
    </div>
    <div class="col-md-6">
        <label class="form-label"><?= e(trans('track_description_en')) ?></label>
        <textarea name="description_en" class="form-control rounded-3" rows="5"><?= e(track_val($trow, 'description_en')) ?></textarea>
    </div>

    <div class="col-md-6">
        <label class="form-label"><?= e(trans('chair_user')) ?></label>
        <select name="chair_user_id" class="form-select rounded-3">
            <option value="0">--</option>
            <?php foreach ($chairs as $chair): ?>
                <?php $selected = (string) ((int) track_val($trow, 'chair_user_id', '0')) === (string) $chair['id']; ?>
                <option value="<?= e((string) $chair['id']) ?>" <?= $selected ? 'selected' : '' ?>>
                    <?= e($chair['first_name'] . ' ' . $chair['last_name'] . ' (' . $chair['email'] . ')') ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>

    <div class="col-md-3">
        <label class="form-label"><?= e(trans('min_reviewers')) ?></label>
        <input type="number" min="1" max="20" name="min_reviewers" class="form-control rounded-3" value="<?= e(track_val($trow, 'min_reviewers', '2')) ?>">
    </div>
    <div class="col-md-3">
        <label class="form-label"><?= e(trans('max_reviewers')) ?></label>
        <input type="number" min="1" max="20" name="max_reviewers" class="form-control rounded-3" value="<?= e(track_val($trow, 'max_reviewers', '3')) ?>">
    </div>

    <div class="col-md-12">
        <div class="form-check">
            <?php $active = $trow ? ((int) $trow['is_active'] === 1) : true; ?>
            <input class="form-check-input" type="checkbox" name="is_active" id="is_active" <?= $active ? 'checked' : '' ?>>
            <label class="form-check-label" for="is_active"><?= e(trans('active')) ?></label>
        </div>
    </div>
</div>
