<?php
/**
 * قالب حساب کاربری Dima Shop
 * نسخه: 1.0.0
 * 
 * شامل:
 * - داشبورد کاربر
 * - لیست سفارشات
 * - جزئیات سفارش
 * - ویرایش آدرس
 * - امتیازات و پاداش
 * - علاقه‌مندی‌ها
 */
if (!defined('ABSPATH')) exit;

if (!is_user_logged_in()) {
    wp_redirect(wp_login_url(get_permalink()));
    exit;
}

$current_user = wp_get_current_user();
$user_id = $current_user->ID;

// دریافت بخش فعلی
$active_tab = get_query_var('dima_account', 'dashboard');

// آمار کاربر
global $wpdb;
$orders_count = $wpdb->get_var($wpdb->prepare(
    "SELECT COUNT(*) FROM {$wpdb->prefix}dima_orders WHERE user_id = %d",
    $user_id
));

$reward = class_exists('Dima_Reward') ? Dima_Reward::get_instance() : null;
$user_points = $reward ? $reward->get_user_points($user_id) : 0;

$wishlist = class_exists('Dima_Wishlist') ? Dima_Wishlist::get_instance() : null;
$wishlist_count = $wishlist ? $wishlist->get_count() : 0;

// پردازش فرم ویرایش آدرس
if (isset($_POST['update_address']) && check_admin_referer('dima_update_address')) {
    $address_data = array(
        'phone' => sanitize_text_field($_POST['phone'] ?? ''),
        'province' => sanitize_text_field($_POST['province'] ?? ''),
        'city' => sanitize_text_field($_POST['city'] ?? ''),
        'address' => sanitize_textarea_field($_POST['address'] ?? ''),
        'postal_code' => sanitize_text_field($_POST['postal_code'] ?? '')
    );
    
    update_user_meta($user_id, 'dima_phone', $address_data['phone']);
    update_user_meta($user_id, 'dima_province', $address_data['province']);
    update_user_meta($user_id, 'dima_city', $address_data['city']);
    update_user_meta($user_id, 'dima_address', $address_data['address']);
    update_user_meta($user_id, 'dima_postal_code', $address_data['postal_code']);
    
    echo '<div class="account-alert success">✅ اطلاعات با موفقیت بروزرسانی شد.</div>';
}

// پردازش تغییر رمز عبور
if (isset($_POST['change_password']) && check_admin_referer('dima_change_password')) {
    $old_pass = $_POST['old_password'] ?? '';
    $new_pass = $_POST['new_password'] ?? '';
    $confirm_pass = $_POST['confirm_password'] ?? '';
    
    if (!wp_check_password($old_pass, $current_user->user_pass, $user_id)) {
        echo '<div class="account-alert error"> رمز عبور فعلی اشتباه است.</div>';
    } elseif ($new_pass !== $confirm_pass) {
        echo '<div class="account-alert error">❌ رمز عبور جدید و تایید آن مطابقت ندارند.</div>';
    } elseif (strlen($new_pass) < 6) {
        echo '<div class="account-alert error">❌ رمز عبور باید حداقل ۶ کاراکتر باشد.</div>';
    } else {
        wp_set_password($new_pass, $user_id);
        echo '<div class="account-alert success">✅ رمز عبور با موفقیت تغییر کرد.</div>';
    }
}
?>

<div class="dima-account-page" dir="rtl">
    
    <div class="account-header">
        <div class="user-welcome">
            <div class="user-avatar-large">
                <?php echo get_avatar($user_id, 80); ?>
            </div>
            <div class="user-info">
                <h1>سلام، <?php echo esc_html($current_user->display_name); ?> 👋</h1>
                <p class="user-email"><?php echo esc_html($current_user->user_email); ?></p>
            </div>
        </div>
    </div>
    
    <div class="account-layout">
        
        <!-- منوی کناری -->
        <aside class="account-sidebar">
            <nav class="account-nav">
                <a href="<?php echo esc_url(get_permalink(get_option('dima_account_page'))); ?>" 
                   class="nav-item <?php echo $active_tab === 'dashboard' ? 'active' : ''; ?>">
                    <span class="nav-icon"></span>
                    <span class="nav-text">داشبورد</span>
                </a>
                
                <a href="<?php echo esc_url(get_permalink(get_option('dima_account_page'))); ?>?dima_account=orders" 
                   class="nav-item <?php echo $active_tab === 'orders' ? 'active' : ''; ?>">
                    <span class="nav-icon">️</span>
                    <span class="nav-text">سفارشات من</span>
                    <span class="nav-badge"><?php echo intval($orders_count); ?></span>
                </a>
                
                <a href="<?php echo esc_url(get_permalink(get_option('dima_wishlist_page'))); ?>" 
                   class="nav-item">
                    <span class="nav-icon">❤️</span>
                    <span class="nav-text">علاقه‌مندی‌ها</span>
                    <span class="nav-badge"><?php echo intval($wishlist_count); ?></span>
                </a>
                
                <a href="<?php echo esc_url(get_permalink(get_option('dima_account_page'))); ?>?dima_account=rewards" 
                   class="nav-item <?php echo $active_tab === 'rewards' ? 'active' : ''; ?>">
                    <span class="nav-icon">🎁</span>
                    <span class="nav-text">امتیازات من</span>
                    <span class="nav-badge"><?php echo intval($user_points); ?></span>
                </a>
                
                <a href="<?php echo esc_url(get_permalink(get_option('dima_account_page'))); ?>?dima_account=edit-address" 
                   class="nav-item <?php echo $active_tab === 'edit-address' ? 'active' : ''; ?>">
                    <span class="nav-icon">📍</span>
                    <span class="nav-text">ویرایش آدرس</span>
                </a>
                
                <a href="<?php echo esc_url(get_permalink(get_option('dima_account_page'))); ?>?dima_account=change-password" 
                   class="nav-item <?php echo $active_tab === 'change-password' ? 'active' : ''; ?>">
                    <span class="nav-icon">🔒</span>
                    <span class="nav-text">تغییر رمز عبور</span>
                </a>
                
                <a href="<?php echo wp_logout_url(home_url()); ?>" class="nav-item logout">
                    <span class="nav-icon">🚪</span>
                    <span class="nav-text">خروج</span>
                </a>
            </nav>
        </aside>
        
        <!-- محتوای اصلی -->
        <main class="account-content">
            
            <?php if ($active_tab === 'dashboard'): ?>
                <!-- داشبورد -->
                <div class="account-section">
                    <h2>📊 داشبورد</h2>
                    
                    <div class="dashboard-stats">
                        <div class="stat-card">
                            <div class="stat-icon">🛍️</div>
                            <div class="stat-info">
                                <div class="stat-value"><?php echo intval($orders_count); ?></div>
                                <div class="stat-label">سفارش</div>
                            </div>
                        </div>
                        
                        <div class="stat-card">
                            <div class="stat-icon">❤️</div>
                            <div class="stat-info">
                                <div class="stat-value"><?php echo intval($wishlist_count); ?></div>
                                <div class="stat-label">علاقه‌مندی</div>
                            </div>
                        </div>
                        
                        <div class="stat-card">
                            <div class="stat-icon"></div>
                            <div class="stat-info">
                                <div class="stat-value"><?php echo intval($user_points); ?></div>
                                <div class="stat-label">امتیاز</div>
                            </div>
                        </div>
                    </div>
                    
                    <!-- سفارشات اخیر -->
                    <h3>🕐 سفارشات اخیر</h3>
                    <?php
                    $recent_orders = $wpdb->get_results($wpdb->prepare(
                        "SELECT * FROM {$wpdb->prefix}dima_orders WHERE user_id = %d ORDER BY created_at DESC LIMIT 5",
                        $user_id
                    ));
                    
                    if (empty($recent_orders)):
                    ?>
                        <div class="empty-box">
                            <div class="empty-icon">📭</div>
                            <p>هنوز سفارشی ثبت نکرده‌اید</p>
                            <a href="<?php echo esc_url(get_permalink(get_option('dima_shop_page'))); ?>" class="btn btn-primary">
                                ️ شروع خرید
                            </a>
                        </div>
                    <?php else: ?>
                        <div class="orders-list">
                            <?php foreach ($recent_orders as $order): 
                                $status_labels = array(
                                    'pending' => 'در انتظار',
                                    'processing' => 'در حال پردازش',
                                    'completed' => 'تکمیل شده',
                                    'failed' => 'ناموفق',
                                    'cancelled' => 'لغو شده'
                                );
                                $status_colors = array(
                                    'pending' => '#f59e0b',
                                    'processing' => '#3b82f6',
                                    'completed' => '#10b981',
                                    'failed' => '#ef4444',
                                    'cancelled' => '#6b7280'
                                );
                            ?>
                            <div class="order-item">
                                <div class="order-info">
                                    <span class="order-id">سفارش #<?php echo intval($order->id); ?></span>
                                    <span class="order-date"><?php echo date_i18n('j F Y', strtotime($order->created_at)); ?></span>
                                </div>
                                <div class="order-status" style="background: <?php echo $status_colors[$order->status] ?? '#6b7280'; ?>">
                                    <?php echo $status_labels[$order->status] ?? $order->status; ?>
                                </div>
                                <div class="order-total">
                                    <?php echo number_format($order->final_amount); ?> تومان
                                </div>
                            </div>
                            <?php endforeach; ?>
                        </div>
                        
                        <a href="<?php echo esc_url(get_permalink(get_option('dima_account_page'))); ?>?dima_account=orders" class="btn btn-outline">
                            مشاهده همه سفارشات ←
                        </a>
                    <?php endif; ?>
                </div>
            
            <?php elseif ($active_tab === 'orders'): ?>
                <!-- لیست سفارشات -->
                <div class="account-section">
                    <h2>🛍️ سفارشات من</h2>
                    
                    <?php
                    $all_orders = $wpdb->get_results($wpdb->prepare(
                        "SELECT * FROM {$wpdb->prefix}dima_orders WHERE user_id = %d ORDER BY created_at DESC",
                        $user_id
                    ));
                    
                    if (empty($all_orders)):
                    ?>
                        <div class="empty-box">
                            <div class="empty-icon">📭</div>
                            <p>هنوز سفارشی ثبت نکرده‌اید</p>
                        </div>
                    <?php else: ?>
                        <div class="orders-list full">
                            <?php foreach ($all_orders as $order): ?>
                            <div class="order-item full">
                                <div class="order-info">
                                    <span class="order-id">سفارش #<?php echo intval($order->id); ?></span>
                                    <span class="order-date"><?php echo date_i18n('j F Y - H:i', strtotime($order->created_at)); ?></span>
                                </div>
                                <div class="order-status" style="background: <?php echo $status_colors[$order->status] ?? '#6b7280'; ?>">
                                    <?php echo $status_labels[$order->status] ?? $order->status; ?>
                                </div>
                                <div class="order-total">
                                    <?php echo number_format($order->final_amount); ?> تومان
                                </div>
                                <a href="<?php echo esc_url(get_permalink(get_option('dima_account_page'))); ?>?dima_account=order-view&order_id=<?php echo $order->id; ?>" class="btn btn-small">
                                    مشاهده جزئیات
                                </a>
                            </div>
                            <?php endforeach; ?>
                        </div>
                    <?php endif; ?>
                </div>
            
            <?php elseif ($active_tab === 'order-view' && isset($_GET['order_id'])): ?>
                <!-- جزئیات سفارش -->
                <?php
                $order_id = intval($_GET['order_id']);
                $order = $wpdb->get_row($wpdb->prepare(
                    "SELECT * FROM {$wpdb->prefix}dima_orders WHERE id = %d AND user_id = %d",
                    $order_id, $user_id
                ));
                
                if (!$order):
                ?>
                    <div class="account-alert error">سفارش یافت نشد.</div>
                <?php else: 
                    $items = $wpdb->get_results($wpdb->prepare(
                        "SELECT i.*, p.post_title FROM {$wpdb->prefix}dima_order_items i LEFT JOIN {$wpdb->posts} p ON i.product_id = p.ID WHERE i.order_id = %d",
                        $order_id
                    ));
                    $customer = json_decode($order->customer_data, true);
                ?>
                <div class="account-section">
                    <a href="<?php echo esc_url(get_permalink(get_option('dima_account_page'))); ?>?dima_account=orders" class="btn btn-outline btn-small">
                        ← بازگشت به لیست سفارشات
                    </a>
                    
                    <h2>سفارش #<?php echo intval($order->id); ?></h2>
                    
                    <div class="order-details-grid">
                        <div class="detail-box">
                            <h3> اطلاعات سفارش</h3>
                            <div class="detail-row">
                                <span>وضعیت:</span>
                                <span class="status-badge" style="background: <?php echo $status_colors[$order->status] ?? '#6b7280'; ?>">
                                    <?php echo $status_labels[$order->status] ?? $order->status; ?>
                                </span>
                            </div>
                            <div class="detail-row">
                                <span>تاریخ:</span>
                                <span><?php echo date_i18n('j F Y - H:i', strtotime($order->created_at)); ?></span>
                            </div>
                            <div class="detail-row">
                                <span>مبلغ کل:</span>
                                <span><?php echo number_format($order->total_amount); ?> تومان</span>
                            </div>
                            <?php if ($order->discount_amount > 0): ?>
                            <div class="detail-row">
                                <span>تخفیف:</span>
                                <span style="color:#10b981;">-<?php echo number_format($order->discount_amount); ?> تومان</span>
                            </div>
                            <?php endif; ?>
                            <div class="detail-row final">
                                <span>مبلغ نهایی:</span>
                                <span><strong><?php echo number_format($order->final_amount); ?> تومان</strong></span>
                            </div>
                        </div>
                        
                        <div class="detail-box">
                            <h3>👤 اطلاعات خریدار</h3>
                            <?php if (!empty($customer['name'])): ?>
                            <div class="detail-row">
                                <span>نام:</span>
                                <span><?php echo esc_html($customer['name']); ?></span>
                            </div>
                            <?php endif; ?>
                            <?php if (!empty($customer['phone'])): ?>
                            <div class="detail-row">
                                <span>موبایل:</span>
                                <span><?php echo esc_html($customer['phone']); ?></span>
                            </div>
                            <?php endif; ?>
                            <?php if (!empty($customer['address'])): ?>
                            <div class="detail-row">
                                <span>آدرس:</span>
                                <span><?php echo esc_html($customer['address']); ?></span>
                            </div>
                            <?php endif; ?>
                        </div>
                    </div>
                    
                    <h3>🛍️ محصولات سفارش</h3>
                    <div class="order-items-table">
                        <?php foreach ($items as $item): ?>
                        <div class="order-item-row">
                            <span class="item-name"><?php echo esc_html($item->post_title ?: 'محصول حذف شده'); ?></span>
                            <span class="item-qty">× <?php echo intval($item->quantity); ?></span>
                            <span class="item-price"><?php echo number_format($item->price * $item->quantity); ?> تومان</span>
                        </div>
                        <?php endforeach; ?>
                    </div>
                </div>
                <?php endif; ?>
            
            <?php elseif ($active_tab === 'rewards'): ?>
                <!-- امتیازات -->
                <div class="account-section">
                    <h2>🎁 امتیازات من</h2>
                    
                    <div class="rewards-summary">
                        <div class="rewards-points">
                            <div class="points-value"><?php echo number_format($user_points); ?></div>
                            <div class="points-label">امتیاز فعلی</div>
                        </div>
                        <div class="rewards-value">
                            <div class="value-amount"><?php echo number_format(intval($user_points / 1000) * 1000); ?></div>
                            <div class="value-label">تومان ارزش</div>
                        </div>
                    </div>
                    
                    <?php if ($user_points >= 1000): ?>
                    <div class="redeem-section">
                        <h3>تبدیل امتیاز به کوپن تخفیف</h3>
                        <p>هر ۱۰۰ امتیاز = ۱۰۰۰ تومان تخفیف</p>
                        <button class="btn btn-primary" id="redeem-points-btn" data-points="1000">
                            🎟️ تبدیل ۱۰۰۰ امتیاز به کوپن
                        </button>
                    </div>
                    <?php else: ?>
                    <div class="empty-box">
                        <p>برای دریافت کوپن تخفیف، حداقل ۱۰۰۰ امتیاز نیاز است.</p>
                        <p>با هر خرید و ثبت نظر، امتیاز جمع کنید!</p>
                    </div>
                    <?php endif; ?>
                    
                    <?php if ($reward): 
                        $history = $reward->get_user_history($user_id, 20);
                        if (!empty($history)):
                    ?>
                    <h3>تاریخچه امتیازات</h3>
                    <div class="rewards-history">
                        <?php foreach ($history as $h): ?>
                        <div class="history-item">
                            <div class="history-info">
                                <span class="history-type"><?php echo esc_html($h->type); ?></span>
                                <span class="history-desc"><?php echo esc_html($h->description); ?></span>
                            </div>
                            <div class="history-points" style="color: <?php echo $h->points > 0 ? '#10b981' : '#ef4444'; ?>">
                                <?php echo ($h->points > 0 ? '+' : '') . intval($h->points); ?>
                            </div>
                            <div class="history-date">
                                <?php echo date_i18n('j F Y', strtotime($h->created_at)); ?>
                            </div>
                        </div>
                        <?php endforeach; ?>
                    </div>
                    <?php endif; endif; ?>
                </div>
            
            <?php elseif ($active_tab === 'edit-address'): ?>
                <!-- ویرایش آدرس -->
                <div class="account-section">
                    <h2>📍 ویرایش آدرس</h2>
                    
                    <form method="post">
                        <?php wp_nonce_field('dima_update_address'); ?>
                        
                        <div class="form-group">
                            <label>شماره موبایل:</label>
                            <input type="tel" name="phone" value="<?php echo esc_attr(get_user_meta($user_id, 'dima_phone', true)); ?>" pattern="09[0-9]{9}" required>
                        </div>
                        
                        <div class="form-row">
                            <div class="form-group">
                                <label>استان:</label>
                                <select name="province" required>
                                    <option value="">— انتخاب استان —</option>
                                    <?php
                                    $provinces = array('تهران', 'اصفهان', 'فارس', 'خراسان رضوی', 'آذربایجان شرقی', 'آذربایجان غربی', 'مازندران', 'گیلان', 'البرز', 'سایر');
                                    $current_province = get_user_meta($user_id, 'dima_province', true);
                                    foreach ($provinces as $prov):
                                    ?>
                                        <option value="<?php echo esc_attr($prov); ?>" <?php selected($current_province, $prov); ?>><?php echo esc_html($prov); ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            
                            <div class="form-group">
                                <label>شهر:</label>
                                <input type="text" name="city" value="<?php echo esc_attr(get_user_meta($user_id, 'dima_city', true)); ?>" required>
                            </div>
                        </div>
                        
                        <div class="form-group">
                            <label>آدرس کامل:</label>
                            <textarea name="address" rows="3" required><?php echo esc_textarea(get_user_meta($user_id, 'dima_address', true)); ?></textarea>
                        </div>
                        
                        <div class="form-group">
                            <label>کد پستی:</label>
                            <input type="text" name="postal_code" value="<?php echo esc_attr(get_user_meta($user_id, 'dima_postal_code', true)); ?>" pattern="[0-9]{10}" required>
                        </div>
                        
                        <button type="submit" name="update_address" class="btn btn-primary">
                            💾 ذخیره تغییرات
                        </button>
                    </form>
                </div>
            
            <?php elseif ($active_tab === 'change-password'): ?>
                <!-- تغییر رمز عبور -->
                <div class="account-section">
                    <h2>🔒 تغییر رمز عبور</h2>
                    
                    <form method="post">
                        <?php wp_nonce_field('dima_change_password'); ?>
                        
                        <div class="form-group">
                            <label>رمز عبور فعلی:</label>
                            <input type="password" name="old_password" required>
                        </div>
                        
                        <div class="form-group">
                            <label>رمز عبور جدید:</label>
                            <input type="password" name="new_password" required minlength="6">
                        </div>
                        
                        <div class="form-group">
                            <label>تایید رمز عبور جدید:</label>
                            <input type="password" name="confirm_password" required minlength="6">
                        </div>
                        
                        <button type="submit" name="change_password" class="btn btn-primary">
                            🔐 تغییر رمز عبور
                        </button>
                    </form>
                </div>
            
            <?php endif; ?>
            
        </main>
        
    </div>
    
</div>

<style>
.dima-account-page {
    max-width: 1200px;
    margin: 0 auto;
    padding: 30px 20px;
    font-family: 'Vazir', Tahoma, sans-serif;
}

.account-header {
    background: linear-gradient(135deg, #6366f1, #a855f7);
    border-radius: 20px;
    padding: 30px;
    margin-bottom: 30px;
    color: white;
}

.user-welcome {
    display: flex;
    align-items: center;
    gap: 20px;
}

.user-avatar-large img {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    border: 4px solid white;
}

.user-info h1 {
    font-size: 28px;
    margin: 0 0 5px 0;
}

.user-email {
    margin: 0;
    opacity: 0.9;
    font-size: 14px;
}

.account-layout {
    display: grid;
    grid-template-columns: 280px 1fr;
    gap: 30px;
}

@media (max-width: 968px) {
    .account-layout {
        grid-template-columns: 1fr;
    }
}

/* Sidebar */
.account-sidebar {
    background: white;
    border-radius: 16px;
    padding: 20px;
    box-shadow: 0 2px 12px rgba(0,0,0,0.06);
    height: fit-content;
    position: sticky;
    top: 100px;
}

.account-nav {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.nav-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    border-radius: 10px;
    text-decoration: none;
    color: #4b5563;
    font-weight: 600;
    font-size: 14px;
    transition: all 0.2s;
}

.nav-item:hover {
    background: #f3f4f6;
    color: #6366f1;
}

.nav-item.active {
    background: linear-gradient(135deg, #6366f1, #a855f7);
    color: white;
}

.nav-icon {
    font-size: 20px;
}

.nav-text {
    flex: 1;
}

.nav-badge {
    background: rgba(99, 102, 241, 0.1);
    color: #6366f1;
    padding: 2px 8px;
    border-radius: 12px;
    font-size: 12px;
    font-weight: 700;
}

.nav-item.active .nav-badge {
    background: rgba(255,255,255,0.2);
    color: white;
}

.nav-item.logout {
    color: #ef4444;
    margin-top: 10px;
    border-top: 1px solid #e5e7eb;
    padding-top: 15px;
}

/* Content */
.account-content {
    background: white;
    border-radius: 16px;
    padding: 30px;
    box-shadow: 0 2px 12px rgba(0,0,0,0.06);
}

.account-section h2 {
    font-size: 22px;
    margin: 0 0 25px 0;
    padding-bottom: 15px;
    border-bottom: 2px solid #f3f4f6;
}

.account-section h3 {
    font-size: 18px;
    margin: 25px 0 15px 0;
}

/* Dashboard Stats */
.dashboard-stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 15px;
    margin-bottom: 30px;
}

.stat-card {
    background: linear-gradient(135deg, #f9fafb, #f3f4f6);
    padding: 20px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    gap: 15px;
}

.stat-icon {
    font-size: 32px;
}

.stat-value {
    font-size: 24px;
    font-weight: 800;
    color: #1f2937;
}

.stat-label {
    font-size: 13px;
    color: #6b7280;
}

/* Orders */
.orders-list {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin-bottom: 20px;
}

.order-item {
    display: flex;
    align-items: center;
    gap: 15px;
    padding: 15px;
    background: #f9fafb;
    border-radius: 10px;
    border: 1px solid #e5e7eb;
}

.order-item.full {
    justify-content: space-between;
    flex-wrap: wrap;
}

.order-info {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.order-id {
    font-weight: 700;
    color: #1f2937;
}

.order-date {
    font-size: 13px;
    color: #6b7280;
}

.order-status {
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 600;
    color: white;
}

.order-total {
    font-weight: 700;
    color: #6366f1;
}

/* Order Details */
.order-details-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
    margin-bottom: 30px;
}

@media (max-width: 768px) {
    .order-details-grid {
        grid-template-columns: 1fr;
    }
}

.detail-box {
    background: #f9fafb;
    padding: 20px;
    border-radius: 12px;
}

.detail-box h3 {
    margin: 0 0 15px 0;
    font-size: 16px;
}

.detail-row {
    display: flex;
    justify-content: space-between;
    padding: 10px 0;
    border-bottom: 1px solid #e5e7eb;
    font-size: 14px;
}

.detail-row:last-child {
    border-bottom: none;
}

.detail-row.final {
    font-size: 16px;
    font-weight: 700;
    color: #6366f1;
}

.order-items-table {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.order-item-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 15px;
    background: #f9fafb;
    border-radius: 8px;
}

.item-name {
    font-weight: 600;
    flex: 1;
}

.item-qty {
    color: #6b7280;
    margin: 0 15px;
}

.item-price {
    font-weight: 700;
    color: #1f2937;
}

/* Rewards */
.rewards-summary {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
    margin-bottom: 30px;
}

.rewards-points, .rewards-value {
    background: linear-gradient(135deg, #eef2ff, #faf5ff);
    padding: 30px;
    border-radius: 16px;
    text-align: center;
}

.points-value, .value-amount {
    font-size: 36px;
    font-weight: 900;
    color: #6366f1;
    margin-bottom: 8px;
}

.points-label, .value-label {
    font-size: 14px;
    color: #6b7280;
}

.redeem-section {
    background: #f9fafb;
    padding: 20px;
    border-radius: 12px;
    margin-bottom: 30px;
}

.rewards-history {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.history-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px;
    background: #f9fafb;
    border-radius: 10px;
}

.history-info {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.history-type {
    font-weight: 700;
    font-size: 14px;
}

.history-desc {
    font-size: 13px;
    color: #6b7280;
}

.history-points {
    font-weight: 800;
    font-size: 18px;
}

.history-date {
    font-size: 12px;
    color: #6b7280;
}

/* Forms */
.form-group {
    margin-bottom: 20px;
}

.form-group label {
    display: block;
    font-weight: 600;
    margin-bottom: 8px;
    color: #374151;
}

.form-group input,
.form-group select,
.form-group textarea {
    width: 100%;
    padding: 12px 14px;
    border: 2px solid #e5e7eb;
    border-radius: 10px;
    font-family: inherit;
    font-size: 14px;
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
    outline: none;
    border-color: #6366f1;
}

.form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 15px;
}

@media (max-width: 768px) {
    .form-row {
        grid-template-columns: 1fr;
    }
}

/* Alerts */
.account-alert {
    padding: 15px 20px;
    border-radius: 10px;
    margin-bottom: 20px;
}

.account-alert.success {
    background: #d1fae5;
    color: #065f46;
    border: 1px solid #a7f3d0;
}

.account-alert.error {
    background: #fee2e2;
    color: #991b1b;
    border: 1px solid #fecaca;
}

/* Empty */
.empty-box {
    text-align: center;
    padding: 50px 20px;
    background: #f9fafb;
    border-radius: 12px;
    border: 2px dashed #e5e7eb;
}

.empty-box .empty-icon {
    font-size: 56px;
    margin-bottom: 15px;
    opacity: 0.5;
}

.empty-box p {
    color: #6b7280;
    margin-bottom: 20px;
}

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px 24px;
    border-radius: 10px;
    font-family: inherit;
    font-weight: 600;
    font-size: 14px;
    cursor: pointer;
    border: none;
    text-decoration: none;
    transition: all 0.2s;
}

.btn-primary {
    background: linear-gradient(135deg, #6366f1, #a855f7);
    color: white;
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 20px rgba(99, 102, 241, 0.3);
}

.btn-outline {
    background: white;
    color: #6366f1;
    border: 2px solid #6366f1;
}

.btn-outline:hover {
    background: #6366f1;
    color: white;
}

.btn-small {
    padding: 8px 16px;
    font-size: 13px;
}
</style>

<script>
jQuery(document).ready(function($) {
    // تبدیل امتیاز به کوپن
    $('#redeem-points-btn').on('click', function() {
        var $btn = $(this);
        var points = $btn.data('points');
        
        if (!confirm('آیا از تبدیل ' + points + ' امتیاز به کوپن تخفیف مطمئن هستید؟')) {
            return;
        }
        
        $btn.prop('disabled', true).text('⏳ در حال پردازش...');
        
        $.ajax({
            url: dima_shop.ajax_url,
            type: 'POST',
            data: {
                action: 'dima_redeem_rewards',
                points: points,
                nonce: dima_shop.nonce
            },
            success: function(response) {
                if (response.success) {
                    alert('✅ ' + response.data.message + '\n\nکد کوپن: ' + response.data.coupon_code);
                    location.reload();
                } else {
                    alert('❌ ' + response.data.message);
                    $btn.prop('disabled', false).text('️ تبدیل ۱۰۰۰ امتیاز به کوپن');
                }
            }
        });
    });
});
</script>