extends ../layouts/main
block content

  - var actionUrl = page ? `/admin/page/${page._id}` : `/admin/page`;

  form(method="POST", action= actionUrl).uk-form
    div(uk-grid).uk-grid-small
      div(class="uk-width-1-1 uk-width-2-3@m")
        .uk-margin
          label(for="content").uk-form-label Page body
          textarea(id="content", name="content", rows="4").uk-textarea= page ? page.content : undefined

      div(class="uk-width-1-1 uk-width-1-3@m")
        .uk-margin
          label(for="title").uk-form-label Page title
          input(id="title", name="title", type="text", placeholder= "Enter page title", value= page ? page.title : undefined).uk-input
        .uk-margin
          label(for="slug").uk-form-label URL slug
          -
            var pageSlug;
            if (page) {
              pageSlug = page.slug.split('-');
              pageSlug.pop();
              pageSlug = pageSlug.join('-');
            }
          input(id="slug", name="slug", type="text", placeholder= "Enter page URL slug", value= page ? pageSlug : undefined).uk-input
          .uk-text-small The slug is used in the link to the page https://#{site.domain}/page/#{page ? page.slug : 'your-slug-here'}
        div(uk-grid)
          .uk-width-auto
            button(type="submit").uk-button.dtp-button-primary= page ? 'Update page' : 'Create page'
        .uk-margin
          label(for="status").uk-form-label Status
          select(id="status", name="status").uk-select
            option(value="draft", selected= page ? page.status === 'draft' : true) Draft
            option(value="published", selected= page ? page.status === 'published' : false) Published
            option(value="archived", selected= page ? page.status === 'archived' : false) Archived
        
        fieldset
          legend Menu
          .uk-margin
            label(for="menu-icon").uk-form-label Menu item icon
            input(id="menu-icon", name="menuIcon", type="text", maxlength="80", placeholder="Enter icon class", value= page ? page.menu.icon : undefined).uk-input
            .uk-text-small Visit #[a(href="https://fontawesome.com/v5.15/icons?d=gallery&p=2&q=blog&m=free", target="_blank") FontAwesome] for a list of usable icons.
          .uk-margin
            label(for="menu-label").uk-form-label Menu item label
            input(id="menu-label", name="menuLabel", type="text", maxlength="80", placeholder="Enter label", value= page ? page.menu.label : undefined).uk-input
          .uk-margin
            label(for="menu-order").uk-form-label Menu item order
            input(id="menu-order", name="menuOrder", type="number", min="0", value= page ? page.menu.order : 0).uk-input
          if (Array.isArray(availablePages) && (availablePages.length > 0)) && !isParentPage
            .uk-margin
              label(for="menu-parent").uk-form-label Parent page
              select(id="menu-parent", name="parentPageId").uk-select
                if page && page.menu.parent
                  option(value= page.menu.parent._id)= page.menu.parent.title
                option(value= "none") --- Select parent page ---
                each menuPage in availablePages
                  if page && page.menu.parent && !page.menu.parent._id.equals(menuPage._id)
                    option(value= menuPage._id)= menuPage.title
                  else if !page || !page.menu.parent
                    option(value= menuPage._id)= menuPage.title

block viewjs
  script(src="/tinymce/tinymce.min.js")
  script.
    window.addEventListener('dtp-load', async ( ) => {
      const toolbarItems = [
        'undo redo',
        'formatselect visualblocks',
        'bold italic backcolor',
        'alignleft aligncenter alignright alignjustify',
        'bullist numlist outdent indent removeformat',
        'link image code',
        'help'
      ];
      const pluginItems = [
        'advlist', 'autolink', 'lists', 'link', 'image', 'charmap', 'print',
        'preview', 'anchor', 'searchreplace', 'visualblocks', 'code',
        'fullscreen', 'insertdatetime', 'media', 'table', 'paste', 'code',
        'help', 'wordcount',
      ]

      const editors = await tinymce.init({
        selector: 'textarea#content',
        height: 500,
        menubar: false,
        plugins: pluginItems.join(' '),
        toolbar: toolbarItems.join('|'),
        branding: false,
        images_upload_url: '/image/tinymce',
        image_class_list: [
          { title: 'Body Image', value: 'dtp-image-body' },
          { title: 'Title Image', value: 'dtp-image-title' },
        ],
        convert_urls: false,
        skin: "oxide-dark",
        content_css: "dark",        
      });

      window.dtp.app.editor = editors[0];
    });