<% statuses = RedmineQboCalendar::AppointmentStatus.all # How many appointments use each status: removing one in use asks where to move them in_use = CustomerAppointment.where(event_type: nil).group(:status).count status_row = lambda do |index, status| count = status ? in_use[status.key].to_i : 0 name = "settings[statuses][#{index}]" content_tag(:tr, class: 'appointment-status-row', data: { in_use: count }) do safe_join([ content_tag(:td, sprite_icon('reorder'), class: 'appointment-status-handle', title: l(:label_drag_to_reorder_status)), content_tag(:td, safe_join([ hidden_field_tag("#{name}[key]", status&.key, id: nil), text_field_tag("#{name}[name]", status&.name, id: nil, size: 24, class: 'appointment-status-name', 'aria-label': l(:field_name)) ])), content_tag(:td, color_field_tag("#{name}[color]", status&.color || '#1fc0ff', id: nil, class: 'appointment-status-color', 'aria-label': l(:field_color))), content_tag(:td, safe_join([ hidden_field_tag("#{name}[counts_hours]", '0', id: nil), check_box_tag("#{name}[counts_hours]", '1', status.nil? || status.counts_hours?, id: nil, 'aria-label': l(:label_status_counts_hours)) ]), class: 'appointment-status-hours'), content_tag(:td, content_tag(:span, status&.name || l(:label_new_status), class: 'status-badge appointment-status-preview', style: "--status-color: #{status&.color || '#1fc0ff'}")), content_tag(:td, count.positive? ? l(:label_status_in_use, count: count) : '', class: 'appointment-status-count'), content_tag(:td, link_to(sprite_icon('del', l(:button_delete)), '#', class: 'icon-only icon-del appointment-status-remove', title: l(:button_delete)), class: 'buttons') ]) end end %>
<%= l(:label_appointment_statuses) %>

<%= l(:text_appointment_statuses) %>

<% statuses.each_with_index do |status, index| %> <%= status_row.call(index, status) %> <% end %>
<%= l(:field_name) %> <%= l(:field_color) %> <%= l(:label_status_counts_hours) %> <%= l(:label_preview) %>

<%= link_to sprite_icon('add', l(:button_add_status)), '#', id: 'appointment-status-add', class: 'icon icon-add' %>

<%= l(:text_call_outs_not_statuses) %>

<%= javascript_tag do %> $(function () { var $body = $('#appointment-statuses'); $body.sortable({ handle: '.appointment-status-handle', axis: 'y' }); // The preview badge follows the name and colour $body.on('input', '.appointment-status-name, .appointment-status-color', function () { var $row = $(this).closest('tr'); var $preview = $row.find('.appointment-status-preview'); $preview.text($row.find('.appointment-status-name').val() || <%= raw l(:label_new_status).to_json %>); $preview[0].style.setProperty('--status-color', $row.find('.appointment-status-color').val()); }); $('#appointment-status-add').on('click', function (e) { e.preventDefault(); var html = $('#appointment-status-template').html().replace(/NEW_STATUS/g, 'new' + Date.now()); $body.append(html); $body.find('tr:last .appointment-status-name').focus(); }); // Removing a status its appointments use: its row becomes a choice of status to move them to (done when // the settings are saved), or keeping it (shown grey). Undo puts the row back. $body.on('click', '.appointment-status-remove', function (e) { e.preventDefault(); var $row = $(this).closest('tr'); var count = parseInt($row.data('in-use'), 10) || 0; var key = $row.find('input[name$="[key]"]').val(); if (!count || !key) { $row.remove(); return; } var $select = $('