'Handle HTML representation
@param r: the S3Request
@param attr: controller arguments'

def _html(self, r, **attr):

    incident_id = r.id
    T = current.T
    auth = current.auth
    db = current.db
    s3db = current.s3db
    ptable = s3db.cms_post
    gtable = s3db.gis_location
    itable = s3db.event_incident
    ertable = s3db.event_team
    eptable = s3db.event_post
    ttable = s3db.cms_tag
    ittable = s3db.event_tag
    date_represent = (lambda dt: S3DateTime.date_represent(dt, format='%b    %d    %Y    %H:%M', utc=True))
    output = {'incident_id': incident_id}
    record = r.record
    output['name'] = record.name
    output['modified_on'] = date_represent(record.modified_on)
    output['start_date'] = date_represent(record.date)
    end_date = record.end_date
    if end_date:
        output['active'] = False
        output['end_date'] = date_represent(end_date)
    else:
        output['active'] = True
        output['end_date'] = ''
    incident_type = record.incident_type_id
    if incident_type:
        output['incident_type'] = itable.incident_type_id.represent(incident_type)
    else:
        output['incident_type'] = None
    output['description'] = record.comments
    location = db((gtable.id == record.location_id)).select(gtable.L1, gtable.L3, gtable.addr_street, gtable.addr_postcode, gtable.lat, gtable.lon, limitby=(0, 1)).first()
    if location:
        output['L1'] = (location.L1 or '')
        output['L3'] = (location.L3 or '')
        output['addr_street'] = (location.addr_street or '')
        output['postcode'] = (location.addr_postcode or '')
        output['lat'] = (location.lat or '')
        output['lon'] = (location.lon or '')
        bbox = current.gis.get_bounds(features=[location])
        output['lat_max'] = bbox['lat_max']
        output['lat_min'] = bbox['lat_min']
        output['lon_max'] = bbox['lon_max']
        output['lon_min'] = bbox['lon_min']
    else:
        output['L1'] = ''
        output['L3'] = ''
        output['addr_street'] = ''
        output['postcode'] = ''
        output['lat'] = ''
        output['lon'] = ''
        output['lat_max'] = ''
        output['lat_min'] = ''
        output['lon_max'] = ''
        output['lon_min'] = ''
    updateable = auth.s3_has_permission('update', itable, record_id=incident_id, c='event', f='incident')
    output['updateable'] = updateable
    tag_list = UL(_class='left    inline-list', _id='incident-tags')
    query = (((ittable.incident_id == incident_id) & (ittable.deleted == False)) & (ittable.tag_id == ttable.id))
    tags = db(query).select(ttable.name)
    tags = [t.name for t in tags]
    for tag in tags:
        tag_list.append(LI(A(tag, _href='#')))
    output['incident_tags'] = tag_list
    if updateable:
        script = ('incident_tags(%s)' % incident_id)
    else:
        script = 'incident_tags(false)'
    current.response.s3.jquery_ready.append(script)
    user = auth.user
    if user:
        user_id = user.id
        ltable = s3db.event_bookmark
        query = ((ltable.incident_id == incident_id) & (ltable.user_id == user_id))
        exists = db(query).select(ltable.id, limitby=(0, 1)).first()
        if exists:
            bookmark = A(ICON('bookmark'), _title=T('Remove    Bookmark'), _class='item    bookmark')
        else:
            bookmark = A(ICON('bookmark-empty'), _title=T('Add    Bookmark'), _class='item    bookmark')
        bookmark['_data-c'] = 'event'
        bookmark['_data-f'] = 'incident'
        bookmark['_data-i'] = incident_id
    else:
        bookmark = ''
    output['bookmark_btn'] = bookmark
    event_id = record.event_id
    if event_id:
        event = Storage()
        etable = s3db.event_event
        erecord = db((etable.id == event_id)).select(etable.name, etable.exercise, etable.start_date, etable.end_date, limitby=(0, 1)).first()
        event.name = erecord.name
        event.start_date = date_represent(erecord.start_date)
        end_date = erecord.end_date
        if end_date:
            event.active = False
            event.end_date = date_represent(end_date)
        else:
            event.active = True
            event.end_date = 'n/a'
        eltable = s3db.event_event_location
        query = ((eltable.event_id == event_id) & (eltable.deleted == False))
        event_location = db(query).select(eltable.location_id, limitby=(0, 1)).first()
        if event_location:
            event.location = eltable.location_id.represent(event_location.location_id)
        else:
            event.location = ''
        query = ((itable.event_id == event_id) & (itable.deleted == False))
        incidents = db(query).count()
        event.incidents = A(('%s    %s' % (incidents, T('Incidents'))), _href=URL(c='event', f='event', args='incident.popup', vars={'view': 1}), _class='s3_modal', _title=T('Incidents'))
        query = ((ertable.event_id == event_id) & (ertable.deleted == False))
        resources = db(query).count()
        event.resources = A(('%s    %s' % (resources, T('Resources'))), _href=URL(c='event', f='event', args='team.popup', vars={'view': 1}), _class='s3_modal', _title=T('Resources'))
        query = ((eptable.event_id == event_id) & (eptable.deleted == False))
        updates = db(query).count()
        event.updates = A(('%s    %s' % (updates, T('Updates'))), _href=URL(c='event', f='event', args='post.popup', vars={'view': 1}), _class='s3_modal', _title=T('Updates'))
        event.url = URL(c='event', f='event', args=[event_id, 'custom'])
        if erecord.exercise:
            event.status = T('Testing')
        elif (not erecord.end_date):
            event.status = T('Open')
        else:
            event.status = T('Closed')
        output['event'] = event
    else:
        output['event'] = None
    datatable = self._datatable
    dt_init = ["$('.dataTables_filter    label,.dataTables_length,.dataTables_info').hide();"]
    tablename = 'event_team'
    customise = current.deployment_settings.customise_resource(tablename)
    if customise:
        customise(r, tablename)
    datatable(output=output, tablename=tablename, updateable=updateable, incident_id=incident_id, dt_init=dt_init)
    tablename = 'project_task'
    customise = current.deployment_settings.customise_resource(tablename)
    if customise:
        customise(r, tablename)
    datatable(output=output, tablename=tablename, updateable=updateable, incident_id=incident_id, dt_init=dt_init)
    tablename = 'event_human_resource'
    customise = current.deployment_settings.customise_resource(tablename)
    if customise:
        customise(r, tablename)
    datatable(output=output, tablename=tablename, updateable=updateable, incident_id=incident_id, dt_init=dt_init)
    tablename = 'event_organisation'
    customise = current.deployment_settings.customise_resource(tablename)
    if customise:
        customise(r, tablename)
    datatable(output=output, tablename=tablename, updateable=updateable, incident_id=incident_id, dt_init=dt_init)
    event_id = None
    self._updates_html(r, output, event_id, incident_id, updateable, **attr)
    self._view(output, 'incident_profile.html')
    return output
