<?php
/**
 * Edit Batch View — Modern, Responsive
 *
 * Upload to: /gmind/branch/Views/batches/edit.php
 */

/** @var array $batch */
/** @var array $courses */
/** @var int $studentCount */
/** @var string $csrfToken */

$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$csrf = htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8');
?>
<style>
    .batch-form-card {
        background: #fff;
        border: none;
        border-radius: 12px;
        box-shadow: 0 2px 8px rgba(0,0,0,0.06);
    }
    .batch-form-card .card-header {
        background: linear-gradient(135deg, #d97706, #b45309);
        color: #fff;
        border-radius: 12px 12px 0 0 !important;
        padding: 18px 24px;
        font-weight: 700;
    }
    .batch-form-card .card-body { padding: 28px; }
    .form-label { font-weight: 600; color: #2c1e4a; font-size: 0.9rem; margin-bottom: 6px; }
    .form-control, .form-select { border-radius: 8px; padding: 10px 12px; border: 1px solid #d0d5dd; }
    .form-control:focus, .form-select:focus { border-color: #d97706; box-shadow: 0 0 0 3px rgba(217,119,6,0.1); }
    .required-mark { color: #dc2626; margin-left: 2px; }
    .btn-submit {
        background: linear-gradient(135deg, #d97706, #b45309);
        color: #fff;
        padding: 14px 32px;
        border: none;
        border-radius: 8px;
        font-weight: 700;
        font-size: 1rem;
        cursor: pointer;
        transition: transform 0.15s ease, box-shadow 0.15s ease;
    }
    .btn-submit:hover { transform: translateY(-1px); box-shadow: 0 4px 12px rgba(217,119,6,0.3); color: #fff; }
    .btn-cancel {
        background: #f3f4f6; color: #6b7280; padding: 14px 28px; border-radius: 8px;
        text-decoration: none; font-weight: 600; display: inline-flex; align-items: center; gap: 8px;
    }
    .btn-cancel:hover { background: #e5e7eb; color: #4b5563; text-decoration: none; }
    .info-banner {
        background: linear-gradient(135deg, rgba(217,119,6,0.08), rgba(180,83,9,0.04));
        border-left: 4px solid #d97706;
        padding: 14px 18px;
        border-radius: 8px;
        margin-bottom: 24px;
        display: flex;
        align-items: center;
        gap: 14px;
    }
    .info-banner i { color: #d97706; font-size: 1.4rem; }
    .info-banner-text { font-size: 0.88rem; color: #2c1e4a; }
    .info-banner-text strong { color: #d97706; }
</style>

<div class="container-fluid py-4">
    <div style="margin-bottom:14px;">
        <a href="<?= $bp ?>/branch/batches" style="color:#d97706;text-decoration:none;font-size:0.88rem;">
            <i class="fas fa-arrow-left"></i> Back to Batches
        </a>
    </div>

    <h2 style="color:#0d3b3b; font-weight:700; margin-bottom:8px;">
        <i class="fas fa-edit"></i> Edit Batch
    </h2>
    <p class="text-muted mb-3" style="font-size:0.9rem;">
        Updating: <strong><?= htmlspecialchars($batch['batch_name'] ?? '', ENT_QUOTES, 'UTF-8') ?></strong>
    </p>

    <?php if ($studentCount > 0): ?>
    <div class="info-banner">
        <i class="fas fa-users"></i>
        <div class="info-banner-text">
            <strong><?= (int)$studentCount ?> student(s)</strong> are currently assigned to this batch. Changing the schedule or status will not unassign them.
        </div>
    </div>
    <?php endif; ?>

    <div class="card batch-form-card" style="max-width:750px;">
        <div class="card-header">
            <i class="fas fa-calendar-edit"></i> Batch Information
        </div>
        <div class="card-body">
            <form method="POST" action="<?= $bp ?>/branch/batches/<?= (int)$batch['id'] ?>/update">
                <input type="hidden" name="csrf_token" value="<?= $csrf ?>">

                <div class="row">
                    <div class="col-md-8 mb-3">
                        <label class="form-label">Batch Name <span class="required-mark">*</span></label>
                        <input type="text" name="batch_name" class="form-control" required
                               value="<?= htmlspecialchars($batch['batch_name'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                    </div>
                    <div class="col-md-4 mb-3">
                        <label class="form-label">Status</label>
                        <select name="status" class="form-select">
                            <?php
                            $currentStatus = $batch['status'] ?? 'upcoming';
                            $statuses = ['upcoming', 'running', 'completed'];
                            foreach ($statuses as $st):
                            ?>
                                <option value="<?= $st ?>" <?= ($currentStatus === $st) ? 'selected' : '' ?>>
                                    <?= ucfirst($st) ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </div>

                <div class="mb-3">
                    <label class="form-label">Course</label>
                    <select name="course_id" class="form-select">
                        <option value="">— Select Course (Optional) —</option>
                        <?php foreach ($courses as $c): ?>
                            <option value="<?= (int)$c['id'] ?>" <?= ((int)($batch['course_id'] ?? 0) === (int)$c['id']) ? 'selected' : '' ?>>
                                <?= htmlspecialchars($c['name'], ENT_QUOTES, 'UTF-8') ?>
                                (<?= htmlspecialchars($c['duration'] ?? '', ENT_QUOTES, 'UTF-8') ?>)
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Start Date</label>
                        <input type="date" name="start_date" class="form-control"
                               value="<?= !empty($batch['start_date']) ? htmlspecialchars($batch['start_date'], ENT_QUOTES, 'UTF-8') : '' ?>">
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">End Date</label>
                        <input type="date" name="end_date" class="form-control"
                               value="<?= !empty($batch['end_date']) ? htmlspecialchars($batch['end_date'], ENT_QUOTES, 'UTF-8') : '' ?>">
                    </div>
                </div>

                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Start Time</label>
                        <input type="time" name="start_time" class="form-control"
                               value="<?= htmlspecialchars($batch['start_time'] ?? '09:00', ENT_QUOTES, 'UTF-8') ?>">
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">End Time</label>
                        <input type="time" name="end_time" class="form-control"
                               value="<?= htmlspecialchars($batch['end_time'] ?? '11:00', ENT_QUOTES, 'UTF-8') ?>">
                    </div>
                </div>

                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Max Students</label>
                        <input type="number" name="max_students" class="form-control" min="1"
                               value="<?= htmlspecialchars($batch['max_students'] ?? 30, ENT_QUOTES, 'UTF-8') ?>">
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Created On</label>
                        <input type="text" class="form-control" disabled
                               value="<?= !empty($batch['created_at']) ? date('d M Y, h:i A', strtotime($batch['created_at'])) : '—' ?>">
                    </div>
                </div>

                <div class="mb-4">
                    <label class="form-label">Description (Optional)</label>
                    <textarea name="description" class="form-control" rows="3"
                              placeholder="Any notes about this batch..."><?= htmlspecialchars($batch['description'] ?? '', ENT_QUOTES, 'UTF-8') ?></textarea>
                </div>

                <div class="d-flex justify-content-between flex-wrap gap-2">
                    <a href="<?= $bp ?>/branch/batches" class="btn-cancel">
                        <i class="fas fa-times"></i> Cancel
                    </a>
                    <button type="submit" class="btn-submit">
                        <i class="fas fa-save"></i> Update Batch
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
