jQuery(document).ready(function ($) {
  $('#lkn-load-more').on('click', function () {
    const button = $(this)
    let offset = parseInt(button.attr('lknwp-offset'))

    const settings = {
      number: parseInt(button.attr('lknwp-number')),
      order: button.attr('lknwp-order'),
      category: button.attr('lknwp-category'),
      thumbnail_size: button.attr('lknwp-image')
    }

    let templateName = ''
    if (button.attr('lknwp-template') && button.attr('lknwp-template') !== 'none') {
      templateName = button.attr('lknwp-template')
    }

    button.text(lknwp_settings.loading)
    button.prop('disabled', true)
    button.css('cursor', 'not-allowed')

    $.ajax({
      url: '/wp-json/lknwpcrowdfunding/v1/load-more-campaigns',
      type: 'POST',
      data: {
        offset,
        settings
      },
      success: function (response) {
        if (response.campaigns) {
          const newCampaigns = response.campaigns
          newCampaigns.forEach(function (campaign) {
            let permalink = campaign.permalink // O permalink original

            if (templateName && templateName !== 'none') {
              // Recupera o base do produto e o slug do produto

              let baseProduct = button.attr('lknwp-base-product') // Base do produto no botão
              baseProduct = baseProduct.replace(/^\/+|\/+$/g, '')
              const completeBaseUrl = permalink.split(baseProduct)
              if (completeBaseUrl.length >= 1) {
                const baseUrl = completeBaseUrl[0].endsWith('/') ? completeBaseUrl[0].slice(0, -1) : completeBaseUrl[0]
                const product = completeBaseUrl[1].startsWith('/') ? completeBaseUrl[1].slice(1) : completeBaseUrl[1]
                permalink = `${baseUrl}/${baseProduct}/${templateName}/${product}`
              }
            }

            const newCard = $('.lknwp-card-container').first().clone()

            newCard.find('.lknwp-thumbnail img').remove()
            newCard.find('.lknwp-thumbnail').prepend(campaign.thumbnail)
            newCard.find('.lknwp-elementor-header-button').attr('href', permalink)

            newCard.find('.lknwp-title').attr('href', campaign.permalink).text(campaign.title)

            newCard.find('.lknwp-link-author').attr('href', campaign.author_url).text(campaign.author_name)

            newCard.find('.lknwp-content-description').text(campaign.excerpt)

            newCard.find('.lknwp-content-location').text(campaign.location)

            newCard.find('.lknwp-content-progress').text(campaign.raised_percent + '%')

            newCard.find('.lknwp-raised-bar div').css('width', campaign.raised_percent + '%')

            newCard.find('.lknwp-content-goal').html(campaign.funding_goal)

            newCard.find('.lknwp-content-fund_raised').html(campaign.fund_raised)

            newCard.find('.lknwp-content-days').text(campaign.days_remaining)

            newCard.find('.lknwp-elementor-bottom-button').attr('href', permalink)

            $('.lknwp-container').append(newCard)
          })

          offset += settings.number
          button.attr('lknwp-offset', offset)
          button.text(lknwp_settings.load)
        } else {
          button.text(lknwp_settings.noMore).hide()
        }
      },
      error: function () {
        button.text(lknwp_settings.error)
      },
      complete: function () {
        button.prop('disabled', false)
        button.css('cursor', 'pointer')
      }
    })
  })
})
