<!-- Modal backup failed -->
<div class="modal fade" id="createBackupFailedModal" tabindex="-1" role="dialog">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title">Unable to create backup</h4>
      </div>
      <div class="modal-body">
        {{ createBackup.errorMessage }}
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-success" data-dismiss="modal">OK</button>
      </div>
    </div>
  </div>
</div>

<!-- modal backup config -->
<div class="modal fade" id="configureBackupModal" tabindex="-1" role="dialog">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title">Configure Backup Storage</h4>
      </div>
      <div class="modal-body">
        <p>Cloudron makes a complete backup of your system every day.</p>

        <form name="configureBackupForm" role="form" novalidate ng-submit="configureBackup.submit()" autocomplete="off">
          <fieldset>
            <p class="has-error text-center" ng-show="configureBackup.error">{{ configureBackup.error.generic }}</p>

            <div class="form-group">
              <label class="control-label" for="storageProviderProvider">Storage provider <sup><a ng-href="https://cloudron.io/documentation/backups/#storage-provider" class="help" target="_blank"><i class="fa fa-question-circle"></i></a></sup></label>
              <select class="form-control" id="storageProviderProvider" ng-model="configureBackup.provider" ng-options="a.value as a.name for a in storageProvider" ng-change=configureBackup.clearForm()></select>
            </div>

            <!-- Noop -->
            <div class="form-group" ng-show="configureBackup.provider === 'noop'">
              <p class="has-error">
                This option breaks the backup and restore functionality of Cloudron and should only be used for testing. Please make sure the server is completely backed up using alternate means.
              </p>
            </div>

            <!-- Filesystem -->
            <div class="form-group" ng-class="{ 'has-error': configureBackup.error.backupFolder }" ng-show="configureBackup.provider === 'filesystem'">
              <label class="control-label" for="inputConfigureBackupFolder">Local backup directory</label>
              <input type="text" class="form-control" ng-model="configureBackup.backupFolder" id="inputConfigureBackupFolder" name="backupFolder" ng-disabled="configureBackup.busy" placeholder="Directory for backups" ng-required="configureBackup.provider === 'filesystem'">

              <p class="checkbox" ng-show="configureBackup.provider === 'filesystem'">
                <label>
                  <input type="checkbox" ng-model="configureBackup.externalDisk" id="inputConfigureExternalDisk">Backup directory is an external EXT4 Disk</input>
                </label>
              </p>
            </div>

            <div class="checkbox" ng-show="configureBackup.provider === 'filesystem'">
              <label>
                <input type="checkbox" ng-model="configureBackup.useHardlinks" id="inputConfigureUseHardlinks">Use hardlinks</input>
              </label>
            </div>

            <!-- S3/Minio/SOS/GCS -->
            <div class="form-group" ng-class="{ 'has-error': configureBackup.error.endpoint }" ng-show="configureBackup.provider === 'minio' || configureBackup.provider === 's3-v4-compat'">
              <label class="control-label" for="inputConfigureBackupEndpoint">Endpoint</label>
              <input type="text" class="form-control" ng-model="configureBackup.endpoint" id="inputConfigureBackupEndpoint" name="endpoint" ng-disabled="configureBackup.busy" placeholder="URL of Minio/S3 Compatible" ng-required="configureBackup.provider === 'minio' || configureBackup.provider === 's3-v4-compat'">
            </div>

            <div class="checkbox" ng-show="configureBackup.provider === 'minio' || configureBackup.provider === 's3-v4-compat'" >
              <label>
                <input type="checkbox" ng-model="configureBackup.acceptSelfSignedCerts" id="inputConfigureBackupSelfSigned">Accept Self-signed certificate</input>
              </label>
            </div>

            <div class="form-group" ng-class="{ 'has-error': configureBackup.error.bucket }" ng-show="s3like(configureBackup.provider) || configureBackup.provider === 'gcs'">
              <label class="control-label" for="inputConfigureBackupBucket">Bucket name</label>
              <input type="text" class="form-control" ng-model="configureBackup.bucket" id="inputConfigureBackupBucket" name="bucket" ng-disabled="configureBackup.busy" ng-required="s3like(configureBackup.provider)">
            </div>

            <div class="form-group" ng-class="{ 'has-error': configureBackup.error.prefix }" ng-show="configureBackup.provider !== 'filesystem' && configureBackup.provider !== 'noop'">
              <label class="control-label" for="inputConfigureBackupPrefix">Prefix</label>
              <input type="text" class="form-control" ng-model="configureBackup.prefix" id="inputConfigureBackupPrefix" name="prefix" ng-disabled="configureBackup.busy" placeholder="Prefix for backup file names">
            </div>

            <div class="form-group" ng-class="{ 'has-error': configureBackup.error.region }" ng-show="configureBackup.provider === 's3'">
              <label class="control-label" for="inputConfigureBackupS3Region">Region</label>
              <select class="form-control" name="region" id="inputConfigureBackupS3Region" ng-model="configureBackup.region" ng-options="a.value as a.name for a in s3Regions" ng-disabled="configureBackup.busy" ng-required="configureBackup.provider === 's3'"></select>
            </div>

            <div class="form-group" ng-class="{ 'has-error': configureBackup.error.region }" ng-show="configureBackup.provider === 'digitalocean-spaces'">
              <label class="control-label" for="inputConfigureBackupDORegion">Region</label>
              <select class="form-control" name="region" id="inputConfigureBackupDORegion" ng-model="configureBackup.endpoint" ng-options="a.value as a.name for a in doSpacesRegions" ng-disabled="configureBackup.busy" ng-required="configureBackup.provider === 'digitalocean-spaces'"></select>
            </div>

            <div class="form-group" ng-class="{ 'has-error': configureBackup.error.region }" ng-show="configureBackup.provider === 'exoscale-sos'">
              <label class="control-label" for="inputConfigureBackupExoscaleRegion">Region</label>
              <select class="form-control" name="region" id="inputConfigureBackupExoscaleRegion" ng-model="configureBackup.endpoint" ng-options="a.value as a.name for a in exoscaleSosRegions" ng-disabled="configureBackup.busy" ng-required="configureBackup.provider === 'exoscale-sos'"></select>
            </div>

            <div class="form-group" ng-class="{ 'has-error': configureBackup.error.region }" ng-show="configureBackup.provider === 'wasabi'">
              <label class="control-label" for="inputConfigureBackupWasabiRegion">Region</label>
              <select class="form-control" name="region" id="inputConfigureBackupWasabiRegion" ng-model="configureBackup.endpoint" ng-options="a.value as a.name for a in wasabiRegions" ng-disabled="configureBackup.busy" ng-required="configureBackup.provider === 'wasabi'"></select>
            </div>

            <div class="form-group" ng-class="{ 'has-error': configureBackup.error.region }" ng-show="configureBackup.provider === 'scaleway-objectstorage'">
              <label class="control-label" for="inputConfigureBackupScalewayRegion">Region</label>
              <select class="form-control" name="region" id="inputConfigureBackupScalewayRegion" ng-model="configureBackup.endpoint" ng-options="a.value as a.name for a in scalewayRegions" ng-disabled="configureBackup.busy" ng-required="configureBackup.provider === 'scaleway-objectstorage'"></select>
            </div>

            <div class="form-group" ng-class="{ 'has-error': configureBackup.error.accessKeyId }" ng-show="s3like(configureBackup.provider)">
              <label class="control-label" for="inputConfigureBackupAccessKeyId">Access key id</label>
              <input type="text" class="form-control" ng-model="configureBackup.accessKeyId" id="inputConfigureBackupAccessKeyId" name="accessKeyId" ng-disabled="configureBackup.busy" ng-required="s3like(configureBackup.provider)">
            </div>

            <div class="form-group" ng-class="{ 'has-error': configureBackup.error.secretAccessKey }" ng-show="s3like(configureBackup.provider)">
              <label class="control-label" for="inputConfigureBackupSecretAccessKey">Secret access key</label>
              <input type="text" class="form-control" ng-model="configureBackup.secretAccessKey" id="inputConfigureBackupSecretAccessKey" name="secretAccessKey" ng-disabled="configureBackup.busy" ng-required="s3like(configureBackup.provider)">
            </div>

            <div class="form-group" ng-class="{ 'has-error': configureBackup.error.gcsKeyInput }" ng-show="configureBackup.provider === 'gcs'">
              <label class="control-label" for="gcsKeyInput">Service Account Key</label>

              <div class="input-group">
                <input type="file" id="gcsKeyFileInput" style="display:none"/>
                <input type="text" class="form-control" placeholder="Service Account Key" ng-model="configureBackup.gcsKey.keyFileName" id="gcsKeyInput" name="cert" onclick="getElementById('gcsKeyFileInput').click();" style="cursor: pointer;" ng-disabled="configureBackup.busy" ng-required="configureBackup.provider === 'gcs'">
                <span class="input-group-addon">
                  <i class="fa fa-upload" onclick="getElementById('gcsKeyFileInput').click();"></i>
                </span>
              </div>
            </div>

            <div class="form-group" ng-show="configureBackup.provider !== 'noop'">
              <label class="control-label" for="storageFormat">Storage Format  <sup><a ng-href="https://cloudron.io/documentation/backups/#backup-formats" class="help" target="_blank"><i class="fa fa-question-circle"></i></a></sup></label>
              <select class="form-control" id="storageFormat" ng-change="configureBackup.key = ''" ng-model="configureBackup.format" ng-options="a.value as a.name for a in formats"></select>
            </div>

            <div class="form-group" ng-show="configureBackup.provider !== 'noop'">
              <label class="control-label" for="storageInterval">Backup Interval <sup><a ng-href="https://cloudron.io/documentation/backups/#backup-interval" class="help" target="_blank"><i class="fa fa-question-circle"></i></a></sup></label>
              <select class="form-control" id="storageInterval" ng-model="configureBackup.intervalSecs" ng-options="a.value as a.name for a in intervalTimes"></select>
            </div>

            <div class="form-group" ng-show="configureBackup.provider !== 'noop'">
              <label class="control-label" for="storageRetention">Retention Time</label>
              <select class="form-control" id="storageRetention" ng-model="configureBackup.retentionSecs" ng-options="a.value as a.name for a in retentionTimes"></select>
            </div>

            <div class="form-group" ng-class="{ 'has-error': configureBackup.error.key }" ng-show="configureBackup.provider !== 'noop'">
              <label class="control-label" for="inputConfigureBackupKey">Encryption key (optional) <sup><a ng-href="https://cloudron.io/documentation/backups/#encrypting-backups" class="help" target="_blank"><i class="fa fa-question-circle"></i></a></sup></label>
              <p class="small text-info">Save this passphrase in a safe place. Backups cannot be decrypted without the passphrase</p>
              <input type="text" class="form-control" ng-model="configureBackup.key" id="inputConfigureBackupKey" name="prefix" ng-disabled="configureBackup.busy" placeholder="Passphrase used to encrypt the backups">
            </div>

            <input class="ng-hide" type="submit" ng-disabled="configureBackupForm.$invalid"/>
          </fieldset>
        </form>
      </div>
      <div class="modal-footer ">
        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
        <button type="submit" class="btn btn-outline btn-success pull-right" ng-click="configureBackup.submit()" ng-disabled="configureBackupForm.$invalid || configureBackup.busy"><i class="fa fa-circle-notch fa-spin" ng-show="configureBackup.busy"></i><span>Save</span></button>
      </div>
    </div>
  </div>
</div>

<div class="content">

  <div class="text-left">
    <h1>Backups</h1>
  </div>

  <div class="card" style="margin-bottom: 15px;">
    <div class="row">
      <div class="col-xs-6">
        <span class="text-muted">Provider</span>
      </div>
      <div class="col-xs-6 text-right">
        <span>{{ prettyProviderName(backupConfig.provider) }}</span>
      </div>
    </div>
    <div class="row" ng-show="config.uiSpec.backups.configurable">
      <div class="col-xs-6">
        <span class="text-muted">Location</span>
      </div>
      <div class="col-xs-6 text-right">
        <span ng-show="backupConfig.provider === 'filesystem'">{{ backupConfig.backupFolder }}</span>
        <span ng-show="backupConfig.provider !== 's3' && (s3like(backupConfig.provider) || backupConfig.provider === 'gcs')">{{ backupConfig.bucket + (backupConfig.prefix ? '/' : '') + backupConfig.prefix }}</span>
        <span ng-show="backupConfig.provider === 's3'">{{ backupConfig.region + '   ' + backupConfig.bucket + (backupConfig.prefix ? '/' : '') + backupConfig.prefix }}</span>
      </div>
    </div>

    <div class="row" ng-show="config.uiSpec.backups.configurable">
      <div class="col-xs-6">
        <span class="text-muted">Storage Format</span>
      </div>
      <div class="col-xs-6 text-right">
        <span>{{ backupConfig.format }}</span>
      </div>
    </div>

    <br/>

    <div class="row">
      <div class="col-xs-4">
        <span class="text-muted">Backup ID</span>
      </div>
      <div class="col-xs-8 text-right">
        <span ng-click-select ng-show="lastBackup">{{ lastBackup.id }}</span>
        <span ng-hide="lastBackup">No backups have been made yet</span>
      </div>
    </div>

    <div class="row">
      <div class="col-xs-6">
        <span class="text-muted">Last backup</span>
      </div>
      <div class="col-xs-6 text-right">
        <span uib-tooltip="{{ lastBackup.creationTime | prettyLongDate }}" ng-show="lastBackup">{{ lastBackup.creationTime | prettyDate }}</span>
        <span ng-hide="lastBackup">-</span>
      </div>
    </div>

    <div class="row">
      <br/>
      <div class="col-md-12" style="margin-bottom: 10px;">
        <div ng-show="createBackup.busy" class="progress progress-striped active animateMe">
          <div class="progress-bar progress-bar-success" role="progressbar" style="width: {{ createBackup.percent }}%"></div>
        </div>
      </div>
    </div>

    <div class="row">
      <div class="col-md-8">
        <p ng-show="createBackup.busy">{{ createBackup.message }}</p>
        <p ng-hide="createBackup.busy">
          <div class="has-error" ng-show="!createBackup.active">{{ createBackup.errorMessage }}</div>
        </p>
      </div>
      <div class="col-md-4 text-right">
        <button class="btn btn-outline btn-primary pull-right" ng-click="configureBackup.show()" ng-disabled="createBackup.busy" ng-show="config.uiSpec.backups.configurable">Configure</button>
        <button class="btn btn-outline btn-primary" ng-click="createBackup.startBackup()" ng-show="!createBackup.busy" style="margin-right: 10px">Backup now</button>
        <button class="btn btn-outline btn-danger" ng-click="createBackup.stopBackup()" ng-show="createBackup.busy" style="margin-right: 10px">Stop Backup</button>
      </div>
    </div>
  </div>

  <div class="text-left">
    <h3>Logs</h3>
  </div>

  <div class="card card-large" style="margin-bottom: 15px;">
    <div class="row">
      <div class="col-md-12">
        <p>
          Please be careful when uploading these logs to a public server since they may contain sensitive information.
        </p>
        <a class="btn btn-primary pull-right" ng-href="/logs.html?taskId={{createBackup.taskId}}" ng-disabled="!createBackup.taskId" target="_blank">Show Logs</a>
      </div>
    </div>
  </div>
</div>
