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

  <p>
    <%= l(:label_ux_toolkit_cron_command) %>:
    <code>*/10 * * * * cd /path/to/redmine &amp;&amp; bundle exec rake redmine_ux_toolkit:run RAILS_ENV=production</code>
  </p>

  <% if ActiveRecord::Base.connection.data_source_exists?('ux_toolkit_cron_states') %>
    <p>
      <%= link_to l(:button_ux_toolkit_run_cron_now),
                  run_ux_toolkit_cron_path,
                  method: :post,
                  class: 'button icon icon-time' %>
    </p>

    <% cron_state = UxToolkitCronState.find_by(name: UxToolkitCronState::DEFAULT_NAME) %>

    <% if cron_state %>
      <% status_label =
           case cron_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 cron_state.last_status.presence || '-'
           end %>

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