<?php
$fullName = trim($contact['emergency_contact_name'] ?? '');
$nameParts = explode(' ', $fullName, 2);
$firstName = $nameParts[0] ?? '';
$lastName  = $nameParts[1] ?? '';
?>

<div class="modal fade" id="editEmergencyContact<?= $contact['id'] ?>" tabindex="-1" aria-labelledby="editEmergencyContactLabel<?= $contact['id'] ?>" aria-hidden="true">
    <div class="modal-dialog">
        <form method="post" action="<?= site_url('parent/edit_emergency_contact/' . $contact['id']) ?>" onsubmit="return validateEmergencyContactForm(<?= $contact['id'] ?>)">
            <?= csrf_field() ?>
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="editEmergencyContactLabel<?= $contact['id'] ?>">Edit Emergency Contact</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>

                <div class="modal-body">
                    <!-- First Name -->
                    <div class="form-group mb-3">
                        <label for="emergencyFirstName<?= $contact['id'] ?>">First Name</label>
                        <input type="text"
                               id="emergencyFirstName<?= $contact['id'] ?>"
                               name="emergency_first_name"
                               class="form-control"
                               value="<?= esc($firstName) ?>"
                               required
                               maxlength="30"
                               title="Only letters, spaces, hyphens, and apostrophes allowed">
                    </div>

                    <!-- Last Name -->
                    <div class="form-group mb-3">
                        <label for="emergencyLastName<?= $contact['id'] ?>">Last Name</label>
                        <input type="text"
                               id="emergencyLastName<?= $contact['id'] ?>"
                               name="emergency_last_name"
                               class="form-control"
                               value="<?= esc($lastName) ?>"
                               required
                               maxlength="30"
                               title="Only letters, spaces, hyphens, and apostrophes allowed">
                    </div>

                    <!-- Phone -->
                    <div class="form-group mb-3">
                        <label for="cellPhone<?= $contact['id'] ?>">Cell Phone</label>
                        <input type="tel"
                               id="cellPhone<?= $contact['id'] ?>"
                               name="cellphone"
                               class="form-control"
                               value="<?= esc($contact['cellphone']) ?>"
                               required
                               maxlength="14"
                               inputmode="numeric"
                               title="Enter a valid 10-digit phone number">
                    </div>

                    <!-- Email -->
                    <div class="form-group mb-3">
                        <label for="email<?= $contact['id'] ?>">Email</label>
                        <input type="email"
                               id="email<?= $contact['id'] ?>"
                               name="email"
                               class="form-control"
                               value="<?= esc($contact['email'] ?? '') ?>"
                               required
                               maxlength="50"
                               title="Enter a valid email address">
                    </div>

                    <!-- Relation -->
                    <div class="form-group mb-3">
                        <label for="relation<?= $contact['id'] ?>">Relation</label>
                        <input type="text"
                               id="relation<?= $contact['id'] ?>"
                               name="relation"
                               class="form-control"
                               value="<?= esc($contact['relation']) ?>"
                               required
                               maxlength="30"
                               title="Only letters, spaces, hyphens, and apostrophes allowed">
                    </div>
                </div>

                <div class="modal-footer">
                    <button type="submit" class="btn btn-primary">Save Changes</button>
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                </div>
            </div>
        </form>
    </div>
</div>
