<?php
// Sous-vue de la fiche animale ; les variables sont préparées par AnimalsController::show().
?>
<!-- PHOTOS -->
<div class="tab-pane" id="tab-photos">
    <?php /** @var array $photos */ ?>

    <?php
$publicPhotos = array_values(array_filter($photos ?? [], fn ($p) => (int)($p['is_public'] ?? 1) === 1));
$privatePhotos = array_values(array_filter($photos ?? [], fn ($p) => (int)($p['is_public'] ?? 1) === 0));
?>

    <div class="row row-cards">

        <!-- PUBLIC -->
        <div class="col-12">
            <div class="card border-success">
                <div class="card-header d-flex align-items-center justify-content-between">
                    <div class="d-flex align-items-baseline gap-2">
                        <h3 class="card-title m-0"><?= h(t('animal.public_photos')) ?></h3>
                        <div class="text-muted small">(<?= h(t('animal.visible_online')) ?>)</div>
                    </div>

                    <form method="post" action="/animal/photos/upload" enctype="multipart/form-data"
                        class="d-flex gap-2">
                        <input type="hidden" name="animal_id" value="<?= (int)$animal['id'] ?>">
                        <input type="hidden" name="is_public" value="1">
                        <input type="hidden" name="redirect_hash" value="#tab-photos">
                        <input class="form-control form-control-sm" type="file" name="photos[]" multiple
                            accept="image/*" required>
                        <button class="btn btn-sm btn-success"
                            type="submit"><?= h(t('animal.upload_public')) ?></button>
                    </form>
                </div>

                <div class="card-body">
                    <?php if (empty($publicPhotos)): ?>
                    <div class="text-muted"><?= h(t('animal.no_public_photo')) ?></div>
                    <?php else: ?>
                    <div class="row row-cards">
                        <?php foreach ($publicPhotos as $p): ?>
                        <?php
    $src = '/media/animals/' . (int)$animal['id'] . '/' . h($p['filename']);
                            $isPrimary = (int)($p['is_primary'] ?? 0) === 1;
                            ?>
                        <div class="col-6 col-sm-4 col-md-3 col-lg-2">
                            <div class="card card-sm">
                                <a href="<?= $src ?>" target="_blank" class="d-block">
                                    <img src="<?= $src ?>" class="card-img-top" style="height:140px;object-fit:cover"
                                        alt="">
                                </a>

                                <div class="card-body p-2">
                                    <?= $isPrimary
                            ? '<span class="badge bg-primary-lt">' . h(t('animal.primary')) . '</span>'
                            : '<span class="badge bg-success-lt">' . h(t('animal.public')) . '</span>' ?>

                                    <?php if ($isPrimary): ?>
                                    <button type="submit" class="btn btn-sm btn-icon btn-light position-absolute"
                                        title="<?= h(t('animal.set_primary')) ?>"
                                        style="border-radius:999px; top:10px;right:10px;">
                                        <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="18" height="18"
                                            viewBox="0 0 24 24" stroke-width="2" stroke="currentColor"
                                            fill="currentColor" stroke-linecap="round" stroke-linejoin="round">
                                            <path stroke="none" d="M0 0h24v24H0z" fill="none" />
                                            <path fill="#edd180"
                                                d="M12 17.75l-5.878 3.09l1.122-6.545l-4.756-4.635l6.57-.955l2.942-5.955l2.942 5.955l6.57 .955l-4.756 4.635l1.122 6.545z" />
                                        </svg>
                                    </button>
                                    <?php else: ?>
                                    <form method="post" action="/animal/photos/primary" class="position-absolute"
                                        style="top:8px;right:8px;">
                                        <input type="hidden" name="animal_id" value="<?= (int)$animal['id'] ?>">
                                        <input type="hidden" name="photo_id" value="<?= (int)$p['id'] ?>">
                                        <input type="hidden" name="redirect_hash" value="#tab-photos">
                                        <button type="submit" class="btn btn-sm btn-icon btn-light"
                                            title="<?= h(t('animal.set_primary')) ?>"
                                            style="border-radius:999px;top:-2px; right:2px;">
                                            <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="18" height="18"
                                                viewBox="0 0 24 24" stroke-width="2" stroke="currentColor"
                                                fill="currentColor" stroke-linecap="round" stroke-linejoin="round">
                                                <path stroke="none" d="M0 0h24v24H0z" fill="none" />
                                                <path
                                                    d="M12 17.75l-5.878 3.09l1.122-6.545l-4.756-4.635l6.57-.955l2.942-5.955l2.942 5.955l6.57 .955l-4.756 4.635l1.122 6.545z" />
                                            </svg>
                                        </button>
                                    </form>
                                    <?php endif; ?>

                                    <div class="d-flex gap-1 mt-2">

                                        <form method="post" action="/animal/photos/toggle" class="flex-fill">
                                            <input type="hidden" name="animal_id" value="<?= (int)$animal['id'] ?>">
                                            <input type="hidden" name="photo_id" value="<?= (int)$p['id'] ?>">
                                            <input type="hidden" name="is_public" value="0">
                                            <input type="hidden" name="redirect_hash" value="#tab-photos">
                                            <button class="btn btn-sm btn-outline-warning w-100"
                                                type="submit"><?= h(t('animal.private')) ?></button>
                                        </form>

                                        <form method="post" action="/animal/photos/delete"
                                            onsubmit="return confirm(<?= h(json_encode(t('animal.delete_photo_confirm'), JSON_HEX_APOS | JSON_HEX_QUOT)) ?>);"
                                            class="flex-fill">
                                            <input type="hidden" name="animal_id" value="<?= (int)$animal['id'] ?>">
                                            <input type="hidden" name="photo_id" value="<?= (int)$p['id'] ?>">
                                            <input type="hidden" name="redirect_hash" value="#tab-photos">
                                            <button class="btn btn-sm btn-outline-danger w-100"
                                                type="submit"><?= h(t('common.delete_short')) ?></button>
                                        </form>


                                    </div>

                                </div>
                            </div>
                        </div>
                        <?php endforeach; ?>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <!-- PRIVE -->
        <div class="col-12">
            <div class="card border-danger">
                <div class="card-header d-flex align-items-center justify-content-between">
                    <div class="d-flex align-items-baseline gap-2">
                        <h3 class="card-title m-0"><?= h(t('animal.private_photos')) ?></h3>
                        <div class="text-muted small">(<?= h(t('animal.not_visible_online')) ?>)</div>
                    </div>

                    <form method="post" action="/animal/photos/upload" enctype="multipart/form-data"
                        class="d-flex gap-2">
                        <input type="hidden" name="animal_id" value="<?= (int)$animal['id'] ?>">
                        <input type="hidden" name="is_public" value="0">
                        <input type="hidden" name="redirect_hash" value="#tab-photos">
                        <input class="form-control form-control-sm" type="file" name="photos[]" multiple
                            accept="image/*" required>
                        <button class="btn btn-sm btn-danger"
                            type="submit"><?= h(t('animal.upload_private')) ?></button>
                    </form>
                </div>

                <div class="card-body">
                    <?php if (empty($privatePhotos)): ?>
                    <div class="text-muted"><?= h(t('animal.no_private_photo')) ?></div>
                    <?php else: ?>
                    <div class="row row-cards">
                        <?php foreach ($privatePhotos as $p): ?>
                        <?php $src = '/private-media?kind=photo&id=' . (int)$p['id']; ?>
                        <div class="col-6 col-sm-4 col-md-3 col-lg-2">
                            <div class="card card-sm bg-muted-lt">
                                <a href="<?= $src ?>" target="_blank" class="d-block">
                                    <img src="<?= $src ?>" class="card-img-top"
                                        style="height:140px;object-fit:cover;filter:grayscale(20%);" alt="">
                                </a>

                                <div class="card-body p-2">
                                    <span class="badge bg-danger-lt"><?= h(t('animal.private')) ?></span>
                                    <?php if (!empty($p['caption'])): ?><div class="small mt-2">
                                        <?= nl2br(h($p['caption'])) ?></div><?php endif; ?>
                                    <?php if (!empty($p['care_round_id'])): ?><div class="text-muted small mt-1">📋
                                        <?= h(t('animal.round_photo')) ?></div><?php endif; ?>

                                    <div class="d-flex gap-1 mt-2">
                                        <form method="post" action="/animal/photos/toggle" class="flex-fill">
                                            <input type="hidden" name="animal_id" value="<?= (int)$animal['id'] ?>">
                                            <input type="hidden" name="photo_id" value="<?= (int)$p['id'] ?>">
                                            <input type="hidden" name="is_public" value="1">
                                            <input type="hidden" name="redirect_hash" value="#tab-photos">
                                            <button class="btn btn-sm btn-outline-success w-100"
                                                type="submit"><?= h(t('animal.public')) ?></button>
                                        </form>

                                        <form method="post" action="/animal/photos/delete"
                                            onsubmit="return confirm(<?= h(json_encode(t('animal.delete_photo_confirm'), JSON_HEX_APOS | JSON_HEX_QUOT)) ?>);"
                                            class="flex-fill">
                                            <input type="hidden" name="animal_id" value="<?= (int)$animal['id'] ?>">
                                            <input type="hidden" name="photo_id" value="<?= (int)$p['id'] ?>">
                                            <input type="hidden" name="redirect_hash" value="#tab-photos">
                                            <button class="btn btn-sm btn-outline-danger w-100"
                                                type="submit"><?= h(t('common.delete_short')) ?></button>
                                        </form>
                                    </div>

                                </div>
                            </div>
                        </div>
                        <?php endforeach; ?>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>

    </div>
</div>

</div>

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