- @page_title = "#{Workarea.config.site_name} #{t('workarea.admin.dashboards.index.admin')}"

.view
  .view__header
    .view__heading
      = link_to "← #{t('workarea.admin.dashboards.index.storefront')}", storefront.root_path
      %h1 #{Workarea.config.site_name} #{t('workarea.admin.dashboards.index.admin')}

  .view__container
    %ul.secondary-nav
      %li= link_to t('workarea.admin.dashboards.index.site_planner'), releases_path
      %li= link_to t('workarea.admin.dashboards.index.store'), store_dashboards_path
      %li= link_to t('workarea.admin.dashboards.index.catalog'), catalog_dashboards_path
      %li= link_to t('workarea.admin.dashboards.index.orders'), orders_dashboards_path
      %li= link_to t('workarea.admin.dashboards.index.people'), people_dashboards_path
      %li= link_to t('workarea.admin.dashboards.index.search'), search_dashboards_path
      %li= link_to t('workarea.admin.dashboards.index.marketing'), marketing_dashboards_path
      %li= link_to t('workarea.admin.dashboards.index.reports'), reports_dashboards_path
      %li= link_to t('workarea.admin.dashboards.index.settings'), settings_dashboards_path

  .view__container
    .grid.grid--huge
      / Left Column
      .grid__cell.grid__cell--50
        .grid.grid--huge
          / 2x1 Yesterday vs Today
          .grid__cell.grid__cell--50
            .insight.insight--with-gutter
              .insight__date
                %span.insight__period= t('workarea.admin.dashboards.index.yesterday')

              .insight__body
                %p
                  %span.heading.heading--6.heading--no-margin= t('workarea.admin.dashboards.index.revenue')
                  = link_to sales_over_time_report_path(starts_at: 1.day.ago.to_s(:date_only), ends_at: 1.day.ago.to_s(:date_only), group_by: 'day'), class: 'heading heading--1 heading--no-margin' do
                    = number_to_currency @dashboard.yesterday.revenue
                %p
                  %span.heading.heading--6.heading--no-margin= t('workarea.admin.dashboards.index.orders')
                  = link_to sales_over_time_report_path(starts_at: 1.day.ago.to_s(:date_only), ends_at: 1.day.ago.to_s(:date_only), group_by: 'day'), class: 'heading heading--1 heading--no-margin' do
                    = number_with_delimiter @dashboard.yesterday.orders
                %p
                  %span.heading.heading--6.heading--no-margin= t('workarea.admin.dashboards.index.units_sold')
                  = link_to sales_over_time_report_path(starts_at: 1.day.ago.to_s(:date_only), ends_at: 1.day.ago.to_s(:date_only), group_by: 'day'), class: 'heading heading--1 heading--no-margin' do
                    = number_with_delimiter @dashboard.yesterday.units_sold
                %p
                  %span.heading.heading--6.heading--no-margin= t('workarea.admin.dashboards.index.customers')
                  = link_to sales_over_time_report_path(starts_at: 1.day.ago.to_s(:date_only), ends_at: 1.day.ago.to_s(:date_only), group_by: 'day'), class: 'heading heading--1 heading--no-margin' do
                    = number_with_delimiter @dashboard.yesterday.customers

          .grid__cell.grid__cell--50
            .insight.insight--with-gutter
              .insight__date
                %span.insight__period= t('workarea.admin.dashboards.index.today')

              .insight__body
                %p
                  %span.heading.heading--6.heading--no-margin= t('workarea.admin.dashboards.index.revenue')
                  = link_to sales_over_time_report_path(starts_at: Time.current.to_s(:date_only), ends_at: Time.current.to_s(:date_only), group_by: 'day'), class: 'heading heading--1 heading--no-margin' do
                    = number_to_currency @dashboard.today.revenue
                %p
                  %span.heading.heading--6.heading--no-margin= t('workarea.admin.dashboards.index.orders')
                  = link_to sales_over_time_report_path(starts_at: Time.current.to_s(:date_only), ends_at: Time.current.to_s(:date_only), group_by: 'day'), class: 'heading heading--1 heading--no-margin' do
                    = number_with_delimiter @dashboard.today.orders
                %p
                  %span.heading.heading--6.heading--no-margin= t('workarea.admin.dashboards.index.units_sold')
                  = link_to sales_over_time_report_path(starts_at: Time.current.to_s(:date_only), ends_at: Time.current.to_s(:date_only), group_by: 'day'), class: 'heading heading--1 heading--no-margin' do
                    = number_with_delimiter @dashboard.today.units_sold
                %p
                  %span.heading.heading--6.heading--no-margin= t('workarea.admin.dashboards.index.customers')
                  = link_to sales_over_time_report_path(starts_at: Time.current.to_s(:date_only), ends_at: Time.current.to_s(:date_only), group_by: 'day'), class: 'heading heading--1 heading--no-margin' do
                    = number_with_delimiter @dashboard.today.customers

        / Insight List
        - @dashboard.insights.take(5).each do |insight|
          = render "workarea/admin/insights/#{insight.slug}", insight: insight

        .section
          .align-center
            = link_to t('workarea.admin.dashboards.index.view_all_insights'), insights_report_path, class: 'button'

      / Right Column
      .grid__cell.grid__cell--50
        / 2x2 Graph Grid
        .insight
          .insight__date
            %span.insight__period
              - if params[:starts_at].blank? && params[:ends_at].blank?
                = t('workarea.admin.dashboards.index.past_week')
              - else
                = t('workarea.admin.date_selector.date_range', starts_at: @dashboard.starts_at.to_s(:date_only), ends_at: @dashboard.ends_at.to_s(:date_only))

          .browsing-controls.browsing-controls--center.browsing-controls--filters-displayed
            = form_tag request.path, method: 'get' do
              = render 'workarea/admin/shared/date_selector', starts_at: @dashboard.starts_at, ends_at: @dashboard.ends_at

          .insight__section
            .grid
              .grid__cell.grid__cell--50
                .align-center
                  %span.heading.heading--6.heading--no-margin= t('workarea.admin.dashboards.index.orders')
                  = link_to sales_over_time_report_path(starts_at: @dashboard.starts_at.to_s(:date_only), ends_at: @dashboard.ends_at.to_s(:date_only), group_by: 'day'), class: 'heading heading--1 heading--no-margin' do
                    = number_with_delimiter @dashboard.orders

                  - if @dashboard.orders_percent_change.present?
                    %p
                      = insights_trend_icon(@dashboard.orders_percent_change)
                      = t('workarea.admin.dashboards.index.from_previous', percentage: insights_number_to_percentage(@dashboard.orders_percent_change))

              .grid__cell.grid__cell--50
                .align-center
                  %span.heading.heading--6.heading--no-margin= t('workarea.admin.dashboards.index.revenue')
                  = link_to sales_over_time_report_path(starts_at: @dashboard.starts_at.to_s(:date_only), ends_at: @dashboard.ends_at.to_s(:date_only), group_by: 'day'), class: 'heading heading--1 heading--no-margin' do
                    = number_to_currency @dashboard.revenue

                  - if @dashboard.revenue_percent_change.present?
                    %p
                      = insights_trend_icon(@dashboard.revenue_percent_change)
                      = t('workarea.admin.dashboards.index.from_previous', percentage: insights_number_to_percentage(@dashboard.revenue_percent_change))

            .grid
              .grid__cell.grid__cell--50
                = area_chart @dashboard.orders_graph_data, Workarea.config.default_chartkick_options

              .grid__cell.grid__cell--50
                = area_chart @dashboard.revenue_graph_data, Workarea.config.default_chartkick_options.merge(prefix: Money.default_currency.symbol)

          .insight__section
            .grid
              .grid__cell.grid__cell--50
                .align-center
                  %span.heading.heading--6.heading--no-margin= t('workarea.admin.dashboards.index.customers')
                  = link_to sales_over_time_report_path(starts_at: @dashboard.starts_at.to_s(:date_only), ends_at: @dashboard.ends_at.to_s(:date_only), group_by: 'day'), class: 'heading heading--1 heading--no-margin' do
                    = number_with_delimiter @dashboard.customers

                  - if @dashboard.customers_percent_change.present?
                    %p
                      = insights_trend_icon(@dashboard.customers_percent_change)
                      = t('workarea.admin.dashboards.index.from_previous', percentage: insights_number_to_percentage(@dashboard.customers_percent_change))

              .grid__cell.grid__cell--50
                .align-center
                  %span.heading.heading--6.heading--no-margin= t('workarea.admin.dashboards.index.units_sold')
                  = link_to sales_over_time_report_path(starts_at: @dashboard.starts_at.to_s(:date_only), ends_at: @dashboard.ends_at.to_s(:date_only), group_by: 'day'), class: 'heading heading--1 heading--no-margin' do
                    = number_with_delimiter @dashboard.units_sold

                  - if @dashboard.units_sold_percent_change.present?
                    %p
                      = insights_trend_icon(@dashboard.units_sold_percent_change)
                      = t('workarea.admin.dashboards.index.from_previous', percentage: insights_number_to_percentage(@dashboard.units_sold_percent_change))

            .grid
              .grid__cell.grid__cell--50
                = area_chart @dashboard.customers_graph_data, Workarea.config.default_chartkick_options

              .grid__cell.grid__cell--50
                = area_chart @dashboard.units_sold_graph_data, Workarea.config.default_chartkick_options

        / Activity List
        .insight
          .insight__date
            %span.insight__period= t('workarea.admin.dashboards.index.latest')

          .insight__heading= t('workarea.admin.dashboards.index.activity')
          .insight__body
            %p.insight__note= t('workarea.admin.dashboards.index.latest_changes')

            - if @activity.entries.blank?
              %p= t('workarea.admin.dashboards.index.no_activity_as_of', time: local_time(Time.current)).html_safe
            - else
              - @activity.days.each_with_index do |(day, entries), index|
                .activity-group
                  .grid
                    .grid__cell.grid__cell--20
                      .date-marker
                        - if day == Date.today
                          .date-marker__annotation= t('workarea.admin.timeline.today')
                        .date-marker__month= local_time(day, format: '%b')
                        .date-marker__day= local_time(day, format: '%e')
                    .grid__cell.grid__cell--80
                      .activity-group__activities
                        - entries.take(15).each do |entry|
                          = render_activity_entry(entry)

        - unless @activity.entries.blank?
          .section
            .align-center
              = link_to t('workarea.admin.dashboards.index.view_all_activity'), activity_path, class: 'button'