<?php
/**
 * Calculator & BMI section for Noesia Smart Waiter.
 *
 * Rendered inside the plugin's main settings form.
 * Legacy markup is preserved; state assembly now lives in a helper service.
 *
 * @package Noesia_Smart_Waiter
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

$noesia_swa_calculator_service = null;
if ( function_exists( 'noesia_swa' ) ) {
    $noesia_swa_calculator_service = noesia_swa()->calculator_data;
}
if ( ! ( $noesia_swa_calculator_service instanceof Noesia_SWA_Calculator_Data ) ) {
    $noesia_swa_calculator_service = new Noesia_SWA_Calculator_Data();
}

$noesia_swa_calculator_context = $noesia_swa_calculator_service->get_context();
if ( is_array( $noesia_swa_calculator_context ) ) {
    extract( $noesia_swa_calculator_context, EXTR_SKIP );
}
?>

<div class="noesia-swa-section-head">
	<h2>
		<span class="dashicons dashicons-calculator" aria-hidden="true"></span>
		<?php esc_html_e( 'Calculator & BMI', 'noesia-smart-waiter' ); ?>
	</h2>
	<p class="noesia-swa-muted">
		<?php esc_html_e( 'A simple calculator and a clinically correct adult BMI estimator.', 'noesia-smart-waiter' ); ?>
	</p>
</div>

<section class="noesia-swa-section-grid">

	<section class="noesia-swa-card" aria-label="<?php esc_attr_e( 'Calculator', 'noesia-smart-waiter' ); ?>">
		<div class="noesia-swa-card-header">
			<div>
				<h3>
					<span class="dashicons dashicons-editor-table" aria-hidden="true"></span>
					<?php esc_html_e( 'Calculator', 'noesia-smart-waiter' ); ?>
				</h3>
			</div>
		</div>

		<?php if ( '' !== $calc_error ) : ?>
			<div class="noesia-swa-status-box" role="alert">
				<strong><?php esc_html_e( 'Notice', 'noesia-smart-waiter' ); ?></strong>
				<span><?php echo esc_html( $calc_error ); ?></span>
			</div>
		<?php endif; ?>

		<?php if ( null !== $calc_result ) : ?>
			<div class="noesia-swa-status-box">
				<strong><?php esc_html_e( 'Result', 'noesia-smart-waiter' ); ?></strong>
				<span><?php echo esc_html( noesia_swa_calc_format_value( (float) $calc_result ) ); ?></span>
			</div>
		<?php endif; ?>

		<?php wp_nonce_field( 'noesia_swa_calcpad_action', 'noesia_swa_calcpad_nonce' ); ?>
		<input type="hidden" name="noesia_swa_calc_expr" value="<?php echo esc_attr( (string) $calc_expr ); ?>" />

		<div class="noesia-swa-status-box">
			<strong><?php esc_html_e( 'Result', 'noesia-smart-waiter' ); ?></strong>
			<input
				id="noesia-swa-calculator-display"
				type="text"
				name="noesia_swa_calc_display_readonly"
				value="<?php echo esc_attr( $calc_display ); ?>"
				readonly
				aria-label="<?php esc_attr_e( 'Calculator display', 'noesia-smart-waiter' ); ?>"
			/>
		</div>

		<table class="widefat striped" role="presentation">
			<tbody>
				<tr>
					<td>
						<button type="button" class="button" name="noesia_swa_calc_key" value="C" formaction="<?php echo esc_url( $section_url ); ?>" formmethod="post" formnovalidate>C</button>
					</td>
					<td>
						<button type="button" class="button" name="noesia_swa_calc_key" value="BS" formaction="<?php echo esc_url( $section_url ); ?>" formmethod="post" formnovalidate><?php esc_html_e( '⌫', 'noesia-smart-waiter' ); ?></button>
					</td>
					<td>
						<button type="button" class="button" name="noesia_swa_calc_key" value="SIGN" formaction="<?php echo esc_url( $section_url ); ?>" formmethod="post" formnovalidate><?php esc_html_e( '±', 'noesia-smart-waiter' ); ?></button>
					</td>
					<td>
						<button type="button" class="button button-primary" name="noesia_swa_calc_key" value="/" formaction="<?php echo esc_url( $section_url ); ?>" formmethod="post" formnovalidate><?php esc_html_e( '÷', 'noesia-smart-waiter' ); ?></button>
					</td>
				</tr>
				<tr>
					<td>
						<button type="button" class="button" name="noesia_swa_calc_key" value="7" formaction="<?php echo esc_url( $section_url ); ?>" formmethod="post" formnovalidate>7</button>
					</td>
					<td>
						<button type="button" class="button" name="noesia_swa_calc_key" value="8" formaction="<?php echo esc_url( $section_url ); ?>" formmethod="post" formnovalidate>8</button>
					</td>
					<td>
						<button type="button" class="button" name="noesia_swa_calc_key" value="9" formaction="<?php echo esc_url( $section_url ); ?>" formmethod="post" formnovalidate>9</button>
					</td>
					<td>
						<button type="button" class="button button-primary" name="noesia_swa_calc_key" value="*" formaction="<?php echo esc_url( $section_url ); ?>" formmethod="post" formnovalidate><?php esc_html_e( '×', 'noesia-smart-waiter' ); ?></button>
					</td>
				</tr>
				<tr>
					<td>
						<button type="button" class="button" name="noesia_swa_calc_key" value="4" formaction="<?php echo esc_url( $section_url ); ?>" formmethod="post" formnovalidate>4</button>
					</td>
					<td>
						<button type="button" class="button" name="noesia_swa_calc_key" value="5" formaction="<?php echo esc_url( $section_url ); ?>" formmethod="post" formnovalidate>5</button>
					</td>
					<td>
						<button type="button" class="button" name="noesia_swa_calc_key" value="6" formaction="<?php echo esc_url( $section_url ); ?>" formmethod="post" formnovalidate>6</button>
					</td>
					<td>
						<button type="button" class="button button-primary" name="noesia_swa_calc_key" value="-" formaction="<?php echo esc_url( $section_url ); ?>" formmethod="post" formnovalidate><?php esc_html_e( '−', 'noesia-smart-waiter' ); ?></button>
					</td>
				</tr>
				<tr>
					<td>
						<button type="button" class="button" name="noesia_swa_calc_key" value="1" formaction="<?php echo esc_url( $section_url ); ?>" formmethod="post" formnovalidate>1</button>
					</td>
					<td>
						<button type="button" class="button" name="noesia_swa_calc_key" value="2" formaction="<?php echo esc_url( $section_url ); ?>" formmethod="post" formnovalidate>2</button>
					</td>
					<td>
						<button type="button" class="button" name="noesia_swa_calc_key" value="3" formaction="<?php echo esc_url( $section_url ); ?>" formmethod="post" formnovalidate>3</button>
					</td>
					<td>
						<button type="button" class="button button-primary" name="noesia_swa_calc_key" value="+" formaction="<?php echo esc_url( $section_url ); ?>" formmethod="post" formnovalidate>+</button>
					</td>
				</tr>
				<tr>
					<td>
						<button type="button" class="button" name="noesia_swa_calc_key" value="0" formaction="<?php echo esc_url( $section_url ); ?>" formmethod="post" formnovalidate>0</button>
					</td>
					<td>
						<button type="button" class="button" name="noesia_swa_calc_key" value="." formaction="<?php echo esc_url( $section_url ); ?>" formmethod="post" formnovalidate>.</button>
					</td>
					<td>
						<button type="button" class="button button-primary" name="noesia_swa_calc_key" value="=" formaction="<?php echo esc_url( $section_url ); ?>" formmethod="post" formnovalidate>=</button>
					</td>
					<td><span aria-hidden="true">&nbsp;</span></td>
				</tr>
			</tbody>
		</table>

		<p class="noesia-swa-muted">
			<?php esc_html_e( 'Supports +, −, ×, ÷ and decimals. This keypad is self-hosted (no CDN).', 'noesia-smart-waiter' ); ?>
		</p>
	</section>

	<section class="noesia-swa-card" aria-label="<?php esc_attr_e( 'BMI calculator', 'noesia-smart-waiter' ); ?>">
		<div class="noesia-swa-card-header">
			<div>
				<h3>
					<span class="dashicons dashicons-heart" aria-hidden="true"></span>
					<?php esc_html_e( 'BMI (Body Mass Index)', 'noesia-smart-waiter' ); ?>
				</h3>
			</div>
		</div>

		<?php if ( '' !== $bmi_error ) : ?>
			<div class="noesia-swa-status-box" role="alert">
				<strong><?php esc_html_e( 'Notice', 'noesia-smart-waiter' ); ?></strong>
				<span><?php echo esc_html( $bmi_error ); ?></span>
			</div>
		<?php endif; ?>

		<?php if ( null !== $bmi_value ) : ?>
			<div class="noesia-swa-mini-stats" aria-live="polite">
				<div>
					<strong><?php esc_html_e( 'Your BMI', 'noesia-smart-waiter' ); ?></strong>
					<span><?php echo esc_html( number_format_i18n( (float) $bmi_value, 1 ) ); ?></span>
					<p class="noesia-swa-muted"><?php echo esc_html( $bmi_category ); ?></p>
				</div>

				<?php if ( null !== $bmi_range_min && null !== $bmi_range_max ) : ?>
					<div>
						<strong><?php esc_html_e( 'Healthy weight range for this height', 'noesia-smart-waiter' ); ?></strong>
						<span>
							<?php echo esc_html( number_format_i18n( (float) $bmi_range_min, 1 ) ); ?>
							<?php esc_html_e( 'to', 'noesia-smart-waiter' ); ?>
							<?php echo esc_html( number_format_i18n( (float) $bmi_range_max, 1 ) ); ?>
							<?php esc_html_e( 'kg', 'noesia-smart-waiter' ); ?>
						</span>
						<p class="noesia-swa-muted"><?php esc_html_e( 'Reference BMI range: 18.5–24.9 (adults).', 'noesia-smart-waiter' ); ?></p>
					</div>
				<?php endif; ?>

				<?php if ( null !== $bmr_value ) : ?>
					<div>
						<strong><?php esc_html_e( 'Optional estimates (age/sex-based)', 'noesia-smart-waiter' ); ?></strong>
						<span>
							<?php
							/* translators: %s: calories per day. */
							echo esc_html( sprintf( __( 'BMR: ~%s kcal/day', 'noesia-smart-waiter' ), number_format_i18n( (float) $bmr_value, 0 ) ) );
							?>
							<?php if ( null !== $bf_percent ) : ?>
								<br />
								<?php
								/* translators: %s: percentage. */
								echo esc_html( sprintf( __( 'Body fat estimate: ~%s%%', 'noesia-smart-waiter' ), number_format_i18n( (float) $bf_percent, 1 ) ) );
								?>
							<?php endif; ?>
						</span>
						<p class="noesia-swa-muted"><?php esc_html_e( 'These are approximations; use clinical judgment.', 'noesia-smart-waiter' ); ?></p>
					</div>
				<?php endif; ?>
			</div>
		<?php else : ?>
			<div class="noesia-swa-status-box">
				<strong><?php esc_html_e( 'BMI Preview', 'noesia-smart-waiter' ); ?></strong>
				<span><?php esc_html_e( 'Enter height and weight to estimate BMI (adult use).', 'noesia-smart-waiter' ); ?></span>
			</div>
		<?php endif; ?>

		<?php wp_nonce_field( 'noesia_swa_bmi_action', 'noesia_swa_bmi_nonce' ); ?>

		<div class="noesia-swa-section-grid">
			<div class="noesia-swa-form-group">
				<label for="noesia-swa-bmi-sex"><?php esc_html_e( 'Sex', 'noesia-smart-waiter' ); ?></label>
				<select id="noesia-swa-bmi-sex" name="noesia_swa_bmi_sex">
					<?php foreach ( $sex_labels as $noesia_swa_sex_key => $noesia_swa_sex_label ) : ?>
						<option value="<?php echo esc_attr( $noesia_swa_sex_key ); ?>" <?php selected( $bmi_sex_raw, $noesia_swa_sex_key ); ?>><?php echo esc_html( $noesia_swa_sex_label ); ?></option>
					<?php endforeach; ?>
				</select>
			</div>

			<div class="noesia-swa-form-group">
				<label for="noesia-swa-bmi-age"><?php esc_html_e( 'Age (optional)', 'noesia-smart-waiter' ); ?></label>
				<input id="noesia-swa-bmi-age" type="number" name="noesia_swa_bmi_age" min="0" max="120" value="<?php echo esc_attr( $bmi_age_raw ); ?>" />
			</div>
		</div>

		<div class="noesia-swa-section-grid">
			<div class="noesia-swa-form-group">
				<label for="noesia-swa-bmi-height"><?php esc_html_e( 'Height (cm)', 'noesia-smart-waiter' ); ?></label>
				<input id="noesia-swa-bmi-height" type="text" name="noesia_swa_bmi_height_cm" value="<?php echo esc_attr( $bmi_height_raw ); ?>" placeholder="<?php esc_attr_e( 'e.g. 175', 'noesia-smart-waiter' ); ?>" autocomplete="off" />
			</div>

			<div class="noesia-swa-form-group">
				<label for="noesia-swa-bmi-weight"><?php esc_html_e( 'Weight (kg)', 'noesia-smart-waiter' ); ?></label>
				<input id="noesia-swa-bmi-weight" type="text" name="noesia_swa_bmi_weight_kg" value="<?php echo esc_attr( $bmi_weight_raw ); ?>" placeholder="<?php esc_attr_e( 'e.g. 74', 'noesia-smart-waiter' ); ?>" autocomplete="off" />
			</div>
		</div>

		<div class="noesia-swa-inline-actions">
    <button
        type="button"
        class="button button-primary"
        name="noesia_swa_bmi_submit"
        value="1"
        id="noesia-swa-bmi-calc"
    >
        <span class="dashicons dashicons-yes" aria-hidden="true"></span>
        <?php esc_html_e( 'Calculate', 'noesia-smart-waiter' ); ?>
    </button>

    <a class="button" href="<?php echo esc_url( $section_url ); ?>">
        <span class="dashicons dashicons-update" aria-hidden="true"></span>
        <?php esc_html_e( 'Reset', 'noesia-smart-waiter' ); ?>
    </a>
</div>

		<p class="noesia-swa-muted">
			<?php esc_html_e( 'BMI is a screening metric. For children/teens or special clinical cases, use appropriate growth charts and clinical protocols.', 'noesia-smart-waiter' ); ?>
		</p>
	</section>

</section>
