{{define "template_detail"}}
<!doctype html>
<html lang="en">
<head>
  <title>{{.Title}}</title>
  {{template "head" .}}
</head>
<body>
  {{template "nav" .}}
  {{if .Msg}}<div class="flash flash-ok">{{.Msg}}</div>{{end}}
  {{if .Err}}<div class="flash flash-err">{{.Err}}</div>{{end}}
  <h1>Template Detail</h1>
  <table>
    <tr><th>ID</th><td>{{.Detail.Template.ID}}</td></tr>
    <tr><th>Name</th><td>{{.Detail.Template.Name}}</td></tr>
    <tr><th>Type</th><td>{{.Detail.Template.Type}}</td></tr>
    <tr><th>Manifest status</th><td>{{.Detail.Template.ManifestStatus}}</td></tr>
    <tr>
      <th>Thumbnail</th>
      <td>
        {{if .Detail.Template.ThumbnailURL}}
        <span class="thumb-hover">
          <a href="{{.Detail.Template.ThumbnailURL}}" target="_blank" rel="noopener">open thumbnail</a>
          <span class="thumb-preview"><img src="{{.Detail.Template.ThumbnailURL}}" alt="Template thumbnail for {{.Detail.Template.Name}}" loading="lazy"></span>
        </span>
        {{else}}none{{end}}
      </td>
    </tr>
    <tr><th>Active manifest</th><td>{{if .Detail.Manifest}}{{.Detail.Manifest.ID}}{{else}}none{{end}}</td></tr>
  </table>

  <form method="post" action="/templates/{{.Detail.Template.ID}}/onboard">
    <button type="submit">Run Onboarding Discovery</button>
  </form>

  {{if .Detail.Manifest}}
  <h2>Manifest</h2>
  <p class="mono">{{prettyJSON .Detail.Manifest.FlattenedManifestJSON}}</p>

  <h2>Fields</h2>
  <form method="post" action="/templates/{{.Detail.Template.ID}}/fields">
    <input type="hidden" name="manifest_id" value="{{.Detail.Manifest.ID}}">
    <input type="hidden" name="field_count" value="{{len .Fields}}">
    <table>
      <thead>
      <tr>
        <th>Path</th>
        <th>Kind</th>
        <th>Enabled</th>
        <th>Required</th>
        <th>Label</th>
        <th>Order</th>
        <th>Website Section</th>
        <th>Notes</th>
        <th>Sample</th>
      </tr>
      </thead>
      <tbody>
      {{range $i, $f := .Fields}}
      <tr>
        <td>
          <input type="hidden" name="field_path_{{$i}}" value="{{$f.Path}}">
          <span class="mono">{{$f.Path}}</span>
        </td>
        <td>{{$f.FieldKind}}</td>
        <td><input type="checkbox" name="field_enabled_{{$i}}" {{if $f.IsEnabled}}checked{{end}}></td>
        <td><input type="checkbox" name="field_required_{{$i}}" {{if $f.IsRequiredByUs}}checked{{end}}></td>
        <td><input type="text" name="field_label_{{$i}}" value="{{$f.DisplayLabel}}"></td>
        <td><input type="number" name="field_order_{{$i}}" value="{{$f.DisplayOrder}}"></td>
        <td>
          <select name="field_website_section_{{$i}}">
            {{range $opt := $.WebsiteSectionOptions}}
            <option value="{{$opt.Value}}" {{if eq $f.WebsiteSection $opt.Value}}selected{{end}}>{{$opt.Label}}</option>
            {{end}}
          </select>
        </td>
        <td><input type="text" name="field_notes_{{$i}}" value="{{$f.Notes}}"></td>
        <td class="mono">{{$f.SampleValue}}</td>
      </tr>
      {{end}}
      </tbody>
    </table>
    <button type="submit">Save Field Settings</button>
  </form>
  {{end}}

  <h2>Raw Template JSON</h2>
  <p class="mono">{{prettyJSON .Detail.Template.RawJSON}}</p>
</body>
</html>
{{end}}
