{% comment %}
Check-In Locations
Renders the check-in locations for a group as a set of "well" cards.
Each card shows the campus, the named location (linked for users with Edit rights),
and a label per active schedule with a tooltip summarizing its open/close/next-start times.
Expects the Rock-provided Group merge object.

Deployment: uploaded to ~/Content/Lava/checkInLocations.lava and pulled into a Group Type's
Group View Lava template (Admin Tools > General Settings > Group Types > [type] > Display Options)
via an include tag for that path, placed between the opening .row div and its first child div.
Add to each group type that needs it (e.g. event and volunteer serving team groups).
{% endcomment %}
<style>
    .label{
        margin: 0 3px;
    }
    a.label:hover, a.label:focus {
        color: black !important;
    }
    hr{
        margin: 3px 0;
    }
    .label i{
        margin-right: 3px;
    }
</style>
<div class="col-md-12">
<dl>
    <dt style="font-weight:600;margin-bottom:0;">Check-In Locations</dt>
    {% assign locations = Group.GroupLocations %}
    {% assign locationsSize = locations | Size %}
    {% if locationsSize > 0 %}
        <div style="display: flex;flex-direction: row;flex-wrap: wrap;gap: 5px;">
        {% for groupLocation in locations %}
            {% assign locationId = groupLocation.Location.Id %}
            {% assign locationName = groupLocation.Location.Name %}
            {% assign campusId = groupLocation.Location.CampusId %}
            {% assign canEditLocation = locationId | HasRightsTo:'Edit','Rock.Model.Location' %}
            {% assign activeSchedules = groupLocation.Schedules | Where:'IsActive','true','equal' %}
            {% assign scheduleSize = activeSchedules | Size %}
            {% capture campus %}
                {% if campusId %}
                    {% campus id:'{{ campusId }}' %}
                        {{ campus.Name }}
                    {% endcampus %}
                {% endif %}
            {% endcapture %}
        <div style="margin-bottom:5px;line-height:1.75;flex:1 0 30%;" class="well">
            <strong>{{ campus }}</strong>
            {% if scheduleSize > 0 %}
                <span class="label label-success">Check-In Ready</span>
            {% endif %}<br>
            {% if canEditLocation %}
                <a href="/admin/checkin/named-locations?LocationId={{ locationId }}" target="_blank"
            {% else %}
                <span
            {% endif %}
             class="label label-campus"><i class="far fa-map-marker-check"></i> {{ locationName }}
             {% if canEditLocation %}
                </a>
             {% else %}
                </span>
             {% endif %}
                {% if scheduleSize > 0 %}
                    {% for schedule in groupLocation.Schedules %}
                        {% assign scheduleIsActive = schedule.IsActive %}
                        {% assign isCheckInActive = schedule.IsCheckInActive %}
                        {% assign canEditSchedule = schedule.Id | HasRightsTo:'Edit','Rock.Model.Schedule' %}
                        {% if scheduleIsActive %}
                                {% assign openOffset = schedule.CheckInStartOffsetMinutes | Times: -1 %}
                                {% assign openTime = schedule.NextStartDateTime | DateAdd: openOffset,'m' | Date: 'yyyy-MM-ddTHH:mm:ss.fffzzz' %}
                                {% assign closeOffset = schedule.CheckInEndOffsetMinutes %}
                                {% assign closeTime = schedule.NextStartDateTime | DateAdd: closeOffset,'m' | Date: 'yyyy-MM-ddTHH:mm:ss.fffzzz' %}
                                {% assign endTime = schedule.NextStartDateTime | DateAdd: schedule.DurationInMinutes, 'm' | Date: 'yyyy-MM-ddTHH:mm:ss.fffzzz' %}
                                {% assign endOffset = schedule.NextStartDateTime | DateDiff: endTime, 'm' %}

                                {% comment %} Find out if check in closes at the offset time or when the schedule closes. {% endcomment %}
                                {% if endOffset < closeOffset %}
                                    {% assign checkInCloseOffset = endOffset %}
                                    {% assign checkInClose = endTime %}
                                {% elseif closeOffset < endOffset %}
                                    {% assign checkInCloseOffset = closeOffset %}
                                    {% assign checkInClose = closeTime %}
                                {% else %}
                                    {% assign checkInCloseOffset = closeOffset %}
                                    {% assign checkInClose = closeTime %}
                                {% endif %}

                                {% capture nextUpSummary %}
                                    <strong>Next Start:</strong> {{ schedule.NextStartDateTime | HumanizeDateTime }} {{ schedule.NextStartDateTime | Date:'dddd, MMMM d, yyyy h:mmtt' }}
                                {% endcapture %}
                                {% capture opensSummary %}
                                    <strong>Opens:</strong> {{ schedule.CheckInStartOffsetMinutes }} Minutes Before Start at {{ openTime | Date:'h:mmtt' }}
                                {% endcapture %}
                                {% capture closesSummary %}
                                    <strong>Closes:</strong> {{ checkInCloseOffset }} Minutes After Start at {{ checkInClose | Date:'h:mmtt' }}
                                {% endcapture %}
                                {% capture friendlySummary %}
                                    <strong>Occurs</strong> {{ schedule.FriendlyScheduleText }}
                                {% endcapture %}

                                {% if canEditSchedule %}
                                    <a href="/page/308?ScheduleId={{ schedule.Id }}" target="_blank"
                                {% else %}
                                    <span
                                {% endif %}
                                {% if isCheckInActive %}
                                    class="label label-type required-indicator" title="<strong>Check-In Currently Open</strong><br><hr>{{ closesSummary }}<br><hr>{{ friendlySummary }}"
                                {% else %}
                                    class="label label-type" title="{{ nextUpSummary }}<br><hr>{{ opensSummary }}<br><hr>{{ closesSummary }}<br><hr>{{ friendlySummary }}"
                                {% endif %}
                                    data-toggle="tooltip" data-placement="bottom" data-html="true"><i class="fal fa-calendar-check"></i> {{ schedule }}
                                {% if canEditSchedule %}
                                    </a>
                                {% else %}
                                    </span>
                                {% endif %}
                        {% endif %}
                    {% endfor %}
                {% endif %}
                {% if scheduleSize == 0 %}
                    <span class="label label-danger">Schedule Missing or Inactive</span>
                {% endif %}
        </div>
        {% endfor %}
        </div>
    {% else %}
    <span class="label label-danger">Location and Schedule Missing</span><br>
    {% endif %}
</dl>
</div>
