<?php
/**
 * Shared product reviews / comments grid (Customer affairs).
 */
if (!defined('ABSPATH')) {
    exit;
}

$zerti_product_feedback_kind = isset($zerti_product_feedback_kind) && zerti_product_feedback_kind_is_valid($zerti_product_feedback_kind)
    ? $zerti_product_feedback_kind
    : 'review';
$is_review_kind = $zerti_product_feedback_kind === 'review';
$id_prefix = $is_review_kind ? 'zertiInvReviews' : 'zertiInvComments';
$wc_ok = zerti_product_reviews_available();
$can_manage = zerti_user_can_manage_product_reviews();
$statuses = zerti_product_review_status_choices();
$zerti_ui_dir = function_exists('zerti_get_text_direction') ? zerti_get_text_direction() : 'rtl';

if ($is_review_kind) {
    $wc_required_msg = __('WooCommerce must be installed and active to manage reviews.', 'zerti');
    $permission_msg = __('You do not have permission to manage product reviews.', 'zerti');
    $toolbar_aria = __('Search and filter reviews list', 'zerti');
    $search_aria = __('Search reviews', 'zerti');
    $search_placeholder = __('Search reviews', 'zerti');
    $grid_aria = __('Product reviews table', 'zerti');
    $modal_title = __('View and edit review', 'zerti');
    $content_label = __('Review text', 'zerti');
    $reply_label = __('Store reply to this review', 'zerti');
    $reply_help = __('This text is saved as a reply to the customer review. To remove the reply, clear the text and save.', 'zerti');
    $delete_btn = __('Delete review', 'zerti');
} else {
    $wc_required_msg = __('WooCommerce must be installed and active to manage comments.', 'zerti');
    $permission_msg = __('You do not have permission to manage product comments.', 'zerti');
    $toolbar_aria = __('Search and filter comments list', 'zerti');
    $search_aria = __('Search comments', 'zerti');
    $search_placeholder = __('Search comments', 'zerti');
    $grid_aria = __('Product comments table', 'zerti');
    $modal_title = __('View and edit comment', 'zerti');
    $content_label = __('Comment text', 'zerti');
    $reply_label = __('Store reply to this comment', 'zerti');
    $reply_help = __('This text is saved as a reply to the customer comment. To remove the reply, clear the text and save.', 'zerti');
    $delete_btn = __('Delete comment', 'zerti');
}
?>
<?php
zerti_ui_component_open('inventory-comments', [
    'class' => 'zerti-component-inventory zerti-inventory-sub-slot',
    'data-zerti-inv-active' => $zerti_product_feedback_kind === 'comment' ? 'comments' : 'reviews',
    'data-zerti-inv-component' => $zerti_product_feedback_kind,
]);
?>
    <?php if (!$wc_ok) : ?>
        <?php zerti_ui_wc_required_notice($wc_required_msg); ?>
    <?php elseif (!$can_manage) : ?>
        <?php zerti_ui_alert($permission_msg, 'warning', ['class' => 'mb-0']); ?>
    <?php else : ?>
        <div
            class="zerti-inv-reviews"
            data-zerti-inv-reviews="1"
            data-zerti-feedback-kind="<?php echo esc_attr($zerti_product_feedback_kind); ?>"
        >
            <div class="zerti-reviews-panel">
                <div class="zerti-reviews-wrap">
                    <?php
                    zerti_ui_toolbar_open(
                        $toolbar_aria,
                        'zerti-reviews-grid-toolbar'
                    );
                    ?>
                        <div class="zerti-reviews-grid-toolbar-search">
                            <label class="zerti-inv-categories__search-label form-label mb-0" for="<?php echo esc_attr($id_prefix . 'Search'); ?>">
                                <i class="fa-solid fa-magnifying-glass zerti-inv-categories__search-icon" aria-hidden="true"></i>
                                <input
                                    type="search"
                                    id="<?php echo esc_attr($id_prefix . 'Search'); ?>"
                                    class="form-control form-control-sm zerti-inv-categories__search-input"
                                    autocomplete="off"
                                    dir="<?php echo esc_attr($zerti_ui_dir); ?>"
                                    aria-label="<?php echo esc_attr($search_aria); ?>"
                                    placeholder="<?php echo esc_attr($search_placeholder); ?>"
                                >
                            </label>
                        </div>
                        <div class="zerti-reviews-grid-toolbar-actions d-flex flex-wrap gap-2"></div>
                    <?php zerti_ui_toolbar_close(); ?>
                    <?php
                    zerti_ui_ag_grid_region(
                        $id_prefix . 'Grid',
                        'zerti-reviews-grid',
                        $grid_aria
                    );
                    ?>
                </div>
            </div>

            <div
                class="modal fade zerti-inv-reviews__edit-modal"
                id="<?php echo esc_attr($id_prefix . 'EditModal'); ?>"
                dir="<?php echo esc_attr($zerti_ui_dir); ?>"
                data-bs-theme="light"
                tabindex="-1"
                aria-labelledby="<?php echo esc_attr($id_prefix . 'EditModalLabel'); ?>"
                aria-hidden="true"
            >
                <div class="modal-dialog modal-lg modal-dialog-centered modal-dialog-scrollable">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h6 class="modal-title zerti-inv-reviews__editor-bar-title" id="<?php echo esc_attr($id_prefix . 'EditModalLabel'); ?>">
                                <?php echo esc_html($modal_title); ?>
                            </h6>
                            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="<?php esc_attr_e('Close', 'zerti'); ?>"></button>
                        </div>
                        <div class="modal-body">
                            <div class="small text-muted mb-3 mb-md-4 zerti-inv-reviews__meta" aria-live="polite">
                                <p class="mb-1 zerti-inv-reviews__author-line">
                                    <span class="zerti-inv-reviews__meta-label"><?php esc_html_e('Customer', 'zerti'); ?>:</span>
                                    <span class="zerti-inv-reviews__author-name"></span>
                                </p>
                                <p class="mb-1 zerti-inv-reviews__product-line">
                                    <span class="zerti-inv-reviews__meta-label"><?php esc_html_e('Product', 'zerti'); ?>:</span>
                                    <span class="zerti-inv-reviews__product-name"></span>
                                </p>
                                <p class="mb-0 zerti-inv-reviews__submitted-line">
                                    <span class="zerti-inv-reviews__meta-label"><?php esc_html_e('Submitted on', 'zerti'); ?>:</span>
                                    <time class="zerti-inv-reviews__submitted-at" datetime=""></time>
                                </p>
                            </div>
                            <div class="alert alert-danger py-2 px-3 small d-none zerti-inv-reviews__form-error" role="alert"></div>
                            <div class="alert alert-success py-2 px-3 small d-none zerti-inv-reviews__form-success" role="status"></div>
                            <form id="<?php echo esc_attr($id_prefix . 'EditForm'); ?>" class="zerti-inv-reviews__form">
                                <input type="hidden" data-zerti-field="id" value="">
                                <input type="hidden" data-zerti-field="reply_id" value="">
                                <div class="mb-3">
                                    <label class="form-label" for="<?php echo esc_attr($id_prefix . 'Content'); ?>"><?php echo esc_html($content_label); ?></label>
                                    <textarea
                                        id="<?php echo esc_attr($id_prefix . 'Content'); ?>"
                                        class="form-control form-control-sm"
                                        rows="6"
                                        dir="<?php echo esc_attr($zerti_ui_dir); ?>"
                                        data-zerti-field="content"
                                    ></textarea>
                                </div>
                                <div class="mb-3">
                                    <label class="form-label" for="<?php echo esc_attr($id_prefix . 'Reply'); ?>"><?php echo esc_html($reply_label); ?></label>
                                    <textarea
                                        id="<?php echo esc_attr($id_prefix . 'Reply'); ?>"
                                        class="form-control form-control-sm"
                                        rows="4"
                                        dir="<?php echo esc_attr($zerti_ui_dir); ?>"
                                        data-zerti-field="reply"
                                        placeholder="<?php esc_attr_e('Write your reply here…', 'zerti'); ?>"
                                    ></textarea>
                                    <p class="form-text small text-muted mb-0">
                                        <?php echo esc_html($reply_help); ?>
                                    </p>
                                </div>
                                <div class="row g-3">
                                    <?php if ($is_review_kind) : ?>
                                    <div class="col-sm-6">
                                        <label class="form-label" for="<?php echo esc_attr($id_prefix . 'Rating'); ?>"><?php esc_html_e('Rating', 'zerti'); ?></label>
                                        <select id="<?php echo esc_attr($id_prefix . 'Rating'); ?>" class="form-select form-select-sm zerti-admin-select" data-zerti-field="rating">
                                            <?php for ($r = 0; $r <= 5; $r++) : ?>
                                                <option value="<?php echo esc_attr((string) $r); ?>">
                                                    <?php echo esc_html($r === 0 ? __('No rating', 'zerti') : zerti_to_persian_digits((string) $r)); ?>
                                                </option>
                                            <?php endfor; ?>
                                        </select>
                                    </div>
                                    <?php endif; ?>
                                    <div class="<?php echo $is_review_kind ? 'col-sm-6' : 'col-12'; ?>">
                                        <label class="form-label" for="<?php echo esc_attr($id_prefix . 'StatusField'); ?>"><?php esc_html_e('Status', 'zerti'); ?></label>
                                        <select id="<?php echo esc_attr($id_prefix . 'StatusField'); ?>" class="form-select form-select-sm zerti-admin-select" data-zerti-field="status">
                                            <?php foreach ($statuses as $value => $label) : ?>
                                                <option value="<?php echo esc_attr($value); ?>"><?php echo esc_html($label); ?></option>
                                            <?php endforeach; ?>
                                        </select>
                                    </div>
                                </div>
                            </form>
                        </div>
                        <div class="modal-footer d-flex flex-wrap gap-2 align-items-center justify-content-between w-100">
                            <button type="button" class="btn btn-sm btn-outline-danger zerti-inv-reviews__delete-btn">
                                <?php echo esc_html($delete_btn); ?>
                            </button>
                            <div class="d-flex flex-wrap gap-2 align-items-center">
                                <button type="button" class="btn btn-sm btn-outline-secondary" data-bs-dismiss="modal">
                                    <?php esc_html_e('Close', 'zerti'); ?>
                                </button>
                                <button type="submit" form="<?php echo esc_attr($id_prefix . 'EditForm'); ?>" class="btn btn-sm btn-primary zerti-inv-reviews__save-btn">
                                    <?php esc_html_e('Save', 'zerti'); ?>
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    <?php endif; ?>
<?php zerti_ui_component_close(); ?>
