<div class="row justify-content-center">
    <div class="col-lg-8">
        <div class="card">
            <div class="card-header"><h5 class="mb-0"><i class="fas fa-edit me-2"></i><?= lang('reports.edit_schedule') ?></h5></div>
            <div class="card-body">
                <form id="schedule-form" data-ajax="true" method="POST" action="/api/reports/schedules/<?= (int)$schedule['id'] ?>">
                    <?= csrf_field() ?>
                    <div class="row g-3">
                        <div class="col-md-6">
                            <label class="form-label">Schedule Name <span class="text-danger">*</span></label>
                            <input type="text" name="schedule_name" class="form-control" value="<?= e($schedule['schedule_name']) ?>" required>
                            <div class="invalid-feedback" data-field="schedule_name"></div>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Report <span class="text-danger">*</span></label>
                            <select name="report_id" class="form-select" required>
                                <?php foreach ($reports as $r): ?>
                                <option value="<?= (int)$r['id'] ?>" <?= $r['id'] == $schedule['report_id'] ? 'selected' : '' ?>><?= e($r['report_name_en']) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Frequency</label>
                            <select name="frequency" class="form-select" required>
                                <?php foreach (['daily','weekly','monthly','quarterly','yearly'] as $f): ?>
                                <option value="<?= $f ?>" <?= $schedule['frequency'] === $f ? 'selected' : '' ?>><?= ucfirst($f) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Day of Week</label>
                            <select name="day_of_week" class="form-select">
                                <option value="">N/A</option>
                                <?php for ($i = 0; $i < 7; $i++): ?>
                                <option value="<?= $i ?>" <?= $schedule['day_of_week'] == $i ? 'selected' : '' ?>><?= date('l', strtotime("Sunday +{$i} days")) ?></option>
                                <?php endfor; ?>
                            </select>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Day of Month</label>
                            <input type="number" name="day_of_month" class="form-control" min="1" max="28" value="<?= (int)($schedule['day_of_month'] ?? 1) ?>">
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Time</label>
                            <input type="time" name="time_of_day" class="form-control" value="<?= e($schedule['time_of_day'] ?? '06:00') ?>" required>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Output Format</label>
                            <select name="output_format" class="form-select" required>
                                <?php foreach (['pdf','excel','csv'] as $f): ?>
                                <option value="<?= $f ?>" <?= $schedule['output_format'] === $f ? 'selected' : '' ?>><?= strtoupper($f) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Delivery</label>
                            <select name="delivery_method" class="form-select" required>
                                <?php foreach (['email','in_app','save','both_email_save'] as $d): ?>
                                <option value="<?= $d ?>" <?= $schedule['delivery_method'] === $d ? 'selected' : '' ?>><?= ucfirst(str_replace('_', ' ', $d)) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-12">
                            <label class="form-label">Recipients</label>
                            <?php $recip = json_decode($schedule['recipients'] ?? '[]', true); ?>
                            <input type="text" name="recipients" class="form-control" value="<?= e(is_array($recip) ? implode(', ', $recip) : '') ?>">
                        </div>
                    </div>
                    <div class="mt-4 d-flex gap-2">
                        <button type="submit" class="btn btn-primary"><i class="fas fa-save me-1"></i><?= lang('common.save') ?></button>
                        <a href="/reports/schedules" class="btn btn-secondary"><?= lang('common.cancel') ?></a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>