'Create the HTML for the Events section'

@staticmethod
def _events_html():

    T = current.T
    db = current.db
    s3db = current.s3db
    tablename = 'event_event'
    resource = s3db.resource(tablename)
    resource.add_filter((FS('~.closed') == False))
    list_fields = ['id', 'name', 'closed', 'start_date', 'end_date', 'exercise', 'comments', 'event_location.location_id$path', 'event_location.location_id$L3', 'event_location.location_id$addr_street', 'event_location.location_id$addr_postcode']
    rows = resource.select(fields=list_fields, start=None, limit=3, orderby='date    desc', represent=True, raw_data=True).rows
    if (len(rows) == 0):
        events = ''
    else:
        events = DIV(H3('Events    ', TAG['small'](A(T('See    all    events'), _href=URL(c='event', f='event', args='browse'))), _class='well-title'), _class='row    well')
        itable = s3db.event_incident
        iquery = (itable.deleted == False)
        rtable = s3db.event_team
        rquery = (rtable.deleted == False)
        gttable = s3db.gis_location_tag
        auth = current.auth
        if auth.is_logged_in():
            logged_in = True
            user_id = auth.user.id
            etable = s3db.event_event
            has_permission = auth.s3_has_permission
            btable = s3db.event_bookmark
            bquery = (((btable.user_id == user_id) & btable.event_id.belongs([row['event_event.id'] for row in rows])) & (btable.deleted == False))
            bookmarks = db(bquery).select(btable.event_id)
            bookmarks = [b.event_id for b in bookmarks]
        else:
            logged_in = False
        for row in rows:
            record_id = row['event_event.id']
            incidents = db((iquery & (itable.event_id == record_id))).count()
            resources = db((rquery & (rtable.event_id == record_id))).count()
            meta = ('%s    Incidents    %s    Resources' % (incidents, resources))
            if row._row['event_event.exercise']:
                status = 'Testing'
            elif row._row['event_event.end_date']:
                status = 'Complete'
            else:
                status = 'Active'
            start_dt = row._row['event_event.start_date']
            start_date = start_dt.date().strftime('%b    %d,    %Y')
            start_time = start_dt.time().strftime('%H:%M')
            end_dt = row._row['event_event.end_date']
            if end_dt:
                end_date = start_dt.date().strftime('%b    %d,    %Y')
                end_time = start_dt.time().strftime('%H:%M')
                end_date = TAG[''](SPAN('Closed    at:', _class='date-label'), SPAN(('%s    @    %s' % (end_date, end_time)), _class='date'))
            else:
                end_date = ''
            if logged_in:
                if (record_id in bookmarks):
                    bookmark = LI(ICON('bookmark'), _title=T('Remove    Bookmark'), _class='item    bookmark')
                else:
                    bookmark = LI(ICON('bookmark-empty'), _title=T('Add    Bookmark'), _class='item    bookmark')
                bookmark['_data-c'] = 'event'
                bookmark['_data-f'] = 'event'
                bookmark['_data-i'] = record_id
                if has_permission('update', etable, record_id):
                    edit = LI(A(ICON('pencil'), _href=URL(c='event', f='event', args=[('%s.popup' % record_id), 'update']), _title=T('Edit    Event'), _class='s3_modal'), _class='item    edit')
                else:
                    edit = ''
            else:
                bookmark = ''
                edit = ''
            path = row._row['gis_location.path']
            if path:
                L1 = path.split('/')[1]
                query = ((gttable.location_id == L1) & (gttable.tag == 'state'))
                L1_abrv = db(query).select(gttable.value, limitby=(0, 1)).first().value
                L3 = row['gis_location.L3']
                addr_street = row['gis_location.addr_street']
                addr_postcode = row['gis_location.addr_postcode']
            else:
                L1_abrv = ''
                L3 = ''
                addr_street = ''
                addr_postcode = ''
            events.append(DIV(TAG['aside'](TAG['header'](UL(LI(status, _class='item    primary    status'), _class='status-bar-left'), UL(bookmark, edit, _class='controls'), _class='status-bar    highlight'), DIV(H1(row['event_event.name'], _class='title'), DIV(DIV(SPAN(addr_street, _class='street-address'), SPAN(L3, _class='locality'), SPAN(L1_abrv, _class='region'), SPAN(addr_postcode, _class='postal-code'), _class='adr'), SPAN('Zero    hour:', _class='date-label'), SPAN(('%s    @    %s' % (start_date, start_time)), _class='date'), BR(), end_date, _class='event-date-location'), P(meta, _class='meta'), DIV(row['event_event.comments'], _class='desc'), _class='body'), TAG['footer'](P(A(T('Read    More'), _href=URL(c='event', f='event', args=[record_id, 'custom']), _class='more')), _class='footer'), _class='card-event'), _class='medium-4    columns'))
        while (len(events) < 4):
            events.append(DIV(_class='medium-6    large-3    columns'))
    return events
