%# app/views/operacoes_dashboard/planilha.html.erb %>
<%# Página da Planilha da Operação: tabela espelho (uma linha por NF) com busca
e paginação. Chega-se aqui pelo botão no Dashboard de Operações, que passa
o contexto (operação/modo global/período/cross-filters) na URL. %>
<% content_for :head do %>
<% end %>
<% ini_iso = @periodo_inicio.strftime('%Y-%m-%d') %>
<% fim_iso = @periodo_fim.strftime('%Y-%m-%d') %>
<%# ── Cabeçalho ─────────────────────────────────────────── %>
📋 Planilha da Operação
<% if @modo == 'global' %>
🌐 Global · <%= @periodo_inicio.strftime('%d/%m/%Y') %> – <%= @periodo_fim.strftime('%d/%m/%Y') %>
<% elsif @metricas&.data_inicio %>
<%= @metricas.operacoes_label %> · <%= @metricas.data_inicio.strftime('%d/%m/%Y') %> – <%= @metricas.data_fim.strftime('%d/%m/%Y') %>
<% elsif @operacao %>
<%= Operacao.label(@operacao) %>
<% end %>
<%= link_to '← Dashboard de Operações',
operacoes_dashboard_path({ modo: @modo, operacao: @operacao, inicio: ini_iso, fim: fim_iso }.compact),
data: { turbo: false },
class: 'px-4 py-2.5 rounded-xl text-sm whitespace-nowrap bg-[#1a1a1a] text-gray-300 border border-white/10 hover:border-orange-500' %>
<%# ── Seleção de escopo: operação única ou global ───────── %>
<% pills = { 'operacao' => '🏥 Operação', 'global' => '🌐 Global' } %>
<% pills.each do |m, label| %>
<% ativo = @modo == m %>
<%= link_to label,
operacoes_planilha_path({ modo: m, operacao: @operacao, inicio: ini_iso, fim: fim_iso, q: @busca.presence }.compact),
data: { turbo: false },
class: "px-4 py-2 rounded-xl text-sm font-semibold whitespace-nowrap #{ativo ? 'bg-orange-500 text-black' : 'bg-[#1a1a1a] text-gray-300 border border-white/10 hover:border-orange-500'}" %>
<% end %>
<% if @modo == 'operacao' %>
<%# Planilha Entregas preenchida (modelo entregue ao cliente) %>
<% if @operacao %>
<%= link_to '⬇️ Baixar Excel preenchido', operacoes_planilha_baixar_path(operacao: @operacao),
data: { turbo: false },
title: 'Planilha Entregas da operação com status/ocorrências preenchidos pelo rastreio',
class: 'px-4 py-2.5 bg-orange-500 hover:bg-orange-600 text-black font-bold rounded-xl text-sm whitespace-nowrap' %>
<% end %>
<% end %>
<%# ── Filtros ativos herdados do dashboard (chips removíveis) ── %>
<% if @chips.any? %>
Filtros ativos:
<% @chips.each do |chip| %>
<%= chip[:rotulo] %>: <%= chip[:display] || chip[:valor] %>
<%= link_to '✕', operacoes_planilha_path(request.query_parameters.except(chip[:param], 'pg')),
data: { turbo: false }, class: 'hover:text-white font-bold', title: 'Remover filtro' %>
<% end %>
<%= link_to 'Limpar tudo',
operacoes_planilha_path(request.query_parameters.except('pg', *@chips.map { |c| c[:param] })),
data: { turbo: false }, class: 'text-xs text-gray-400 hover:text-orange-400 underline' %>
<% end %>
<%# ── Tabela espelho ─────────────────────────────────────── %>
<% if @metricas %>
<%= render 'espelho' %>
<% else %>
Nenhuma operação encontrada no banco (tabelas gade_entregas_*).
<% end %>
<%# Troca de operação recarrega a planilha %>