<% settings = Setting.plugin_redmine_ux_toolkit || {} %>
<% reminder_service = RedmineUxToolkit::Services::IssueReminderEmails %>
<% reminders_enabled = reminder_service.enabled?(settings) %>
<% send_time = reminder_service.send_time(settings) %>
<% reminder_time_zone = reminder_service.time_zone(settings) %>
<% weekdays = reminder_service.weekday_keys(settings) %>
<% days_before_due = reminder_service.days_before_due(settings) %>
<% weekday_options = [
  [l(:label_ux_toolkit_weekday_monday), '1'],
  [l(:label_ux_toolkit_weekday_tuesday), '2'],
  [l(:label_ux_toolkit_weekday_wednesday), '3'],
  [l(:label_ux_toolkit_weekday_thursday), '4'],
  [l(:label_ux_toolkit_weekday_friday), '5'],
  [l(:label_ux_toolkit_weekday_saturday), '6'],
  [l(:label_ux_toolkit_weekday_sunday), '7']
] %>

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

  <p>
    <%= hidden_field_tag 'settings[enable_issue_reminder_emails]', '0', id: nil %>
    <%= check_box_tag 'settings[enable_issue_reminder_emails]',
                      '1',
                      reminders_enabled,
                      id: 'settings_enable_issue_reminder_emails' %>
    <%= label_tag 'settings_enable_issue_reminder_emails',
                  l(:label_ux_toolkit_enable_issue_reminder_emails) %>
  </p>

  <p>
    <%= label_tag 'settings_issue_reminder_send_time',
                  l(:label_ux_toolkit_issue_reminder_send_time) %>
    <%= time_field_tag 'settings[issue_reminder_send_time]',
                       send_time,
                       id: 'settings_issue_reminder_send_time',
                       size: 6 %>
    <em class="info"><%= l(:text_ux_toolkit_issue_reminder_send_time_help) %></em>
  </p>

  <p>
    <%= label_tag 'settings_issue_reminder_time_zone',
                  l(:label_ux_toolkit_issue_reminder_time_zone) %>
    <%= select_tag 'settings[issue_reminder_time_zone]',
                   options_for_select(ActiveSupport::TimeZone.all.map { |zone| [zone.to_s, zone.name] }, reminder_time_zone.name),
                   id: 'settings_issue_reminder_time_zone' %>
    <em class="info"><%= l(:text_ux_toolkit_issue_reminder_time_zone_help) %></em>
  </p>

  <p>
    <label><%= l(:label_ux_toolkit_issue_reminder_weekdays) %></label>
    <%= hidden_field_tag 'settings[issue_reminder_weekdays][]', '', id: nil %>
    <% weekday_options.each do |label, value| %>
      <label class="block">
        <%= check_box_tag 'settings[issue_reminder_weekdays][]',
                          value,
                          weekdays.include?(value),
                          id: "settings_issue_reminder_weekday_#{value}" %>
        <%= label %>
      </label>
    <% end %>
  </p>

  <p>
    <%= label_tag 'settings_issue_reminder_days_before_due',
                  l(:label_ux_toolkit_issue_reminder_days_before_due) %>
    <%= number_field_tag 'settings[issue_reminder_days_before_due]',
                         days_before_due,
                         min: 1,
                         step: 1,
                         id: 'settings_issue_reminder_days_before_due',
                         size: 6 %>
    <em class="info"><%= l(:text_ux_toolkit_issue_reminder_days_before_due_help) %></em>
  </p>
</div>

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

  <% if reminder_service.table_available? %>
    <% reminder_state = UxToolkitIssueReminderState.default_record %>
    <% status_label =
         case reminder_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 reminder_state.last_status.presence || '-'
         end %>
    <% configured_weekdays = weekday_options.select { |_label, value| weekdays.include?(value) }.map(&:first).join(', ') %>

    <table class="list">
      <tbody>
        <tr>
          <th><%= l(:label_ux_toolkit_scheduled_reminders) %></th>
          <td><%= reminders_enabled ? l(:label_ux_toolkit_enabled) : l(:label_ux_toolkit_disabled) %></td>
        </tr>
        <tr>
          <th><%= l(:label_ux_toolkit_issue_reminder_send_time) %></th>
          <td><%= send_time %> (<%= reminder_time_zone.name %>)</td>
        </tr>
        <tr>
          <th><%= l(:label_ux_toolkit_issue_reminder_weekdays) %></th>
          <td><%= configured_weekdays.presence || '-' %></td>
        </tr>
        <tr>
          <th><%= l(:label_ux_toolkit_issue_reminder_days_before_due) %></th>
          <td><%= days_before_due %></td>
        </tr>
        <tr>
          <th><%= l(:label_ux_toolkit_issue_reminder_last_successful_at) %></th>
          <td><%= reminder_state.last_successful_at ? format_time(reminder_state.last_successful_at) : '-' %></td>
        </tr>
        <tr>
          <th><%= l(:label_ux_toolkit_issue_reminder_last_attempt_at) %></th>
          <td><%= reminder_state.last_attempt_at ? format_time(reminder_state.last_attempt_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><%= reminder_state.locked_until ? format_time(reminder_state.locked_until) : '-' %></td>
        </tr>
        <tr>
          <th><%= l(:label_ux_toolkit_cron_last_message) %></th>
          <td><%= reminder_state.last_message.presence || '-' %></td>
        </tr>
      </tbody>
    </table>

    <p class="info"><%= l(:text_ux_toolkit_issue_reminder_send_now_help) %></p>
    <p>
      <%= link_to l(:button_ux_toolkit_send_issue_reminders_now),
                  send_ux_toolkit_issue_reminders_path,
                  method: :post,
                  data: (reminder_service.sent_today?(reminder_state) ? {confirm: l(:text_ux_toolkit_confirm_send_issue_reminders_again)} : {}),
                  class: 'button icon icon-email' %>
    </p>
  <% else %>
    <p class="nodata"><%= l(:text_ux_toolkit_issue_reminder_status_unavailable) %></p>
  <% end %>
</div>
