{{template "base/head" .}}
<div role="main" aria-label="{{.Title}}" class="page-content repository security">
	{{template "repo/header" .}}
	<div class="ui container">
		<div class="tw-flex tw-justify-between tw-items-center tw-mb-4">
			<h2>{{svg "octicon-shield" 20 "tw-mr-2"}}{{ctx.Locale.Tr "repo.security"}}</h2>
			{{if .Permission.IsAdmin}}
			<div class="tw-flex tw-gap-2">
				<form method="post" action="{{.RepoLink}}/security/scan" class="tw-inline">
					{{.CsrfTokenHtml}}
					<button class="ui small primary button" type="submit">{{svg "octicon-sync" 14}} {{ctx.Locale.Tr "repo.settings.security_scan_now"}}</button>
				</form>
				<a class="ui small button" href="{{.RepoLink}}/settings/security">{{svg "octicon-gear" 14}} Settings</a>
			</div>
			{{end}}
		</div>

		{{if .AlertCounts}}
		<div class="tw-flex tw-gap-3 tw-mb-4">
			{{range $sev, $count := .AlertCounts}}
				<div class="ui {{if eq $sev "critical"}}red{{else if eq $sev "high"}}orange{{else if eq $sev "medium"}}yellow{{else if eq $sev "low"}}blue{{else}}grey{{end}} label">
					{{$sev}}: {{$count}}
				</div>
			{{end}}
		</div>
		{{end}}

		{{if .SecurityAlerts}}
		<table class="ui compact table">
			<thead>
				<tr>
					<th>{{ctx.Locale.Tr "repo.settings.security_severity"}}</th>
					<th>{{ctx.Locale.Tr "repo.settings.security_scanner_type"}}</th>
					<th>{{ctx.Locale.Tr "repo.settings.security_finding"}}</th>
					<th>{{ctx.Locale.Tr "repo.settings.security_file"}}</th>
					<th>{{ctx.Locale.Tr "repo.settings.security_status"}}</th>
					{{if .Permission.IsAdmin}}<th></th>{{end}}
				</tr>
			</thead>
			<tbody>
				{{range .SecurityAlerts}}
				<tr {{if ne .Status "active"}}class="tw-opacity-50"{{end}}>
					<td>
						<span class="ui mini {{if eq .Severity "critical"}}red{{else if eq .Severity "high"}}orange{{else if eq .Severity "medium"}}yellow{{else if eq .Severity "low"}}blue{{else}}grey{{end}} label">
							{{.Severity}}
						</span>
					</td>
					<td>{{.Scanner}}</td>
					<td>
						<strong>{{.Title}}</strong>
						{{if .Description}}<br><small class="text grey">{{.Description}}</small>{{end}}
					</td>
					<td>
						{{if .FilePath}}
							<a href="{{$.RepoLink}}/src/branch/{{$.BranchName}}/{{.FilePath}}{{if .LineNumber}}#L{{.LineNumber}}{{end}}">
								<code class="tw-text-xs">{{.FilePath}}{{if .LineNumber}}:{{.LineNumber}}{{end}}</code>
							</a>
						{{end}}
					</td>
					<td>
						{{if eq .Status "active"}}
							<span class="ui mini red label">Active</span>
						{{else if eq .Status "resolved"}}
							<span class="ui mini green label">Resolved</span>
						{{else}}
							<span class="ui mini grey label">Dismissed</span>
						{{end}}
					</td>
					{{if $.Permission.IsAdmin}}
					<td class="tw-text-right">
						{{if eq .Status "active"}}
							<form method="post" action="{{$.RepoLink}}/security/alert/{{.ID}}" class="tw-inline">
								{{$.CsrfTokenHtml}}
								<input type="hidden" name="status" value="resolved">
								<button class="ui tiny green icon button" type="submit" title="Resolve">{{svg "octicon-check" 14}}</button>
							</form>
							<form method="post" action="{{$.RepoLink}}/security/alert/{{.ID}}" class="tw-inline">
								{{$.CsrfTokenHtml}}
								<input type="hidden" name="status" value="dismissed">
								<button class="ui tiny grey icon button" type="submit" title="Dismiss">{{svg "octicon-x" 14}}</button>
							</form>
						{{end}}
					</td>
					{{end}}
				</tr>
				{{end}}
			</tbody>
		</table>
		{{else}}
		<div class="ui segment">
			<div class="empty-placeholder">
				<p>{{svg "octicon-shield-check" 48}}</p>
				<p>{{ctx.Locale.Tr "repo.settings.security_no_alerts"}}</p>
			</div>
		</div>
		{{end}}
	</div>
</div>
{{template "base/footer" .}}
