{{define "build_new"}}
<!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>New Build</h1>

  <form method="get" action="/builds/new">
    <label for="draft_id">Draft laden</label>
    <select id="draft_id" name="draft_id">
      <option value="">Kein Draft</option>
      {{range .Drafts}}
      <option value="{{.ID}}" {{if eq $.SelectedDraftID .ID}}selected{{end}}>{{.RequestName}} | {{.Status}} | {{.Source}} ({{.TemplateID}})</option>
      {{end}}
    </select>
    <button type="submit">Draft laden</button>
  </form>

  <form method="get" action="/builds/new">
    {{if .SelectedDraftID}}<input type="hidden" name="draft_id" value="{{.SelectedDraftID}}">{{end}}
    <label for="template_id">Template</label>
    <select id="template_id" name="template_id">
      <option value="">Select template</option>
      {{range .Templates}}
      <option value="{{.ID}}" {{if eq $.SelectedTemplateID .ID}}selected{{end}}>{{.Name}} ({{.ID}})</option>
      {{end}}
    </select>
    <button type="submit">Load Fields</button>
  </form>

  {{if gt .SelectedTemplateID 0}}
  <form method="post" action="/builds">
    <input type="hidden" name="draft_id" value="{{.Form.DraftID}}">
    <input type="hidden" name="template_id" value="{{.SelectedTemplateID}}">
    <input type="hidden" name="manifest_id" value="{{.SelectedManifestID}}">
    <input type="hidden" name="field_count" value="{{len .EditableFields}}">
    <input type="hidden" name="suggestion_state_json" value="{{.SuggestionStateJSON}}">

    <h2>Global Data</h2>
    <div class="grid2">
      <div><label>Draft Source<input type="text" name="draft_source" value="{{.Form.DraftSource}}" placeholder="ui"></label></div>
      <div>
        <label>Draft Status
          <select name="draft_status">
            <option value="draft" {{if eq .Form.DraftStatus "draft"}}selected{{end}}>draft</option>
            <option value="reviewed" {{if eq .Form.DraftStatus "reviewed"}}selected{{end}}>reviewed</option>
            <option value="submitted" {{if eq .Form.DraftStatus "submitted"}}selected{{end}}>submitted</option>
          </select>
        </label>
      </div>
    </div>
    <div><label>Request Name<input type="text" name="request_name" value="{{.Form.RequestName}}"></label></div>
    <div><label>Draft Notes<textarea name="draft_notes">{{.Form.DraftNotes}}</textarea></label></div>

    <h3>Basis / Firma</h3>
    <div class="grid2">
      <div><label>Firmenname*<input type="text" name="company_name" value="{{.Form.CompanyName}}" required></label></div>
      <div><label>Benutzername*<input type="text" name="username" value="{{.Form.Username}}" required></label></div>
      <div><label>Branche / Business Type<input type="text" name="business_type" value="{{.Form.BusinessType}}"></label></div>
      <div><label>Website-Sprache<input type="text" name="site_language" value="{{.Form.SiteLanguage}}"></label></div>
    </div>

    <h3>Intake / Website-Kontext</h3>
    <div class="grid2">
      <div><label>Website URL<input type="url" name="website_url" value="{{.Form.WebsiteURL}}" placeholder="https://example.com"></label></div>
      <div>
        <label>Locale Style
          <select name="locale_style">
            <option value="">Bitte waehlen</option>
            <option value="de-CH" {{if eq .Form.LocaleStyle "de-CH"}}selected{{end}}>de-CH</option>
            <option value="de-DE" {{if eq .Form.LocaleStyle "de-DE"}}selected{{end}}>de-DE</option>
            <option value="de-AT" {{if eq .Form.LocaleStyle "de-AT"}}selected{{end}}>de-AT</option>
          </select>
        </label>
      </div>
      <div>
        <label>Market Style
          <select name="market_style">
            <option value="">Bitte waehlen</option>
            <option value="DACH" {{if eq .Form.MarketStyle "DACH"}}selected{{end}}>DACH</option>
            <option value="Schweiz" {{if eq .Form.MarketStyle "Schweiz"}}selected{{end}}>Schweiz</option>
            <option value="Deutschland" {{if eq .Form.MarketStyle "Deutschland"}}selected{{end}}>Deutschland</option>
            <option value="Oesterreich" {{if eq .Form.MarketStyle "Oesterreich"}}selected{{end}}>Oesterreich</option>
          </select>
        </label>
      </div>
      <div>
        <label>Address Mode
          <select name="address_mode">
            <option value="">Bitte waehlen</option>
            <option value="du" {{if eq .Form.AddressMode "du"}}selected{{end}}>du</option>
            <option value="sie" {{if eq .Form.AddressMode "sie"}}selected{{end}}>sie</option>
          </select>
        </label>
      </div>
      <div>
        <label>Content Tone
          <select name="content_tone">
            <option value="">Bitte waehlen</option>
            <option value="sachlich" {{if eq .Form.ContentTone "sachlich"}}selected{{end}}>sachlich</option>
            <option value="freundlich" {{if eq .Form.ContentTone "freundlich"}}selected{{end}}>freundlich</option>
            <option value="modern" {{if eq .Form.ContentTone "modern"}}selected{{end}}>modern</option>
            <option value="professionell" {{if eq .Form.ContentTone "professionell"}}selected{{end}}>professionell</option>
            <option value="locker" {{if eq .Form.ContentTone "locker"}}selected{{end}}>locker</option>
            <option value="premium" {{if eq .Form.ContentTone "premium"}}selected{{end}}>premium</option>
          </select>
        </label>
      </div>
    </div>
    <div><label>Website Summary<textarea name="website_summary">{{.Form.WebsiteSummary}}</textarea></label></div>
    <p><small>Prompt-/Systemsteuerung wird global unter <a href="/settings">Settings</a> gepflegt.</small></p>

    <h3>Kontakt</h3>
    <div class="grid2">
      <div><label>E-Mail*<input type="email" name="email" value="{{.Form.Email}}" required></label></div>
      <div><label>Telefon<input type="text" name="phone" value="{{.Form.Phone}}"></label></div>
    </div>

    <h3>Firmeninformation</h3>
    <div class="grid2">
      <div><label>Organisationsnummer<input type="text" name="org_number" value="{{.Form.OrgNumber}}"></label></div>
      <div><label>Startdatum<input type="text" name="start_date" value="{{.Form.StartDate}}" placeholder="YYYY-MM-DD"></label></div>
      <div><label>Mission<textarea name="mission">{{.Form.Mission}}</textarea></label></div>
      <div><label>Kurzbeschreibung<textarea name="description_short">{{.Form.DescriptionShort}}</textarea></label></div>
    </div>
    <div><label>Langbeschreibung<textarea name="description_long">{{.Form.DescriptionLong}}</textarea></label></div>

    <h3>Adresse</h3>
    <div class="grid2">
      <div><label>Adresszeile 1<input type="text" name="address_line1" value="{{.Form.AddressLine1}}"></label></div>
      <div><label>Adresszeile 2<input type="text" name="address_line2" value="{{.Form.AddressLine2}}"></label></div>
      <div><label>Stadt<input type="text" name="address_city" value="{{.Form.AddressCity}}"></label></div>
      <div><label>Region / Bundesland<input type="text" name="address_region" value="{{.Form.AddressRegion}}"></label></div>
      <div><label>PLZ<input type="text" name="address_zip" value="{{.Form.AddressZIP}}"></label></div>
      <div><label>Land<input type="text" name="address_country" value="{{.Form.AddressCountry}}"></label></div>
    </div>

    <h2>Template-Felder</h2>
    <div>
      <button type="submit" formaction="/builds/drafts/autofill" name="autofill_action" value="generate_all">Generate all suggestions</button>
      <button type="submit" formaction="/builds/drafts/autofill" name="autofill_action" value="regenerate_all">Regenerate all suggestions</button>
      <button type="submit" formaction="/builds/drafts/autofill" name="autofill_action" value="apply_all">Apply all suggestions</button>
      <button type="submit" formaction="/builds/drafts/autofill" name="autofill_action" value="apply_all_empty">Apply all suggestions to empty fields (safe)</button>
    </div>
    {{if .SemanticSlots}}
    <details>
      <summary>Technik-Preview: Semantische Zielslots (intern)</summary>
      <table>
        <thead>
        <tr><th>Slot</th><th>Zuordnungen</th><th>Beispiele</th></tr>
        </thead>
        <tbody>
        {{range .SemanticSlots}}
        <tr>
          <td class="mono">{{.Slot}}</td>
          <td>{{.Count}}</td>
          <td class="mono">{{.Examples}}</td>
        </tr>
        {{end}}
        </tbody>
      </table>
    </details>
    {{end}}

    {{range .FieldSections}}
    <h3>{{.Title}}</h3>
    {{if .Description}}<p>{{.Description}}</p>{{end}}

    {{range .EditableGroups}}
    <h4>{{.Title}}</h4>
    <table>
      <thead>
      <tr><th>Field</th><th>Value</th><th>Sample</th></tr>
      </thead>
      <tbody>
      {{range .Fields}}
      <tr id="{{.AnchorID}}">
        <td>
          <input type="hidden" name="field_path_{{.Index}}" value="{{.Path}}">
          {{.DisplayLabel}}<br><span class="mono">{{.Path}}</span>
        </td>
        <td>
          <textarea name="field_value_{{.Index}}">{{.Value}}</textarea>
          {{if .SuggestedValue}}
          <div><small>Vorschlag: {{.SuggestedValue}}</small></div>
          {{if .SuggestionReason}}<div><small>Grund: {{.SuggestionReason}}</small></div>{{end}}
          {{if .SuggestionStatus}}<div><small>Status: {{.SuggestionStatus}}</small></div>{{end}}
          {{if .SuggestionSource}}<div><small>Quelle: {{.SuggestionSource}}</small></div>{{end}}
          <div>
            <button type="submit" formaction="/builds/drafts/autofill" name="autofill_action" value="apply_field::{{.Path}}">Apply</button>
            <button type="submit" formaction="/builds/drafts/autofill" name="autofill_action" value="regenerate_field::{{.Path}}">Regenerate</button>
          </div>
          {{else}}
          <div>
            <button type="submit" formaction="/builds/drafts/autofill" name="autofill_action" value="regenerate_field::{{.Path}}">Regenerate</button>
          </div>
          {{end}}
        </td>
        <td class="mono">{{.SampleValue}}</td>
      </tr>
      {{end}}
      </tbody>
    </table>
    {{end}}

    {{if .EditableFields}}
    <table>
      <thead>
      <tr><th>Field</th><th>Value</th><th>Sample</th></tr>
      </thead>
      <tbody>
      {{range .EditableFields}}
      <tr id="{{.AnchorID}}">
        <td>
          <input type="hidden" name="field_path_{{.Index}}" value="{{.Path}}">
          {{.DisplayLabel}}<br><span class="mono">{{.Path}}</span>
        </td>
        <td>
          <textarea name="field_value_{{.Index}}">{{.Value}}</textarea>
          {{if .SuggestedValue}}
          <div><small>Vorschlag: {{.SuggestedValue}}</small></div>
          {{if .SuggestionReason}}<div><small>Grund: {{.SuggestionReason}}</small></div>{{end}}
          {{if .SuggestionStatus}}<div><small>Status: {{.SuggestionStatus}}</small></div>{{end}}
          {{if .SuggestionSource}}<div><small>Quelle: {{.SuggestionSource}}</small></div>{{end}}
          <div>
            <button type="submit" formaction="/builds/drafts/autofill" name="autofill_action" value="apply_field::{{.Path}}">Apply</button>
            <button type="submit" formaction="/builds/drafts/autofill" name="autofill_action" value="regenerate_field::{{.Path}}">Regenerate</button>
          </div>
          {{else}}
          <div>
            <button type="submit" formaction="/builds/drafts/autofill" name="autofill_action" value="regenerate_field::{{.Path}}">Regenerate</button>
          </div>
          {{end}}
        </td>
        <td class="mono">{{.SampleValue}}</td>
      </tr>
      {{end}}
      </tbody>
    </table>
    {{end}}

    {{if .DisabledFields}}
    <table>
      <thead>
      <tr><th>Field</th><th>Status</th><th>Sample</th></tr>
      </thead>
      <tbody>
      {{range .DisabledFields}}
      <tr>
        <td>{{.DisplayLabel}}<br><span class="mono">{{.Path}}</span></td>
        <td>Erkannt, deaktiviert (MVP ohne Bildlogik)</td>
        <td class="mono">{{.SampleValue}}</td>
      </tr>
      {{end}}
      </tbody>
    </table>
    {{end}}
    {{end}}

    {{if eq (len .EditableFields) 0}}
    <p>No enabled text fields found for this template.</p>
    {{end}}

    <button type="submit" formaction="/builds/drafts">Save Draft</button>
    <button type="submit">Start Build</button>
  </form>
  {{if .AutofillFocusID}}
  <script>
    window.addEventListener("load", function () {
      var target = document.getElementById("{{.AutofillFocusID}}");
      if (target) {
        target.scrollIntoView({ block: "center" });
      }
    });
  </script>
  {{end}}
  {{end}}
</body>
</html>
{{end}}
