{{!
  Copyright (c) HashiCorp, Inc.
  SPDX-License-Identifier: MPL-2.0
~}}

{{page-title "Manage templates"}}
<section class="section">
    <header class="run-job-header">
        <h1 class="title is-3">Manage Job Templates</h1>
        <p>Modify or Delete a job template from the list below. Default job templates cannot be removed.</p>
    </header>
    {{#if (eq this.model.length 0)}}
        <h3 data-test-empty-templates-list-headline class="empty-message-headline">
          You have no templates to choose from. Would you like to <Hds::Link::Inline @route="jobs.run.templates.new" data-test-create-inline>create</Hds::Link::Inline> one?
        </h3>
        <Hds::Button class="button-group" @text="Back to editor" @route="jobs.run" @icon="arrow-left" data-test-cancel />
    {{else}}
    <main class="radio-group" data-test-template-list>
        <Hds::Table @model={{this.templates}} @columns={{this.columns}} @isFixedLayout={{true}}>
            <:body as |B|>
                <B.Tr>
                    <B.Td>
                        {{#if B.data.isDefaultJobTemplate}}
                            {{format-template-label B.data.path}}
                        {{else}}
													<LinkTo @route="jobs.run.templates.template" @model={{concat B.data.path "@" B.data.namespace}} data-test-edit-template={{B.data.path}}>
														{{format-template-label B.data.path}}
													</LinkTo>
                        {{/if}}
                    </B.Td>
                    <B.Td>{{B.data.namespace}}</B.Td>
                    <B.Td>
											{{B.data.items.description}}
										</B.Td>
                    <B.Td>
                        {{#if B.data.isDefaultJobTemplate}}
													<em>(Default Job — cannot be deleted)</em>
                        {{else}}
													<TwoStepButton
														data-test-delete
														@idleText="Delete"
														@cancelText="Cancel"
														@confirmText="Yes"
														@inlineText={{true}}
														@confirmationMessage="Are you sure?"
														@awaitingConfirmation={{this.deleteTemplate.isRunning}}
														@disabled={{cannot "destroy variable" namespace="*"}}
														@onConfirm={{perform this.deleteTemplate B.data}}
													/>
                        {{/if}}
                    </B.Td>
                </B.Tr>
            </:body>
        </Hds::Table>
    </main>
    <footer class="buttonset">
        <Hds::ButtonSet class="button-group">
            <Hds::Button @text="Cancel" @color="secondary" @route="jobs.run.templates" data-test-done />
        </Hds::ButtonSet>
    </footer>
    {{/if}}
</section>
