<?php

declare(strict_types=1);

/**
 * 技師進階資料表單欄位（出生年、三圍、罩杯）。
 *
 * 預期變數：
 *  - $mprefix string 欄位 id 前綴
 *  - $masseur array|null 既有資料（編輯時）
 *  - $fieldClass string CSS class（masseur-field / forum-masseur-field）
 *  - $hintClass string 提示文字 class（選填）
 */

if (!function_exists('fate_masseur_birth_year_options')) {
    require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'fate_masseur_profile_stats.php';
}

$mprefix = isset($mprefix) ? (string) $mprefix : '';
$masseur = is_array($masseur ?? null) ? $masseur : [];
$fieldClass = isset($fieldClass) ? (string) $fieldClass : 'masseur-field';
$hintClass = isset($hintClass) ? (string) $hintClass : 'masseur-hint';
$curYear = (int) date('Y');

$birthYear = isset($masseur['birth_year']) && $masseur['birth_year'] !== null && $masseur['birth_year'] !== ''
    ? (int) $masseur['birth_year'] : null;
$bust = isset($masseur['measure_bust']) && $masseur['measure_bust'] !== null && $masseur['measure_bust'] !== ''
    ? (int) $masseur['measure_bust'] : null;
$waist = isset($masseur['measure_waist']) && $masseur['measure_waist'] !== null && $masseur['measure_waist'] !== ''
    ? (int) $masseur['measure_waist'] : null;
$hip = isset($masseur['measure_hip']) && $masseur['measure_hip'] !== null && $masseur['measure_hip'] !== ''
    ? (int) $masseur['measure_hip'] : null;
$cup = trim((string) ($masseur['cup_size'] ?? ''));
$measureRanges = fate_masseur_measure_inch_ranges();
$statsHidden = fate_masseur_profile_stats_hidden_from_row($masseur);

$birthId = $mprefix . 'birth-year';
$agePreviewId = $mprefix . 'age-preview';
$bustId = $mprefix . 'measure-bust';
$waistId = $mprefix . 'measure-waist';
$hipId = $mprefix . 'measure-hip';
$cupId = $mprefix . 'cup-size';
?>
<div class="masseur-profile-stats-fields" data-masseur-profile-stats-form data-current-year="<?= $curYear ?>">
    <div class="<?= htmlspecialchars($fieldClass, ENT_QUOTES, 'UTF-8') ?>">
        <label for="<?= htmlspecialchars($birthId, ENT_QUOTES, 'UTF-8') ?>">出生年（西元）</label>
        <select id="<?= htmlspecialchars($birthId, ENT_QUOTES, 'UTF-8') ?>" name="birth_year" data-masseur-birth-year data-age-preview="<?= htmlspecialchars($agePreviewId, ENT_QUOTES, 'UTF-8') ?>">
            <option value="none"<?= $birthYear === null ? ' selected' : '' ?>>無資料</option>
            <?php foreach (fate_masseur_birth_year_options() as $y): ?>
                <option value="<?= $y ?>"<?= $birthYear === $y ? ' selected' : '' ?>><?= $y ?> 年</option>
            <?php endforeach; ?>
        </select>
        <span class="masseur-profile-stats-age-preview" id="<?= htmlspecialchars($agePreviewId, ENT_QUOTES, 'UTF-8') ?>" aria-live="polite"><?php
            $previewAge = fate_masseur_age_from_birth_year($birthYear);
            if ($previewAge !== null) {
                echo '約 ' . $previewAge . ' 歲';
            }
        ?></span>
        <p class="<?= htmlspecialchars($hintClass, ENT_QUOTES, 'UTF-8') ?>">選擇出生年後即時換算年齡；前台顯示亦會隨年份自動更新。</p>
    </div>

    <div class="<?= htmlspecialchars($fieldClass, ENT_QUOTES, 'UTF-8') ?> masseur-profile-stats-measures">
        <span class="masseur-profile-stats-measures__label">三圍（英吋，可留空＝無資料）</span>
        <div class="masseur-profile-stats-measures__row">
            <label class="masseur-profile-stats-measures__item" for="<?= htmlspecialchars($bustId, ENT_QUOTES, 'UTF-8') ?>">胸
                <input type="number" id="<?= htmlspecialchars($bustId, ENT_QUOTES, 'UTF-8') ?>" name="measure_bust" min="<?= $measureRanges['bust']['min'] ?>" max="<?= $measureRanges['bust']['max'] ?>" step="1" placeholder="—" value="<?= $bust !== null ? $bust : '' ?>">
            </label>
            <label class="masseur-profile-stats-measures__item" for="<?= htmlspecialchars($waistId, ENT_QUOTES, 'UTF-8') ?>">腰
                <input type="number" id="<?= htmlspecialchars($waistId, ENT_QUOTES, 'UTF-8') ?>" name="measure_waist" min="<?= $measureRanges['waist']['min'] ?>" max="<?= $measureRanges['waist']['max'] ?>" step="1" placeholder="—" value="<?= $waist !== null ? $waist : '' ?>">
            </label>
            <label class="masseur-profile-stats-measures__item" for="<?= htmlspecialchars($hipId, ENT_QUOTES, 'UTF-8') ?>">臀
                <input type="number" id="<?= htmlspecialchars($hipId, ENT_QUOTES, 'UTF-8') ?>" name="measure_hip" min="<?= $measureRanges['hip']['min'] ?>" max="<?= $measureRanges['hip']['max'] ?>" step="1" placeholder="—" value="<?= $hip !== null ? $hip : '' ?>">
            </label>
        </div>
    </div>

    <div class="<?= htmlspecialchars($fieldClass, ENT_QUOTES, 'UTF-8') ?>">
        <label for="<?= htmlspecialchars($cupId, ENT_QUOTES, 'UTF-8') ?>">罩杯</label>
        <select id="<?= htmlspecialchars($cupId, ENT_QUOTES, 'UTF-8') ?>" name="cup_size">
            <option value="none"<?= $cup === '' ? ' selected' : '' ?>>無資料</option>
            <?php foreach (fate_masseur_cup_size_options() as $cupOpt): ?>
                <option value="<?= htmlspecialchars($cupOpt, ENT_QUOTES, 'UTF-8') ?>"<?= $cup === $cupOpt ? ' selected' : '' ?>><?= htmlspecialchars($cupOpt, ENT_QUOTES, 'UTF-8') ?></option>
            <?php endforeach; ?>
        </select>
    </div>

    <div class="<?= htmlspecialchars($fieldClass, ENT_QUOTES, 'UTF-8') ?> masseur-profile-stats-visibility">
        <label class="masseur-profile-stats-visibility__label">
            <input type="checkbox" name="profile_stats_hidden" value="1"<?= $statsHidden ? ' checked' : '' ?>>
            前台不顯示年齡／身材（含店家頁與討論區）
        </label>
        <p class="<?= htmlspecialchars($hintClass, ENT_QUOTES, 'UTF-8') ?>">勾選後仍可在後台或編輯表單維護資料，但訪客與會員將看不到此區塊。</p>
    </div>
</div>
