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

global $wpdb;
$flash = Dima_Flash_Sale::get_instance();

// افزودن شگفت‌انگیز جدید
if (isset($_POST['add_flash_sale']) && check_admin_referer('dima_flash_sale_nonce')) {
    $product_id = intval($_POST['product_id']);
    $sale_price = floatval($_POST['sale_price']);
    $start_time = sanitize_text_field($_POST['start_time']);
    $end_time = sanitize_text_field($_POST['end_time']);
    $stock_limit = intval($_POST['stock_limit']);
    
    $result = $flash->create_flash_sale($product_id, $sale_price, $start_time, $end_time, $stock_limit);
    
    if ($result) {
        echo '<div class="notice notice-success"><p>محصول شگفت‌انگیز اضافه شد.</p></div>';
    } else {
        echo '<div class="notice notice-error"><p>خطا در ایجاد.</p></div>';
    }
}

// حذف
if (isset($_GET['delete']) && check_admin_referer('dima_delete_flash')) {
    $flash->delete_flash_sale(intval($_GET['delete']));
    echo '<div class="notice notice-success"><p>حذف شد.</p></div>';
}

$sales = $flash->get_all_sales();
?>

<div class="wrap">
    <h1>🔥 محصولات شگفت‌انگیز</h1>
    
    <!-- فرم افزودن -->
    <div class="dima-form-box">
        <h2>➕ افزودن محصول شگفت‌انگیز جدید</h2>
        <form method="post">
            <?php wp_nonce_field('dima_flash_sale_nonce'); ?>
            <table class="form-table">
                <tr>
                    <th>محصول:</th>
                    <td>
                        <select name="product_id" required style="min-width:300px;">
                            <option value="">— انتخاب محصول —</option>
                            <?php
                            $products = get_posts(array('post_type' => 'dima_product', 'posts_per_page' => -1, 'post_status' => 'publish'));
                            foreach ($products as $p):
                            ?>
                                <option value="<?php echo $p->ID; ?>"><?php echo esc_html($p->post_title); ?> - <?php echo number_format(Dima_Product::get_price($p->ID)); ?> تومان</option>
                            <?php endforeach; ?>
                        </select>
                    </td>
                </tr>
                <tr>
                    <th>قیمت تخفیف‌دار (تومان):</th>
                    <td><input type="number" name="sale_price" required min="0" class="regular-text"></td>
                </tr>
                <tr>
                    <th>زمان شروع:</th>
                    <td><input type="datetime-local" name="start_time" required class="regular-text"></td>
                </tr>
                <tr>
                    <th>زمان پایان:</th>
                    <td><input type="datetime-local" name="end_time" required class="regular-text"></td>
                </tr>
                <tr>
                    <th>محدودیت موجودی (۰ = نامحدود):</th>
                    <td><input type="number" name="stock_limit" value="0" min="0" class="small-text"></td>
                </tr>
            </table>
            <?php submit_button('افزودن شگفت‌انگیز', 'primary', 'add_flash_sale'); ?>
        </form>
    </div>
    
    <!-- لیست -->
    <h2>📋 لیست شگفت‌انگیزها</h2>
    
    <?php if (empty($sales)): ?>
    <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>
                    <th>وضعیت</th>
                    <th>عملیات</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($sales as $sale): 
                    $original = Dima_Product::get_price($sale->product_id);
                    $discount = $original > 0 ? round((($original - $sale->sale_price) / $original) * 100) : 0;
                ?>
                <tr>
                    <td>#<?php echo intval($sale->id); ?></td>
                    <td><?php echo esc_html($sale->product_title ?: 'حذف شده'); ?></td>
                    <td><del><?php echo number_format($original); ?></del></td>
                    <td><strong style="color:#ef4444;"><?php echo number_format($sale->sale_price); ?> تومان</strong></td>
                    <td><span style="background:#ef4444; color:white; padding:3px 8px; border-radius:12px; font-size:12px; font-weight:700;">-<?php echo $discount; ?>٪</span></td>
                    <td><?php echo intval($sale->sold_count); ?> / <?php echo $sale->stock_limit ?: '∞'; ?></td>
                    <td><?php echo date_i18n('j F H:i', strtotime($sale->start_time)); ?></td>
                    <td><?php echo date_i18n('j F H:i', strtotime($sale->end_time)); ?></td>
                    <td>
                        <span style="background:<?php echo $sale->status === 'active' ? '#10b981' : '#6b7280'; ?>; color:white; padding:4px 12px; border-radius:20px; font-size:12px;">
                            <?php echo $sale->status === 'active' ? 'فعال' : 'منقضی'; ?>
                        </span>
                    </td>
                    <td>
                        <a href="<?php echo wp_nonce_url(admin_url('admin.php?page=dima-shop-flash-sales&delete=' . $sale->id), 'dima_delete_flash'); ?>" class="button button-small button-link-delete" onclick="return confirm('حذف؟');">🗑️</a>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?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; }
</style>