<?php

$title =
    'Homepage Snapshot Compare';

function hsc_cmp_h($v)
{
    return htmlspecialchars(
        (string)$v,
        ENT_QUOTES,
        'UTF-8'
    );
}

function hsc_sections_by_key($json)
{
    $rows =
        json_decode(
            (string)$json,
            true
        );

    if (!is_array($rows)) {
        return [];
    }

    $out = [];

    foreach ($rows as $row) {

        if (!is_array($row)) {
            continue;
        }

        $key =
            (string)(
                $row['section_key']
                ??
                $row['section_code']
                ??
                $row['key']
                ??
                ''
            );

        if ($key === '') {
            continue;
        }

        $out[$key] =
            $row;
    }

    return $out;
}

$left =
    $templates[0]
    ?? null;

$right =
    $templates[1]
    ?? null;

$added = [];
$removed = [];
$modified = [];

if ($left && $right) {

    $leftSections =
        hsc_sections_by_key(
            $left['sections_json']
            ?? '[]'
        );

    $rightSections =
        hsc_sections_by_key(
            $right['sections_json']
            ?? '[]'
        );

    foreach ($rightSections as $key => $row) {

        if (!isset($leftSections[$key])) {

            $added[$key] =
                $row;

            continue;
        }

        if (
            json_encode($leftSections[$key], JSON_UNESCAPED_UNICODE)
            !==
            json_encode($row, JSON_UNESCAPED_UNICODE)
        ) {
            $modified[$key] =
                $row;
        }
    }

    foreach ($leftSections as $key => $row) {

        if (!isset($rightSections[$key])) {

            $removed[$key] =
                $row;
        }
    }
}

?>

<div class="container-fluid py-4">

<div class="card border-0 shadow-lg rounded-4 mb-4">

<div class="card-body">

<h2 class="fw-bold mb-2">

<i class="fa fa-code-compare text-primary"></i>

Homepage Snapshot Compare Center

</h2>

<div class="text-muted">

Compare two saved homepage snapshots from conference_home_templates.

</div>

</div>

</div>

<div class="card border-0 shadow-sm rounded-4 mb-4">

<div class="card-body">

<form
method="get"
action="/easychair/admin/conference-home/template/compare">

<div class="row g-3 align-items-end">

<div class="col-lg-5">

<label class="form-label fw-bold">

Snapshot A

</label>

<select
name="a"
class="form-select"
required>

<option value="">

Select snapshot A

</option>

<?php foreach (($allTemplates ?? []) as $t): ?>

<option
value="<?= (int)$t['id'] ?>"
<?= ((int)($_GET['a'] ?? 0) === (int)$t['id']) ? 'selected' : '' ?>>

#<?= (int)$t['id'] ?>
-
<?= hsc_cmp_h($t['name'] ?? 'Untitled') ?>
-
<?= hsc_cmp_h($t['created_at'] ?? '') ?>

</option>

<?php endforeach; ?>

</select>

</div>

<div class="col-lg-5">

<label class="form-label fw-bold">

Snapshot B

</label>

<select
name="b"
class="form-select"
required>

<option value="">

Select snapshot B

</option>

<?php foreach (($allTemplates ?? []) as $t): ?>

<option
value="<?= (int)$t['id'] ?>"
<?= ((int)($_GET['b'] ?? 0) === (int)$t['id']) ? 'selected' : '' ?>>

#<?= (int)$t['id'] ?>
-
<?= hsc_cmp_h($t['name'] ?? 'Untitled') ?>
-
<?= hsc_cmp_h($t['created_at'] ?? '') ?>

</option>

<?php endforeach; ?>

</select>

</div>

<div class="col-lg-2">

<button
class="btn btn-primary w-100 rounded-pill">

<i class="fa fa-code-compare"></i>

Compare

</button>

</div>

</div>

</form>

</div>

</div>

<?php if ($left && $right): ?>

<div class="row g-4 mb-4">

<div class="col-lg-6">

<div class="card border-0 shadow-sm rounded-4 h-100">

<div class="card-body">

<h5 class="fw-bold">

Snapshot A

</h5>

<div>

<?= hsc_cmp_h($left['name'] ?? '') ?>

</div>

<div class="text-muted small">

<?= hsc_cmp_h($left['created_at'] ?? '') ?>

</div>

</div>

</div>

</div>

<div class="col-lg-6">

<div class="card border-0 shadow-sm rounded-4 h-100">

<div class="card-body">

<h5 class="fw-bold">

Snapshot B

</h5>

<div>

<?= hsc_cmp_h($right['name'] ?? '') ?>

</div>

<div class="text-muted small">

<?= hsc_cmp_h($right['created_at'] ?? '') ?>

</div>

</div>

</div>

</div>

</div>

<div class="row g-4">

<div class="col-lg-4">

<div class="card border-0 shadow-sm rounded-4 h-100">

<div class="card-body">

<h4 class="fw-bold text-success">

Added Sections

</h4>

<?php if (empty($added)): ?>

<div class="text-muted">

No added sections.

</div>

<?php else: ?>

<?php foreach ($added as $key => $row): ?>

<div class="border rounded-3 p-2 mb-2">

+ <?= hsc_cmp_h($key) ?>

</div>

<?php endforeach; ?>

<?php endif; ?>

</div>

</div>

</div>

<div class="col-lg-4">

<div class="card border-0 shadow-sm rounded-4 h-100">

<div class="card-body">

<h4 class="fw-bold text-danger">

Removed Sections

</h4>

<?php if (empty($removed)): ?>

<div class="text-muted">

No removed sections.

</div>

<?php else: ?>

<?php foreach ($removed as $key => $row): ?>

<div class="border rounded-3 p-2 mb-2">

- <?= hsc_cmp_h($key) ?>

</div>

<?php endforeach; ?>

<?php endif; ?>

</div>

</div>

</div>

<div class="col-lg-4">

<div class="card border-0 shadow-sm rounded-4 h-100">

<div class="card-body">

<h4 class="fw-bold text-warning">

Modified Sections

</h4>

<?php if (empty($modified)): ?>

<div class="text-muted">

No modified sections.

</div>

<?php else: ?>

<?php foreach ($modified as $key => $row): ?>

<div class="border rounded-3 p-2 mb-2">

* <?= hsc_cmp_h($key) ?>

</div>

<?php endforeach; ?>

<?php endif; ?>

</div>

</div>

</div>

</div>

<?php else: ?>

<div class="alert alert-info rounded-4">

Select two snapshots to compare.

</div>

<?php endif; ?>

</div>
