{{template "base/head" .}}
<div role="main" aria-label="{{.Title}}" class="page-content organization">
	{{template "org/header" .}}
	<div class="ui container">

		{{if .NewMasterKey}}
			<div class="ui info message">
				<div class="header">{{ctx.Locale.Tr "repo.licenses.master_key_created"}}</div>
				<p>{{ctx.Locale.Tr "repo.licenses.master_key_created_copy"}}</p>
				<div class="ui action input tw-w-full tw-mt-2">
					<input class="js-new-master-key" type="text" readonly value="{{.NewMasterKey}}" onclick="this.select()">
					<button class="ui button" data-clipboard-target=".js-new-master-key" data-tooltip-content="{{ctx.Locale.Tr "copy_url"}}">{{svg "octicon-copy" 14}}</button>
				</div>
			</div>
		{{end}}

		{{if .NewKeyCreated}}
			<div class="ui success message">
				<div class="header">{{ctx.Locale.Tr "repo.licenses.key_created"}}</div>
				<p>{{ctx.Locale.Tr "repo.licenses.key_created_copy"}}</p>
				<div class="ui action input tw-w-full tw-mt-2">
					<input class="js-new-license-key" type="text" readonly value="{{.NewKeyCreated}}" onclick="this.select()">
					<button class="ui button" data-clipboard-target=".js-new-license-key" data-tooltip-content="{{ctx.Locale.Tr "copy_url"}}">{{svg "octicon-copy" 14}}</button>
				</div>
			</div>
		{{end}}

		<details id="new-package-details">
		<h4 class="ui top attached header tw-flex tw-items-center tw-justify-between">
			<span>{{svg "octicon-key" 16}} {{ctx.Locale.Tr "repo.licenses.packages"}}</span>
			{{if .IsRepoAdmin}}
			<summary class="ui primary small button">{{svg "octicon-plus" 14}} {{ctx.Locale.Tr "repo.licenses.new_package"}}</summary>
			{{end}}
		</h4>
		<div class="ui attached segment">
			{{if .IsRepoAdmin}}
			<div class="tw-mb-4">
						<form class="ui form" method="post" action="{{$.Org.HomeLink}}/-/licenses/packages">
							{{.CsrfTokenHtml}}
							<div class="two fields">
								<div class="required field">
									<label>{{ctx.Locale.Tr "repo.licenses.package_name"}}</label>
									<input name="name" required placeholder="e.g. Pro Annual, Basic Monthly">
								</div>
								<div class="field">
									<label>{{ctx.Locale.Tr "repo.licenses.description"}}</label>
									<input name="description" placeholder="e.g. Annual pro subscription">
								</div>
							</div>
							<div class="fields">
								<div class="four wide field">
									<label>{{ctx.Locale.Tr "repo.licenses.duration"}} ({{ctx.Locale.Tr "repo.licenses.days"}})</label>
									<input name="duration_days" type="number" value="0" min="0">
									<p class="help">0 = {{ctx.Locale.Tr "repo.licenses.lifetime"}}</p>
								</div>
								<div class="four wide field">
									<label>{{ctx.Locale.Tr "repo.licenses.max_sites"}}</label>
									<input name="max_sites" type="number" value="0" min="0">
									<p class="help">0 = {{ctx.Locale.Tr "repo.licenses.unlimited"}}</p>
								</div>
								<div class="four wide field">
									<label>{{ctx.Locale.Tr "repo.licenses.domain_lock_hours"}}</label>
									<input name="domain_lock_hours" type="number" value="0" min="0">
									<p class="help">{{ctx.Locale.Tr "repo.licenses.domain_lock_hours_help"}}</p>
								</div>
								<div class="field">
									{{template "shared/combolist" dict "Name" "allowed_channels" "Title" (ctx.Locale.Tr "repo.licenses.channels") "Items" $.ChannelItems "SelectedValues" "" "EmptyText" (ctx.Locale.Tr "repo.licenses.all_channels") "Icon" "octicon-tag"}}
									<p class="help">{{ctx.Locale.Tr "repo.licenses.channels_help"}}</p>
								</div>
							</div>
							<div class="field">
								<label>{{ctx.Locale.Tr "repo.licenses.repo_scope"}}</label>
								<select name="repo_scope" class="ui dropdown">
									<option value="all">{{ctx.Locale.Tr "repo.licenses.repo_scope_all"}}</option>
									{{if $.OrgRepos}}
										{{range $.OrgRepos}}
										<option value="{{.ID}}">{{.Name}}</option>
										{{end}}
									{{end}}
								</select>
								<p class="help">{{ctx.Locale.Tr "repo.licenses.repo_scope_help"}}</p>
							</div>
							<button class="ui primary button" type="submit">{{ctx.Locale.Tr "repo.licenses.create_package"}}</button>
						</form>
			</div>
			{{end}}
		</details>
			{{if .LicensePackages}}
				<table class="ui compact table">
					<thead>
						<tr>
							<th>{{ctx.Locale.Tr "repo.licenses.package_name"}}</th>
							<th>{{ctx.Locale.Tr "repo.licenses.duration"}}</th>
							<th>{{ctx.Locale.Tr "repo.licenses.channels"}}</th>
							<th>{{ctx.Locale.Tr "repo.licenses.keys_issued"}}</th>
							<th>{{ctx.Locale.Tr "repo.licenses.status"}}</th>
							{{if .IsRepoAdmin}}<th></th>{{end}}
						</tr>
					</thead>
					<tbody>
						{{range .LicensePackages}}
							<tr>
								<td><strong>{{.Name}}</strong>{{if eq .Name "Master (Internal)"}} <span class="ui tiny orange label">{{ctx.Locale.Tr "repo.licenses.master_label"}}</span>{{end}}{{if .Description}}<br><small class="text grey">{{.Description}}</small>{{end}}</td>
								<td>{{if eq .DurationDays 0}}{{ctx.Locale.Tr "repo.licenses.lifetime"}}{{else}}{{.DurationDays}} {{ctx.Locale.Tr "repo.licenses.days"}}{{end}}</td>
								<td>{{if .AllowedChannels}}<code>{{.AllowedChannels}}</code>{{else}}{{ctx.Locale.Tr "repo.licenses.all_channels"}}{{end}}</td>
								<td>{{.KeyCount}}</td>
								<td>{{if .IsActive}}<span class="ui green label">{{ctx.Locale.Tr "repo.licenses.active"}}</span>{{else}}<span class="ui grey label">{{ctx.Locale.Tr "repo.licenses.inactive"}}</span>{{end}}</td>
								{{if $.IsRepoAdmin}}
									<td class="tw-text-right tw-flex tw-gap-1 tw-justify-end">
										<form method="post" action="{{$.Org.HomeLink}}/-/licenses/keys/generate" class="tw-inline tw-flex tw-gap-1 tw-items-center">
											{{$.CsrfTokenHtml}}
											<input type="hidden" name="package_id" value="{{.ID}}">
											{{if or $.IsSiteAdmin $.IsOrganizationOwner}}
											<input type="text" name="custom_key" placeholder="{{ctx.Locale.Tr "repo.licenses.custom_key_placeholder"}}" class="tw-w-32 tw-text-xs" title="{{ctx.Locale.Tr "repo.licenses.custom_key_help"}}">
											{{end}}
											<button class="ui tiny primary button" type="submit" title="{{ctx.Locale.Tr "repo.licenses.generate_key"}}">
												{{svg "octicon-plus" 14}}
											</button>
										</form>
										{{if ne .Name "Master (Internal)"}}
										<a class="ui tiny button" href="{{$.Org.HomeLink}}/-/licenses/packages/{{.ID}}/edit" title="{{ctx.Locale.Tr "repo.licenses.edit_package"}}">
											{{svg "octicon-pencil" 14}}
										</a>
										<button class="ui tiny orange button link-action" data-url="{{$.Org.HomeLink}}/-/licenses/packages/{{.ID}}/archive" data-modal-confirm="{{ctx.Locale.Tr "repo.licenses.confirm_archive_package"}}" title="{{ctx.Locale.Tr "repo.licenses.archive_package"}}">
											{{svg "octicon-archive" 14}}
										</button>
										{{if $.CanDelete}}
										<button class="ui tiny red button show-modal" data-modal="#license-delete-package-modal" data-modal-form.action="{{$.Org.HomeLink}}/-/licenses/packages/{{.ID}}/delete" title="{{ctx.Locale.Tr "repo.licenses.delete_package"}}">
											{{svg "octicon-trash" 14}}
										</button>
										{{end}}
										{{end}}
									</td>
								{{end}}
							</tr>
						{{end}}
					</tbody>
				</table>
			{{else}}
				<div class="empty-placeholder">
					{{svg "octicon-key" 48}}
					<h2>{{ctx.Locale.Tr "repo.licenses.none"}}</h2>
					<p>{{ctx.Locale.Tr "repo.licenses.none_desc"}}</p>
				</div>
			{{end}}
		</div>

		{{if or .LicenseKeys .SearchQuery}}
			<h4 class="ui top attached header tw-mt-4">
				{{svg "octicon-lock" 16}} {{ctx.Locale.Tr "repo.licenses.issued_keys"}}
			</h4>
			<div class="ui attached segment">
				<form class="ui form tw-mb-4" method="get" action="{{$.Org.HomeLink}}/-/licenses">
					<div class="tw-flex tw-gap-2 tw-items-center tw-max-w-lg">
						<div class="ui input tw-flex-1">
							<input type="text" name="q" value="{{.SearchQuery}}" placeholder="{{ctx.Locale.Tr "repo.licenses.search_placeholder"}}">
						</div>
						<button class="ui primary button" type="submit">{{svg "octicon-search" 14}}</button>
						{{if .SearchQuery}}<a class="ui button" href="{{$.Org.HomeLink}}/-/licenses">{{ctx.Locale.Tr "repo.licenses.clear_search"}}</a>{{end}}
					</div>
					{{if .SearchQuery}}<p class="help tw-mt-2">{{ctx.Locale.Tr "repo.licenses.search_results" (len .LicenseKeys) .SearchQuery}}</p>{{end}}
				</form>
				{{if .LicenseKeys}}
				<table class="ui sortable compact table">
					<thead>
						<tr>
							<th>{{ctx.Locale.Tr "repo.licenses.key_prefix"}}</th>
							<th>{{ctx.Locale.Tr "repo.licenses.licensee"}}</th>
							<th>{{ctx.Locale.Tr "repo.licenses.domain"}}</th>
							<th>{{ctx.Locale.Tr "repo.licenses.expires"}}</th>
							<th>{{ctx.Locale.Tr "repo.licenses.last_seen"}}</th>
							<th>{{ctx.Locale.Tr "repo.licenses.status"}}</th>
							{{if .IsRepoAdmin}}<th class="no-sort"></th>{{end}}
						</tr>
					</thead>
					<tbody>
						{{range .LicenseKeys}}
							<tr>
								<td>
									<div class="tw-flex tw-items-center tw-gap-1">
										<code class="js-license-key-{{.ID}}">{{if .KeyRaw}}{{.KeyRaw}}{{else}}{{.KeyPrefix}}{{end}}</code>
										{{if .KeyRaw}}<button class="ui tiny icon button" data-clipboard-target=".js-license-key-{{.ID}}" data-tooltip-content="{{ctx.Locale.Tr "copy_url"}}">{{svg "octicon-copy" 12}}</button>{{end}}
										{{if .IsInternal}} <span class="ui tiny orange label">{{ctx.Locale.Tr "repo.licenses.master_label"}}</span>{{end}}
									</div>
								</td>
								<td data-sort-value="{{.LicenseeName}}">{{.LicenseeName}}{{if .LicenseeEmail}} <small>({{.LicenseeEmail}})</small>{{end}}</td>
								<td data-sort-value="{{.DomainRestriction}}">{{if .DomainRestriction}}<code>{{.DomainRestriction}}</code>{{else}}<span class="text grey">{{ctx.Locale.Tr "repo.licenses.any_domain"}}</span>{{end}}</td>
								<td>{{if eq .ExpiresUnix 0}}{{ctx.Locale.Tr "repo.licenses.never"}}{{else}}{{DateUtils.AbsoluteShort .ExpiresUnix}}{{end}}</td>
								<td>{{if eq .LastHeartbeatUnix 0}}{{ctx.Locale.Tr "repo.licenses.never"}}{{else}}{{DateUtils.AbsoluteShort .LastHeartbeatUnix}}{{end}}</td>
								<td>{{if .IsActive}}<span class="ui green label">{{ctx.Locale.Tr "repo.licenses.active"}}</span>{{else}}<span class="ui grey label">{{ctx.Locale.Tr "repo.licenses.inactive"}}</span>{{end}}</td>
								{{if $.IsRepoAdmin}}
									<td class="tw-text-right tw-flex tw-gap-1 tw-justify-end">
										{{if not .IsInternal}}
										<a class="ui tiny button" href="{{$.Org.HomeLink}}/-/licenses/keys/{{.ID}}/edit" title="{{ctx.Locale.Tr "repo.licenses.edit_key"}}">
											{{svg "octicon-pencil" 14}}
										</a>
										<button class="ui tiny green button link-action" data-url="{{$.Org.HomeLink}}/-/licenses/keys/{{.ID}}/renew" data-modal-confirm="{{ctx.Locale.Tr "repo.licenses.confirm_renew_key"}}" title="{{ctx.Locale.Tr "repo.licenses.renew"}}">
											{{svg "octicon-sync" 14}}
										</button>
										{{end}}
										<button class="ui tiny red button link-action" data-url="{{$.Org.HomeLink}}/-/licenses/keys/{{.ID}}/revoke" data-modal-confirm="{{ctx.Locale.Tr "repo.licenses.confirm_revoke_key"}}" title="{{ctx.Locale.Tr "repo.licenses.revoke"}}">
											{{svg "octicon-x" 14}}
										</button>
										{{if $.CanDelete}}
										<button class="ui tiny red button show-modal" data-modal="#license-delete-key-modal" data-modal-form.action="{{$.Org.HomeLink}}/-/licenses/keys/{{.ID}}/delete" title="{{ctx.Locale.Tr "repo.licenses.delete_key"}}">
											{{svg "octicon-trash" 14}}
										</button>
										{{end}}
									</td>
								{{end}}
							</tr>
						{{end}}
					</tbody>
				</table>
				{{else if .SearchQuery}}
				<p class="tw-text-center text grey">{{ctx.Locale.Tr "repo.licenses.no_search_results"}}</p>
				{{end}}
			</div>
		{{end}}

		{{/* ── Archived Packages ── */}}
		{{if .ArchivedPackages}}
			<details class="tw-mt-4">
				<summary class="ui grey button">{{svg "octicon-archive" 14}} {{ctx.Locale.Tr "repo.licenses.archived_packages"}} ({{len .ArchivedPackages}})</summary>
				<div class="ui segment tw-mt-2">
					<table class="ui compact table">
						<thead>
							<tr>
								<th>{{ctx.Locale.Tr "repo.licenses.package_name"}}</th>
								<th>{{ctx.Locale.Tr "repo.licenses.keys_issued"}}</th>
								{{if .IsRepoAdmin}}<th></th>{{end}}
							</tr>
						</thead>
						<tbody>
							{{range .ArchivedPackages}}
								<tr>
									<td><strong>{{.Name}}</strong>{{if .Description}}<br><small class="text grey">{{.Description}}</small>{{end}}</td>
									<td>{{.KeyCount}}</td>
									{{if $.IsRepoAdmin}}
										<td class="tw-text-right tw-flex tw-gap-1 tw-justify-end">
											<button class="ui tiny primary button link-action" data-url="{{$.Org.HomeLink}}/-/licenses/packages/{{.ID}}/unarchive" title="{{ctx.Locale.Tr "repo.licenses.unarchive_package"}}">
												{{svg "octicon-reply" 14}}
											</button>
											{{if $.CanDelete}}
											<button class="ui tiny red button show-modal" data-modal="#license-delete-package-modal" data-modal-form.action="{{$.Org.HomeLink}}/-/licenses/packages/{{.ID}}/delete" title="{{ctx.Locale.Tr "repo.licenses.delete_package"}}">
												{{svg "octicon-trash" 14}}
											</button>
											{{end}}
										</td>
									{{end}}
								</tr>
							{{end}}
						</tbody>
					</table>
				</div>
			</details>
		{{end}}
	</div>
</div>

<div class="ui small modal" id="license-delete-package-modal">
	<div class="header">{{svg "octicon-trash"}} {{ctx.Locale.Tr "repo.licenses.delete_package"}}</div>
	<div class="content">
		<div class="ui warning message">
			<p>{{ctx.Locale.Tr "repo.licenses.confirm_delete_package_typed"}}</p>
		</div>
		<form class="ui form form-fetch-action" method="post">
			{{$.CsrfTokenHtml}}
			<div class="required field">
				<label>{{ctx.Locale.Tr "repo.licenses.type_name_to_confirm"}}</label>
				<input name="confirm_name" required>
			</div>
			{{template "base/modal_actions_confirm" (dict "ModalButtonDangerText" (ctx.Locale.Tr "repo.licenses.delete_package"))}}
		</form>
	</div>
</div>

<div class="ui small modal" id="license-delete-key-modal">
	<div class="header">{{svg "octicon-trash"}} {{ctx.Locale.Tr "repo.licenses.delete_key"}}</div>
	<div class="content">
		<div class="ui warning message">
			<p>{{ctx.Locale.Tr "repo.licenses.confirm_delete_key_typed"}}</p>
		</div>
		<form class="ui form form-fetch-action" method="post">
			{{$.CsrfTokenHtml}}
			{{template "base/modal_actions_confirm" (dict "ModalButtonDangerText" (ctx.Locale.Tr "repo.licenses.delete_key"))}}
		</form>
	</div>
</div>

{{template "base/footer" .}}
