<?php

/**
 * Review form modal.
 *
 * Rendered at wp_footer (page root) so its overlay/z-index is not trapped inside a
 * stacking context (e.g. the sticky summary column or constrained layout).
 *
 * @package kubelio
 */

defined('ABSPATH') || exit;

global $product;

if (! $product instanceof WC_Product) {
    $product = wc_get_product(get_queried_object_id());
}

if (! $product || ! comments_open()) {
    return;
}

$can_review = get_option('woocommerce_review_rating_verification_required') === 'no' || wc_customer_bought_product('', get_current_user_id(), $product->get_id());

if (! $can_review) {
    return;
}
?>
<div id="review_form_modal" class="rt-modal rt-reviews-modal is-scrollable" style="display: none;">
    <span class="rt-modal__overlay js-modal-close"></span>
    <div class="rt-modal__inner" style="max-width: 700px;">
        <a class="rt-modal__close js-modal-close" role="button" aria-label="<?php echo esc_attr__('Close', 'kubelio'); ?>">
            <i class="rt-icon">
                <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-x-lg" viewBox="0 0 16 16">
                    <path d="M2.146 2.854a.5.5 0 1 1 .708-.708L8 7.293l5.146-5.147a.5.5 0 0 1 .708.708L8.707 8l5.147 5.146a.5.5 0 0 1-.708.708L8 8.707l-5.146 5.147a.5.5 0 0 1-.708-.708L7.293 8z"></path>
                </svg>
            </i>
        </a>
        <div class="rt-modal__header">
            <h3 class="rt-modal__title has-medium-font-size"><?php esc_html_e('Write a review', 'kubelio'); ?></h3>
            <p class="rt-modal__desc has-x-small-font-size has-meta-color mb-0 mt-10"><?php echo esc_html($product->get_name()); ?></p>
        </div>
        <div class="rt-modal__body" id="review_form_wrapper">
            <div id="review_form" class="woocommerce-Reviews">
                <?php
                $commenter    = wp_get_current_commenter();
                $comment_form = array(
                    /* translators: %s is product title */
                    'title_reply'         => '',
                    /* translators: %s: comment author name. */
                    'title_reply_to'      => esc_html__('Leave a Reply to %s', 'kubelio'),
                    'title_reply_before'  => '<span id="reply-title" class="comment-reply-title" style="display:none;">',
                    'title_reply_after'   => '</span>',
                    'comment_notes_after' => '',
                    'label_submit'        => esc_html__('Submit Review', 'kubelio'),
                    'class_submit'        => 'submit flex-1',
                    'logged_in_as'        => '',
                    'comment_field'       => '',
                );

                $name_email_required = (bool) get_option('require_name_email', 1);
                $fields              = array(
                    'author' => array(
                        'label'    => esc_html__('Name', 'kubelio'),
                        'type'     => 'text',
                        'value'    => $commenter['comment_author'],
                        'required' => $name_email_required,
                    ),
                    'email'  => array(
                        'label'    => esc_html__('Email', 'kubelio'),
                        'type'     => 'email',
                        'value'    => $commenter['comment_author_email'],
                        'required' => $name_email_required,
                    ),
                );

                if (wc_review_ratings_enabled()) {
                    $comment_form['comment_field'] = '<div class="comment-form-rating mb-0"><label for="rating">' . esc_html__('Your rating', 'kubelio') . ($name_email_required ? '&nbsp;<span class="required">*</span>' : '') . '</label><select name="rating" id="rating" required>
						<option value="">' . esc_html__('Rate&hellip;', 'kubelio') . '</option>
						<option value="5">' . esc_html__('Perfect', 'kubelio') . '</option>
						<option value="4">' . esc_html__('Good', 'kubelio') . '</option>
						<option value="3">' . esc_html__('Average', 'kubelio') . '</option>
						<option value="2">' . esc_html__('Not that bad', 'kubelio') . '</option>
						<option value="1">' . esc_html__('Very poor', 'kubelio') . '</option>
					</select></div>';
                }

                $comment_form['comment_field'] .= '<p class="comment-form-comment mb-0"><label for="comment">' . esc_html__('Your review', 'kubelio') . '&nbsp;<span class="required">*</span></label><textarea id="comment" name="comment" cols="45" rows="6" required placeholder="' . esc_attr__('Describe your experience...', 'kubelio') . '"></textarea></p>';

                $comment_form['submit_field'] = '%2$s<p class="form-submit flex items-center gap-30 mt-20"><button type="button" class="rt-btn is-style-outline has-background-background-color js-modal-close">' . esc_html__('Cancel Review', 'kubelio') . '</button> %1$s</p>';

                comment_form(apply_filters('woocommerce_product_review_comment_form_args', $comment_form), $product->get_id());
                ?>
            </div>
        </div>
    </div>
</div>