<?php
/** @var array $animal */
/** @var array $statuses */
/** @var array $speciesOptions */

// Liste races (tu pourras l’externaliser plus tard en table ref)
$breeds = [
    'Chat Européen',
'Abyssin','Bengal','British Shorthair','Chartreux','Maine Coon','Norvégien',
'Persan','Ragdoll','Siamois','Sphynx','Sacré de Birmanie','Scottish Fold',
'Autre / Inconnu',
];

$statuses = ['refuge' => t('status.refuge'),'fa' => t('status.fa'),'fa_permanente' => t('status.fa_permanente'),'chat_libre' => t('status.chat_libre'),'quarantaine' => t('status.quarantaine'),'soin' => t('status.soin'),'isolation' => t('status.isolation'),'hospitalise' => t('status.hospitalise'),'reserve' => t('status.reserve'),'adopte' => t('status.adopte'),'enfui' => t('status.enfui')];
if (in_array((string)($animal['status'] ?? ''), ['decede','décédé'], true)) {
    $statuses['decede'] = t('status.decede');
}

$sexLabels = [
    'M' => t('sex.male'),
'F' => t('sex.female'),
'U' => t('animals.to_define'),
];

$hairTypes = [
    '' => t('animals.to_define'),
    'court' => t('animals.short_hair'),
    'mi-long' => t('animals.medium_hair'),
    'long' => t('animals.long_hair'),
    'frise' => t('animals.curly_hair'),
    'sans-poil' => t('animals.hairless'),
];

$lifeProfileOptions = [
    'unknown' => t('animals.test_unknown'),
    'yes' => 'Oui',
    'no' => 'Non',
];

$coatColors = [
    'Bicolore Noir / Blanc',
    'Bicolore Gris / Blanc',
    'Bicolore Roux / Blanc',
    'Bicolore Tigré et Blanc',
    'Gris',
    'Roux',
    'Noir',
    'Chocolat',
    'Blanc',
    'Tricolore / Calico',
    'Écaille de tortue',
    'Colourpoint / Siamois',
    'Tigré / Tabby',
];

$breed = (string)($animal['breed'] ?? '');


$id = (int)($animal['id'] ?? 0);
$isEdit = $id > 0;
$isArchivedEdit = $isEdit && !empty($animal['archived_at']);

$status = (string)($animal['status'] ?? 'refuge');
$sex = (string)($animal['sex'] ?? 'U');

$birth_is_estimated = (int)($animal['birth_is_estimated'] ?? 0);
$birth_estimated_months = (int)($animal['birth_estimated_months'] ?? 0);

$estYears = $birth_estimated_months > 0 ? intdiv($birth_estimated_months, 12) : '';
$estMonths = $birth_estimated_months > 0 ? ($birth_estimated_months % 12) : '';

?>
<div class="row row-cards">
    <div class="col-12">
        <form method="post" action="/animal/save" class="card">

            <div class="card-body">
                <input type="hidden" name="id" value="<?= $id ?>">
                <?php if ($isArchivedEdit): ?><input type="hidden" name="status" value="<?= h($status) ?>">
                <div class="alert alert-info">📦 <?= h(t('animal.archive_edit_help')) ?></div><?php endif; ?>

                <div class="row g-3">

                    <!-- IDENTITÉ -->
                    <div class="col-12">
                        <div class="card card-sm">
                            <div class="card-status-top bg-primary"></div>
                            <div class="card-body">
                                <div class="d-flex align-items-center mb-3">
                                    <span class="status status-blue me-2"></span>
                                    <h4 class="m-0"><?= h(t('animal.tab.identity')) ?></h4>
                                </div>

                                <div class="row g-3">
                                    <div class="col-12 col-md-8">
                                        <label class="form-label"><?= h(t('common.name')) ?></label>
                                        <input class="form-control" name="name" value="<?= h($animal['name'] ?? '') ?>"
                                            required>
                                    </div>

                                    <div class="col-12 col-md-4">
                                        <label class="form-label required"><?= h(t('animal.species')) ?></label>
                                        <select class="form-select" name="species" id="animalSpecies" required>
                                            <?php foreach (($speciesOptions ?? []) as $option): ?>
                                            <option value="<?= h($option['code']) ?>"
                                                data-category="<?= h($option['category']) ?>"
                                                <?= SpeciesService::normalize((string)($animal['species'] ?? 'chat')) === $option['code'] ? 'selected' : '' ?>>
                                                <?= h($option['icon'] . ' ' . $option['name']) ?><?= empty($option['active']) ? ' (' . h(t('common.disabled')) . ')' : '' ?>
                                            </option>
                                            <?php endforeach; ?>
                                        </select>
                                        <div class="form-hint"><?= h(t('animal.form.species_hint')) ?></div>
                                    </div>

                                    <div class="col-12 col-md-4">
                                        <label class="form-label"><?= h(t('common.weight')) ?></label>
                                        <input class="form-control" name="initial_weight" type="number" step="0.01"
                                            min="0" value="<?= h($animal['initial_weight'] ?? '') ?>"
                                            placeholder="ex: 4.25">
                                        <div class="form-hint"><?= h(t('animal.form.weight_hint')) ?></div>
                                    </div>

                                    <div class="col-12 col-md-4">
                                        <label class="form-label"><?= h(t('animal.status')) ?></label>
                                        <select class="form-select" name="status_display" id="status"
                                            <?= $isArchivedEdit ? 'disabled' : '' ?>>
                                            <?php foreach ($statuses as $k => $label): ?>
                                            <option value="<?= h($k) ?>" <?= ($status === $k) ? 'selected' : '' ?>>
                                                <?= h($label) ?></option>
                                            <?php endforeach; ?>
                                        </select>
                                        <div class="form-hint"><?= h(t('animal.form.status_hint')) ?></div>
                                    </div>

                                    <div class="col-12 col-md-4" id="refugeRoomField">
                                        <label class="form-label"><?= h(t('animal.form.shelter_room')) ?></label>
                                        <select class="form-select" name="refuge_room"
                                            <?= $isArchivedEdit ? 'disabled' : '' ?>>
                                            <option value=""><?= h(t('animal.assign')) ?></option>
                                            <?php foreach (($shelterRooms ?? []) as $room): ?><option
                                                value="<?= h($room['code']) ?>"
                                                data-status="<?= h($room['status_code']) ?>"
                                                <?= (($animal['refuge_room'] ?? '') === $room['code']) ? 'selected' : '' ?>>
                                                <?= h($room['name']) ?></option><?php endforeach; ?>
                                        </select>
                                    </div>

                                    <div class="col-12 col-md-4" id="fosterContactField">
                                        <label class="form-label"><?= h(t('animal.form.foster')) ?></label>
                                        <select class="form-select" name="foster_contact_id">
                                            <option value=""><?= h(t('animal.form.provide_later')) ?></option>
                                            <?php foreach (($fosterContacts ?? []) as $contact): ?>
                                            <option value="<?= (int)$contact['id'] ?>"
                                                <?= (int)($currentFosterId ?? 0) === (int)$contact['id'] ? 'selected' : '' ?>>
                                                <?= h($contact['name'] . (!empty($contact['city']) ? ' · ' . $contact['city'] : '')) ?>
                                            </option>
                                            <?php endforeach; ?>
                                        </select>
                                        <div class="form-hint"><?= h(t('animal.form.foster_hint')) ?></div>
                                    </div>

                                    <div class="col-12 d-none" id="freeCatFields">
                                        <div class="card bg-green-lt border-green-lt">
                                            <div class="card-body py-3">
                                                <div class="d-flex align-items-center gap-2 mb-3"><span
                                                        aria-hidden="true">🌿</span>
                                                    <div><strong><?= h(t('freecat.journey')) ?></strong>
                                                        <div class="text-muted small">
                                                            <?= h(t('freecat.journey_help')) ?></div>
                                                    </div>
                                                </div>
                                                <div class="row g-3">
                                                    <div class="col-md-6"><label
                                                            class="form-label"><?= h(t('freecat.site')) ?></label><input
                                                            class="form-control" name="free_cat_site_name"
                                                            value="<?= h($animal['free_cat_site_name'] ?? '') ?>"
                                                            maxlength="180"
                                                            placeholder="<?= h(t('freecat.site_example')) ?>"></div>
                                                    <div class="col-md-3"><label
                                                            class="form-label"><?= h(t('common.city')) ?></label><input
                                                            class="form-control" name="free_cat_city"
                                                            value="<?= h($animal['free_cat_city'] ?? '') ?>"
                                                            maxlength="120"></div>
                                                    <div class="col-md-3"><label
                                                            class="form-label"><?= h(t('freecat.tracking_state')) ?></label><select
                                                            class="form-select"
                                                            name="free_cat_tracking_status"><?php foreach (['present' => t('freecat.present'),'missing' => t('freecat.missing'),'moved' => t('freecat.moved')] as $key => $label): ?>
                                                            <option value="<?= h($key) ?>"
                                                                <?= ($animal['free_cat_tracking_status'] ?? 'present') === $key ? 'selected' : '' ?>>
                                                                <?= h($label) ?></option><?php endforeach; ?></select>
                                                        <div class="form-hint"><?= h(t('freecat.death_help')) ?></div>
                                                    </div>
                                                    <div class="col-md-6"><label
                                                            class="form-label"><?= h(t('freecat.precise_location')) ?>
                                                            <span
                                                                class="text-muted">(<?= h(t('common.internal')) ?>)</span></label><input
                                                            class="form-control" name="free_cat_address"
                                                            value="<?= h($animal['free_cat_address'] ?? '') ?>"
                                                            maxlength="255"
                                                            placeholder="<?= h(t('freecat.address_placeholder')) ?>">
                                                    </div>
                                                    <div class="col-md-6"><label
                                                            class="form-label"><?= h(t('freecat.caretaker')) ?></label><select
                                                            class="form-select" name="free_cat_caretaker_contact_id">
                                                            <option value=""><?= h(t('common.not_provided')) ?></option>
                                                            <?php foreach (($freeCatCaretakers ?? []) as $contact): ?>
                                                            <option value="<?= (int)$contact['id'] ?>"
                                                                <?= (int)($animal['free_cat_caretaker_contact_id'] ?? 0) === (int)$contact['id'] ? 'selected' : '' ?>>
                                                                <?= h($contact['name'] . (!empty($contact['city']) ? ' · ' . $contact['city'] : '')) ?>
                                                            </option><?php endforeach; ?>
                                                        </select>
                                                        <div class="form-hint"><?= h(t('freecat.directory_person')) ?>
                                                        </div>
                                                    </div>
                                                    <div class="col-md-6"><label
                                                            class="form-label"><?= h(t('freecat.capture_date')) ?></label><input
                                                            class="form-control" type="date" name="free_cat_captured_at"
                                                            value="<?= h($animal['free_cat_captured_at'] ?? '') ?>">
                                                    </div>
                                                    <div class="col-md-6"><label
                                                            class="form-label"><?= h(t('freecat.release_date')) ?></label><input
                                                            class="form-control" type="date" name="free_cat_released_at"
                                                            value="<?= h($animal['free_cat_released_at'] ?? '') ?>">
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                    <?php if (!empty($animal['id'])): ?>
                                    <div class="col-12 col-md-4">
                                        <label class="form-label"><?= h(t('animal.form.location_reason')) ?></label>
                                        <input class="form-control" name="location_change_reason" maxlength="255"
                                            placeholder="Facultatif : changement de salle, retour de FA…"
                                            <?= $isArchivedEdit ? 'disabled' : '' ?>>
                                        <div class="form-hint"><?= h(t('animal.form.location_reason_hint')) ?></div>
                                    </div>
                                    <?php endif; ?>

                                    <div class="col-12 col-md-4">
                                        <label class="form-label"><?= h(t('animal.breed')) ?></label>
                                        <input class="form-control" name="breed" list="breedSuggestions"
                                            value="<?= h($breed) ?>" placeholder="Race ou type, facultatif">
                                        <datalist id="breedSuggestions"><?php foreach ($breeds as $b): ?><option
                                                value="<?= h($b) ?>"><?php endforeach; ?></datalist>
                                    </div>

                                    <div class="col-12 col-md-6">
                                        <label class="form-label"><?= h(t('animal.coat')) ?></label>
                                        <?php $currentColor = trim((string)($animal['color'] ?? '')); ?>
                                        <div class="input-group">
                                            <span class="input-group-text"><span id="coatColorSwatch"
                                                    class="coat-swatch"
                                                    style="<?= h(coat_swatch_style($currentColor)) ?>"></span></span>
                                            <select class="form-select" name="color" id="coatColorSelect">
                                                <option value="" data-swatch="<?= h(coat_swatch_style('')) ?>"
                                                    <?= $currentColor === '' ? 'selected' : '' ?>>
                                                    <?= h(t('animals.to_define')) ?></option>
                                                <?php if ($currentColor !== '' && !in_array($currentColor, $coatColors, true)): ?>
                                                <option value="<?= h($currentColor) ?>"
                                                    data-swatch="<?= h(coat_swatch_style($currentColor)) ?>" selected>
                                                    <?= h($currentColor) ?> (valeur actuelle)</option>
                                                <?php endif; ?>
                                                <?php foreach ($coatColors as $coatColor): ?>
                                                <option value="<?= h($coatColor) ?>"
                                                    data-swatch="<?= h(coat_swatch_style($coatColor)) ?>"
                                                    <?= $currentColor === $coatColor ? 'selected' : '' ?>>
                                                    <?= h($coatColor) ?></option>
                                                <?php endforeach; ?>
                                            </select>
                                        </div>
                                    </div>

                                    <div class="col-12 col-md-6">
                                        <label class="form-label"><?= h(t('animal.form.hair_type')) ?></label>
                                        <select class="form-select" name="hair_type">
                                            <?php foreach ($hairTypes as $value => $label): ?>
                                            <option value="<?= h($value) ?>"
                                                <?= ((string)($animal['hair_type'] ?? '') === $value) ? 'selected' : '' ?>>
                                                <?= h($label) ?></option>
                                            <?php endforeach; ?>
                                        </select>
                                    </div>

                                    <div class="col-12 col-md-3">
                                        <label class="form-label"><?= h(t('animal.sex')) ?></label>
                                        <select class="form-select" name="sex">
                                            <option value="U" <?= $sex === 'U' ? 'selected' : '' ?>>
                                                <?= h(t('animals.to_define')) ?></option>
                                            <option value="M" <?= $sex === 'M' ? 'selected' : '' ?>>
                                                <?= h(t('sex.male')) ?></option>
                                            <option value="F" <?= $sex === 'F' ? 'selected' : '' ?>>
                                                <?= h(t('sex.female')) ?></option>
                                        </select>
                                    </div>

                                    <div class="col-12 col-md-3">
                                        <label class="form-label"><?= h(t('animal.form.sterilization')) ?></label>
                                        <select class="form-select" name="sterilization_status"
                                            id="sterilizationStatus">
                                            <option value="unknown"
                                                <?= (($animal['sterilization_status'] ?? 'unknown') === 'unknown') ? 'selected' : '' ?>>
                                                <?= h(t('common.unknown')) ?></option>
                                            <option value="no"
                                                <?= (($animal['sterilization_status'] ?? 'unknown') === 'no') ? 'selected' : '' ?>>
                                                <?= h(t('animals.not_sterilized')) ?></option>
                                            <option value="yes"
                                                <?= (($animal['sterilization_status'] ?? 'unknown') === 'yes') ? 'selected' : '' ?>>
                                                <?= h(t('animal.sterilized')) ?></option>
                                        </select>
                                    </div>

                                    <div class="col-12 row g-3 m-0 p-0 <?= (($animal['sterilization_status'] ?? 'unknown') === 'yes') ? '' : 'd-none' ?>"
                                        id="sterilizationDetails">
                                        <div class="col-md-4"><label
                                                class="form-label"><?= h(t('animal.form.sterilization_date')) ?></label><input
                                                class="form-control" type="date" name="sterilization_date"
                                                value="<?= h($animal['sterilization_date'] ?? '') ?>"></div>
                                        <div class="col-md-4"><label
                                                class="form-label"><?= h(t('animal.form.veterinarian')) ?></label><select
                                                class="form-select" name="sterilization_vet_contact_id">
                                                <option value=""><?= h(t('common.not_provided')) ?></option>
                                                <?php foreach (($sterilizationVets ?? []) as $contact): ?><option
                                                    value="<?= (int)$contact['id'] ?>"
                                                    <?= ((int)($animal['sterilization_vet_contact_id'] ?? 0) === (int)$contact['id']) ? 'selected' : '' ?>>
                                                    <?= h($contact['name']) ?></option><?php endforeach; ?>
                                            </select></div>
                                        <div class="col-md-4"><label
                                                class="form-label"><?= h(t('animal.form.clinic')) ?></label><select
                                                class="form-select" name="sterilization_clinic_contact_id">
                                                <option value=""><?= h(t('common.not_provided')) ?></option>
                                                <?php foreach (($sterilizationClinics ?? []) as $contact): ?><option
                                                    value="<?= (int)$contact['id'] ?>"
                                                    <?= ((int)($animal['sterilization_clinic_contact_id'] ?? 0) === (int)$contact['id']) ? 'selected' : '' ?>>
                                                    <?= h($contact['name']) ?></option><?php endforeach; ?>
                                            </select></div>
                                    </div>

                                    <div class="col-12">
                                        <hr class="my-1">
                                        <h5 class="mb-0"><?= h(t('animal.form.identification')) ?></h5>
                                    </div>
                                    <div class="col-12 col-md-3"><label
                                            class="form-label"><?= h(t('animal.form.type')) ?></label><select
                                            class="form-select" name="identification_type">
                                            <option value="unknown"
                                                <?= (($animal['identification_type'] ?? 'unknown') === 'unknown') ? 'selected' : '' ?>>
                                                <?= h(t('animal.form.none_identification')) ?></option>
                                            <option value="microchip"
                                                <?= (($animal['identification_type'] ?? 'unknown') === 'microchip') ? 'selected' : '' ?>>
                                                <?= h(t('animal.microchip')) ?></option>
                                            <option value="tattoo"
                                                <?= (($animal['identification_type'] ?? 'unknown') === 'tattoo') ? 'selected' : '' ?>>
                                                <?= h(t('animal.tattoo')) ?></option>
                                        </select></div>
                                    <div class="col-12 col-md-3"><label
                                            class="form-label"><?= h(t('animal.form.number')) ?></label><input
                                            class="form-control" name="chip_id"
                                            value="<?= h($animal['chip_id'] ?? '') ?>"
                                            placeholder="<?= h(t('animal.form.chip_number')) ?>"></div>
                                    <div class="col-12 col-md-3"><label
                                            class="form-label"><?= h(t('animal.form.implant_date')) ?></label><input
                                            class="form-control" type="date" lang="<?= h(I18n::locale()) ?>"
                                            name="identification_date"
                                            value="<?= h($animal['identification_date'] ?? '') ?>"></div>
                                    <div class="col-12 col-md-3"><label
                                            class="form-label"><?= h(t('animal.form.registration')) ?></label><select
                                            class="form-select" name="identification_registration_status">
                                            <option value="unknown"
                                                <?= (($animal['identification_registration_status'] ?? 'unknown') === 'unknown') ? 'selected' : '' ?>>
                                                <?= h(t('common.unknown')) ?></option>
                                            <option value="pending"
                                                <?= (($animal['identification_registration_status'] ?? 'unknown') === 'pending') ? 'selected' : '' ?>>
                                                <?= h(t('animal.form.pending')) ?></option>
                                            <option value="registered"
                                                <?= (($animal['identification_registration_status'] ?? 'unknown') === 'registered') ? 'selected' : '' ?>>
                                                <?= h(t('animal.form.registered')) ?></option>
                                        </select></div>

                                    <div class="col-12">
                                        <hr class="my-1">
                                        <h5 class="mb-0"><?= h(t('animal.form.adoption')) ?></h5>
                                    </div>
                                    <div class="col-12 col-md-4"><label
                                            class="form-label"><?= h(t('animal.form.availability')) ?></label><select
                                            class="form-select" name="adoption_availability" id="adoptionAvailability">
                                            <option value="unknown"
                                                <?= (($animal['adoption_availability'] ?? 'unknown') === 'unknown') ? 'selected' : '' ?>>
                                                <?= h(t('animals.to_define')) ?></option>
                                            <option value="not_available"
                                                <?= (($animal['adoption_availability'] ?? 'unknown') === 'not_available') ? 'selected' : '' ?>>
                                                <?= h(t('animal.form.not_available')) ?></option>
                                            <option value="available"
                                                <?= (($animal['adoption_availability'] ?? 'unknown') === 'available') ? 'selected' : '' ?>>
                                                <?= h(t('animals.available_title')) ?></option>
                                        </select></div>
                                    <div class="col-12 col-md-4" id="adoptionAvailableFrom"><label
                                            class="form-label"><?= h(t('animal.form.available_from')) ?></label><input
                                            class="form-control" type="date" lang="<?= h(I18n::locale()) ?>"
                                            name="adoption_available_from"
                                            value="<?= h($animal['adoption_available_from'] ?? '') ?>"></div>
                                    <div class="col-12 col-md-4" id="adoptionUnavailableReason"><label
                                            class="form-label"><?= h(t('animal.form.unavailable_reason')) ?></label><input
                                            class="form-control" name="adoption_unavailability_reason" maxlength="255"
                                            value="<?= h($animal['adoption_unavailability_reason'] ?? '') ?>"
                                            placeholder="Soins, sociabilisation…"></div>
                                    <div class="col-12">
                                        <div class="card card-sm bg-green-lt">
                                            <div class="card-body"><label class="form-check"><input
                                                        class="form-check-input" type="checkbox"
                                                        name="website_published" value="1"
                                                        <?= !empty($animal['website_published']) ? 'checked' : '' ?>><span
                                                        class="form-check-label fw-bold"><?= h(t('public_site.publish_animal')) ?></span></label>
                                                <div class="form-hint mb-2">
                                                    <?= h(t('public_site.publish_animal_help')) ?></div><label
                                                    class="form-label"><?= h(t('public_site.public_description')) ?></label><textarea
                                                    class="form-control" name="website_description" rows="4"
                                                    maxlength="5000"
                                                    placeholder="<?= h(t('public_site.public_description_example')) ?>"><?= h($animal['website_description'] ?? '') ?></textarea>
                                            </div>
                                        </div>
                                    </div>

                                    <div class="row">
                                        <div class="col-md-8">
                                            <div class="text-muted"><?= h(t('animal.rescue_location')) ?></div>
                                            <input class="form-control" type="text" name="rescue_address"
                                                value="<?= h($animal['rescue_address'] ?? '') ?>"
                                                placeholder="<?= h(t('animal.rescue_location_placeholder')) ?>">
                                            <div class="text-muted small mt-1"><?= h(t('animal.geocode_help')) ?></div>
                                        </div>

                                        <div class="col-md-4">
                                            <div class="text-muted"><?= h(t('animal.form.current_location')) ?></div>
                                            <input class="form-control" type="text" name="current_address"
                                                value="<?= h($animal['current_address'] ?? '5 RUE NOTRE DAME 29260 LESNEVEN') ?>"
                                                <?= $isArchivedEdit ? 'readonly' : '' ?> disabled>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- PROFIL DE VIE -->
                    <div class="col-12">
                        <div class="card card-sm">
                            <div class="card-status-top bg-cyan"></div>
                            <div class="card-body">
                                <div class="d-flex align-items-center mb-3">
                                    <span class="status status-cyan me-2"></span>
                                    <div>
                                        <h4 class="m-0"><?= h(t('animal.form.life_profile')) ?></h4>
                                        <div class="text-muted small"><?= h(t('animal.form.life_profile_hint')) ?></div>
                                    </div>
                                </div>
                                <div class="row g-3">
                                    <?php foreach ([
    'compatibility_dogs' => ['🐶',t('animal.compatible_dogs')],
    'compatibility_cats' => ['🐱',t('animal.compatible_cats')],
    'compatibility_children' => ['🧒',t('animal.compatible_children')],
    'house_trained' => ['🧼',t('animal.house_trained')],
] as $field => $profile): ?>
                                    <div class="col-12 col-sm-6 col-lg-3">
                                        <label class="form-label"><span aria-hidden="true"><?= $profile[0] ?></span>
                                            <?= h($profile[1]) ?></label>
                                        <select class="form-select" name="<?= h($field) ?>">
                                            <?php foreach ($lifeProfileOptions as $value => $label): ?><option
                                                value="<?= h($value) ?>"
                                                <?= (($animal[$field] ?? 'unknown') === $value) ? 'selected' : '' ?>>
                                                <?= h($label) ?></option><?php endforeach; ?>
                                        </select>
                                    </div>
                                    <?php endforeach; ?>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- ÂGE -->
                    <div class="col-12">
                        <div class="card card-sm">
                            <div class="card-status-top bg-red"></div>
                            <div class="card-body">
                                <div class="d-flex align-items-center mb-3">
                                    <span class="status status-red me-2"></span>
                                    <h4 class="m-0"><?= h(t('animal.form.age')) ?></h4>
                                </div>

                                <div class="row g-3 align-items-start">
                                    <div class="col-12 col-md-5">
                                        <label class="form-label"><?= h(t('animal.form.birth_date')) ?></label>
                                        <input class="form-control" type="date" name="birth_date"
                                            value="<?= h($animal['birth_date'] ?? '') ?>">
                                    </div>

                                    <div class="col-12 col-md-2">
                                        <label class="form-label"><?= h(t('animal.form.estimated_birth')) ?></label>
                                        <div class="pt-2">
                                            <label class="form-check form-switch">
                                                <input class="form-check-input" type="checkbox"
                                                    name="birth_is_estimated" value="1" id="birth_is_estimated"
                                                    <?= $birth_is_estimated ? 'checked' : '' ?>>
                                                <span class="form-check-label">Oui</span>
                                            </label>
                                        </div>
                                    </div>

                                    <div class="col-6 col-md-2">
                                        <label class="form-label"><?= h(t('animal.form.estimated_years')) ?></label>
                                        <input class="form-control" type="number" min="0" max="40"
                                            name="birth_est_years" value="<?= h($estYears) ?>" placeholder="ex: 7">
                                    </div>

                                    <div class="col-6 col-md-3">
                                        <label class="form-label"><?= h(t('animal.form.estimated_months')) ?></label>
                                        <input class="form-control" type="number" min="0" max="11"
                                            name="birth_est_months" value="<?= h($estMonths) ?>" placeholder="ex: 4">
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- LOCALISATION -->
                    <div class="col-12">
                        <div class="card card-sm">
                            <div class="card-status-top bg-orange"></div>
                            <div class="card-body">
                                <div class="d-flex align-items-center mb-3">
                                    <span class="status status-orange me-2"></span>
                                    <h4 class="m-0"><?= h(t('animal.form.location')) ?></h4>
                                </div>

                                <div class="row g-3">
                                    <div class="col-md-6">
                                        <label class="form-label"><?= h(t('animal.form.origin_city')) ?></label>
                                        <input type="text" name="rescue_location_name" class="form-control"
                                            value="<?= h($animal['rescue_location_name'] ?? '') ?>"
                                            placeholder="<?= h(t('common.city_example')) ?>">
                                        <div class="form-hint"><?= h(t('animal.form.origin_hint')) ?></div>

                                        <input type="hidden" name="rescue_lat"
                                            value="<?= h($animal['rescue_lat'] ?? '') ?>">
                                        <input type="hidden" name="rescue_lng"
                                            value="<?= h($animal['rescue_lng'] ?? '') ?>">
                                    </div>

                                    <div class="col-md-6">
                                        <label class="form-label"><?= h(t('animal.form.current_location')) ?></label>
                                        <input type="text" name="location_name" class="form-control"
                                            value="<?= h($animal['location_name'] ?? '5 Rue Notre Dame 29260 LESNEVEN') ?>"
                                            readonly>
                                        <div class="form-hint"><?= h(t('animal.form.current_location_hint')) ?></div>

                                        <input type="hidden" name="location_lat"
                                            value="<?= h($animal['location_lat'] ?? '') ?>">
                                        <input type="hidden" name="location_lng"
                                            value="<?= h($animal['location_lng'] ?? '') ?>">
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- ENTRÉE AU REFUGE -->
                    <div class="col-12">
                        <div class="card card-sm">
                            <div class="card-status-top bg-green"></div>
                            <div class="card-body">
                                <div class="d-flex align-items-center mb-3">
                                    <span class="status status-green me-2"></span>
                                    <div>
                                        <h4 class="m-0"><?= h(t('animal.form.intake')) ?></h4>
                                        <div class="text-muted small"><?= h(t('animal.form.intake_hint')) ?></div>
                                    </div>
                                </div>

                                <div class="row g-3">
                                    <div class="col-12 col-md-4" id="intakeWrap">
                                        <label class="form-label"><?= h(t('animal.form.intake_date')) ?></label>
                                        <input class="form-control" type="date" name="intake_date"
                                            value="<?= h($animal['intake_date'] ?? '') ?>">
                                    </div>

                                    <div class="col-12 col-md-4">
                                        <label class="form-label"><?= h(t('animal.form.intake_type')) ?></label>
                                        <select class="form-select"
                                            name="intake_type"><?php foreach (($intakeTypes ?? []) as $value => $label): ?>
                                            <option value="<?= h($value) ?>"
                                                <?= (($animal['intake_type'] ?? 'unknown') === $value) ? 'selected' : '' ?>>
                                                <?= h($label) ?></option><?php endforeach; ?></select>
                                    </div>

                                    <div class="col-12 col-md-4">
                                        <label class="form-label"><?= h(t('animal.form.intake_reason')) ?></label>
                                        <select class="form-select"
                                            name="intake_reason"><?php foreach (($intakeReasons ?? []) as $value => $label): ?>
                                            <option value="<?= h($value) ?>"
                                                <?= (($animal['intake_reason'] ?? 'unknown') === $value) ? 'selected' : '' ?>>
                                                <?= h($label) ?></option><?php endforeach; ?></select>
                                    </div>

                                    <div class="col-12 col-md-6">
                                        <label class="form-label"><?= h(t('animal.form.depositor')) ?></label>
                                        <select class="form-select" name="depositor_contact_id" id="depositorContact">
                                            <option value=""><?= h(t('animal.form.no_depositor')) ?></option>
                                            <?php foreach (($depositors ?? []) as $contact): ?><option
                                                value="<?= (int)$contact['id'] ?>"
                                                <?= ((int)($animal['depositor_contact_id'] ?? 0) === (int)$contact['id']) ? 'selected' : '' ?>>
                                                <?= h($contact['name'] . (!empty($contact['city']) ? ' · ' . $contact['city'] : '')) ?>
                                            </option><?php endforeach; ?><option value="new">＋
                                                <?= h(t('animal.form.new_depositor')) ?></option>
                                        </select>
                                    </div>

                                    <div class="col-12 col-md-6 d-none" id="newDepositorWrap">
                                        <label class="form-label"><?= h(t('animal.form.new_depositor_name')) ?></label>
                                        <input class="form-control" name="new_depositor_name" maxlength="160"
                                            placeholder="Personne ou structure">
                                        <div class="form-hint"><?= h(t('animal.form.depositor_hint')) ?></div>
                                    </div>

                                    <div class="col-12">
                                        <label class="form-label"><?= h(t('animal.form.circumstances')) ?></label>
                                        <textarea class="form-control" name="intake_circumstances" rows="3"
                                            placeholder="<?= h(t('animal.intake_circumstances_placeholder')) ?>"><?= h($animal['intake_circumstances'] ?? '') ?></textarea>
                                    </div>

                                    <div class="col-12 col-md-6" id="quUntilWrap">
                                        <label class="form-label"><?= h(t('animal.form.quarantine_exit')) ?></label>
                                        <input class="form-control" value="<?= h($animal['quarantine_until'] ?? '') ?>"
                                            readonly>
                                        <div class="form-hint">
                                            <?= h(t('animal.form.quarantine_hint', ['count' => AppSettings::int('quarantine_days', 1, 90)])) ?>
                                        </div>
                                    </div>

                                    <div class="col-12">
                                        <label class="form-label"><?= h(t('common.notes')) ?></label>
                                        <textarea class="form-control" name="notes"
                                            rows="5"><?= h($animal['notes'] ?? '') ?></textarea>
                                    </div>
                                </div>

                            </div>
                        </div>
                    </div>

                </div>
            </div>

            <div class="card-footer d-flex justify-content-between">
                <a class="btn" href="/animals">Annuler</a>
                <button class="btn btn-primary" type="submit"><?= h(t('common.save')) ?></button>
            </div>
        </form>
    </div>
</div>

<script>
    (function() {
        const status = document.getElementById('status');
        const intakeWrap = document.getElementById('intakeWrap');
        const quUntilWrap = document.getElementById('quUntilWrap');
        const refugeRoomField = document.getElementById('refugeRoomField');
        const fosterContactField = document.getElementById('fosterContactField');
        const freeCatFields = document.getElementById('freeCatFields');
        const coatSelect = document.getElementById('coatColorSelect');
        const coatSwatch = document.getElementById('coatColorSwatch');

        function sync() {
            const v = (status.value || '').toLowerCase();
            const show = (v === 'quarantaine');
            quUntilWrap.style.display = show ? '' : 'none';
            const roomSelect = refugeRoomField.querySelector('select');
            const hasRooms = Array.from(roomSelect.options).some(option => option.dataset.status === v);
            refugeRoomField.style.display = hasRooms ? '' : 'none';
            roomSelect.querySelectorAll('option[data-status]').forEach(option => option.hidden = option.dataset
                .status !== v);
            if (roomSelect.selectedOptions[0]?.hidden) roomSelect.value = '';
            fosterContactField.style.display = (v === 'fa' || v === 'fa_permanente') ? '' : 'none';
            freeCatFields?.classList.toggle('d-none', v !== 'chat_libre');
        }
        status.addEventListener('change', sync);
        sync();

        function syncCoatSwatch() {
            const option = coatSelect?.options[coatSelect.selectedIndex];
            if (coatSwatch && option) coatSwatch.style.cssText = option.dataset.swatch || 'background:#d9dde3';
        }
        coatSelect?.addEventListener('change', syncCoatSwatch);
        syncCoatSwatch();
        const sterilizationStatus = document.getElementById('sterilizationStatus');
        const sterilizationDetails = document.getElementById('sterilizationDetails');
        const syncSterilization = () => sterilizationDetails?.classList.toggle('d-none', sterilizationStatus
            ?.value !== 'yes');
        sterilizationStatus?.addEventListener('change', syncSterilization);
        syncSterilization();
        const depositorContact = document.getElementById('depositorContact');
        const newDepositorWrap = document.getElementById('newDepositorWrap');
        const syncDepositor = () => newDepositorWrap?.classList.toggle('d-none', depositorContact?.value !== 'new');
        depositorContact?.addEventListener('change', syncDepositor);
        syncDepositor();
        const adoptionAvailability = document.getElementById('adoptionAvailability');
        const adoptionAvailableFrom = document.getElementById('adoptionAvailableFrom');
        const adoptionUnavailableReason = document.getElementById('adoptionUnavailableReason');
        const syncAdoption = () => {
            const value = adoptionAvailability?.value;
            adoptionAvailableFrom?.classList.toggle('d-none', value !== 'available');
            adoptionUnavailableReason?.classList.toggle('d-none', value !== 'not_available');
        };
        adoptionAvailability?.addEventListener('change', syncAdoption);
        syncAdoption();
    })();

</script>
