module Royalties
  class AccountingPeriodsDetailsPage < RoyaltiesPage
    page_url "/accounting-period/<%=params[:period_id]%>"
    elements(:run_details_links, css: "a[href$='/summary']")

    div(:page_title, class: "PageTitle")
    span(:breadcrumb_active, class: "Breadcrumb-active")

    button(:close_period, text: "Close Period")
    div(:eligible_sales, class: "EligibleSales")
    div(:manage_runs_table, class: "AccountingRuns")
    react_select(:staging_url) do
      div_element(id: "addSalesTableName").div_element(class: "Select__control")
    end
    text_field(:file_display_name, id: "fileDisplayName")

    div(:exchange_rates_div, class: %w[UploadArea UploadExchangeRates-uploadcsv])
    file_field(:exchange_rates_upload_field) { exchange_rates_div_element.file_field_element }
    def upload_exchange_rates(file)
      upload_workaround(element: exchange_rates_upload_field_element, path: file)
    end
    div(:upload_exchange_rates_div, class: "UploadExchangeRates")

    svg(:exchange_rates_checkmark) do
      span_element(class: "UploadExchangeRates-text").svg_element(class: %w[Glyph Checkmark])
    end

    svg(:sales_delivered_checkmark) do
      span_element(class: "salesDeliveredText").svg_element(class: %w[Glyph Checkmark])
    end

    div(:add_sales_form, class: "AddSalesForm")
    button(:add_sales_file, id: "addSalesBtn")
    button(:deliver_sales, id: "deliverSalesBtn")
    button(:run_mech_deductions, id: "runMechDeductionBtn")
    button(:confirm_deliver_sales, text: "Confirm")
    span(:create_accounting_run, text: "Create")
    span(:skip_accounting_run, text: "Skip")
    span(:undo_skip_accounting_run, text: "Undo")
    span(:invalidate_accounting_run, text: "Invalidate")
    span(:commit_accounting_run, text: "Commit")
    button(:confirm_commit_accounting_run, text: "COMMIT")

    div(:sales_file_dropdown, id: "stagingUrl")

    def select_sales_file(file_name)
      sales_file_dropdown_element.click
      sales_file_dropdown_element.text_field_element.send_keys file_name
      div(css: "div[class*='is-focused']", visible_text: file_name).click
    end
  end
end
