<% helpdesk_service = RedmineUxToolkit::Services::HelpdeskMailFetch %>
<% settings = Setting.plugin_redmine_ux_toolkit || {} %>
<% interval = helpdesk_service.interval_minutes(settings) %>
<% interval_options = [
  [l(:label_ux_toolkit_helpdesk_mail_fetch_every_cron_run), 0],
  [l(:label_ux_toolkit_helpdesk_mail_fetch_every_30_minutes), 30],
  [l(:label_ux_toolkit_helpdesk_mail_fetch_every_60_minutes), 60],
  [l(:label_ux_toolkit_helpdesk_mail_fetch_every_120_minutes), 120],
  [l(:label_ux_toolkit_helpdesk_mail_fetch_every_4_hours), 240],
  [l(:label_ux_toolkit_helpdesk_mail_fetch_every_8_hours), 480]
] %>

<div class="ux-toolkit-settings-section">
  <p><strong><%= l(:label_ux_toolkit_helpdesk_mail_fetch) %></strong></p>
  <p class="info"><%= l(:text_ux_toolkit_helpdesk_mail_fetch_help) %></p>

  <p>
    <%= label_tag 'settings_helpdesk_mail_fetch_interval_minutes',
                  l(:label_ux_toolkit_helpdesk_mail_fetch_interval) %>
    <%= select_tag 'settings[helpdesk_mail_fetch_interval_minutes]',
                   options_for_select(interval_options, interval),
                   id: 'settings_helpdesk_mail_fetch_interval_minutes' %>
  </p>

  <% if helpdesk_service.table_available? %>
    <% helpdesk_state = helpdesk_service.state_record %>
    <% status_label =
         case helpdesk_state.last_status.to_s
         when 'running' then l(:label_ux_toolkit_cron_status_running)
         when 'success' then l(:label_ux_toolkit_cron_status_success)
         when 'failed' then l(:label_ux_toolkit_cron_status_failed)
         else helpdesk_state.last_status.presence || '-'
         end %>

    <table class="list">
      <tbody>
        <tr>
          <th><%= l(:label_ux_toolkit_helpdesk_mail_fetch_last_successful_at) %></th>
          <td><%= helpdesk_state.last_finished_at ? format_time(helpdesk_state.last_finished_at) : '-' %></td>
        </tr>
        <tr>
          <th><%= l(:label_ux_toolkit_cron_last_started_at) %></th>
          <td><%= helpdesk_state.last_started_at ? format_time(helpdesk_state.last_started_at) : '-' %></td>
        </tr>
        <tr>
          <th><%= l(:label_ux_toolkit_cron_status) %></th>
          <td><%= status_label %></td>
        </tr>
        <tr>
          <th><%= l(:label_ux_toolkit_cron_last_message) %></th>
          <td><%= helpdesk_state.last_message.presence || '-' %></td>
        </tr>
      </tbody>
    </table>
  <% else %>
    <p class="nodata"><%= l(:text_ux_toolkit_helpdesk_mail_fetch_status_unavailable) %></p>
  <% end %>
</div>
