<div class="page-header">
  <h1>
    {{ "subscription" | i18n }}
    <small *ngIf="firstLoaded && loading">
      <i
        class="bwi bwi-spinner bwi-spin text-muted"
        title="{{ 'loading' | i18n }}"
        aria-hidden="true"
      ></i>
      <span class="sr-only">{{ "loading" | i18n }}</span>
    </small>
  </h1>
</div>
<ng-container *ngIf="!firstLoaded && loading">
  <i class="bwi bwi-spinner bwi-spin text-muted" title="{{ 'loading' | i18n }}"></i>
  <span class="sr-only">{{ "loading" | i18n }}</span>
</ng-container>

<app-org-subscription-hidden
  *ngIf="firstLoaded && !userOrg.canViewSubscription"
  [providerName]="userOrg.providerName"
></app-org-subscription-hidden>

<ng-container *ngIf="sub">
  <bit-callout
    type="warning"
    title="{{ 'canceled' | i18n }}"
    *ngIf="subscription && subscription.cancelled"
  >
    {{ "subscriptionCanceled" | i18n }}</bit-callout
  >
  <bit-callout
    type="warning"
    title="{{ 'pendingCancellation' | i18n }}"
    *ngIf="subscriptionMarkedForCancel"
  >
    <p>{{ "subscriptionPendingCanceled" | i18n }}</p>
    <button bitButton buttonType="secondary" [bitAction]="reinstate" type="button">
      {{ "reinstateSubscription" | i18n }}
    </button>
  </bit-callout>

  <div class="row">
    <div class="col-4">
      <dl>
        <dt>{{ "billingPlan" | i18n }}</dt>
        <dd>{{ sub.plan.name }}</dd>
        <ng-container *ngIf="subscription">
          <dt>{{ "status" | i18n }}</dt>
          <dd>
            <span class="text-capitalize">{{
              isSponsoredSubscription ? "sponsored" : subscription.status || "-"
            }}</span>
            <span bitBadge badgeType="warning" *ngIf="subscriptionMarkedForCancel">{{
              "pendingCancellation" | i18n
            }}</span>
          </dd>
          <dt [ngClass]="{ 'tw-text-danger': isExpired }">
            {{ "subscriptionExpiration" | i18n }}
          </dt>
          <dd [ngClass]="{ 'tw-text-danger': isExpired }">
            {{ nextInvoice ? (nextInvoice.date | date : "mediumDate") : "-" }}
          </dd>
        </ng-container>
      </dl>
    </div>
    <ng-container *ngIf="userOrg.canEditSubscription">
      <div class="col-8" *ngIf="subscription">
        <strong class="d-block mb-1">{{ "details" | i18n }}</strong>
        <table class="table">
          <tbody>
            <tr *ngFor="let i of subscription.items">
              <td>
                {{ i.name }} {{ i.quantity > 1 ? "&times;" + i.quantity : "" }} @
                {{ i.amount | currency : "$" }}
              </td>
              <td>{{ i.quantity * i.amount | currency : "$" }} /{{ i.interval | i18n }}</td>
            </tr>
          </tbody>
        </table>
      </div>
    </ng-container>
  </div>
  <ng-container *ngIf="userOrg.canEditSubscription">
    <button
      bitButton
      buttonType="secondary"
      type="button"
      (click)="changePlan()"
      *ngIf="showChangePlanButton"
    >
      {{ "changeBillingPlan" | i18n }}
    </button>
    <app-change-plan
      [organizationId]="organizationId"
      (onChanged)="closeChangePlan()"
      (onCanceled)="closeChangePlan()"
      *ngIf="showChangePlan"
    ></app-change-plan>
  </ng-container>

  <ng-container *ngIf="userOrg.canEditSubscription">
    <sm-enroll
      *ngIf="isAdmin"
      [enabled]="sub?.useSecretsManager"
      [organizationId]="organizationId"
    ></sm-enroll>
  </ng-container>

  <ng-container *ngIf="userOrg.canEditSubscription">
    <h2 class="spaced-header">{{ "manageSubscription" | i18n }}</h2>
    <p class="mb-4">{{ subscriptionDesc }}</p>
    <ng-container
      *ngIf="
        subscription && canAdjustSeats && !subscription.cancelled && !subscriptionMarkedForCancel
      "
    >
      <div class="mt-3">
        <app-adjust-subscription
          [seatPrice]="seatPrice"
          [organizationId]="organizationId"
          [interval]="billingInterval"
          [currentSeatCount]="seats"
          [maxAutoscaleSeats]="maxAutoscaleSeats"
          (onAdjusted)="subscriptionAdjusted()"
        >
        </app-adjust-subscription>
      </div>
    </ng-container>
    <button
      bitButton
      buttonType="danger"
      type="button"
      [bitAction]="removeSponsorship"
      *ngIf="isSponsoredSubscription"
    >
      {{ "removeSponsorship" | i18n }}
    </button>
    <h2 class="spaced-header">{{ "storage" | i18n }}</h2>
    <p>{{ "subscriptionStorage" | i18n : sub.maxStorageGb || 0 : sub.storageName || "0 MB" }}</p>
    <div class="progress">
      <div
        class="progress-bar bg-success"
        role="progressbar"
        [ngStyle]="{ width: storageProgressWidth + '%' }"
        [attr.aria-valuenow]="storagePercentage"
        aria-valuemin="0"
        aria-valuemax="100"
      >
        {{ storagePercentage / 100 | percent }}
      </div>
    </div>
    <ng-container *ngIf="subscription && !subscription.cancelled && !subscriptionMarkedForCancel">
      <div class="mt-3">
        <div class="d-flex" *ngIf="!showAdjustStorage">
          <button bitButton buttonType="secondary" type="button" (click)="adjustStorage(true)">
            {{ "addStorage" | i18n }}
          </button>
          <button
            bitButton
            buttonType="secondary"
            type="button"
            class="ml-1"
            (click)="adjustStorage(false)"
          >
            {{ "removeStorage" | i18n }}
          </button>
        </div>
        <app-adjust-storage
          [storageGbPrice]="storageGbPrice"
          [add]="adjustStorageAdd"
          [organizationId]="organizationId"
          [interval]="billingInterval"
          (onAdjusted)="closeStorage(true)"
          (onCanceled)="closeStorage(false)"
          *ngIf="showAdjustStorage"
        ></app-adjust-storage>
      </div>
    </ng-container>
  </ng-container>

  <h2 class="spaced-header">{{ "selfHostingTitle" | i18n }}</h2>
  <p class="mb-4">
    {{ "selfHostingEnterpriseOrganizationSectionCopy" | i18n }}
  </p>
  <div class="d-flex">
    <button
      bitButton
      buttonType="secondary"
      type="button"
      (click)="downloadLicense()"
      *ngIf="canDownloadLicense"
      [disabled]="showDownloadLicense"
    >
      {{ "downloadLicense" | i18n }}
    </button>
    <button
      bitButton
      buttonType="secondary"
      type="button"
      class="ml-1"
      (click)="manageBillingSync()"
      *ngIf="canManageBillingSync"
    >
      {{ (hasBillingSyncToken ? "manageBillingSync" : "setUpBillingSync") | i18n }}
    </button>
  </div>
  <div class="mt-3" *ngIf="showDownloadLicense">
    <app-download-license
      [organizationId]="organizationId"
      (onDownloaded)="closeDownloadLicense()"
      (onCanceled)="closeDownloadLicense()"
    ></app-download-license>
  </div>
  <ng-container *ngIf="userOrg.canEditSubscription">
    <h2 class="spaced-header">{{ "additionalOptions" | i18n }}</h2>
    <p class="mb-4">
      {{ "additionalOptionsDesc" | i18n }}
    </p>
    <div class="d-flex">
      <button
        bitButton
        buttonType="danger"
        [bitAction]="cancel"
        type="button"
        class="ml-1"
        *ngIf="subscription && !subscription.cancelled && !subscriptionMarkedForCancel"
      >
        {{ "cancelSubscription" | i18n }}
      </button>
    </div>
  </ng-container>
</ng-container>
