<?php
/**
 * Saved Carts My Account Page Template
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

use Lean_CSNS\Database;

$user_id = get_current_user_id();
$saved_carts = Database::get_instance()->get_saved_carts_by_user($user_id);
?>

<h3><?php echo esc_html(__('Saved Carts', 'lean-cart-share-and-save')); ?></h3>

<?php if (empty($saved_carts)): ?>
    <div class="woocommerce-message woocommerce-message--info woocommerce-Message woocommerce-Message--info">
        <p><?php echo esc_html(__('No saved carts found.', 'lean-cart-share-and-save')); ?></p>
        <p>
            <a href="<?php echo esc_url(wc_get_page_permalink('shop')); ?>" class="button">
                <?php echo esc_html(__('Start Shopping', 'lean-cart-share-and-save')); ?>
            </a>
        </p>
    </div>
<?php else: ?>
    <table class="woocommerce-orders-table woocommerce-MyAccount-orders shop_table shop_table_responsive my_account_orders account-orders-table">
        <thead>
            <tr>
                <th class="woocommerce-orders-table__header woocommerce-orders-table__header-cart-name">
                    <span class="nobr"><?php echo esc_html(__('Cart Name', 'lean-cart-share-and-save')); ?></span>
                </th>
                <th class="woocommerce-orders-table__header woocommerce-orders-table__header-date">
                    <span class="nobr"><?php echo esc_html(__('Saved On', 'lean-cart-share-and-save')); ?></span>
                </th>
                <th class="woocommerce-orders-table__header woocommerce-orders-table__header-actions">
                    <span class="nobr"><?php echo esc_html(__('Actions', 'lean-cart-share-and-save')); ?></span>
                </th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($saved_carts as $cart): ?>
                <tr class="woocommerce-orders-table__row woocommerce-orders-table__row--status-<?php echo esc_attr('saved'); ?>">
                    <td class="woocommerce-orders-table__cell woocommerce-orders-table__cell-cart-name" data-title="<?php echo esc_attr(__('Cart Name', 'lean-cart-share-and-save')); ?>">
                        <strong><?php echo esc_html($cart->save_name); ?></strong>
                    </td>
                    <td class="woocommerce-orders-table__cell woocommerce-orders-table__cell-date" data-title="<?php echo esc_attr(__('Saved On', 'lean-cart-share-and-save')); ?>">
                        <time datetime="<?php echo esc_attr(gmdate('c', strtotime($cart->created_at))); ?>">
                            <?php echo esc_html(date_i18n(get_option('date_format'), strtotime($cart->created_at))); ?>
                        </time>
                    </td>
                    <td class="woocommerce-orders-table__cell woocommerce-orders-table__cell-actions" data-title="<?php echo esc_attr(__('Actions', 'lean-cart-share-and-save')); ?>">
                        <a href="<?php echo esc_url(add_query_arg('share', $cart->share_token, wc_get_cart_url())); ?>" 
                            class="woocommerce-button button restore-cart"
                            title="<?php echo esc_attr(__('Restore this cart', 'lean-cart-share-and-save')); ?>">
                            <?php echo esc_html(__('Restore', 'lean-cart-share-and-save')); ?>
                        </a>
                        <a href="<?php echo esc_url(wp_nonce_url(add_query_arg(['action' => 'delete_saved_cart', 'cart_id' => $cart->id]), 'delete_cart_' . $cart->id)); ?>" 
                            class="woocommerce-button button delete-cart"
                            title="<?php echo esc_attr(__('Delete this cart', 'lean-cart-share-and-save')); ?>"
                            onclick="return confirm('<?php echo esc_js(__('Are you sure you want to delete this saved cart? This action cannot be undone.', 'lean-cart-share-and-save')); ?>')">
                            <?php echo esc_html(__('Delete', 'lean-cart-share-and-save')); ?>
                        </a>
                    </td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
<?php endif; ?>