<% issue = local_assigns[:issue] || @issue %>
<% checklist_service = RedmineUxToolkit::Services::Checklists %>
<% if checklist_service.view_allowed?(issue, User.current) %>
  <% checklist_items = UxToolkitChecklistItem.where(issue_id: issue.id).includes(:done_by).ordered %>
  <% can_edit_checklist = checklist_service.edit_allowed?(issue, User.current) %>
  <% completed_count = checklist_items.count(&:is_done?) %>

  <div class="box ux-toolkit-checklist" id="ux-toolkit-checklist">
    <div class="ux-toolkit-checklist-header">
      <h3><%= l(:label_ux_toolkit_checklist) %></h3>
      <% if checklist_items.any? %>
        <span class="ux-toolkit-checklist-progress">
          <progress value="<%= completed_count %>" max="<%= checklist_items.size %>"><%= completed_count %>/<%= checklist_items.size %></progress>
          <span><%= completed_count %>/<%= checklist_items.size %></span>
        </span>
      <% end %>
    </div>

    <% if checklist_service.closing_blocked_hint?(issue) %>
      <p class="ux-toolkit-checklist-close-hint">
        <%= l(:text_ux_toolkit_issue_close_blocked_by_open_checklist_items_hint) %>
      </p>
    <% end %>

    <% if checklist_items.any? %>
      <ul class="ux-toolkit-checklist-items">
        <% checklist_items.each do |item| %>
          <li class="ux-toolkit-checklist-item <%= 'is-done' if item.is_done? %>">
            <% if can_edit_checklist %>
              <%= link_to '',
                          toggle_ux_toolkit_issue_checklist_item_path(issue, item),
                          method: :post,
                          class: "ux-toolkit-checklist-toggle #{'is-checked' if item.is_done?}",
                          title: item.is_done? ? l(:label_ux_toolkit_reopen_checklist_item) : l(:label_ux_toolkit_mark_checklist_item_done),
                          aria: {label: item.is_done? ? l(:label_ux_toolkit_reopen_checklist_item) : l(:label_ux_toolkit_mark_checklist_item_done)} %>
            <% else %>
              <span class="ux-toolkit-checklist-toggle <%= 'is-checked' if item.is_done? %>" aria-hidden="true"></span>
            <% end %>

            <span class="ux-toolkit-checklist-subject">
              <% if item.is_done? %>
                <span><%= item.subject %></span>
                <% if item.done_by_user || item.done_at_time %>
                  <span class="ux-toolkit-checklist-done-meta">
                    <%= l(:label_ux_toolkit_checklist_item_done_by, user: item.done_by_user ? item.done_by_user.name : '-', time: item.done_at_time ? format_time(item.done_at_time) : '-') %>
                  </span>
                <% end %>
              <% else %>
                <%= item.subject %>
              <% end %>
            </span>

            <% if can_edit_checklist && !item.is_done? %>
              <span class="ux-toolkit-checklist-actions">
                <details class="ux-toolkit-checklist-edit">
                  <summary class="ux-toolkit-checklist-action"><%= l(:label_ux_toolkit_edit_checklist_item) %></summary>
                  <%= form_for item,
                               as: :ux_toolkit_checklist_item,
                               url: ux_toolkit_issue_checklist_item_path(issue, item),
                               method: :patch,
                               html: {class: 'ux-toolkit-checklist-edit-form'} do |f| %>
                    <%= f.text_field :subject, maxlength: 255 %>
                    <%= f.submit l(:button_save), class: 'button-small' %>
                    <%= button_tag l(:button_cancel),
                                   type: 'button',
                                   class: 'button-small',
                                   data: {ux_toolkit_checklist_edit_cancel: '1'} %>
                  <% end %>
                </details>

                <%= link_to l(:label_ux_toolkit_delete_checklist_item),
                            ux_toolkit_issue_checklist_item_path(issue, item),
                            method: :delete,
                            data: {confirm: l(:text_ux_toolkit_confirm_delete_checklist_item)},
                            class: 'ux-toolkit-checklist-action ux-toolkit-checklist-action-delete' %>
              </span>
            <% end %>
          </li>
        <% end %>
      </ul>
    <% else %>
      <p class="nodata"><%= l(:text_ux_toolkit_no_checklist_items) %></p>
    <% end %>

    <% if can_edit_checklist %>
      <details class="ux-toolkit-checklist-controls">
        <summary class="ux-toolkit-checklist-controls-toggle">
          <span class="ux-toolkit-checklist-controls-icon ux-toolkit-checklist-controls-icon-show" aria-hidden="true">+</span>
          <span class="ux-toolkit-checklist-controls-icon ux-toolkit-checklist-controls-icon-hide" aria-hidden="true">-</span>
          <span class="ux-toolkit-checklist-controls-show-label"><%= l(:label_ux_toolkit_show_checklist_controls) %></span>
          <span class="ux-toolkit-checklist-controls-hide-label"><%= l(:label_ux_toolkit_hide_checklist_controls) %></span>
        </summary>

        <div class="ux-toolkit-checklist-controls-body">
          <% available_templates = UxToolkitChecklistTemplate.active.with_items.ordered.includes(:items) %>
          <% if available_templates.any? %>
            <%= form_tag insert_ux_toolkit_issue_checklist_template_path(issue),
                         method: :post,
                         class: 'ux-toolkit-checklist-template-insert' do %>
              <label for="ux_toolkit_checklist_template_id_<%= issue.id %>">
                <%= l(:label_ux_toolkit_insert_checklist_template) %>
              </label>
              <%= select_tag :template_id,
                             options_for_select(available_templates.map { |template|
                               [template.name, template.id, {'data-template-items' => template.items.sort_by(&:position).map(&:subject).to_json}]
                             }),
                             id: "ux_toolkit_checklist_template_id_#{issue.id}" %>
              <%= submit_tag l(:button_ux_toolkit_insert_checklist_template), class: 'button-small' %>
              <div class="ux-toolkit-template-preview" data-ux-toolkit-checklist-template-preview>
                <strong><%= l(:label_ux_toolkit_template_preview) %></strong>
                <ol></ol>
                <em class="info"><%= l(:text_ux_toolkit_checklist_template_appends_items) %></em>
              </div>
            <% end %>
          <% else %>
            <p class="nodata ux-toolkit-checklist-no-templates">
              <%= l(:text_ux_toolkit_no_active_checklist_templates) %>
            </p>
          <% end %>

          <%= form_for UxToolkitChecklistItem.new,
                       as: :ux_toolkit_checklist_item,
                       url: ux_toolkit_issue_checklist_items_path(issue),
                       html: {class: 'ux-toolkit-checklist-new-item'} do |f| %>
            <div class="ux-toolkit-checklist-add-row">
              <%= f.text_field :subject,
                               maxlength: 255,
                               placeholder: l(:label_ux_toolkit_checklist_item) %>
              <%= f.submit l(:label_ux_toolkit_add_checklist_item), class: 'button-small ux-toolkit-checklist-add-button' %>
            </div>
          <% end %>
        </div>
      </details>
    <% end %>
  </div>
<% end %>
