{% assign instanceId = 'Global' | PageParameter:'RegistrationInstanceId' | AsInteger %}
{% assign rckipid = 'Global' | PageParameter:'rckipid' %}
{% if instanceId > 0 and rckipid == '' %}
{% assign personId = CurrentPerson.Id %}
{% assign familyId = CurrentPerson.PrimaryFamilyId %}
{% if CurrentPerson != null and personId != null and familyId != null and familyId != '' %}
{% sql return:'data' %}
SELECT 
    r.Id RegistrationId,
    r.CreatedDateTime,
    r.ModifiedDateTime,
    rp.NickName RegistrarNickName,
    rp.LastName RegistrarLastName,
    p.NickName RegistrantNickName,
    p.LastName RegistrantLastName,
    CASE WHEN rpa.PersonId = {{ personId }} THEN 1 ELSE 0 END IsOwn
FROM Registration r
JOIN PersonAlias rpa ON rpa.Id = r.PersonAliasId
JOIN Person rp ON rp.Id = rpa.PersonId
JOIN RegistrationRegistrant rr ON rr.RegistrationId = r.Id
JOIN PersonAlias pa ON pa.Id = rr.PersonAliasId
JOIN Person p ON p.Id = pa.PersonId
WHERE r.RegistrationInstanceId = {{ instanceId }}
  AND rpa.PersonId IN (SELECT PersonId FROM GroupMember WHERE GroupId = {{ familyId }})
  AND EXISTS (
      SELECT 1 FROM RegistrationInstance
      WHERE Id = {{ instanceId }}
        AND IsActive = 1
        AND (StartDateTime IS NULL OR StartDateTime <= GETDATE())
        AND (EndDateTime IS NULL OR EndDateTime >= GETDATE())
  )
ORDER BY IsOwn DESC, r.CreatedDateTime DESC, p.LastName, p.NickName
{% endsql %}
{% assign own = data | Where:'IsOwn','1' %}
{% assign family = data | Where:'IsOwn','0' %}
{% if family.size > 0 %}
<div class="my-3"></div>
<div class="alert alert-warning">
    <div><strong>A family member has already registered.</strong></div>
    {% assign lastRegId = 0 %}
    {% for row in family %}
        {% if row.RegistrationId != lastRegId %}
            {% if lastRegId != 0 %}
                </ul>
            </div>
            {% endif %}
            <div class="mt-2">
                <div><strong>Registered by:</strong> {{ row.RegistrarNickName }} {{ row.RegistrarLastName }}</div>
                <div><strong>Registered:</strong></div>
                <ul class="mb-0">
            {% assign lastRegId = row.RegistrationId %}
        {% endif %}
        <li>{{ row.RegistrantNickName }} {{ row.RegistrantLastName }}</li>
        {% if forloop.last %}
                </ul>
            </div>
        {% endif %}
    {% endfor %}
</div>
{% endif %}
{% if own.size > 0 %}
{% assign reg = own | First %}
<div class="my-3"></div>
<div class="alert alert-info">
    <div>You are already registered for this registration.</div>
    <div class="mt-2">
        <a class="btn btn-primary" href="?RegistrationInstanceId={{ instanceId }}&RegistrationId={{ reg.RegistrationId }}">View/Update</a>
    </div>
    <div class="mt-2">
        {% if reg.ModifiedDateTime %}
            <strong>Last Updated:</strong> {{ reg.ModifiedDateTime }}
        {% else %}
            <strong>Created:</strong> {{ reg.CreatedDateTime }}
        {% endif %}
    </div>
    <div class="mt-2">
        <strong>Registered:</strong>
        <ul class="mb-0">
            {% for row in own %}
                <li>{{ row.RegistrantNickName }} {{ row.RegistrantLastName }}</li>
            {% endfor %}
        </ul>
    </div>
</div>
{% endif %}
{% endif %}
{% endif %}