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

global $wpdb;
$table = $wpdb->prefix . 'dima_coupons';

// افزودن کوپن
if (isset($_POST['add_coupon']) && check_admin_referer('dima_coupon_nonce')) {
    $wpdb->insert($table, array(
        'code' => strtoupper(sanitize_text_field($_POST['coupon_code'])),
        'type' => sanitize_text_field($_POST['coupon_type']),
        'value' => floatval($_POST['coupon_value']),
        'min_amount' => floatval($_POST['min_amount']),
        'max_uses' => intval($_POST['max_uses']),
        'expiry_date' => !empty($_POST['expiry_date']) ? sanitize_text_field($_POST['expiry_date']) : null,
        'status' => 'active'
    ));
    echo '<div class="notice notice-success"><p>کوپن ایجاد شد.</p></div>';
}

// حذف
if (isset($_GET['delete']) && check_admin_referer('dima_delete_coupon')) {
    $wpdb->delete($table, array('id' => intval($_GET['delete'])));
    echo '<div class="notice notice-success"><p>کوپن حذف شد.</p></div>';
}

$coupons = $wpdb->get_results("SELECT * FROM $table ORDER BY created_at DESC");
?>

<div class="wrap">
    <h1>🎟️ کوپن‌های تخفیف</h1>
    
    <div class="dima-form-box">
        <h2>➕ ایجاد کوپن جدید</h2>
        <form method="post">
            <?php wp_nonce_field('dima_coupon_nonce'); ?>
            <table class="form-table">
                <tr><th>کد کوپن:</th><td><input type="text" name="coupon_code" required class="regular-text" style="text-transform:uppercase;"></td></tr>
                <tr>
                    <th>نوع تخفیف:</th>
                    <td>
                        <select name="coupon_type">
                            <option value="percent">درصدی (%)</option>
                            <option value="fixed">مبلغ ثابت (تومان)</option>
                        </select>
                    </td>
                </tr>
                <tr><th>مقدار:</th><td><input type="number" name="coupon_value" required step="0.01" min="0" class="regular-text"></td></tr>
                <tr><th>حداقل مبلغ خرید:</th><td><input type="number" name="min_amount" step="0.01" min="0" value="0" class="regular-text"></td></tr>
                <tr><th>حداکثر استفاده (۰ = نامحدود):</th><td><input type="number" name="max_uses" min="0" value="0" class="small-text"></td></tr>
                <tr><th>تاریخ انقضا:</th><td><input type="date" name="expiry_date" class="regular-text"></td></tr>
            </table>
            <?php submit_button('ایجاد کوپن', 'primary', 'add_coupon'); ?>
        </form>
    </div>
    
    <h2>📋 لیست کوپن‌ها</h2>
    
    <?php if (empty($coupons)): ?>
    <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 ($coupons as $c): ?>
                <tr>
                    <td><strong style="color:#6366f1; font-family:monospace;"><?php echo esc_html($c->code); ?></strong></td>
                    <td><?php echo $c->type === 'percent' ? 'درصدی' : 'مبلغ ثابت'; ?></td>
                    <td>
                        <?php echo $c->type === 'percent' ? $c->value . '٪' : number_format($c->value) . ' تومان'; ?>
                    </td>
                    <td><?php echo number_format($c->min_amount); ?> تومان</td>
                    <td><?php echo intval($c->used_count); ?> / <?php echo $c->max_uses ?: '∞'; ?></td>
                    <td><?php echo $c->expiry_date ? date_i18n('j F Y', strtotime($c->expiry_date)) : 'نامحدود'; ?></td>
                    <td>
                        <span style="background:<?php echo $c->status === 'active' ? '#10b981' : '#6b7280'; ?>; color:white; padding:4px 12px; border-radius:20px; font-size:12px;">
                            <?php echo $c->status === 'active' ? 'فعال' : 'غیرفعال'; ?>
                        </span>
                    </td>
                    <td>
                        <a href="<?php echo wp_nonce_url(admin_url('admin.php?page=dima-shop-coupons&delete=' . $c->id), 'dima_delete_coupon'); ?>" 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>