<?php
/**
 * Wizard Step 7: Review & Publish
 * 
 * @package WahraRentalsBookings
 */

defined('ABSPATH') || exit;

$step6 = $wizard_data['step_6'] ?? [];
$cancellation_policy = $step6['cancellation_policy'] ?? 'flexible';
$terms_accepted = !empty($step6['terms_accepted']);

// Get admin terms settings
$general_settings = get_option('wahrb_settings_general', []);
$terms_url = $general_settings['terms_url'] ?? '';
$vendor_consent_text = $general_settings['vendor_consent_text'] ?? __('I accept the terms and conditions', 'wahra-rentals-bookings');
$terms_required = $general_settings['terms_required'] ?? true;

// Get data from previous steps for summary
$offer_type = $wizard_data['step_1']['offer_type'] ?? 'day';

// Get product name - check multiple sources
$product_name = $wizard_data['step_2']['product_name'] ?? '';
if (empty($product_name) && !empty($wizard_data['product_id'])) {
    $existing_product = wc_get_product($wizard_data['product_id']);
    if ($existing_product) {
        $product_name = $existing_product->get_name();
    }
}
if (empty($product_name)) {
    $product_name = __('New product', 'wahra-rentals-bookings');
}

$base_price = floatval($wizard_data['step_4']['base_price'] ?? 0);
$currency_symbol = html_entity_decode(get_woocommerce_currency_symbol());

$policies = [
    'flexible' => [
        'title' => __('Flexible', 'wahra-rentals-bookings'),
        'description' => __('Free cancellation up to 24h before', 'wahra-rentals-bookings'),
    ],
    'moderate' => [
        'title' => __('Moderate', 'wahra-rentals-bookings'),
        'description' => __('Free cancellation up to 5 days before, 50% after', 'wahra-rentals-bookings'),
    ],
    'strict' => [
        'title' => __('Strict', 'wahra-rentals-bookings'),
        'description' => __('No refund after confirmation', 'wahra-rentals-bookings'),
    ],
];

$offer_types = [
    'day' => __('Daily rental', 'wahra-rentals-bookings'),
    'slot' => __('Time slot', 'wahra-rentals-bookings'),
    'service' => __('One-time service', 'wahra-rentals-bookings'),
];
?>

<div class="wahrb-step wahrb-step-7" data-step="7">
    <div class="wahrb-step-header">
        <h2><?php esc_html_e('Summary & Publication', 'wahra-rentals-bookings'); ?></h2>
        <p class="wahrb-step-description"><?php esc_html_e('Review the information and publish your listing', 'wahra-rentals-bookings'); ?></p>
    </div>

    <!-- Summary Card -->
    <div class="wahrb-summary-card">
        <h3><?php esc_html_e('Summary of your listing', 'wahra-rentals-bookings'); ?></h3>
        
        <div class="wahrb-summary-grid">
            <div class="wahrb-summary-item">
                <span class="wahrb-summary-label"><?php esc_html_e('Product', 'wahra-rentals-bookings'); ?></span>
                <span class="wahrb-summary-value"><?php echo esc_html($product_name); ?></span>
            </div>
            <div class="wahrb-summary-item">
                <span class="wahrb-summary-label"><?php esc_html_e('Type', 'wahra-rentals-bookings'); ?></span>
                <span class="wahrb-summary-value"><?php echo esc_html($offer_types[$offer_type] ?? $offer_type); ?></span>
            </div>
            <div class="wahrb-summary-item">
                <span class="wahrb-summary-label"><?php esc_html_e('Price', 'wahra-rentals-bookings'); ?></span>
                <span class="wahrb-summary-value wahrb-summary-price"><?php echo esc_html($currency_symbol . ' ' . number_format($base_price, 2)); ?></span>
            </div>
        </div>
        
        <?php if ($base_price <= 0): ?>
            <div class="wahrb-alert wahrb-alert-info" style="margin-top: 15px;">
                <span class="wahrb-alert-icon">ℹ️</span>
                <span><?php esc_html_e('Warning: The base price is set to 0. This item will be free.', 'wahra-rentals-bookings'); ?></span>
            </div>
        <?php endif; ?>
    </div>

    <!-- Cancellation Policy -->
    <div class="wahrb-form-group">
        <label><?php esc_html_e('Cancellation policy', 'wahra-rentals-bookings'); ?></label>
        <div class="wahrb-policy-options">
            <?php foreach ($policies as $key => $policy): ?>
                <label class="wahrb-policy-card <?php echo esc_attr( $cancellation_policy === $key ? 'selected' : '' ); ?>">
                    <input type="radio" name="cancellation_policy" value="<?php echo esc_attr($key); ?>" 
                           <?php checked($cancellation_policy, $key); ?>>
                    <div class="wahrb-policy-content">
                        <strong><?php echo esc_html($policy['title']); ?></strong>
                        <span><?php echo esc_html($policy['description']); ?></span>
                    </div>
                </label>
            <?php endforeach; ?>
            
            <label class="wahrb-policy-card <?php echo esc_attr( $cancellation_policy === 'custom' ? 'selected' : '' ); ?>">
                <input type="radio" name="cancellation_policy" value="custom" <?php checked($cancellation_policy, 'custom'); ?>>
                <div class="wahrb-policy-content">
                    <strong><?php esc_html_e('Custom', 'wahra-rentals-bookings'); ?></strong>
                    <span><?php esc_html_e('Configure your own rules', 'wahra-rentals-bookings'); ?></span>
                </div>
            </label>
        </div>
        
        <!-- Custom Policy Settings -->
        <div class="wahrb-custom-policy-settings" id="wahrb-custom-policy-settings" style="display: <?php echo esc_attr( $cancellation_policy === 'custom' ? 'block' : 'none' ); ?>;">
            <div class="wahrb-form-row">
                <div class="wahrb-form-group">
                    <label><?php esc_html_e('Free cancellation up to', 'wahra-rentals-bookings'); ?></label>
                    <div class="wahrb-input-group">
                        <input type="number" name="cancellation_days" value="<?php echo esc_attr($step6['cancellation_days'] ?? 1); ?>" min="0">
                        <span class="wahrb-input-suffix"><?php esc_html_e('days before', 'wahra-rentals-bookings'); ?></span>
                    </div>
                </div>
                <div class="wahrb-form-group">
                    <label><?php esc_html_e('Refund', 'wahra-rentals-bookings'); ?></label>
                    <div class="wahrb-input-group">
                        <input type="number" name="cancellation_refund" value="<?php echo esc_attr($step6['cancellation_refund'] ?? 100); ?>" min="0" max="100">
                        <span class="wahrb-input-suffix">%</span>
                    </div>
                </div>
            </div>
            <p class="wahrb-hint"><?php esc_html_e('Partial or full refund if the customer cancels before the deadline.', 'wahra-rentals-bookings'); ?></p>
        </div>
    </div>

    <!-- Terms & Conditions -->
    <div class="wahrb-form-group">
        <label><?php esc_html_e('Rental terms', 'wahra-rentals-bookings'); ?></label>
        <p class="wahrb-description"><?php esc_html_e('Add your specific rental conditions.', 'wahra-rentals-bookings'); ?></p>
        
        <div class="wahrb-terms-editor">
            <textarea name="terms_content" class="wahrb-form-control" rows="5" placeholder="<?php esc_attr_e('Enter your terms here...', 'wahra-rentals-bookings'); ?>"><?php echo esc_textarea($step6['terms_content'] ?? ''); ?></textarea>
        </div>
    </div>

    <!-- Acceptance Checkbox -->
    <div class="wahrb-form-group wahrb-terms-group">
        <label class="wahrb-checkbox-label">
            <input type="checkbox" name="terms_accepted" id="terms_accepted" value="1" <?php checked($terms_accepted); ?> <?php echo esc_attr( $terms_required ? 'required' : '' ); ?>>
            <?php
            // Display consent text with optional link to terms page
            if (!empty($terms_url)) {
                printf(
                    /* translators: %1$s: consent text, %2$s: terms URL */
                    '%1$s <a href="%2$s" target="_blank" rel="noopener">%3$s</a>',
                    esc_html($vendor_consent_text),
                    esc_url($terms_url),
                    esc_html__('View Terms & Conditions', 'wahra-rentals-bookings')
                );
            } else {
                echo esc_html($vendor_consent_text);
            }
            ?>
        </label>
        <?php if (!$terms_required): ?>
            <p class="wahrb-hint"><?php esc_html_e('(Optional)', 'wahra-rentals-bookings'); ?></p>
        <?php endif; ?>
    </div>

    <!-- Action Buttons -->
    <div class="wahrb-publish-actions">
        <button type="button" class="wahrb-btn wahrb-btn-secondary wahrb-btn-lg" id="wahrb-save-draft-btn">
            <?php esc_html_e('Save draft', 'wahra-rentals-bookings'); ?>
        </button>
        <button type="submit" class="wahrb-btn wahrb-btn-primary wahrb-btn-lg" id="wahrb-publish-btn" disabled>
            <?php esc_html_e('Publish listing', 'wahra-rentals-bookings'); ?>
        </button>
    </div>
</div>

<?php
// Step 7 JS is handled by wahrb-wizard-steps.js (enqueued in booking-wizard.php)
wp_localize_script('wahrb-wizard-steps', 'wahrbStep7Config', [
    'termsRequired' => (bool) $terms_required,
]);
?>
