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

global $wpdb;
$reward = Dima_Reward::get_instance();

// جستجوی کاربر
$search_user = isset($_GET['user_id']) ? intval($_GET['user_id']) : 0;
$user_points = 0;
$user_history = array();

if ($search_user > 0) {
    $user_points = $reward->get_user_points($search_user);
    $user_history = $reward->get_user_history($search_user, 50);
}

// افزودن دستی امتیاز
if (isset($_POST['add_points']) && check_admin_referer('dima_rewards_nonce')) {
    $user_id = intval($_POST['user_id']);
    $points = intval($_POST['points']);
    $desc = sanitize_text_field($_POST['description']);
    
    $reward->add_points($user_id, $points, 'manual', null, $desc);
    echo '<div class="notice notice-success"><p>امتیاز اضافه شد.</p></div>';
}

$users = get_users(array('number' => 100, 'orderby' => 'display_name'));
?>

<div class="wrap">
    <h1>🎁 سیستم پاداش و امتیازات</h1>
    
    <!-- جستجو -->
    <div class="dima-form-box">
        <h2>🔍 جستجوی کاربر</h2>
        <form method="get">
            <input type="hidden" name="page" value="dima-shop-rewards">
            <select name="user_id" style="min-width:300px;">
                <option value="">— انتخاب کاربر —</option>
                <?php foreach ($users as $u): ?>
                    <option value="<?php echo $u->ID; ?>" <?php selected($search_user, $u->ID); ?>>
                        <?php echo esc_html($u->display_name); ?> (<?php echo esc_html($u->user_email); ?>)
                    </option>
                <?php endforeach; ?>
            </select>
            <button type="submit" class="button button-primary">جستجو</button>
        </form>
    </div>
    
    <?php if ($search_user > 0): ?>
    <!-- امتیاز کاربر -->
    <div class="dima-reward-summary">
        <div class="reward-box">
            <div class="reward-icon">👤</div>
            <div class="reward-info">
                <div class="reward-label">کاربر</div>
                <div class="reward-value"><?php echo esc_html(get_userdata($search_user)->display_name); ?></div>
            </div>
        </div>
        <div class="reward-box" style="background: linear-gradient(135deg, #f59e0b 0%, #ef4444 100%);">
            <div class="reward-icon">⭐</div>
            <div class="reward-info">
                <div class="reward-label">امتیاز فعلی</div>
                <div class="reward-value"><?php echo number_format($user_points); ?></div>
            </div>
        </div>
    </div>
    
    <!-- افزودن امتیاز -->
    <div class="dima-form-box">
        <h2>➕ افزودن امتیاز دستی</h2>
        <form method="post">
            <?php wp_nonce_field('dima_rewards_nonce'); ?>
            <input type="hidden" name="user_id" value="<?php echo $search_user; ?>">
            <table class="form-table">
                <tr><th>تعداد امتیاز:</th><td><input type="number" name="points" required class="regular-text"></td></tr>
                <tr><th>توضیحات:</th><td><input type="text" name="description" class="regular-text"></td></tr>
            </table>
            <?php submit_button('افزودن امتیاز', 'primary', 'add_points'); ?>
        </form>
    </div>
    
    <!-- تاریخچه -->
    <h2> تاریخچه امتیازات</h2>
    <?php if (empty($user_history)): ?>
        <p>تاریخچه‌ای موجود نیست.</p>
    <?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></tr>
            </thead>
            <tbody>
                <?php foreach ($user_history as $h): ?>
                <tr>
                    <td>#<?php echo intval($h->id); ?></td>
                    <td><strong style="color:<?php echo $h->points > 0 ? '#10b981' : '#ef4444'; ?>;"><?php echo ($h->points > 0 ? '+' : '') . intval($h->points); ?></strong></td>
                    <td><?php echo esc_html($h->type); ?></td>
                    <td><?php echo esc_html($h->description); ?></td>
                    <td><?php echo date_i18n('j F Y - H:i', strtotime($h->created_at)); ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php endif; ?>
    
    <?php endif; ?>
</div>

<style>
.dima-form-box { background: white; padding: 24px; border-radius: 12px; margin-bottom: 24px; box-shadow: 0 2px 8px rgba(0,0,0,0.06); border: 1px solid #e5e7eb; }
.dima-form-box h2 { margin-top: 0; padding-bottom: 15px; border-bottom: 2px solid #f3f4f6; }
.dima-reward-summary { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 15px; margin-bottom: 24px; }
.reward-box { background: white; padding: 20px; border-radius: 12px; display: flex; align-items: center; gap: 15px; box-shadow: 0 2px 8px rgba(0,0,0,0.06); color: white; }
.reward-icon { font-size: 36px; }
.reward-label { font-size: 12px; opacity: 0.9; }
.reward-value { font-size: 22px; font-weight: 800; }
</style>