<?php
/** @var array $doc */
/** @var array $subjects */
/** @var string $csrfToken */

$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';

$isApproved = ($doc['status'] ?? '') === 'generated';
?>
<div class="container-fluid py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2><i class="fas fa-edit"></i> Edit Result</h2>
            <p class="text-muted mb-0">
                <strong><?= htmlspecialchars($doc['student_name'] ?? '') ?></strong>
                — <?= htmlspecialchars($doc['certificate_no'] ?? '') ?>
                (<?= htmlspecialchars($doc['course_name'] ?? '') ?>)
            </p>
        </div>
        <a href="<?= $bp ?>/branch/cert-gen" class="btn btn-secondary">
            <i class="fas fa-arrow-left"></i> Back
        </a>
    </div>

    <?php if ($isApproved): ?>
    <div class="alert alert-success">
        <i class="fas fa-check-circle"></i>
        <strong>This certificate is already approved!</strong>
        You can edit student name, father name, and marks. Status will remain "Approved" — no re-approval needed.
        PDF will need to be regenerated by Super Admin to reflect changes.
    </div>
    <?php endif; ?>

    <form method="POST" action="<?= $bp ?>/branch/cert-gen/update/<?= (int)$doc['id'] ?>">
        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '') ?>">

        <div class="card mb-3">
            <div class="card-header">
                <h5 class="mb-0"><i class="fas fa-info-circle"></i> Student Information</h5>
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-md-6">
                        <label class="form-label">Student Name</label>
                        <input type="text" name="student_name" class="form-control" 
                               value="<?= htmlspecialchars($doc['student_name'] ?? '') ?>" required>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Father's Name</label>
                        <input type="text" name="father_name" class="form-control"
                               value="<?= htmlspecialchars($doc['father_name'] ?? '') ?>">
                    </div>
                </div>
                <div class="row mt-2">
                    <div class="col-md-4">
                        <label class="form-label">Enrollment No</label>
                        <input type="text" class="form-control" value="<?= htmlspecialchars($doc['enrollment_no'] ?? '') ?>" disabled>
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">Course</label>
                        <input type="text" class="form-control" value="<?= htmlspecialchars($doc['course_name'] ?? '') ?>" disabled>
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">Certificate No</label>
                        <input type="text" class="form-control" value="<?= htmlspecialchars($doc['certificate_no'] ?? '') ?>" disabled>
                    </div>
                </div>
            </div>
        </div>

        <div class="card mb-3">
            <div class="card-header">
                <h5 class="mb-0"><i class="fas fa-book"></i> Subjects & Marks</h5>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-bordered">
                        <thead>
                            <tr>
                                <th width="50">#</th>
                                <th>Subject Name</th>
                                <th width="120">Max Marks</th>
                                <th width="120">Obtained Marks</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($subjects as $i => $s): ?>
                            <tr>
                                <td><?= $i + 1 ?></td>
                                <td>
                                    <input type="text" name="subject_name[]" class="form-control"
                                           value="<?= htmlspecialchars($s['name'] ?? '') ?>" required>
                                </td>
                                <td>
                                    <input type="number" name="max_marks[]" class="form-control"
                                           value="<?= htmlspecialchars($s['max_marks'] ?? 100) ?>" min="1" required>
                                </td>
                                <td>
                                    <input type="number" name="obtained_marks[]" class="form-control"
                                           value="<?= htmlspecialchars($s['obtained'] ?? 0) ?>" min="0" required>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <div class="d-flex justify-content-between">
            <a href="<?= $bp ?>/branch/cert-gen" class="btn btn-secondary">
                <i class="fas fa-times"></i> Cancel
            </a>
            <button type="submit" class="btn btn-primary">
                <i class="fas fa-save"></i> Update Result
            </button>
        </div>
    </form>
</div>
