<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title><%= isEdit ? 'Edit Documentation' : 'Create Documentation' %> &bull; <%= appName %></title>
  <%- headAssets %>
  <link rel="stylesheet" href="<%= asset('assets/css/docs-module.css') %>">
</head>
<body>

  <%- topNav %>

  <main class="admin-container">

    <% for (const f of flashes) { %>
      <div class="flash-alert <%= f.type %>">
        <span><%= f.message %></span>
      </div>
    <% } %>

    <div class="page-header">
      <div>
        <h1 class="page-title"><%= isEdit ? 'Edit Documentation' : 'Create Documentation' %></h1>
        <p class="page-subtitle"><%= isEdit ? 'Refine content, formatting, cover, or metadata' : 'Write a structured technical or product document' %></p>
      </div>
      <div class="page-actions">
        <a href="<%= isEdit ? url('documentation/' + doc.slug) : url('documentation') %>" class="btn btn-secondary">&larr; Back</a>
      </div>
    </div>

    <div id="docEditorAlert" class="flash-alert error is-msg is-hidden" role="alert">
      <span id="docEditorAlertText"></span>
    </div>

    <form
      id="docEditorForm"
      data-mode="<%= isEdit ? 'edit' : 'create' %>"
      data-doc-id="<%= isEdit ? Number(doc.id) : '' %>"
      data-save-url="<%= isEdit ? url('api/documentations/' + Number(doc.id)) : url('api/documentations') %>"
      data-upload-url="<%= url('api/documentations/upload-image') %>"
      data-csrf="<%= csrfToken %>"
      novalidate
    >
      <div class="doc-editor-layout">

        <!-- Left Column: Word / Blog Canvas Editor -->
        <div class="form-card doc-editor-main">
          <div class="form-group">
            <label class="form-label" for="docTitleInput">Document Title <span class="required">*</span></label>
            <input
              type="text"
              id="docTitleInput"
              name="title"
              class="form-control doc-title-input"
              required
              maxlength="255"
              value="<%= isEdit && doc ? doc.title : '' %>"
              placeholder="e.g. PRD: Modul Documentation Report"
            >
          </div>

          <div class="doc-editor-shell" id="docEditorShell">
            <!-- Formatting Toolbar -->
            <div class="doc-toolbar" id="docToolbar" role="toolbar" aria-label="Rich text formatting">
              <div class="doc-tb-group">
                <button type="button" class="doc-tb-btn" data-action="undo" title="Undo (Ctrl+Z)" aria-label="Undo">&#x21B6;</button>
                <button type="button" class="doc-tb-btn" data-action="redo" title="Redo (Ctrl+Y)" aria-label="Redo">&#x21B7;</button>
              </div>

              <div class="doc-tb-group">
                <select id="docBlockSelect" class="doc-tb-select" title="Heading / Paragraph" aria-label="Heading">
                  <option value="p">Paragraph</option>
                  <option value="h1">Heading 1</option>
                  <option value="h2">Heading 2</option>
                  <option value="h3">Heading 3</option>
                  <option value="h4">Heading 4</option>
                </select>

                <select id="docFontSelect" class="doc-tb-select" title="Font Family" aria-label="Font Family">
                  <option value="sans">Inter</option>
                  <option value="display">Montserrat</option>
                  <option value="mono">Monospace</option>
                  <option value="serif">Serif</option>
                </select>

                <select id="docSizeSelect" class="doc-tb-select" title="Font Size" aria-label="Font Size">
                  <option value="14">Normal (14px)</option>
                  <option value="12">Small (12px)</option>
                  <option value="16">Medium (16px)</option>
                  <option value="18">Large (18px)</option>
                  <option value="24">Huge (24px)</option>
                  <option value="32">Display (32px)</option>
                </select>
              </div>

              <div class="doc-tb-group">
                <button type="button" class="doc-tb-btn" data-cmd="bold" title="Bold (Ctrl+B)" aria-label="Bold"><strong>B</strong></button>
                <button type="button" class="doc-tb-btn" data-cmd="italic" title="Italic (Ctrl+I)" aria-label="Italic"><em>I</em></button>
                <button type="button" class="doc-tb-btn" data-cmd="underline" title="Underline (Ctrl+U)" aria-label="Underline"><u>U</u></button>
                <button type="button" class="doc-tb-btn" data-cmd="strike" title="Strikethrough" aria-label="Strikethrough"><s>S</s></button>

                <div class="doc-tb-pop-wrap">
                  <button type="button" class="doc-tb-btn" id="docTextColorBtn" title="Text Color" aria-label="Text Color" aria-expanded="false">A</button>
                  <div class="doc-swatch-pop is-hidden" id="docTextColorMenu" role="menu">
                    <button type="button" class="doc-swatch doc-swatch-default" data-color="default" title="Default"></button>
                    <button type="button" class="doc-swatch doc-swatch-muted" data-color="muted" title="Muted Brown"></button>
                    <button type="button" class="doc-swatch doc-swatch-gold" data-color="gold" title="Gold"></button>
                    <button type="button" class="doc-swatch doc-swatch-red" data-color="red" title="Crimson"></button>
                    <button type="button" class="doc-swatch doc-swatch-green" data-color="green" title="Forest Green"></button>
                    <button type="button" class="doc-swatch doc-swatch-blue" data-color="blue" title="Ocean Blue"></button>
                    <button type="button" class="doc-swatch doc-swatch-amber" data-color="amber" title="Amber"></button>
                    <button type="button" class="doc-swatch doc-swatch-purple" data-color="purple" title="Violet"></button>
                  </div>
                </div>

                <div class="doc-tb-pop-wrap">
                  <button type="button" class="doc-tb-btn" id="docBgColorBtn" title="Background Highlight" aria-label="Background Highlight" aria-expanded="false">HL</button>
                  <div class="doc-swatch-pop is-hidden" id="docBgColorMenu" role="menu">
                    <button type="button" class="doc-swatch doc-swatch-bg-none" data-bg="none" title="Clear Highlight">&times;</button>
                    <button type="button" class="doc-swatch doc-swatch-bg-yellow" data-bg="yellow" title="Yellow"></button>
                    <button type="button" class="doc-swatch doc-swatch-bg-gold" data-bg="gold" title="Soft Gold"></button>
                    <button type="button" class="doc-swatch doc-swatch-bg-green" data-bg="green" title="Soft Green"></button>
                    <button type="button" class="doc-swatch doc-swatch-bg-blue" data-bg="blue" title="Soft Blue"></button>
                    <button type="button" class="doc-swatch doc-swatch-bg-red" data-bg="red" title="Soft Red"></button>
                    <button type="button" class="doc-swatch doc-swatch-bg-gray" data-bg="gray" title="Warm Gray"></button>
                  </div>
                </div>
              </div>

              <div class="doc-tb-group">
                <button type="button" class="doc-tb-btn" data-align="left" title="Align Left" aria-label="Align Left">&#x2261;</button>
                <button type="button" class="doc-tb-btn" data-align="center" title="Align Center" aria-label="Align Center">&#x25A4;</button>
                <button type="button" class="doc-tb-btn" data-align="right" title="Align Right" aria-label="Align Right">&#x2262;</button>
                <button type="button" class="doc-tb-btn" data-align="justify" title="Justify" aria-label="Justify">&#x2630;</button>
                <button type="button" class="doc-tb-btn" data-list="ul" title="Bulleted List" aria-label="Bulleted List">&bull; List</button>
                <button type="button" class="doc-tb-btn" data-list="ol" title="Numbered List" aria-label="Numbered List">1. List</button>
                <button type="button" class="doc-tb-btn" data-indent="-1" title="Outdent" aria-label="Outdent">&larr;</button>
                <button type="button" class="doc-tb-btn" data-indent="1" title="Indent" aria-label="Indent">&rarr;</button>
                <button type="button" class="doc-tb-btn" data-action="blockquote" title="Blockquote" aria-label="Blockquote">&ldquo;&rdquo;</button>
              </div>

              <div class="doc-tb-group">
                <button type="button" class="doc-tb-btn" id="docOpenLinkModalBtn" title="Insert or Edit Hyperlink" aria-label="Insert Link">Link</button>
                <button type="button" class="doc-tb-btn" id="docOpenImageModalBtn" title="Upload or Insert Image" aria-label="Insert Image">Image</button>
                <button type="button" class="doc-tb-btn" data-action="codeblock" title="Insert Code Block" aria-label="Code Block">&lt;/&gt;</button>
                <button type="button" class="doc-tb-btn" data-action="hr" title="Horizontal Divider" aria-label="Horizontal Divider">&mdash;</button>
                <button type="button" class="doc-tb-btn" data-action="clear" title="Clear Formatting" aria-label="Clear Formatting">Tx</button>
              </div>
            </div>

            <!-- Selected Image Context Toolbar -->
            <div class="doc-context-bar is-hidden" id="docImageBar">
              <span class="doc-context-label">Image Size:</span>
              <button type="button" class="btn btn-secondary btn-sm" data-img-w="25">25%</button>
              <button type="button" class="btn btn-secondary btn-sm" data-img-w="50">50%</button>
              <button type="button" class="btn btn-secondary btn-sm" data-img-w="75">75%</button>
              <button type="button" class="btn btn-secondary btn-sm" data-img-w="100">100%</button>
              <span class="doc-context-sep"></span>
              <span class="doc-context-label">Align:</span>
              <button type="button" class="btn btn-secondary btn-sm" data-img-align="left">Left</button>
              <button type="button" class="btn btn-secondary btn-sm" data-img-align="center">Center</button>
              <button type="button" class="btn btn-secondary btn-sm" data-img-align="right">Right</button>
              <button type="button" class="btn btn-danger btn-sm" id="docRemoveSelImgBtn">Remove</button>
            </div>

            <!-- Selected Code Block Context Toolbar -->
            <div class="doc-context-bar is-hidden" id="docCodeBar">
              <span class="doc-context-label">Code Language:</span>
              <select id="docCodeLangSelect" class="doc-tb-select" aria-label="Code block language">
                <option value="text">Plain Text</option>
                <option value="json">JSON</option>
                <option value="sql">SQL</option>
                <option value="javascript">JavaScript</option>
                <option value="bash">Bash / Shell</option>
                <option value="html">HTML / XML</option>
              </select>
              <button type="button" class="btn btn-secondary btn-sm" id="docExitCodeBtn">Insert Paragraph Below</button>
            </div>

            <!-- Editable Document Paper Canvas -->
            <div class="doc-editor-paper">
              <div
                id="docEditorCanvas"
                class="doc-editor-canvas doc-content"
                contenteditable="true"
                role="textbox"
                aria-multiline="true"
                aria-label="Document content editor"
                data-placeholder="Start writing documentation here... Use the toolbar above for headings, colors, images, links, or code blocks."
              ><%- isEdit && doc ? doc.content_html : '<p><br></p>' %></div>
            </div>
          </div>
        </div>

        <!-- Right Column: Document Settings Panel -->
        <aside class="doc-editor-sidebar">
          <div class="form-card doc-settings-card">
            <div class="doc-settings-actions">
              <button type="button" id="docPreviewBtn" class="btn btn-secondary">Preview</button>
              <button type="submit" id="docSubmitBtn" class="btn btn-primary"><%= isEdit ? 'Update' : 'Publish' %></button>
            </div>

            <div class="form-group">
              <label class="form-label" for="docCategorySelect">Category Documentation <span class="required">*</span></label>
              <select id="docCategorySelect" name="category_id" class="form-control" required>
                <option value="">Select category...</option>
                <% for (const cat of categories) { %>
                  <option
                    value="<%= Number(cat.id) %>"
                    data-code="<%= cat.code %>"
                    data-name="<%= cat.name %>"
                    <%= isEdit && doc && Number(doc.category_id) === Number(cat.id) ? 'selected' : '' %>
                  >
                    <%= cat.code %> (<%= cat.name %>)<%= Number(cat.is_active) === 1 ? '' : ' [Inactive]' %>
                  </option>
                <% } %>
              </select>
            </div>

            <div class="form-group">
              <label class="form-label" for="docCoverInput">Cover / Thumbnail Image</label>
              <input type="hidden" id="docRemoveCoverFlag" name="remove_cover" value="0">

              <div class="doc-cover-preview-box" id="docCoverPreviewBox">
                <% if (isEdit && doc && doc.cover_image) { %>
                  <img id="docCoverPreviewImg" src="<%= url(doc.cover_image) %>" alt="Cover preview">
                  <div id="docCoverFallbackPreview" class="doc-cover-fallback is-hidden">
                    <span class="doc-cover-code" id="docCoverFallbackCode"><%= doc.category_code %></span>
                    <span class="doc-cover-sub" id="docCoverFallbackName"><%= doc.category_name %></span>
                  </div>
                <% } else { %>
                  <img id="docCoverPreviewImg" src="" alt="Cover preview" class="is-hidden">
                  <div id="docCoverFallbackPreview" class="doc-cover-fallback">
                    <span class="doc-cover-code" id="docCoverFallbackCode"><%= isEdit && doc ? doc.category_code : 'DOC' %></span>
                    <span class="doc-cover-sub" id="docCoverFallbackName"><%= isEdit && doc ? doc.category_name : 'Auto Default Cover' %></span>
                  </div>
                <% } %>
              </div>

              <input
                type="file"
                id="docCoverInput"
                name="cover_image"
                accept="image/png,image/jpeg,image/webp"
                class="form-control doc-cover-file"
              >
              <div class="doc-cover-actions">
                <span class="form-help">Optional. JPG, PNG, WEBP (max 100MB).</span>
                <button
                  type="button"
                  id="docRemoveCoverBtn"
                  class="btn btn-secondary btn-sm<%= isEdit && doc && doc.cover_image ? '' : ' is-hidden' %>"
                >
                  Remove Cover
                </button>
              </div>
            </div>

            <div class="form-group form-group-last">
              <label class="form-label" for="docTagsInput">Tags / Labels</label>
              <input
                type="text"
                id="docTagsInput"
                name="tags"
                class="form-control"
                maxlength="255"
                value="<%= isEdit && doc ? doc.tags : '' %>"
                placeholder="e.g. Quinos, SAP, POS, API"
              >
              <div class="form-help">Separate multiple tags with commas.</div>
            </div>
          </div>

          <% if (isEdit && doc) { %>
            <div class="form-card doc-info-card">
              <h2 class="card-heading">Document Info</h2>
              <table class="kv-table">
                <tr>
                  <td class="kv-key">Author</td>
                  <td class="kv-val kv-strong"><%= doc.author_name %></td>
                </tr>
                <tr>
                  <td class="kv-key">Created</td>
                  <td class="kv-val kv-soft"><%= doc.created_at_label %></td>
                </tr>
                <tr>
                  <td class="kv-key">Updated</td>
                  <td class="kv-val kv-soft"><%= doc.updated_at_label %></td>
                </tr>
                <tr>
                  <td class="kv-key">Views</td>
                  <td class="kv-val kv-soft"><%= Number(doc.views_count || 0) %> x dilihat</td>
                </tr>
              </table>
            </div>
          <% } %>
        </aside>

      </div>
    </form>

  </main>

  <%- shellEnd %>

  <!-- Hyperlink Modal -->
  <div class="modal-overlay" id="docLinkModal">
    <div class="modal-box" role="dialog" aria-modal="true" aria-labelledby="docLinkModalTitle">
      <h3 class="modal-title" id="docLinkModalTitle">Insert / Edit Hyperlink</h3>
      <div class="form-group form-group-first">
        <label class="form-label" for="docLinkUrlInput">Destination URL <span class="required">*</span></label>
        <input type="text" id="docLinkUrlInput" class="form-control" placeholder="https://example.portaltug.com/docs">
      </div>
      <div class="form-group form-group-modal-last">
        <label class="form-label" for="docLinkTextInput">Link Label</label>
        <input type="text" id="docLinkTextInput" class="form-control" placeholder="Leave blank to use selected text or URL">
      </div>
      <div class="modal-actions">
        <button type="button" class="btn btn-danger is-hidden" id="docRemoveLinkBtn">Unlink</button>
        <button type="button" class="btn btn-secondary" data-doc-modal-close>Cancel</button>
        <button type="button" class="btn btn-primary" id="docApplyLinkBtn">Apply Link</button>
      </div>
    </div>
  </div>

  <!-- Image Upload / Embed Modal -->
  <div class="modal-overlay" id="docImageModal">
    <div class="modal-box" role="dialog" aria-modal="true" aria-labelledby="docImageModalTitle">
      <h3 class="modal-title" id="docImageModalTitle">Insert Image</h3>
      <p class="modal-desc">Upload an image from your computer or enter an existing portal image path.</p>

      <div id="docImageModalError" class="flash-alert error is-msg is-hidden" role="alert">
        <span id="docImageModalErrorText"></span>
      </div>

      <div class="form-group">
        <label class="form-label" for="docInlineImgFile">Upload Image File (PNG, JPG, WEBP, SVG &mdash; max 100MB)</label>
        <input type="file" id="docInlineImgFile" accept="image/png,image/jpeg,image/webp,image/svg+xml" class="form-control">
      </div>

      <div class="form-group">
        <label class="form-label" for="docInlineImgPath">Or Portal Image Path</label>
        <input type="text" id="docInlineImgPath" class="form-control" placeholder="/assets/uploads/docs/...">
      </div>

      <div class="form-group form-group-modal-last">
        <label class="form-label" for="docInlineImgAlt">Alt Text / Caption</label>
        <input type="text" id="docInlineImgAlt" class="form-control" placeholder="Short description of the diagram or screenshot">
      </div>

      <div class="modal-actions">
        <button type="button" class="btn btn-secondary" data-doc-modal-close>Cancel</button>
        <button type="button" class="btn btn-primary" id="docApplyImageBtn">Insert Image</button>
      </div>
    </div>
  </div>

  <!-- Document Preview Modal -->
  <div class="modal-overlay" id="docPreviewModal">
    <div class="modal-box doc-preview-modal-box" role="dialog" aria-modal="true" aria-labelledby="docPreviewModalTitle">
      <div class="doc-preview-modal-head">
        <h3 class="modal-title" id="docPreviewModalTitle">Document Preview</h3>
        <button type="button" class="btn btn-secondary btn-sm" data-doc-modal-close>Close</button>
      </div>
      <div class="doc-preview-modal-scroll">
        <div class="doc-reader-badges">
          <span class="cat-pill" id="docPreviewCatBadge">-</span>
        </div>
        <h2 class="doc-preview-doc-title" id="docPreviewDocTitle">Untitled Document</h2>
        <div class="doc-reader-tags" id="docPreviewTags"></div>
        <div class="doc-reader-cover is-hidden" id="docPreviewCoverWrap">
          <img id="docPreviewCoverImg" src="" alt="Cover Preview">
        </div>
        <div class="doc-content doc-reader-body" id="docPreviewBody"></div>
      </div>
    </div>
  </div>

  <script src="<%= asset('assets/js/docs-editor.js') %>"></script>
</body>
</html>
