<page-header title="'Create Custom template'" breadcrumbs="[{label:'Custom Templates', link:'docker.templates.custom'}, 'Create Custom template']"> </page-header>

<div class="row" ng-if="!$ctrl.state.loading">
  <div class="col-sm-12">
    <rd-widget>
      <rd-widget-body>
        <form class="form-horizontal" name="customTemplateForm">
          <custom-template-common-fields
            form-values="$ctrl.formValues"
            show-platform-field="true"
            show-type-field="true"
            name-regex="$ctrl.state.templateNameRegex"
            name-regex-error="'This field must consist of lower-case alphanumeric characters, \'_\' or \'-\' (e.g. \'my-name\', or \'abc-123\').'"
          ></custom-template-common-fields>

          <!-- build-method -->
          <div ng-if="!$ctrl.state.fromStack">
            <div class="col-sm-12 form-section-title"> Build method </div>

            <box-selector
              slim="true"
              options="$ctrl.buildMethods"
              value="$ctrl.state.Method"
              on-change="($ctrl.onChangeMethod)"
              radio-name="'buildMethod'"
              slim="true"
            ></box-selector>
          </div>
          <!-- !build-method -->
          <!-- web-editor -->
          <web-editor-form
            ng-if="$ctrl.state.Method === 'editor'"
            identifier="custom-template-creation-editor"
            value="$ctrl.formValues.FileContent"
            on-change="($ctrl.editorUpdate)"
            ng-required="true"
            yml="true"
            placeholder="Define or paste the content of your docker compose file here"
          >
            <editor-description>
              <p>
                You can get more information about Compose file format in the
                <a href="https://docs.docker.com/compose/compose-file/" target="_blank"> official documentation </a>
                .
              </p>
            </editor-description>
          </web-editor-form>
          <!-- !web-editor -->
          <!-- upload -->
          <div ng-show="$ctrl.state.Method === 'upload'">
            <div class="col-sm-12 form-section-title"> Upload </div>
            <div class="form-group">
              <span class="col-sm-12 text-muted small"> You can upload a Compose file from your computer. </span>
            </div>
            <div class="form-group">
              <div class="col-sm-12">
                <button type="button" class="btn btn-sm btn-primary" ngf-select ng-model="$ctrl.formValues.File"> Select file </button>
                <span class="space-left">
                  {{ $ctrl.formValues.File.name }}
                </span>
              </div>
            </div>
          </div>
          <!-- !upload -->
          <!-- repository -->
          <git-form
            ng-if="$ctrl.state.Method === 'repository'"
            value="$ctrl.formValues"
            on-change="($ctrl.handleChange)"
            is-docker-standalone="$ctrl.isDockerStandalone"
          ></git-form>

          <div class="form-group" ng-if="!$ctrl.state.isTemplateValid">
            <div class="col-sm-12">
              <div class="small text-warning">
                <pr-icon icon="'alert-triangle'" class-name="'space-right'"></pr-icon>
                Template is invalid.
              </div>
            </div>
          </div>

          <custom-templates-variables-definition-field
            ng-if="$ctrl.isTemplateVariablesEnabled"
            value="$ctrl.formValues.Variables"
            on-change="($ctrl.onVariablesChange)"
            is-variables-names-from-parent="$ctrl.state.Method === 'editor'"
          ></custom-templates-variables-definition-field>

          <!-- !repository -->
          <por-access-control-form form-data="$ctrl.formValues.AccessControlData"></por-access-control-form>

          <!-- actions -->
          <div class="col-sm-12 form-section-title"> Actions </div>
          <div class="form-group">
            <div class="col-sm-12">
              <button
                type="button"
                class="btn btn-primary btn-sm !ml-0"
                ng-disabled="$ctrl.state.actionInProgress || customTemplateForm.$invalid
              || ($ctrl.state.Method === 'editor' && !$ctrl.formValues.FileContent)
              || ($ctrl.state.Method === 'upload' && !$ctrl.formValues.File)
              || ($ctrl.state.Method === 'repository' && ((!$ctrl.formValues.RepositoryURL || !$ctrl.formValues.ComposeFilePathInRepository) || ($ctrl.formValues.RepositoryAuthentication && (!$ctrl.formValues.RepositoryUsername || !$ctrl.formValues.RepositoryPassword))))
              || !$ctrl.formValues.Title
              || !$ctrl.state.isTemplateValid"
                ng-click="$ctrl.createCustomTemplate()"
                button-spinner="$ctrl.state.actionInProgress"
              >
                <span ng-hide="$ctrl.state.actionInProgress">Create custom template</span>
                <span ng-show="$ctrl.state.actionInProgress">Creation in progress...</span>
              </button>
              <span class="text-danger space-left" ng-if="$ctrl.state.formValidationError">
                {{ $ctrl.state.formValidationError }}
              </span>
            </div>
          </div>
          <!-- !actions -->
        </form>
      </rd-widget-body>
    </rd-widget>
  </div>
</div>
