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

global $wpdb;

// پردازش تغییر وضعیت
if (isset($_POST['review_action']) && check_admin_referer('dima_review_action')) {
    $review_id = intval($_POST['review_id']);
    $action = sanitize_text_field($_POST['review_action']);
    $review = Dima_Review::get_instance();
    
    if ($action === 'approve') {
        $review->approve_review($review_id);
        echo '<div class="notice notice-success"><p>نظر تایید شد.</p></div>';
    } elseif ($action === 'reject') {
        $review->reject_review($review_id);
        echo '<div class="notice notice-warning"><p>نظر رد شد.</p></div>';
    } elseif ($action === 'delete') {
        $review->delete_review($review_id);
        echo '<div class="notice notice-success"><p>نظر حذف شد.</p></div>';
    }
}

$reviews = $wpdb->get_results("
    SELECT r.*, p.post_title as product_title 
    FROM {$wpdb->prefix}dima_reviews r
    LEFT JOIN {$wpdb->posts} p ON r.product_id = p.ID
    ORDER BY r.created_at DESC
    LIMIT 100
");

$status_labels = array('pending' => 'در انتظار', 'approved' => 'تایید شده', 'rejected' => 'رد شده');
$status_colors = array('pending' => '#f59e0b', 'approved' => '#10b981', 'rejected' => '#ef4444');
?>

<div class="wrap">
    <h1>⭐ نظرات و امتیازدهی</h1>
    
    <?php if (empty($reviews)): ?>
    <div class="empty-state">
        <div class="empty-icon">💬</div>
        <h3>هنوز نظری ثبت نشده است</h3>
    </div>
    <?php else: ?>
    <div class="table-responsive">
        <table class="wp-list-table widefat fixed striped">
            <thead>
                <tr>
                    <th>شناسه</th>
                    <th>محصول</th>
                    <th>نویسنده</th>
                    <th>امتیاز</th>
                    <th>عنوان</th>
                    <th>وضعیت</th>
                    <th>تاریخ</th>
                    <th>عملیات</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($reviews as $review): ?>
                <tr>
                    <td>#<?php echo intval($review->id); ?></td>
                    <td><?php echo esc_html($review->product_title ?: 'حذف شده'); ?></td>
                    <td>
                        <strong><?php echo esc_html($review->author_name); ?></strong>
                        <?php if ($review->verified_purchase): ?>
                            <br><small style="color:#10b981;">✓ خریدار</small>
                        <?php endif; ?>
                    </td>
                    <td>
                        <span style="color:#fbbf24; font-size:16px;">
                            <?php echo str_repeat('★', intval($review->rating)) . str_repeat('☆', 5 - intval($review->rating)); ?>
                        </span>
                    </td>
                    <td>
                        <?php if ($review->title): ?>
                            <strong><?php echo esc_html($review->title); ?></strong><br>
                        <?php endif; ?>
                        <small><?php echo wp_trim_words($review->content, 15); ?></small>
                    </td>
                    <td>
                        <span style="background:<?php echo $status_colors[$review->status] ?? '#6b7280'; ?>; color:white; padding:4px 12px; border-radius:20px; font-size:12px;">
                            <?php echo $status_labels[$review->status] ?? $review->status; ?>
                        </span>
                    </td>
                    <td><?php echo date_i18n('j F Y', strtotime($review->created_at)); ?></td>
                    <td>
                        <form method="post" style="display:inline;">
                            <?php wp_nonce_field('dima_review_action'); ?>
                            <input type="hidden" name="review_id" value="<?php echo intval($review->id); ?>">
                            <?php if ($review->status !== 'approved'): ?>
                                <button type="submit" name="review_action" value="approve" class="button button-small" style="color:#10b981;">✓</button>
                            <?php endif; ?>
                            <?php if ($review->status !== 'rejected'): ?>
                                <button type="submit" name="review_action" value="reject" class="button button-small" style="color:#f59e0b;">✗</button>
                            <?php endif; ?>
                            <button type="submit" name="review_action" value="delete" class="button button-small button-link-delete" onclick="return confirm('حذف نظر؟');">🗑️</button>
                        </form>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php endif; ?>
</div>