extends layout.pug

block title
  h1
    .level
      .level-left
        .level-item
          button.button.is-rounded(onclick='this.firstChild.firstChild.classList.add("fa-pulse"); location.reload()')
            span.icon
              i.fas.fa-sync-alt
        .level-item
          span  #{config.siteTitle} - #{title} 

block content
  script(type='data/user_id')= req.user._id
  if global._CC.config.wishlist.table
    .box
      table.table.has-mobile-cards
        thead
          th #
          th(style='width: 15%;') Image
          th(style='width: 25%;') Name
          th(style='width: 50%;') Note
          th(style='width: 10%;') Price
          th Edit Item
          th Added By
          if req.params.user === req.user._id
            th Move Top
            th Move Up
            th Move Down
          else
            th Pledge
          th Delete
        tbody
          each item, index in wishlist
            if req.user._id === item.addedBy || req.params.user !== req.user._id
              tr(id=item.id)
                td.rank= index + 1
                td
                  figure(style='width: 100%; margin: 0;')
                    img(src=item.image, style='width: 100%; max-height: 20em; object-fit: contain;')
                if item.url
                  td.ugc(data-label='Name')
                    a(
                      href=item.url,
                      rel='noopener noreferrer',
                      target='_blank'
                    )= (item.name ? item.name : item.url)
                else
                  td.ugc(data-label='Name')= item.name
                if _CC.config.wishlist.note.markdown
                  td.ugc(data-label='Note')
                    div!= item.note
                else
                  td.ugc(data-label='Note')= item.note
                td.ugc(data-label='Price')= item.price
                td(data-label='Edit Item')
                  form.inline(method='GET', action=`${_CC.config.base}wishlist/${req.params.user}/note/${item.id}`)
                    .field.inline
                      .control.inline
                        button.button.is-text(
                          type='submit',
                          style='text-decoration: none;'
                          disabled=item.addedBy !== req.user._id
                        )
                          span.icon
                            i.far.fa-edit
                td.ugc(data-label='Added By')= item.addedBy
                if req.params.user === req.user._id
                  td(data-label='Move Item Top')
                    form.topForm.inline(method='POST', action=`${_CC.config.base}wishlist/${req.params.user}/move/top/${item.id}`)
                      .field.inline
                        .control.inline
                          button.button.is-text(
                            type='submit',
                            style='text-decoration: none;',
                            disabled=index === firstCanSee
                          )
                            span.icon
                              i.fas.fa-angle-double-up
                  td(data-label='Move Item Up')
                    form.upForm.inline(method='POST', action=`${_CC.config.base}wishlist/${req.params.user}/move/up/${item.id}`)
                      .field.inline
                        .control.inline
                          button.button.is-text(
                            type='submit',
                            style='text-decoration: none;',
                            disabled=index === firstCanSee
                          )
                            span.icon
                              i.fas.fa-arrow-up
                  td(data-label='Move Item Down')
                    form.downForm.inline(method='POST', action=`${_CC.config.base}wishlist/${req.params.user}/move/down/${item.id}`)
                      .field.inline
                        .control.inline
                          button.button.is-text(
                            type='submit',
                            style='text-decoration: none;',
                            disabled=index === lastCanSee
                          )
                            span.icon
                              i.fas.fa-arrow-down
                else
                  td(data-label='Pledge')
                    if req.params.user !== req.user._id && !item.pledgedBy
                      form.inline(method='POST', action=`${_CC.config.base}wishlist/${req.params.user}/pledge/${item.id}`)
                        .field.inline
                          .control.inline
                            input.inline.button.is-primary(type='submit' value='Pledge')
                    if item.pledgedBy === req.user._id
                      form.inline(method='POST', action=`${_CC.config.base}wishlist/${req.params.user}/unpledge/${item.id}`)
                        .field.inline
                          .control.inline
                            input.inline.button(type='submit' value='Unpledge')
                    if item.pledgedBy && item.pledgedBy !== req.user._id
                      span.ugc Pledged for by #{item.pledgedBy}
                td(data-label='Delete Item')
                  form.inline(
                    method='POST',
                    action=`${_CC.config.base}wishlist/${req.params.user}/${req.user._id === req.params.user ? 'remove' : 'unpledge'}/${item.id}`
                  )
                    .field.inline
                      .control.inline
                        button.button.is-text(
                          type='submit',
                          style='text-decoration: none;',
                          disabled=item.addedBy !== req.user._id
                        )
                          span.icon
                            i.fas.fa-trash

  else
    each item, index in wishlist
      if req.user._id === item.addedBy || req.params.user !== req.user._id
        .box
          if item.price
            .tag
              i.fas.fa-tag(style='margin-right: .5em;')
              span= item.price
          span.overflowWrap
            if item.price
              span= ' '
            if item.url
              a(
                href=item.url,
                rel='noopener noreferrer',
                target='_blank'
              )= (item.name ? item.name : item.url)
            else
              span= item.name
          hr
          if item.note || item.image
            .columns
              if item.image
                .column
                  figure(style='width: 100%; padding: 2em;')
                    img(src=item.image, style='width: 100%;')
              .column.is-three-quarters
                if item.note
                  textarea.textarea.has-fixed-size(
                    style='height: 100%;'
                    readonly
                  )= item.note
          span.overflowWrap Added by: #{item.addedBy}
          hr
          div
            if req.params.user !== req.user._id && !item.pledgedBy
              form.inline(method='POST', action=`${_CC.config.base}wishlist/${req.params.user}/pledge/${item.id}`)
                .field.inline
                  .control.inline
                    input.inline.button.is-primary(type='submit' value='Pledge')
            if item.pledgedBy === req.user._id
              form.inline(method='POST', action=`${_CC.config.base}wishlist/${req.params.user}/unpledge/${item.id}`)
                .field.inline
                  .control.inline
                    input.inline.button(type='submit' value='Unpledge')
            if req.user._id === req.params.user
              form.inline(method='POST', action=`${_CC.config.base}wishlist/${req.params.user}/remove/${item.id}`)
                .field.inline
                  .control.inline
                    input.inline.button.is-warning(type='submit' value='Remove')
            if req.user._id === req.params.user || req.user._id === item.addedBy
              form.inline(method='GET', action=`${_CC.config.base}wishlist/${req.params.user}/note/${item.id}`)
                .field.inline
                  .control.inline
                    input.inline.button(type='submit', value='Edit item')
              if index !== firstCanSee && req.user._id === req.params.user
                form.inline(method='POST', action=`${_CC.config.base}wishlist/${req.params.user}/move/up/${item.id}`)
                  .field.inline
                    .control.inline
                      input.inline.button(type='submit' value='Move item up')
              if index !== lastCanSee && req.user._id === req.params.user
                form.inline(method='POST', action=`${_CC.config.base}wishlist/${req.params.user}/move/down/${item.id}`)
                  .field.inline
                    .control.inline
                      input.inline.button(type='submit' value='Move item down')
  form(method='POST')
    .field
      label.label Item URL or Name (<a href="/supported-sites">Supported Sites</a>)
      .control.has-icons-left
        input.input(
          type='text',
          name='itemUrlOrName',
          placeholder='https://www.amazon.com/dp/B00ZV9RDKK'
        )
        span.icon.is-small.is-left
          i.fas.fa-gift
    .field
      label.label Note
      .control
        textarea.textarea(
          name='note',
          placeholder='Optional'
        )
    .field
      .control
        input.button(type='submit' value=(req.user._id === req.params.user ? 'Add item to wishlist' : 'Pledge item'))
  script(src=`${_CC.config.base}js/wishlist.js`)

block print
  h1 #{req.params.user}'s Wishlist
  .print-gifts
    each item, index in wishlist.filter(item => item.addedBy === req.params.user)
      .print-gift(style='page-break-inside: avoid;')
        if index > 0
          hr(style='margin-top: .5em; margin-bottom: .5em; background-color: black;')
        if item.price
          .tag
            i.fas.fa-tag(style='margin-right: .5em;')
            span= item.price
          span= ' '
        span.is-size-6.ugc= item.name
        span= ' '
        span.ugc.has-text-grey= item.url
        span.is-block Added by: #{item.addedBy}
        if item.note
          .box
            if _CC.config.wishlist.note.markdown
              span.is-block.ugc!= item.note
            else
              span.is-block.ugc= item.note