<%# Engine monitor recording: summary and charts. Locals: flight_log, recording, error %>

<%= t(".engine_data") %>

<% if flight_log.engine_data.attached? %>
<%= link_to flight_log.engine_data.filename, rails_blob_path(flight_log.engine_data, disposition: :attachment), class: "link" %> <% if manage_engine_data? %><%= delete_button t(".remove"), engine_data_aircraft_flight_log_path(@aircraft, flight_log), confirm: t(".remove_the_engine_data_file") %><% end %>
<% end %>
<% if recording %> <% hours_line = ->(key, places) { (a = recording.first(key)) && (b = recording.last(key)) ? "#{number_with_precision(a, precision: places)} → #{number_with_precision(b, precision: places)} (#{number_with_precision(b - a, precision: places)})" : "—" } %> <% egt, cht = recording.hottest(:egt), recording.hottest(:cht) %> <% tail = recording.metadata[:aircraft_id] %> <% if tail.present? && tail.upcase != @aircraft.tail_number.to_s.upcase %>

<%= t(".this_recording_is_from_not_html", tail: tail, tail_number: @aircraft.tail_number) %>

<% end %>
<%= t(".recorded") %>
<%= t(".monitor_time_html", start: (recording.local(recording.started_at).strftime("%Y-%m-%d %H:%M")), end: (recording.local(recording.ended_at).strftime("%H:%M"))) %> <%= t(".samples_html", file_format: recording.format, flight_number: (" flight ##{recording.metadata[:flight_number]}" if recording.metadata[:flight_number]), samples: (number_with_delimiter(recording.samples))) %>
<%= t(".engine_run") %>
<%= recording.engine_start ? run_duration(recording.run_time) : t(".engine_not_run") %>
<%= t(".fuel_burned") %>
<%= engine_value(recording.fuel_burned, "gal", precision: 1) %>
<%= t(".tach") %>
<%= hours_line.(:tach, 2) %>
<%= t(".engine_hrs") %>
<%= hours_line.(:engine_hours, 2) %>
<% if (flt = recording.last(:flight_hours)) %>
<%= t(".airborne") %>
<%= flt.zero? ? t(".no_ground_run") : "#{number_with_precision(flt, precision: 2)} hrs" %>
<% end %>
<% peaks = [ [ "RPM", recording.max(:rpm), "rpm" ], [ t(".manifold_pressure"), recording.max(:map), "inHg", 1 ], [ (egt ? t(".egt_cyl", cylinder: (egt[:cylinder])) : "EGT"), egt && [ egt[:value], egt[:at] ], "°F" ], [ (cht ? t(".cht_cyl", cylinder: (cht[:cylinder])) : "CHT"), cht && [ cht[:value], cht[:at] ], "°F" ], [ "TIT", recording.max(:tit), "°F" ], [ t(".oil_temp"), recording.max(:oil_t), "°F" ], [ t(".oil_pressure"), recording.max(:oil_p), "psi" ], [ t(".fuel_flow"), recording.max(:fuel_flow), "gph", 1 ], [ t(".volts_low"), recording.min(:volts), "V", 1 ] ].select { _2 } %> <% peaks.each do |label, (value, at), unit, places| %> <% end %>
<%= t(".peak_engine_running") %><%= t(".value") %><%= t(".at") %>
<%= label %><%= engine_value(value, unit, precision: places || 0) %> <%= recording.local(at).strftime("%H:%M:%S") %>

<%= t(".click_a_chart_to_explore") %>

<% engine_chart_groups(recording).each_with_index do |group, i| %> <% chart = engine_chart(recording, *group[:keys], ytitle: group[:unit], colors: group[:colors].many? ? group[:colors] : nil) %> <% next unless chart %>
<%= group[:title] %>
<%= chart %>
<% end %>
" class="m-auto h-dvh max-h-none w-screen max-w-none bg-surface p-0 text-slate-800 shadow-2xl backdrop:bg-black/60 sm:h-[min(52rem,94dvh)] sm:w-[min(80rem,96vw)] sm:rounded-lg" data-action="keydown->engine-chart#keydown close->engine-chart#closed">
<%= t(".drag_to_zoom") %>

<%= t(".loading") %>

<%= t(".series") %> <%= t(".visible_range") %><%= t(".min") %><%= t(".avg") %><%= t(".max") %>
<% elsif error %>

<%= error %>

<% end %> <% if manage_engine_data? && !flight_log.engine_data.attached? %> <%= form_with model: flight_log, url: engine_data_aircraft_flight_log_path(@aircraft, flight_log), method: :patch, class: "card-body flex flex-wrap items-end gap-3" do |f| %> <%= f.field :engine_data, as: :file, label: t(".attach_engine_monitor_data"), hint: t(".cgr_30p_cgr_30c_flight"), accept: ".csv,text/csv" %> <%= f.submit t(".upload"), class: "btn-secondary" %> <% end %> <% elsif !flight_log.engine_data.attached? %> <%= empty_state t(".no_engine_data_for_this") %> <% end %>