<?php
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

require_once __DIR__ . "/../../lib/requiredpublic.php";

$childinfo = ['name' => '', 'month' => 1, 'year' => date('Y', strtotime('now - 10 years')), 'graduated' => false];

if (isset($childid) && $database->has('people', ['personid' => $childid])) {
    $randomid = $childid;
    $chinfo = $database->get('people', ['name', 'birthday', 'graduated'], ['personid' => $childid]);
    $childinfo['name'] = $chinfo['name'];
    $childinfo['graduated'] = $chinfo['graduated'] == true;
    $childinfo['month'] = date('m', strtotime($chinfo['birthday']));
    $childinfo['year'] = date('Y', strtotime($chinfo['birthday']));
} else {
    do {
        $randomid = mt_rand(0, 9999999999);
    } while ($database->has('people', ['personid' => $randomid]));
}
?>

<div class="list-group-item">
    <input type="hidden" name="child[ids][]" value="<?php echo $randomid; ?>" />
    <div class="row">
        <div class="col-12 col-sm-4">
            <div class="form-group">
                <label>Name:</label>
                <div class="input-group">
                    <div class="input-group-prepend">
                        <span class="input-group-text"><i class="fas fa-user-graduate"></i></span>
                    </div>
                    <input type="text" name="child[name][<?php echo $randomid; ?>]" class="form-control" value="<?php echo htmlspecialchars($childinfo['name']); ?>" />
                </div>
            </div>
        </div>

        <div class="col-12 col-sm-3">
            <div class="form-group">
                <label>Birth month:</label>
                <div class="input-group">
                    <div class="input-group-prepend">
                        <span class="input-group-text"><i class="fas fa-calendar"></i></span>
                    </div>
                    <select name="child[month][<?php echo $randomid; ?>]" class="form-control" value="<?php echo $childinfo['month']; ?>" >
                        <?php
                        for ($i = 1; $i <= 12; $i++) {
                            $selected = "";
                            if ($childinfo['month'] == $i) {
                                $selected = " selected";
                            }
                            echo "<option value=$i$selected>" . date("F", mktime(0, 0, 0, $i, 2)) . "</option>\n";
                        }
                        ?>
                    </select>
                </div>
            </div>
        </div>

        <div class="col-12 col-sm-3">
            <div class="form-group">
                <label>Birth year:</label>
                <div class="input-group">
                    <div class="input-group-prepend">
                        <span class="input-group-text"><i class="fas fa-calendar-alt"></i></span>
                    </div>
                    <input type="number" name="child[year][<?php echo $randomid; ?>]" class="form-control" min="1980" max="<?php echo date('Y'); ?>" value="<?php echo $childinfo['year']; ?>"/>
                </div>
            </div>
        </div>

        <div class="col-12 col-sm-2">
            <div class="form-group">
                <label>&nbsp;</label>
                <div class="form-check">
                    <input class="form-check-input" type="checkbox" value="1" name="child[graduate][<?php echo $randomid; ?>]"<?php
                    if ($childinfo['graduated']) {
                        echo " checked";
                    }
                    ?>>
                    <label class="form-check-label mt-1">Graduated</label>
                </div>
            </div>
        </div>
    </div>
</div>