<?php
/**
 * Wizard Step 5: Payment Settings
 * 
 * @package WahraRentalsBookings
 */

defined('ABSPATH') || exit;

$step5 = $wizard_data['step_5'] ?? [];

// Get admin settings with fallback defaults
$default_payment = [
    'deposit_enabled' => true,
    'deposit_percent' => 30,
    'security_enabled' => true,
    'balance_days_before' => 3,
];
$admin_settings = get_option('wahrb_admin_settings', ['payment' => $default_payment]);
$payment_defaults = $admin_settings['payment'] ?? $default_payment;

// Payment Methods
$accepted_methods = $step5['accepted_methods'] ?? ['online'];
if (!is_array($accepted_methods)) {
    $accepted_methods = ['online'];
}

// Get available WooCommerce payment gateways
$available_gateways = [];
if (function_exists('WC') && WC()->payment_gateways()) {
    foreach (WC()->payment_gateways()->get_available_payment_gateways() as $gateway_id => $gateway) {
        $available_gateways[$gateway_id] = $gateway->get_title();
    }
}
$selected_gateways = $step5['selected_gateways'] ?? array_keys($available_gateways);
if (!is_array($selected_gateways)) {
    $selected_gateways = array_keys($available_gateways);
}

// Deposit
$deposit_enabled = $step5['deposit_enabled'] ?? ($payment_defaults['deposit_enabled'] ?? false);
$deposit_type = $step5['deposit_type'] ?? 'percent';
$deposit_value = floatval($step5['deposit_value'] ?? ($payment_defaults['deposit_percent'] ?? 30));

// Security
$security_enabled = $step5['security_enabled'] ?? ($payment_defaults['security_enabled'] ?? false);
$security_amount = floatval($step5['security_amount'] ?? 0);
$security_mode = $step5['security_mode'] ?? 'capture';
$security_payment_methods = $step5['security_payment_methods'] ?? ['online'];
if (!is_array($security_payment_methods)) {
    $security_payment_methods = ['online'];
}

// Balance
$balance_enabled = $step5['balance_enabled'] ?? true;
$balance_due_days = absint($step5['balance_due_days'] ?? ($payment_defaults['balance_days_before'] ?? 3));
$balance_allow_cod = $step5['balance_allow_cod'] ?? false;

$currency_symbol = html_entity_decode(get_woocommerce_currency_symbol());
?>

<div class="wahrb-step wahrb-step-5" data-step="5">
    <div class="wahrb-step-header">
        <h2><?php esc_html_e('Payment Terms', 'wahra-rentals-bookings'); ?></h2>
        <p class="wahrb-step-description"><?php esc_html_e('Configure payment methods, deposit and security deposit', 'wahra-rentals-bookings'); ?></p>
    </div>

    <!-- Payment Methods Section -->
    <div class="wahrb-payment-section">
        <div class="wahrb-section-title-text" style="margin-bottom: 16px;">
            <h3>💳 <?php esc_html_e('Accepted payment methods', 'wahra-rentals-bookings'); ?></h3>
            <p><?php esc_html_e('Select the payment methods you accept', 'wahra-rentals-bookings'); ?></p>
        </div>

        <div class="wahrb-payment-methods-grid">
            <label class="wahrb-payment-method-card <?php echo esc_attr( in_array('online', $accepted_methods) ? 'active' : '' ); ?>">
                <input type="checkbox" name="accepted_methods[]" value="online" <?php checked(in_array('online', $accepted_methods)); ?>>
                <span class="method-icon">💳</span>
                <span class="method-name"><?php esc_html_e('Online payment', 'wahra-rentals-bookings'); ?></span>
                <span class="method-desc"><?php esc_html_e('Credit card, PayPal, etc.', 'wahra-rentals-bookings'); ?></span>
            </label>
            
            <label class="wahrb-payment-method-card <?php echo esc_attr( in_array('cash', $accepted_methods) ? 'active' : '' ); ?>">
                <input type="checkbox" name="accepted_methods[]" value="cash" <?php checked(in_array('cash', $accepted_methods)); ?>>
                <span class="method-icon">💵</span>
                <span class="method-name"><?php esc_html_e('Cash', 'wahra-rentals-bookings'); ?></span>
                <span class="method-desc"><?php esc_html_e('In-person payment', 'wahra-rentals-bookings'); ?></span>
            </label>
            
            <label class="wahrb-payment-method-card <?php echo esc_attr( in_array('bank_transfer', $accepted_methods) ? 'active' : '' ); ?>">
                <input type="checkbox" name="accepted_methods[]" value="bank_transfer" <?php checked(in_array('bank_transfer', $accepted_methods)); ?>>
                <span class="method-icon">🏦</span>
                <span class="method-name"><?php esc_html_e('Bank transfer', 'wahra-rentals-bookings'); ?></span>
                <span class="method-desc"><?php esc_html_e('IBAN/RIB transfer', 'wahra-rentals-bookings'); ?></span>
            </label>
            
            <label class="wahrb-payment-method-card <?php echo esc_attr( in_array('cheque', $accepted_methods) ? 'active' : '' ); ?>">
                <input type="checkbox" name="accepted_methods[]" value="cheque" <?php checked(in_array('cheque', $accepted_methods)); ?>>
                <span class="method-icon">📝</span>
                <span class="method-name"><?php esc_html_e('Cheque', 'wahra-rentals-bookings'); ?></span>
                <span class="method-desc"><?php esc_html_e('Bank cheque', 'wahra-rentals-bookings'); ?></span>
            </label>
        </div>

        <?php if (!empty($available_gateways)): ?>
        <div class="wahrb-gateways-section" id="gateways-section" style="<?php echo esc_attr( in_array('online', $accepted_methods) ? '' : 'display:none;' ); ?>">
            <label class="wahrb-subsection-label"><?php esc_html_e('Online payment gateways', 'wahra-rentals-bookings'); ?></label>
            <div class="wahrb-gateways-grid">
                <?php foreach ($available_gateways as $gateway_id => $gateway_title): ?>
                <label class="wahrb-gateway-checkbox">
                    <input type="checkbox" name="selected_gateways[]" value="<?php echo esc_attr($gateway_id); ?>" 
                           <?php checked(in_array($gateway_id, $selected_gateways)); ?>>
                    <span class="gateway-name"><?php echo esc_html($gateway_title); ?></span>
                </label>
                <?php endforeach; ?>
            </div>
        </div>
        <?php endif; ?>
    </div>

    <!-- Deposit Section -->
    <div class="wahrb-payment-section">
        <div class="wahrb-section-title">
            <label class="wahrb-toggle">
                <input type="checkbox" name="deposit_enabled" id="deposit_enabled" value="1" <?php checked($deposit_enabled); ?>>
                <span class="wahrb-toggle-slider"></span>
            </label>
            <div class="wahrb-section-title-text">
                <h3><?php esc_html_e('Booking deposit', 'wahra-rentals-bookings'); ?></h3>
                <p><?php esc_html_e('Require a deposit to confirm the booking', 'wahra-rentals-bookings'); ?></p>
            </div>
        </div>

        <div class="wahrb-section-content" id="deposit-options" style="<?php echo esc_attr( $deposit_enabled ? '' : 'display:none;' ); ?>">
            <div class="wahrb-form-row">
                <div class="wahrb-form-group wahrb-form-col">
                    <label for="deposit_type"><?php esc_html_e('Type', 'wahra-rentals-bookings'); ?></label>
                    <select name="deposit_type" id="deposit_type" class="wahrb-form-control">
                        <option value="percent" <?php selected($deposit_type, 'percent'); ?>><?php esc_html_e('Percentage', 'wahra-rentals-bookings'); ?></option>
                        <option value="fixed" <?php selected($deposit_type, 'fixed'); ?>><?php esc_html_e('Fixed amount', 'wahra-rentals-bookings'); ?></option>
                    </select>
                </div>
                <div class="wahrb-form-group wahrb-form-col">
                    <label for="deposit_value"><?php esc_html_e('Value', 'wahra-rentals-bookings'); ?></label>
                    <div class="wahrb-input-group">
                        <input type="number" name="deposit_value" id="deposit_value" class="wahrb-form-control" 
                               value="<?php echo esc_attr($deposit_value); ?>" min="0" step="0.01">
                        <span class="wahrb-input-suffix" id="deposit-suffix"><?php echo esc_html( $deposit_type === 'percent' ? '%' : $currency_symbol ); ?></span>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Security Deposit Section -->
    <div class="wahrb-payment-section">
        <div class="wahrb-section-title">
            <label class="wahrb-toggle">
                <input type="checkbox" name="security_enabled" id="security_enabled" value="1" <?php checked($security_enabled); ?>>
                <span class="wahrb-toggle-slider"></span>
            </label>
            <div class="wahrb-section-title-text">
                <h3><?php esc_html_e('Security deposit', 'wahra-rentals-bookings'); ?></h3>
                <p><?php esc_html_e('Request a refundable security deposit', 'wahra-rentals-bookings'); ?></p>
            </div>
        </div>

        <div class="wahrb-section-content" id="security-options" style="<?php echo esc_attr( $security_enabled ? '' : 'display:none;' ); ?>">
            <div class="wahrb-form-row">
                <div class="wahrb-form-group wahrb-form-col">
                    <label for="security_amount"><?php esc_html_e('Deposit amount', 'wahra-rentals-bookings'); ?></label>
                    <div class="wahrb-input-group">
                        <span class="wahrb-input-prefix"><?php echo esc_html($currency_symbol); ?></span>
                        <input type="number" name="security_amount" id="security_amount" class="wahrb-form-control" 
                               value="<?php echo esc_attr($security_amount); ?>" min="0" step="0.01">
                    </div>
                </div>
                <div class="wahrb-form-group wahrb-form-col">
                    <label for="security_mode"><?php esc_html_e('Capture mode', 'wahra-rentals-bookings'); ?></label>
                    <select name="security_mode" id="security_mode" class="wahrb-form-control">
                        <option value="capture" <?php selected($security_mode, 'capture'); ?>><?php esc_html_e('Immediate charge', 'wahra-rentals-bookings'); ?></option>
                        <option value="authorization" <?php selected($security_mode, 'authorization'); ?>><?php esc_html_e('Authorization (Stripe)', 'wahra-rentals-bookings'); ?></option>
                        <option value="manual" <?php selected($security_mode, 'manual'); ?>><?php esc_html_e('Manual (on-site)', 'wahra-rentals-bookings'); ?></option>
                    </select>
                </div>
            </div>
            
            <div class="wahrb-form-group" style="margin-top: 16px;">
                <label style="display: block; margin-bottom: 10px; font-weight: 600;"><?php esc_html_e('Accepted payment methods for security deposit', 'wahra-rentals-bookings'); ?></label>
                <div class="wahrb-security-methods-grid">
                    <label class="wahrb-security-method <?php echo esc_attr( in_array('online', $security_payment_methods) ? 'active' : '' ); ?>">
                        <input type="checkbox" name="security_payment_methods[]" value="online" <?php checked(in_array('online', $security_payment_methods)); ?>>
                        <span class="method-icon">💳</span>
                        <span class="method-label"><?php esc_html_e('Online payment', 'wahra-rentals-bookings'); ?></span>
                    </label>
                    <label class="wahrb-security-method <?php echo esc_attr( in_array('cash', $security_payment_methods) ? 'active' : '' ); ?>">
                        <input type="checkbox" name="security_payment_methods[]" value="cash" <?php checked(in_array('cash', $security_payment_methods)); ?>>
                        <span class="method-icon">💵</span>
                        <span class="method-label"><?php esc_html_e('Cash', 'wahra-rentals-bookings'); ?></span>
                    </label>
                    <label class="wahrb-security-method <?php echo esc_attr( in_array('cheque', $security_payment_methods) ? 'active' : '' ); ?>">
                        <input type="checkbox" name="security_payment_methods[]" value="cheque" <?php checked(in_array('cheque', $security_payment_methods)); ?>>
                        <span class="method-icon">📝</span>
                        <span class="method-label"><?php esc_html_e('Cheque', 'wahra-rentals-bookings'); ?></span>
                    </label>
                </div>
                <p class="wahrb-help-text" style="margin-top: 8px; color: #64748b; font-size: 12px;">
                    <?php esc_html_e('Online payment uses active Dokan gateways.', 'wahra-rentals-bookings'); ?>
                </p>
            </div>
        </div>
    </div>

    <!-- Balance Section -->
    <div class="wahrb-payment-section">
        <div class="wahrb-section-title">
            <label class="wahrb-toggle">
                <input type="checkbox" name="balance_enabled" id="balance_enabled" value="1" <?php checked($balance_enabled); ?>>
                <span class="wahrb-toggle-slider"></span>
            </label>
            <div class="wahrb-section-title-text">
                <h3><?php esc_html_e('Balance before rental', 'wahra-rentals-bookings'); ?></h3>
                <p><?php esc_html_e('Payment of the balance before the start of the rental', 'wahra-rentals-bookings'); ?></p>
            </div>
        </div>

        <div class="wahrb-section-content" id="balance-options" style="<?php echo esc_attr( $balance_enabled ? '' : 'display:none;' ); ?>">
            <div class="wahrb-form-row">
                <div class="wahrb-form-group wahrb-form-col">
                    <label for="balance_due_days"><?php esc_html_e('Payment required', 'wahra-rentals-bookings'); ?></label>
                    <div class="wahrb-input-group">
                        <input type="number" name="balance_due_days" id="balance_due_days" class="wahrb-form-control" 
                               value="<?php echo esc_attr($balance_due_days); ?>" min="0" max="30">
                        <span class="wahrb-input-suffix"><?php esc_html_e('days before', 'wahra-rentals-bookings'); ?></span>
                    </div>
                </div>
                <div class="wahrb-form-group wahrb-form-col" style="display:flex;align-items:flex-end;padding-bottom:4px;">
                    <label class="wahrb-checkbox-label">
                        <input type="checkbox" name="balance_allow_cod" value="1" <?php checked($balance_allow_cod); ?>>
                        <?php esc_html_e('Allow on-site payment', 'wahra-rentals-bookings'); ?>
                    </label>
                </div>
            </div>
        </div>
    </div>
</div>

<?php
// Step 5 JS is handled by wahrb-wizard-steps.js (enqueued in booking-wizard.php)
wp_localize_script('wahrb-wizard-steps', 'wahrbStep5Config', [
    'currencySymbol' => html_entity_decode(get_woocommerce_currency_symbol()),
]);
?>
<?php
// Second step-5 JS block removed — all logic consolidated in wahrb-wizard-steps.js
?>
