{{define "condition"}}
<fieldset class="fld-cndtns">
	<table>
		<tr class="field-cndtn">
			<th>Field/Condition:</th>
			<td>
				<select name="field{{.conditionNo}}" class="sel-field">
					{{- range $_,$field := .fields -}}
						<option value="{{$field.Value}}" {{if eq $field.Value $.condition.Field}}selected{{end}}>{{$field.Text}}</option>
					{{- end -}}
				</select>
			</td>
		</tr>
		<tr class="search-cndtn" {{if not .condition.HasSearchField}}style="display:none"{{end}}>
			<th>Search:</th>
			<td><input type="text" name="search{{.conditionNo}}" value="{{.condition.Search}}"></td>
		</tr>
		<tr class="strmatch-cndtn" {{if not .condition.ShowStringMatchOptions}}style="display: none"{{end}}>
			<th>Match mode</th>
			<td><select name="matchmode{{.conditionNo}}">
				<option value="substr" {{if eq .condition.MatchMode 0}}selected{{end}}>Substring (case sensitive)</option>
				<option value="substrci" {{if eq .condition.MatchMode 1}}selected{{end}}>Substring (not case sensitive)</option>
				<option value="regex" {{if eq .condition.MatchMode 2}}selected{{end}}>Regular expression</option>
				<option value="exact" {{if eq .condition.MatchMode 3}}selected{{end}}>Exact match</option>
			</select></td>
		</tr>
		<tr class="btns-cndtn">
			<td></td>
			<td><a href="#" class="rem-cndtn">Remove condition</a></td>
		</tr>
	</table>
</fieldset>
{{end -}}

<h2>{{with .sourcePostID -}}
	New filter from post <a href="{{webPathDir $.sourcePostBoard `res`}}{{$.sourcePostThread}}.html#{{$.sourcePostID}}">/{{$.sourcePostBoard}}/{{$.sourcePostID}}</a>
{{- else -}}
	{{- if gt $.filter.ID 0}}Edit filter{{else}}New filter{{end -}}
{{- end}}</h2>
<form id="filterform" action="{{webPath `/manage/filters`}}{{if gt $.filter.ID 0}}?edit={{.ID}}{{end}}" method="POST">
	{{- if gt $.filter.ID 0}}<input type="hidden" name="filterid" value="{{$.filter.ID}}">{{end -}}
	Filter conditions are checked against a post after the IP is checked against the <a href="{{webPath `/manage/bans`}}">ban list</a>.
	The filter action will be executed only if all conditions are met. Tripcode searches do not include the prefix "!". <br/>
	For information on the expected regular expression syntax, see <a href="https://pkg.go.dev/regexp/syntax">here</a> (mostly the same as most regular expression implementations with a few changes, e.g. no lookahead/lookbehind).
	<table>
		<tr>
			<th>Conditions</th>
			<td id="conditions">
				{{- with $.filterConditions -}}
					{{range $t,$condition := $.filterConditions -}}
						{{template "condition" map "conditionNo" (add $t 1) "condition" $condition "fields" $.fields "filter" $.filter}}
					{{end}}
				{{- else -}}
					{{template "condition" map "conditionNo" 1 "fields" $.fields}}
				{{- end -}}
			</td>
			<tr>
				<td></td>
				<td><a href="#" id="add-cndtn">Add condition</a></td>
			</tr>
		</tr>
		<tr>
			<th>Action</th>
			<td>
				<select name="action" id="action">
					<option value="reject" {{if eq $.filter.MatchAction `reject`}}selected{{end}}>Reject post</option>
					<option value="ban" {{if eq $.filter.MatchAction `ban`}}selected{{end}}>Ban IP</option>
					<option value="log" {{if eq $.filter.MatchAction `log`}}selected{{end}}>Log match</option>
				</select>
			</td>
		</tr>
		<tr>
			<th></th>
			<td>
				<label for="handleifany" title="If checked, the filter action will activate if any of the above conditions match an incoming post. Otherwise, all conditions must match">
					<input type="checkbox" name="handleifany" id="handleifany" {{if $.filter.HandleIfAny}}checked{{end}}> Activate if any condition matches</label><br />
			</td>
		</tr>
		<tr>
			<th id="detail">Reason</th>
			<td><textarea name="detail" rows="5" cols="35">{{$.filter.MatchDetail}}</textarea></td>
		</tr>
		<tr>
			<th id="note">Staff Note</th>
			<td><textarea name="note" rows="5" cols="35">{{$.filter.StaffNote}}</textarea></td>
		</tr>
		<tr>
			<th>Board(s):</th>
			<td id="boardslist">
				If no boards are selected, the filter will be applied to all boards<br/>
				{{- range $_,$board := .allBoards -}}
					<label for="applyboard{{$board.ID}}">/{{$board.Dir}}/ - {{$board.Title}} <input type="checkbox" name="applyboard{{$board.ID}}" id="applyboard{{$board.ID}}"
					{{- range $_,$boardID := $.filterBoards -}}
						{{if eq $boardID $board.ID}}checked{{end}}
					{{- end}}></label>
				{{- end -}}
			</td>
		</tr>
	</table>
	<input type="submit" name="dofilter{{if gt $.filter.ID 0}}edit{{else}}add{{end}}" value="Submit Filter">
	{{with $.cancelURL}}<input type="button" onclick="window.location='{{.}}'" value="Cancel"/>{{end}}
</form>
<hr/>
<h2>Filter list</h2>
For wordfilters go <a href="{{webPath `/manage/wordfilters`}}">here</a>.

<form action="{{webPath `/manage/filters`}}" method="GET">
	Show only filters for: <select name="boardsearch" id="boardsearch">
		<option value="">All boards</option>
		{{- range $_, $board := $.allBoards}}
		<option value="{{$board.Dir}}" {{if eq $.boardSearch $board.Dir}}selected{{end}}>/{{$board.Dir}}/ - {{$board.Title}}</option>
		{{- end -}}
	</select>
	<select name="show" id="">
		<option value="active" {{if or (eq $.show `active`) (eq $.show ``)}}selected{{end}}>Only active</option>
		<option value="inactive" {{if eq $.show `inactive`}}selected{{end}}>Only inactive</option>
		<option value="all" {{if eq $.show `all`}}selected{{end}}>All filters</option>
	</select>
	<input type="submit" value="Show">
</form>
<table class="mgmt-table filterlist">
	<colgroup>
		<col class="filter-actions">
		<col class="filter-action">
		<col class="filter-conditions">
		<col class="staff-boards">
		<col class="staff-note">
		<col class="row-staff">
		<col class="is-active">
		<col class="row-date">
	</colgroup>
	<tr>
		<th>Actions</th><th>Filter action</th><th>Conditions</th><th>Boards</th><th>Staff note</th><th>Staff</th><th>Is active</th><th>Last modified</th>
	</tr>
	{{- range $f, $filter := $.filters -}}
	{{$boardsCell := index $.filterTableBoards $f}}
	<tr>
		<td class="filter-actions">
			<a href="{{webPath `/manage/filters`}}?edit={{$filter.ID}}">Edit</a> | 
			<a href="{{webPath `/manage/filters`}}?{{if $filter.IsActive}}disable{{else}}enable{{end}}={{$filter.ID}}">{{if $filter.IsActive}}Disable{{else}}Enable{{end}}</a> | 
			<a href="{{webPathDir `/manage/filters/hits/`}}{{$filter.ID}}">Hits ({{index $.filterHits $f}})</a>
		</td>
		<td>{{index $.actions $filter.MatchAction}}</td>
		<td>{{index $.conditions $f}}</td>
		<td>{{if eq $boardsCell ""}}<i>all boards</i>{{else}}{{$boardsCell}}{{end}}</td>
		<td>{{$filter.StaffNote}}</td>
		<td>{{index $.staff $f}}</td>
		<td>{{$filter.IsActive}}</td>
		<td>{{formatTimestamp $filter.IssuedAt}}</td>
	</tr>
	{{- end -}}
</table>