{{/* Template attributes:
- BaseLink: Base URL for the repository settings
- WebHook: Webhook object containing details about the webhook
- UseAuthorizationHeader: optional or required
- UseRequestSecret: optional or required
*/}}
{{$isNew := not .Webhook.ID}}

<div class="inline field">
	<div class="ui checkbox">
		<input name="active" type="checkbox" {{if or $isNew .Webhook.IsActive}}checked{{end}}>
		<label>{{ctx.Locale.Tr "repo.settings.active"}}</label>
		<span class="help">{{ctx.Locale.Tr "repo.settings.active_helper"}}</span>
	</div>
</div>

<!-- Authorization Header -->
{{if .UseAuthorizationHeader}}
	{{$attributeValid := or (eq .UseAuthorizationHeader "optional") (eq .UseAuthorizationHeader "required")}}
	{{if not $attributeValid}}<div class="ui error message">Invalid UseAuthorizationHeader: {{.UseAuthorizationHeader}}}</div>{{end}}
	{{$required := eq .UseAuthorizationHeader "required"}}
	<div class="field {{if $required}}required{{end}}">
		<label>{{ctx.Locale.Tr "repo.settings.authorization_header"}}</label>
		<input name="authorization_header" type="text" value="{{.Webhook.HeaderAuthorization}}" {{if $required}}required placeholder="Bearer $access_token"{{end}}>
		{{if not $required}}
			<span class="help">{{ctx.Locale.Tr "repo.settings.authorization_header_desc" (HTMLFormat "<code>%s</code>, <code>%s</code>" "Bearer token123456" "Basic YWxhZGRpbjpvcGVuc2VzYW1l")}}</span>
		{{end}}
	</div>
{{end}}

<!-- Secret -->
{{if .UseRequestSecret}}
	{{$attributeValid := or (eq .UseRequestSecret "optional") (eq .UseRequestSecret "required")}}
	{{if not $attributeValid}}<div class="ui error message">Invalid UseRequestSecret: {{.UseRequestSecret}}}</div>{{end}}
	{{$required := eq .UseRequestSecret "required"}}
	<div class="field {{if $required}}required{{end}}">
		<label>{{ctx.Locale.Tr "repo.settings.secret"}}</label>
		<input name="secret" type="password" value="{{.Webhook.Secret}}" autocomplete="off" {{if $required}}required{{end}}>
		<span class="help">{{ctx.Locale.Tr "repo.settings.webhook_secret_desc"}}</span>
	</div>
{{end}}

<!-- Branch filter -->
<div class="field">
	<label>{{ctx.Locale.Tr "repo.settings.branch_filter"}}</label>
	<input name="branch_filter" type="text" value="{{or .Webhook.BranchFilter "*"}}">
	<span class="help">{{ctx.Locale.Tr "repo.settings.branch_filter_desc" "https://pkg.go.dev/github.com/gobwas/glob#Compile" "github.com/gobwas/glob"}}</span>
</div>

<div class="field">
	<h4>{{ctx.Locale.Tr "repo.settings.event_desc"}}</h4>
	<div class="grouped event type fields">
		<div class="field">
			<div class="ui radio non-events checkbox">
				<input name="events" type="radio" value="push_only" {{if or $isNew .Webhook.PushOnly}}checked{{end}}>
				<label>{{ctx.Locale.Tr "repo.settings.event_push_only"}}</label>
			</div>
		</div>
		<div class="field">
			<div class="ui radio non-events checkbox">
				<input name="events" type="radio" value="send_everything" {{if .Webhook.SendEverything}}checked{{end}}>
				<label>{{ctx.Locale.Tr "repo.settings.event_send_everything"}}</label>
			</div>
		</div>
		<div class="field">
			<div class="ui radio events checkbox">
				<input name="events" type="radio" value="choose_events" {{if .Webhook.ChooseEvents}}checked{{end}}>
				<label>{{ctx.Locale.Tr "repo.settings.event_choose"}}</label>
			</div>
		</div>
	</div>

	<div class="events fields ui grid {{if not .Webhook.ChooseEvents}}tw-hidden{{end}}">
		<!-- Repository Events -->
		<div class="fourteen wide column">
			<label>{{ctx.Locale.Tr "repo.settings.event_header_repository"}}</label>
		</div>
		<!-- Create -->
		<div class="seven wide column">
			<div class="field">
				<div class="ui checkbox">
					<input name="create" type="checkbox" {{if .Webhook.HookEvents.Get "create"}}checked{{end}}>
					<label>{{ctx.Locale.Tr "repo.settings.event_create"}}</label>
					<span class="help">{{ctx.Locale.Tr "repo.settings.event_create_desc"}}</span>
				</div>
			</div>
		</div>
		<!-- Delete -->
		<div class="seven wide column">
			<div class="field">
				<div class="ui checkbox">
					<input name="delete" type="checkbox" {{if .Webhook.HookEvents.Get "delete"}}checked{{end}}>
					<label>{{ctx.Locale.Tr "repo.settings.event_delete"}}</label>
					<span class="help">{{ctx.Locale.Tr "repo.settings.event_delete_desc"}}</span>
				</div>
			</div>
		</div>
		<!-- Fork -->
		<div class="seven wide column">
			<div class="field">
				<div class="ui checkbox">
					<input name="fork" type="checkbox" {{if .Webhook.HookEvents.Get "fork"}}checked{{end}}>
					<label>{{ctx.Locale.Tr "repo.settings.event_fork"}}</label>
					<span class="help">{{ctx.Locale.Tr "repo.settings.event_fork_desc"}}</span>
				</div>
			</div>
		</div>
		<!-- Push -->
		<div class="seven wide column">
			<div class="field">
				<div class="ui checkbox">
					<input name="push" type="checkbox" {{if .Webhook.HookEvents.Get "push"}}checked{{end}}>
					<label>{{ctx.Locale.Tr "repo.settings.event_push"}}</label>
					<span class="help">{{ctx.Locale.Tr "repo.settings.event_push_desc"}}</span>
				</div>
			</div>
		</div>
		<!-- Repository -->
		<div class="seven wide column">
			<div class="field">
				<div class="ui checkbox">
					<input name="repository" type="checkbox" {{if .Webhook.HookEvents.Get "repository"}}checked{{end}}>
					<label>{{ctx.Locale.Tr "repo.settings.event_repository"}}</label>
					<span class="help">{{ctx.Locale.Tr "repo.settings.event_repository_desc"}}</span>
				</div>
			</div>
		</div>
		<!-- Release -->
		<div class="seven wide column">
			<div class="field">
				<div class="ui checkbox">
					<input name="release" type="checkbox" {{if .Webhook.HookEvents.Get "release"}}checked{{end}}>
					<label>{{ctx.Locale.Tr "repo.settings.event_release"}}</label>
					<span class="help">{{ctx.Locale.Tr "repo.settings.event_release_desc"}}</span>
				</div>
			</div>
		</div>
		<!-- Package -->
		<div class="seven wide column">
			<div class="field">
				<div class="ui checkbox">
					<input name="package" type="checkbox" {{if .Webhook.HookEvents.Get "package"}}checked{{end}}>
					<label>{{ctx.Locale.Tr "repo.settings.event_package"}}</label>
					<span class="help">{{ctx.Locale.Tr "repo.settings.event_package_desc"}}</span>
				</div>
			</div>
		</div>

		<!-- Wiki -->
		<div class="seven wide column">
			<div class="field">
				<div class="ui checkbox">
					<input name="wiki" type="checkbox" {{if .Webhook.HookEvents.Get "wiki"}}checked{{end}}>
					<label>{{ctx.Locale.Tr "repo.settings.event_wiki"}}</label>
					<span class="help">{{ctx.Locale.Tr "repo.settings.event_wiki_desc"}}</span>
				</div>
			</div>
		</div>

		<!-- Status -->
		<div class="seven wide column">
			<div class="field">
				<div class="ui checkbox">
					<input name="status" type="checkbox" {{if .Webhook.HookEvents.Get "status"}}checked{{end}}>
					<label>{{ctx.Locale.Tr "repo.settings.event_statuses"}}</label>
					<span class="help">{{ctx.Locale.Tr "repo.settings.event_statuses_desc"}}</span>
				</div>
			</div>
		</div>

		<!-- Issue Events -->
		<div class="fourteen wide column">
			<label>{{ctx.Locale.Tr "repo.settings.event_header_issue"}}</label>
		</div>
		<!-- Issues -->
		<div class="seven wide column">
			<div class="field">
				<div class="ui checkbox">
					<input name="issues" type="checkbox" {{if .Webhook.HookEvents.Get "issues"}}checked{{end}}>
					<label>{{ctx.Locale.Tr "repo.settings.event_issues"}}</label>
					<span class="help">{{ctx.Locale.Tr "repo.settings.event_issues_desc"}}</span>
				</div>
			</div>
		</div>
		<!-- Issue Assign -->
		<div class="seven wide column">
			<div class="field">
				<div class="ui checkbox">
					<input name="issue_assign" type="checkbox" {{if .Webhook.HookEvents.Get "issue_assign"}}checked{{end}}>
					<label>{{ctx.Locale.Tr "repo.settings.event_issue_assign"}}</label>
					<span class="help">{{ctx.Locale.Tr "repo.settings.event_issue_assign_desc"}}</span>
				</div>
			</div>
		</div>
		<!-- Issue Label -->
		<div class="seven wide column">
			<div class="field">
				<div class="ui checkbox">
					<input name="issue_label" type="checkbox" {{if .Webhook.HookEvents.Get "issue_label"}}checked{{end}}>
					<label>{{ctx.Locale.Tr "repo.settings.event_issue_label"}}</label>
					<span class="help">{{ctx.Locale.Tr "repo.settings.event_issue_label_desc"}}</span>
				</div>
			</div>
		</div>
		<!-- Issue Milestone -->
		<div class="seven wide column">
			<div class="field">
				<div class="ui checkbox">
					<input name="issue_milestone" type="checkbox" {{if .Webhook.HookEvents.Get "issue_milestone"}}checked{{end}}>
					<label>{{ctx.Locale.Tr "repo.settings.event_issue_milestone"}}</label>
					<span class="help">{{ctx.Locale.Tr "repo.settings.event_issue_milestone_desc"}}</span>
				</div>
			</div>
		</div>
		<!-- Issue Comment -->
		<div class="seven wide column">
			<div class="field">
				<div class="ui checkbox">
					<input name="issue_comment" type="checkbox" {{if .Webhook.HookEvents.Get "issue_comment"}}checked{{end}}>
					<label>{{ctx.Locale.Tr "repo.settings.event_issue_comment"}}</label>
					<span class="help">{{ctx.Locale.Tr "repo.settings.event_issue_comment_desc"}}</span>
				</div>
			</div>
		</div>

		<!-- Pull Request Events -->
		<div class="fourteen wide column">
			<label>{{ctx.Locale.Tr "repo.settings.event_header_pull_request"}}</label>
		</div>
		<!-- Pull Request -->
		<div class="seven wide column">
			<div class="field">
				<div class="ui checkbox">
					<input name="pull_request" type="checkbox" {{if .Webhook.HookEvents.Get "pull_request"}}checked{{end}}>
					<label>{{ctx.Locale.Tr "repo.settings.event_pull_request"}}</label>
					<span class="help">{{ctx.Locale.Tr "repo.settings.event_pull_request_desc"}}</span>
				</div>
			</div>
		</div>
		<!-- Pull Request Assign -->
		<div class="seven wide column">
			<div class="field">
				<div class="ui checkbox">
					<input name="pull_request_assign" type="checkbox" {{if .Webhook.HookEvents.Get "pull_request_assign"}}checked{{end}}>
					<label>{{ctx.Locale.Tr "repo.settings.event_pull_request_assign"}}</label>
					<span class="help">{{ctx.Locale.Tr "repo.settings.event_pull_request_assign_desc"}}</span>
				</div>
			</div>
		</div>
		<!-- Pull Request Label -->
		<div class="seven wide column">
			<div class="field">
				<div class="ui checkbox">
					<input name="pull_request_label" type="checkbox" {{if .Webhook.HookEvents.Get "pull_request_label"}}checked{{end}}>
					<label>{{ctx.Locale.Tr "repo.settings.event_pull_request_label"}}</label>
					<span class="help">{{ctx.Locale.Tr "repo.settings.event_pull_request_label_desc"}}</span>
				</div>
			</div>
		</div>
		<!-- Pull Request Milestone -->
		<div class="seven wide column">
			<div class="field">
				<div class="ui checkbox">
					<input name="pull_request_milestone" type="checkbox" {{if .Webhook.HookEvents.Get "pull_request_milestone"}}checked{{end}}>
					<label>{{ctx.Locale.Tr "repo.settings.event_pull_request_milestone"}}</label>
					<span class="help">{{ctx.Locale.Tr "repo.settings.event_pull_request_milestone_desc"}}</span>
				</div>
			</div>
		</div>
		<!-- Pull Request Comment -->
		<div class="seven wide column">
			<div class="field">
				<div class="ui checkbox">
					<input name="pull_request_comment" type="checkbox" {{if .Webhook.HookEvents.Get "pull_request_comment"}}checked{{end}}>
					<label>{{ctx.Locale.Tr "repo.settings.event_pull_request_comment"}}</label>
					<span class="help">{{ctx.Locale.Tr "repo.settings.event_pull_request_comment_desc"}}</span>
				</div>
			</div>
		</div>
		<!-- Pull Request Review -->
		<div class="seven wide column">
			<div class="field">
				<div class="ui checkbox">
					<input name="pull_request_review" type="checkbox" {{if .Webhook.HookEvents.Get "pull_request_review"}}checked{{end}}>
					<label>{{ctx.Locale.Tr "repo.settings.event_pull_request_review"}}</label>
					<span class="help">{{ctx.Locale.Tr "repo.settings.event_pull_request_review_desc"}}</span>
				</div>
			</div>
		</div>
		<!-- Pull Request Sync -->
		<div class="seven wide column">
			<div class="field">
				<div class="ui checkbox">
					<input name="pull_request_sync" type="checkbox" {{if .Webhook.HookEvents.Get "pull_request_sync"}}checked{{end}}>
					<label>{{ctx.Locale.Tr "repo.settings.event_pull_request_sync"}}</label>
					<span class="help">{{ctx.Locale.Tr "repo.settings.event_pull_request_sync_desc"}}</span>
				</div>
			</div>
		</div>
		<!-- Pull Request Review Request -->
		<div class="seven wide column">
			<div class="field">
				<div class="ui checkbox">
					<input name="pull_request_review_request" type="checkbox" {{if .Webhook.HookEvents.Get "pull_request_review_request"}}checked{{end}}>
					<label>{{ctx.Locale.Tr "repo.settings.event_pull_request_review_request"}}</label>
					<span class="help">{{ctx.Locale.Tr "repo.settings.event_pull_request_review_request_desc"}}</span>
				</div>
			</div>
		</div>
		<!-- Workflow Events -->
		<div class="fourteen wide column">
			<label>{{ctx.Locale.Tr "repo.settings.event_header_workflow"}}</label>
		</div>
		<!-- Workflow Run Event -->
		<div class="seven wide column">
			<div class="field">
				<div class="ui checkbox">
					<input name="workflow_run" type="checkbox" {{if .Webhook.HookEvents.Get "workflow_run"}}checked{{end}}>
					<label>{{ctx.Locale.Tr "repo.settings.event_workflow_run"}}</label>
					<span class="help">{{ctx.Locale.Tr "repo.settings.event_workflow_run_desc"}}</span>
				</div>
			</div>
		</div>
		<!-- Workflow Job Event -->
		<div class="seven wide column">
			<div class="field">
				<div class="ui checkbox">
					<input name="workflow_job" type="checkbox" {{if .Webhook.HookEvents.Get "workflow_job"}}checked{{end}}>
					<label>{{ctx.Locale.Tr "repo.settings.event_workflow_job"}}</label>
					<span class="help">{{ctx.Locale.Tr "repo.settings.event_workflow_job_desc"}}</span>
				</div>
			</div>
		</div>
	</div>
</div>

<div class="field">
	{{if $isNew}}
		<button class="ui primary button">{{ctx.Locale.Tr "repo.settings.add_webhook"}}</button>
	{{else}}
		<button class="ui primary button">{{ctx.Locale.Tr "repo.settings.update_webhook"}}</button>
		<a class="ui red button link-action"
			data-url="{{.BaseLink}}/delete?id={{.Webhook.ID}}"
			data-modal-confirm="{{ctx.Locale.Tr "repo.settings.webhook_deletion_desc"}}"
		>{{ctx.Locale.Tr "repo.settings.delete_webhook"}}</a>
	{{end}}
</div>
