<%= page("data-action": "turbo:before-cache@window->#{stimulus_id}#closeMenus") do %>
  <%= page_header do %>
    <%= page_header_back(solidus_admin.orders_path) %>
    <%= page_header_title(t('.title', number: @order.number)) %>
    <%= page_header_actions do %>
      <%= render component("ui/button").new(tag: :button, scheme: :secondary, text: t(".discard"), form: form_id) %>
      <%= render component("ui/button").new(tag: :button, text: t(".save"), form: form_id) %>
    <% end %>
  <% end %>

  <%= page_with_sidebar do %>
    <%= page_with_sidebar_main do %>
      <%= render component("orders/cart").new(order: @order) %>
      <%= render component("orders/show/summary").new(order: @order) %>
    <% end %>

    <%= page_with_sidebar_aside do %>
      <%= render component('ui/panel').new do |panel| %>
        <% panel.with_menu t(".edit_email"), solidus_admin.order_customer_path(@order), data: { turbo_frame: :edit_order_email_modal } %>
        <% panel.with_menu t(".edit_shipping"), solidus_admin.edit_order_ship_address_path(@order), data: { turbo_frame: :edit_order_ship_address_modal } %>
        <% panel.with_menu t(".edit_billing"), solidus_admin.edit_order_bill_address_path(@order), data: { turbo_frame: :edit_order_bill_address_modal } %>
        <% panel.with_menu t(".remove_customer"), solidus_admin.order_customer_path(@order), method: :delete, class: "text-red-500" if @order.user %>

        <% panel.with_section(class: 'flex flex-col gap-6') do %>
          <div class="flex flex-col gap-2">
            <span class="font-semibold text-sm"><%= t(".customer") %></span>
            <% if @order.user %>
              <div class="font-normal text-sm"><%= customer_name(@order.user) || tag.span(t('.no_name'), class: "text-gray-500") %></div>
              <div class="font-normal text-sm body-link"><%= link_to @order.user.email, spree.admin_user_path(@order.user) %></div>
              <div class="font-normal text-sm text-gray-500"><%= t(".orders_count", count: @order.user.orders.complete.count) %></div>
            <% else %>
              <%= render component('orders/show/customer_search').new(order: @order) %>
            <% end %>
          </div>

          <div class="flex flex-col gap-2">
            <span class="font-semibold text-sm"><%= t('.order_email') %></span>
            <div class="font-normal text-sm">
              <% if @order.email? %>
                <%= @order.email %>
              <% else %>
                <%= link_to(t(".add_email"), solidus_admin.order_customer_path(@order), class: "body-link") %>
              <% end %>
            </div>
          </div>

          <div class="flex flex-col gap-2">
            <span class="font-semibold text-sm"><%= @order.class.human_attribute_name(:ship_address) %></span>
            <div class="font-normal text-sm">
              <% if @order.ship_address %>
                <%= format_address @order.ship_address %>
              <% else %>
                <%= link_to t(".add_shipping"), solidus_admin.edit_order_ship_address_path(@order), class: 'body-link' %>
              <% end %>
            </div>
          </div>

          <div class="flex flex-col gap-2">
            <span class="font-semibold text-sm"><%= @order.class.human_attribute_name(:bill_address) %></span>
            <div class="font-normal text-sm">
              <% if @order.bill_address.blank? %>
                <%= link_to t(".add_billing"), solidus_admin.edit_order_bill_address_path(@order), class: 'body-link' %>
              <% elsif @order.bill_address == @order.ship_address %>
                <span class="text-gray-500"><%= t('.same_as_shipping') %></span>
              <% else %>
                <%= format_address @order.bill_address %>
              <% end %>
            </div>
          </div>

        <% end %>
      <% end %>

    <% end %>
  <% end %>

  <% turbo_frames.each do |frame| %>
    <%= turbo_frame_tag frame %>
  <% end %>
<% end %>
