<?php if ( ! defined( 'ABSPATH' ) ) exit; ?>

<?php if (!empty($campaigns)) : ?>
<div class="lknwp-container lknwp-elementor-gap">
    <?php foreach ($campaigns as $campaign) : ?>
    <?php
            // Recupera a base da URL do produto configurada no WooCommerce
            $product_base = get_option('woocommerce_permalinks')['product_base'];

        // Certifique-se de que o product_base não tenha barras extras no começo ou final
        $product_base = trim($product_base, '/'); // Remove as barras iniciais e finais

        $product_url = explode($product_base, $campaign['permalink']);

        // Verifica se o valor de lkn_template_page não é 'none'
        if ($settings['lkn_template_page'] !== 'none') {
            $custom_url = $product_url[0] . $product_base . '/' . $settings['lkn_template_page'] . $product_url[1];

            $campaign['permalink'] = $custom_url;
        }
        ?>
    <div
        class="lknwp-card-container lknwp-elementor-width lknwp-elementor-height"
        style="<?php echo esc_attr($mainBackgroundColor); ?>"
    >
        <?php if ($settings['show_image'] == 'yes'): ?>
        <div class="lknwp-thumbnail lknwp-header-button">
            <?php echo wp_kses_post($campaign['thumbnail']); ?>

            <?php if ($settings['show_header_button'] == 'yes'): ?>
            <div class="overlay-lkn">
                <div
                    class="lknwp-button-container"
                    style="padding: 5px; <?php echo esc_attr(implode(' ', $buttonsSettings["lkn_header_button_container"])) ?>"
                >
                    <a
                        class="<?php echo esc_attr($buttonsSettings["lkn_header_button_class"]) ?> lknwp-button lknwp-elementor-header-button"
                        href="<?php echo esc_url($campaign['permalink']); ?>"
                        style="<?php echo esc_attr(implode(' ', $buttonsSettings["lkn_header_button_styles"])) ?>"
                    >
                        <?php echo esc_html($buttonsSettings["header_button_text"]) ?>
                    </a>
                </div>
            </div>
            <?php endif; ?>
        </div>
        <?php endif; ?>

        <div class="lknwp-content">
            <?php if ($settings['show_title'] == 'yes'): ?>
            <div
                class="lnkwp-title-container"
                style="<?php echo esc_attr(implode(' ', $titleSettings["lkn_title_container"])) ?>"
            >
                <h4><a
                        class="<?php echo esc_attr($titleSettings["lkn_title_class"]) ?>"
                        style="<?php echo esc_attr(implode(' ', $titleSettings["lkn_title_styles"])) ?>"
                        href="<?php echo esc_url($campaign['permalink']); ?>"
                    >
                        <?php echo esc_html($campaign['title']); ?>
                    </a>
                </h4>

                <?php if ($settings['show_author'] == 'yes'): ?>
                <div
                    style="<?php echo esc_attr(implode(' ', $authorSettings["lkn_author_container"])) ?>">
                    <p
                        class="<?php echo esc_attr($authorSettings["lkn_author_class"]) ?>"
                        style="<?php echo esc_attr(implode(' ', $authorSettings["lkn_author_styles"])) ?>"
                    >
                        <?php esc_html_e('by', 'link-nacional-wp-crowdfunding-extension'); ?>
                        <a
                            href="<?php echo esc_url($campaign['author_url']); ?>"
                            class="<?php echo esc_attr($authorSettings["lkn_link_author_class"]) ?>"
                            style="<?php echo esc_attr(implode(' ', $authorSettings["lkn_link_author_styles"])) ?>"
                        >
                            <?php echo esc_html($campaign['author_name']); ?>
                        </a>
                    </p>
                </div>
                <?php endif; ?>
            </div>
            <?php endif; ?>

            <?php if ($settings['show_description'] == 'yes'): ?>
            <div
                class="lnkwp-meta-container"
                style="<?php echo esc_attr(implode(' ', $descriptionSettings["lkn_description_container"])) ?>"
            >
                <?php if ($settings['show_label'] == 'yes'): ?>
                <strong
                    class="<?php echo esc_attr($descriptionSettings["lkn_description_class"]) ?>"
                    style="<?php echo esc_attr(implode(' ', $descriptionSettings["lkn_description_styles"])) ?>"
                >
                    <?php echo esc_html_e('Description:', 'link-nacional-wp-crowdfunding-extension') ?>
                </strong>
                <?php endif; ?>
                <p
                    class="<?php echo esc_attr($descriptionSettings["lkn_content_description_class"]) ?>"
                    style="<?php echo esc_attr(implode(' ', $descriptionSettings["lkn_content_description_styles"])) ?>"
                >
                    <?php echo esc_html($campaign['excerpt']); ?>
                </p>
            </div>
            <?php endif; ?>

            <?php if ($settings['show_location'] == 'yes'): ?>
            <div
                class="lnkwp-meta-container"
                style="<?php echo esc_attr(implode(' ', $locationSettings["lkn_location_container"])) ?>"
            >
                <strong
                    class="<?php echo esc_attr($locationSettings["lkn_location_class"]) ?>"
                    style="<?php echo esc_attr(implode(' ', $locationSettings["lkn_location_styles"])) ?>"
                ><?php echo esc_html_e('Location:', 'link-nacional-wp-crowdfunding-extension'); ?></strong>
                <p
                    class="<?php echo esc_attr($locationSettings["lkn_content_location_class"]) ?>"
                    style="<?php echo esc_attr(implode(' ', $locationSettings["lkn_content_location_styles"])) ?>"
                >
                    <?php echo esc_html($campaign['location']); ?>
                </p>
            </div>
            <?php endif; ?>

            <?php if ($settings['show_progress'] == 'yes'): ?>
            <div
                class="lnkwp-meta-container"
                style="margin: 10px 0px 20px 0px; <?php echo esc_attr(implode(' ', $progressSettings["lkn_progress_container"])) ?>"
            >
                <strong
                    class="<?php echo esc_attr($progressSettings["lkn_progress_class"]) ?>"
                    style="<?php echo esc_attr(implode(' ', $progressSettings["lkn_progress_styles"])) ?>"
                >
                    <?php echo esc_html_e('Progress:', 'link-nacional-wp-crowdfunding-extension'); ?>
                </strong>


                <div
                    class="lknwp-raised-bar"
                    style="<?php echo esc_attr(implode(' ', $progressSettings["progress_background_inside_bar"])) ?>"
                >
                    <p
                        class="<?php echo esc_attr($progressSettings["lkn_content_progress_class"]) ?>"
                        style="<?php echo esc_attr(implode(' ', $progressSettings["lkn_content_progress_styles"])) ?>"
                    >
                        <?php echo esc_html($campaign['raised_percent']); ?>%
                    </p>
                    <div
                        style="width: <?php echo esc_attr(min($campaign['raised_percent'], 100)); ?>%; <?php echo esc_attr(implode(' ', $progressSettings["progress_background_ouside_bar"])); ?>">
                    </div>
                </div>
            </div>
            <?php endif; ?>


            <?php if ($settings['show_goal'] == 'yes'): ?>
            <div
                class="lnkwp-meta-container"
                style="<?php echo esc_attr(implode(' ', $goalSettings["lkn_goal_container"])) ?>"
            >
                <strong
                    class="<?php echo esc_attr($goalSettings["lkn_goal_class"]) ?>"
                    style="<?php echo esc_attr(implode(' ', $goalSettings["lkn_goal_styles"])) ?>"
                >
                    <?php echo esc_html_e('Funding Goal:', 'link-nacional-wp-crowdfunding-extension'); ?>
                </strong>
                <p
                    class="<?php echo esc_attr($goalSettings["lkn_content_goal_class"]) ?>"
                    style="<?php echo esc_attr(implode(' ', $goalSettings["lkn_content_goal_styles"])) ?>"
                >
                    <?php echo wp_kses_post(wc_price($campaign['funding_goal'])); ?>
                </p>
            </div>
            <?php endif; ?>

            <?php if ($settings['show_fund_raised'] == 'yes'): ?>
            <div
                class="lnkwp-meta-container"
                style="<?php echo esc_attr(implode(' ', $fundRaisedSettings["lkn_fund_raised_container"])) ?>"
            >
                <strong
                    class="<?php echo esc_attr($fundRaisedSettings["lkn_fund_raised_class"]) ?>"
                    style="<?php echo esc_attr(implode(' ', $fundRaisedSettings["lkn_fund_raised_styles"])) ?>"
                >
                    <?php echo esc_html_e('Fund Raised:', 'link-nacional-wp-crowdfunding-extension'); ?>
                </strong>
                <p
                    class="<?php echo esc_attr($fundRaisedSettings["lkn_content_fund_raised_class"]) ?>"
                    style="<?php echo esc_attr(implode(' ', $fundRaisedSettings["lkn_content_fund_raised_styles"])) ?>"
                >
                    <?php echo wp_kses_post(wc_price($campaign['fund_raised'])); ?>
                </p>
            </div>
            <?php endif; ?>

            <?php if ($settings['show_days'] == 'yes'): ?>
            <div
                class="lnkwp-meta-container"
                style="<?php echo esc_attr(implode(' ', $daysSettings["lkn_days_container"])) ?>"
            >
                <strong
                    class="<?php echo esc_attr($daysSettings["lkn_days_class"]) ?>"
                    style="<?php echo esc_attr(implode(' ', $daysSettings["lkn_days_styles"])) ?>"
                >
                    <?php echo esc_html_e('Days to Go:', 'link-nacional-wp-crowdfunding-extension'); ?>
                </strong>
                <p
                    class="<?php echo esc_attr($daysSettings["lkn_content_days_class"]) ?>"
                    style="<?php echo esc_attr(implode(' ', $daysSettings["lkn_content_days_styles"])) ?>"
                >
                    <?php if ($campaign['end_method'] === "never_end") {
                        echo esc_html_e('Never end', 'link-nacional-wp-crowdfunding-extension');
                    } elseif ($campaign['days_remaining'] == '0') {
                        echo esc_html_e('Expired', 'link-nacional-wp-crowdfunding-extension');
                    } else {
                        echo esc_html($campaign['days_remaining']);
                    }?>
                </p>
            </div>
            <?php endif; ?>

            <?php if ($settings['show_bottom_button'] == 'yes'): ?>
            <div
                class="lnkwp-meta-container"
                style="height: 70px;"
            >
                <div
                    class="lknwp-button-container"
                    style="padding: 5px; <?php echo esc_attr(implode(' ', $buttonsSettings["lkn_bottom_button_container"])) ?>"
                >
                    <a
                        class="<?php echo esc_attr($buttonsSettings["lkn_bottom_button_class"]) ?> lknwp-button lknwp-elementor-bottom-button"
                        href="<?php echo esc_url($campaign['permalink']); ?>"
                        style="<?php echo esc_attr(implode(' ', $buttonsSettings["lkn_bottom_button_styles"])) ?>"
                    >
                        <?php echo esc_html($buttonsSettings["bottom_button_text"]) ?>
                    </a>
                </div>
            </div>
            <?php endif; ?>
        </div>
    </div>
    <?php endforeach; ?>
</div>
<div
    class="lkn-more-card lknwp-button-container"
    style="padding: 5px; <?php echo esc_attr(implode(' ', $buttonsSettings["lkn_more_button_container"])) ?>"
>
    <button
        id="lkn-load-more"
        class="<?php echo esc_attr($buttonsSettings["lkn_more_button_class"]) ?> lknwp-button lknwp-elementor-more-button"
        style="<?php echo esc_attr(implode(' ', $buttonsSettings["lkn_more_button_styles"])) ?>"
        lknwp-offset="<?php echo esc_attr($settings['number'] ?? 10); ?>"
        lknwp-number="<?php echo esc_attr($settings['number'] ?? 10); ?>"
        lknwp-order="<?php echo esc_attr($settings['order'] ?? 'ASC'); ?>"
        lknwp-category="<?php echo esc_attr($settings['category'] ?? ''); ?>"
        lknwp-image="<?php echo esc_attr($settings['thumbnail_size'] ?? ''); ?>"
        lknwp-base-product="<?php echo esc_attr($product_base ?? ''); ?>"
        lknwp-template="<?php echo esc_attr($settings['lkn_template_page'] ?? ''); ?>"
    >
        <?php echo esc_html__('Load more', 'link-nacional-wp-crowdfunding-extension'); ?>
    </button>
</div>
<?php endif; ?>