/ TODO / Replace ApplicationName .home

.banners
  .row
    .col-md-10.col-md-offset-2
      %h1 ApplicationName Connector
      %p
        -if current_organization
          Link your company <strong>#{current_organization.name} (#{current_organization.uid})</strong> to ApplicationName to get your business in synch. Check the status of your connection on this screen.
        -else
          ApplicationName add-on is a microservice providing data synchronization between your platform and ApplicationName

.container
  - if current_user
    - unless is_admin
      .row
        .col-md-12.alert.alert-warning
          Only administrators can modify the application settings

    .row.link-step{class: "#{current_organization.oauth_uid ? 'done' : 'todo'}"}
      .col-md-1.text-center.link-step-number
        %span.badge.link-step-badge
          1
      .col-md-6.link-step-description
        %p
          - if current_organization.oauth_uid
            Your ApplicationName account <strong>#{current_organization.oauth_name} (#{current_organization.oauth_uid})</strong> is currently linked
          - else
            Your ApplicationName account is not linked
      .col-md-2.col-md-offset-3.text-center.link-step-action
        - if current_organization.oauth_uid
          = link_to "Disconnect", signout_omniauth_path(organization_id: current_organization.id), class: "btn btn-warning btn-lg #{is_admin ? '' : 'disabled'}"
        - else
          - if is_admin
            = link_to "Link to ApplicationName", "/auth/ApplicationName/request?org_uid=#{current_organization.uid}", class: 'btn btn-warning btn-lg'
            %br
            %small If you don’t have an account #{link_to 'create yours here', Maestrano::Connector::Rails::External.create_account_link(current_organization || nil)}

    .spacer1

    .row.link-step{class: "#{(current_organization.sync_enabled && current_organization.synchronized_entities.values.any?) ? 'done' : 'todo'}"}
      = form_tag home_update_path(id: current_organization.id), method: :put do
        .col-md-1.text-center.link-step-number
          %span.badge.link-step-badge 2
        .col-md-9.link-step-description
          %p You can customize which entities are synchronized by the connector:
          %p (#{image_tag "logos/to_connec.png", class: "small-image"} : from ApplicationName to Connec! and #{image_tag "logos/to_external.png", class: "small-image"} : from Connec! to ApplicationName)
          .spacer1
          .row
            .col-md-11.col-md-offset-1.center
              .row
                .col-md-1
                  =image_tag "logos/to_connec.png", class: "image"
                .col-md-1
                  =image_tag "logos/to_external.png", class: "image"
                .col-md-4
                  ApplicationName wording
                .col-md-3
                  Universal wording
          .spacer1
          .row
            .col-md-11.col-md-offset-1
              - current_organization.displayable_synchronized_entities.each do |k, v|
                .row.sync-entity
                  .col-md-1.link-step-action
                    #{check_box("#{k}", "to_connec", {checked: (v[:can_push_to_connec] || v[:can_push_to_external]) && !current_organization.pull_disabled, onclick: "return !#{k}_to_external.checked;", disabled: current_organization.pull_disabled})}
                  .col-md-1.link-step-action
                    #{check_box("#{k}", "to_external", {checked: v[:can_push_to_external] && !current_organization.push_disabled, onchange: "#{k}_to_connec.checked = #{!current_organization.pull_disabled};", disabled: current_organization.push_disabled})}
                  %label.col-md-7{:for => "#{k}", style: 'padding-top: 5px;'}
                    .col-md-6
                      #{v[:external_name]}
                    .col-md-6
                      #{v[:connec_name]}
                  -if is_admin
                    .col-md-3.text-right
                      - if v && current_organization.oauth_uid && current_organization.sync_enabled
                        = link_to 'Force a synchronization', home_synchronize_path(opts: {only_entities: [k.to_s]}), method: :post, class: 'btn btn-warning btn-sm', title: "Force a synchronization for #{v[:external_name]} only", 'data-toggle' => 'tooltip', 'data-placement' => 'right'

          .spacer2
          .row
            %h Choose whether to synchronize your historical data:
          .spacer1
          .row
            .col-md-4.col-md-offset-1
              %label{:for => 'historical-data'} Synchronize my historical data
            .col-md-1
              #myModal.modal.fade{:role => "dialog"}
                .modal-dialog
                  .modal-content
                    .modal-header
                      %button.close{"data-dismiss" => "modal", :type => "button"} ×
                      %h4.modal-title Warning!
                    .modal-body
                      %p
                        %b All data
                        created prior to the date you linked ApplicationName
                        %b will be synchronised both ways.
                      %p
                        It means that:
                        %br
                        \- all data from applications you already have linked to the platform will be sent to your ApplicationName account
                        %br
                        \- all exisiting data from ApplicationName will be sent to your other applications
                        %br
                      %p
                        If you have been manually copying records in multiple applications,
                        %b you risk seeing duplicates arising!
                      %p
                        %b This action cannot be undone at any time!
                    .modal-footer
                      %button.btn.btn-primary{id: 'confirm', :type => "button", onclick: "closeModal(confirm);"}  Confirm
                      %button.btn.btn-secondary{id: 'close', :type => "button", onclick: "closeModal(close);"} Close
              %input{type: 'checkbox', id: 'historical-data', name: 'historical-data', checked: current_organization.historical_data, onchange: 'historicalDataDisplay();', disabled: current_organization.historical_data}
            .col-md-6
              %small#historical-data-display-unchecked{style: "display: #{current_organization.historical_data ? 'none' : 'block'}"} Only data created after #{(current_organization.date_filtering_limit && current_organization.date_filtering_limit.utc || Time.now.utc).to_formatted_s(:long_ordinal)} will be synchronized
              %small#historical-data-display-checked{style: "display: #{!current_organization.historical_data ? 'none' : 'block'}"}
                Synchronizing your historical data will share all data in ApplicationName. This action is not reversible. Want to know more? Check #{link_to 'here', 'https://maestrano.atlassian.net/wiki/display/UKB/How+Connec%21+manages+Historical+Data+Sharing'}

        .spacer1
        .row
          .col-md-2.col-md-offset-10.text-center.link-step-action
            =submit_tag "#{current_organization.sync_enabled ? 'Update' : 'Start synchronizing!'}", class: "btn btn-lg btn-warning #{current_organization.oauth_uid ? '' : 'disabled'} text-sm"

    -if current_organization.oauth_uid && current_organization.sync_enabled
      .spacer2
      .row
        .col-md-4.col-md-offset-4.text-center
          = link_to 'Go to ApplicationName', home_redirect_to_external_path, class: 'btn btn-lg btn-primary'
    -else
      .row
        .col-md-4.col-md-offset-4.center
          = link_to 'Link your Maestrano account', Maestrano::Connector::Rails::Engine.routes.url_helpers.default_maestrano_auth_saml_index_path(tenant: :default), class: 'btn btn-warning'