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

class VS_Restore
{

    public static function render($user)
    {

        $args = [
            'post_type' => 'product',
            'post_status' => 'trash',
            'posts_per_page' => -1,
        ];

        // If not admin, only show own trashed products
        if (!current_user_can('administrator')) {
            $args['author'] = $user->ID;
        }

        $trashed_products = get_posts($args);
        $count = count($trashed_products);

        ob_start();
        ?>

        <div id="tab-restore-products" class="vs-tab" style="display:none;">

            <div class="vs-dashboard-header"
                style="display:flex; justify-content:space-between; align-items:center; margin-bottom:20px; border-bottom: 2px solid #2563eb; padding-bottom: 10px;">

                <h2 style="margin:0; font-size:24px; color:#1e293b;">
                    <?php echo esc_html__('Restore Products', 'vendor-space'); ?></h2>

                <div style="display:flex; gap:10px; align-items:center;">

                    <button id="vs-restore-selected" disabled
                        style="background:#10b981;color:#fff;border:none;padding:8px 16px;border-radius:8px;cursor:not-allowed;opacity:0.5;font-size:13px;font-weight:600;display:flex;align-items:center;gap:6px;transition:0.2s;">
                        <i class="fas fa-undo-alt"></i> <?php echo esc_html__('Restore Selected', 'vendor-space'); ?>
                    </button>

                    <button id="vs-delete-selected-restore" disabled
                        style="background:#ef4444;color:#fff;border:none;padding:8px 16px;border-radius:8px;cursor:not-allowed;opacity:0.5;font-size:13px;font-weight:600;display:flex;align-items:center;gap:6px;transition:0.2s;">
                        <i class="fas fa-trash-alt"></i> <?php echo esc_html__('Delete Selected', 'vendor-space'); ?>
                    </button>

                    <div class="vs-restore-stats"
                        style="background:#f1f5f9;padding:8px 16px;border-radius:8px;font-weight:600;color:#475569;font-size:14px;">
                        <i class="fas fa-trash-alt" style="margin-right:8px;"></i>
                        <?php echo esc_html($count . ' ' . __('In Trash', 'vendor-space')); ?>
                    </div>

                </div>

            </div>

            <input type="hidden" name="security" value="<?php echo wp_create_nonce('vs_delete_product_nonce'); ?>">

            <div class="vs-card" style="padding:0;overflow:hidden;border-radius:12px;border:1px solid #e2e8f0;background:#fff;">

                <?php if (empty($trashed_products)): ?>

                    <div style="text-align:center;padding:80px 40px;">

                        <div
                            style="width:80px;height:80px;background:#f8fafc;border-radius:50%;display:flex;align-items:center;justify-content:center;margin:0 auto 20px;">
                            <i class="fas fa-trash-restore-alt" style="font-size:32px;color:#cbd5e1;"></i>
                        </div>

                        <h3 style="margin:0;color:#1e293b;"><?php echo esc_html__('Trash is Empty', 'vendor-space'); ?></h3>

                        <p style="color:#64748b;margin-top:8px;">
                            <?php echo esc_html__('You do not have any deleted products at the moment.', 'vendor-space'); ?>
                        </p>

                    </div>

                <?php else: ?>

                    <table class="vs-products-table" style="width:100%;border-collapse:collapse;margin-top:0;">

                        <thead>

                            <tr style="background: #f8fafc; border-bottom: 1px solid #e2e8f0;">

                                <th style="padding: 15px 20px; font-weight: 600; color: #475569;background-color: #f8fafc;">
                                    <input type="checkbox" id="vs-select-all-restore"
                                        style="width:18px;height:18px;cursor:pointer;">
                                </th>

                                <th style="padding: 15px 20px; font-weight: 600; color: #475569;background-color: #f8fafc;">
                                    <?php echo esc_html__('Product', 'vendor-space'); ?></th>

                                <?php if (current_user_can('administrator')): ?>
                                    <th style="padding: 15px 20px; font-weight: 600; color: #475569;background-color: #f8fafc;">
                                        <?php echo esc_html__('Vendor', 'vendor-space'); ?></th>
                                <?php endif; ?>

                                <th style="padding: 15px 20px; font-weight: 600; color: #475569;background-color: #f8fafc;">
                                    <?php echo esc_html__('Price', 'vendor-space'); ?></th>

                                <th style="padding: 15px 20px; font-weight: 600; color: #475569;background-color: #f8fafc;">
                                    <?php echo esc_html__('Actions', 'vendor-space'); ?></th>

                            </tr>

                        </thead>

                        <tbody>

                            <?php foreach ($trashed_products as $p):

                                $price = get_post_meta($p->ID, '_price', true);
                                $vendor = get_userdata($p->post_author);
                                $img = get_the_post_thumbnail($p->ID, [50, 50]) ?: '<i class="fas fa-box" style="font-size:30px;color:#cbd5e1;"></i>';

                                ?>

                                <tr id="vs-restore-row-<?php echo $p->ID; ?>"
                                    style="border-bottom:1px solid #f1f5f9;transition:background 0.2s;">

                                    <td style="padding:15px;">
                                        <input type="checkbox" class="vs-select-product-restore" value="<?php echo $p->ID; ?>"
                                            style="width:18px;height:18px;cursor:pointer;">
                                    </td>

                                    <td style="padding:15px;">

                                        <div style="display:flex;align-items:center;gap:15px;">

                                            <div
                                                style="width:50px;height:50px;border-radius:8px;overflow:hidden;border:1px solid #f1f5f9;flex-shrink:0;">
                                                <?php echo $img; ?>
                                            </div>

                                            <span style="font-weight:600;color:#1e293b;">
                                                <?php echo esc_html($p->post_title); ?>
                                            </span>

                                        </div>

                                    </td>

                                    <?php if (current_user_can('administrator')): ?>

                                        <td style="padding:15px;">

                                            <span
                                                style="background:#e0f2fe;color:#0369a1;padding:4px 10px;border-radius:6px;font-size:12px;font-weight:600;">

                                                <?php echo esc_html($vendor ? $vendor->display_name : __('Guest', 'vendor-space')); ?>

                                            </span>

                                        </td>

                                    <?php endif; ?>

                                    <td style="padding:15px;font-weight:600;color:#1e293b;">
                                        <?php echo wc_price($price); ?>
                                    </td>

                                    <td style="padding:15px;text-align:right;">

                                        <div style="display:inline-flex;gap:10px;">

                                            <button class="vs-restore-btn" data-product-id="<?php echo $p->ID; ?>"
                                                style="background:#10b981;color:#fff;border:none;padding:8px 16px;border-radius:8px;cursor:pointer;font-size:13px;font-weight:600;display:flex;align-items:center;gap:6px;transition:0.2s;">

                                                <i class="fas fa-undo-alt"></i> <?php echo esc_html__('Restore', 'vendor-space'); ?>

                                            </button>

                                            <button class="vs-delete-btn" data-product-id="<?php echo $p->ID; ?>" data-mode="permanent"
                                                style="background:#fff;color:#ef4444;border:1px solid #fca5a5;padding:8px 16px;border-radius:8px;cursor:pointer;font-size:13px;font-weight:600;display:flex;align-items:center;gap:6px;transition:0.2s;">

                                                <i class="fas fa-trash-alt"></i> <?php echo esc_html__('Delete', 'vendor-space'); ?>

                                            </button>

                                        </div>

                                    </td>

                                </tr>

                            <?php endforeach; ?>

                        </tbody>

                    </table>

                    <div class="vs-orders-pagination" id="vs-restore-pagination"
                        style="margin:15px 20px 18px;display:flex;gap:8px;justify-content:flex-end;flex-wrap:wrap;">
                    </div>

                <?php endif; ?>

            </div>

        </div>

        <?php
        return ob_get_clean();
    }
}
