<?php
/**
 * Wizard Step 2: Product/Inventory
 * 
 * @package WahraRentalsBookings
 */

defined('ABSPATH') || exit;

$step2 = $wizard_data['step_2'] ?? [];
$rental_description = $step2['rental_description'] ?? '';
$total_units = absint($step2['total_units'] ?? 1);

// Check if product is already selected (from initial screen or URL)
$selected_product_id = absint($wizard_data['product_id'] ?? ($step2['product_id'] ?? 0));
$existing_product = $selected_product_id ? wc_get_product($selected_product_id) : null;
$product_name = $existing_product ? $existing_product->get_name() : ($step2['product_name'] ?? '');
$has_product = !empty($selected_product_id) && $existing_product;

// Get vendor's products only if no product selected
$vendor_products = [];
if (!$has_product) {
    $vendor_products = wc_get_products([
        'author' => $vendor_id,
        'limit' => 100,
        'status' => 'publish',
        'orderby' => 'title',
        'order' => 'ASC',
    ]);
}
?>

<div class="wahrb-step wahrb-step-2" data-step="2">
    <?php if ($has_product): ?>
        <!-- Product already selected from initial screen -->
        <div class="wahrb-step-header">
            <h2><?php esc_html_e('Rental configuration', 'wahra-rentals-bookings'); ?></h2>
        </div>

        <!-- Selected Product Card -->
        <div class="wahrb-selected-product-card" style="background: linear-gradient(135deg, #f0f9ff 0%, #e0f2fe 100%); border: 2px solid #0ea5e9; border-radius: 12px; padding: 20px; margin-bottom: 25px;">
            <div style="display: flex; align-items: center; gap: 15px;">
                <?php 
                $thumbnail = $existing_product->get_image_id() ? wp_get_attachment_image_url($existing_product->get_image_id(), 'thumbnail') : wc_placeholder_img_src('thumbnail');
                ?>
                <img src="<?php echo esc_url($thumbnail); ?>" alt="" style="width: 60px; height: 60px; object-fit: cover; border-radius: 8px; border: 2px solid #fff; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
                <div style="flex: 1;">
                    <span style="font-size: 12px; color: #0369a1; font-weight: 500; text-transform: uppercase; letter-spacing: 0.5px;">
                        <?php esc_html_e('Selected product', 'wahra-rentals-bookings'); ?>
                    </span>
                    <h3 style="margin: 5px 0 0 0; font-size: 18px; color: #1e3a5f;"><?php echo esc_html($product_name); ?></h3>
                </div>
                <span style="font-size: 28px;">📦</span>
            </div>
            <input type="hidden" name="use_existing" value="1">
            <input type="hidden" name="product_id" value="<?php echo esc_attr($selected_product_id); ?>">
            <input type="hidden" name="product_name" value="<?php echo esc_attr($product_name); ?>">
        </div>

        <p style="color: #64748b; font-size: 14px; margin-bottom: 20px;">
            <?php esc_html_e('Now add the rental-specific details for this item.', 'wahra-rentals-bookings'); ?>
        </p>

    <?php else: ?>
        <!-- No product pre-selected - show selection -->
        <div class="wahrb-step-header">
            <h2><?php esc_html_e('Product selection', 'wahra-rentals-bookings'); ?></h2>
            <p class="wahrb-step-description"><?php esc_html_e('Choose the product to offer for rent', 'wahra-rentals-bookings'); ?></p>
        </div>

        <div class="wahrb-form-group">
            <label for="product_id"><?php esc_html_e('Product to rent', 'wahra-rentals-bookings'); ?> <span class="required">*</span></label>
            <select name="product_id" id="product_id" class="wahrb-form-control" required>
                <option value=""><?php esc_html_e('-- Select a product --', 'wahra-rentals-bookings'); ?></option>
                <?php foreach ($vendor_products as $product): ?>
                    <option value="<?php echo esc_attr($product->get_id()); ?>" 
                            data-name="<?php echo esc_attr($product->get_name()); ?>"
                            <?php selected($selected_product_id, $product->get_id()); ?>>
                        <?php echo esc_html($product->get_name()); ?>
                    </option>
                <?php endforeach; ?>
            </select>
            <?php if (empty($vendor_products)): ?>
                <p class="wahrb-alert-warning" style="margin-top: 10px; padding: 10px; background: #fef3c7; border-radius: 6px;">
                    <?php esc_html_e('You don\'t have any products yet. Create a product in your store first.', 'wahra-rentals-bookings'); ?>
                </p>
            <?php endif; ?>
            <input type="hidden" name="use_existing" value="1">
        </div>
    <?php endif; ?>

    <hr style="border: none; border-top: 1px solid #e2e8f0; margin: 25px 0;">

    <!-- Rental Description -->
    <div class="wahrb-form-group">
        <label for="rental_description">
            <span style="font-size: 16px;">📝</span> <?php esc_html_e('Rental description', 'wahra-rentals-bookings'); ?>
        </label>
        <p class="wahrb-help-text" style="margin: 5px 0 10px 0; color: #64748b; font-size: 13px;">
            <?php esc_html_e('Rental-specific information: conditions, what\'s included, instructions...', 'wahra-rentals-bookings'); ?>
        </p>
        <textarea name="rental_description" id="rental_description" class="wahrb-form-control" rows="4" 
                  placeholder="<?php esc_attr_e('Ex: Rental includes cleaning. Pickup available from 10am...', 'wahra-rentals-bookings'); ?>"><?php echo esc_textarea($rental_description); ?></textarea>
    </div>

    <!-- Inventory -->
    <div class="wahrb-form-group">
        <label for="total_units">
            <span style="font-size: 16px;">📦</span> <?php esc_html_e('Available units', 'wahra-rentals-bookings'); ?>
        </label>
        <div style="display: flex; align-items: center; gap: 15px;">
            <input type="number" name="total_units" id="total_units" class="wahrb-form-control" 
                   style="width: 100px; text-align: center; font-size: 18px; font-weight: 600;"
                   value="<?php echo esc_attr($total_units); ?>" min="1" max="999">
            <span style="color: #64748b; font-size: 14px;">
                <?php esc_html_e('units that can be rented simultaneously', 'wahra-rentals-bookings'); ?>
            </span>
        </div>
    </div>
</div>

<?php
// Step 2 JS is handled by wahrb-wizard-steps.js (enqueued in booking-wizard.php)
?>

