<?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";

// Get the person type, either from the URL or a preset variable in the including script
$type = "camper";
if (!empty($_GET["type"]) && preg_match("/^(camper|adult|youth)$/", $_GET["type"])) {
    $type = $_GET["type"];
} else if (!empty($persontype)) {
    $type = $persontype;
}

$personinfo = ["firstname" => "", "lastname" => "", "address" => "", "zip" => "", "phone1" => "", "phone2" => "", "email" => "", "unit" => "", "shirt" => "", "sex" => ""];

switch ($type) {
    case "camper":
        $personinfo["parentname"] = "";
        $personinfo["rank"] = "";
        break;
    case "youth":
        $personinfo["parentname"] = "";
        $personinfo["days"] = "";
        $personinfo["position"] = "";
        break;
    case "adult":
        $personinfo["days"] = "";
        $personinfo["position"] = "";
        $personinfo["child_care"] = "";
        break;
}

foreach ($_GET as $key => $val) {
    if (array_key_exists($key, $personinfo)) {
        $personinfo[$key] = htmlentities($val);
    }
}

if (isset($personid) && $database->has('people', ['personid' => $personid])) {
    $personinfo = $database->get('people', '*', ['personid' => $personid]);
} else {
    do {
        $personid = mt_rand(0, 9999999999);
    } while ($database->has('people', ['personid' => $personid]));
}
?>

<div class="list-group-item person-list-item" data-persontype="<?php echo $type; ?>">
    <div class="btn btn-outline-danger btn-sm rmpersonbtn"><i class="fas fa-trash"></i> Remove</div>
    <input type="hidden" name="people[ids][]" value="<?php echo $personid; ?>" />
    <input type="hidden" name="people[type][<?php echo $personid; ?>]" value="<?php echo $type; ?>" />
    <div class="row">

        <?php
        $textboxes = [
            [
                "label" => "First Name",
                "name" => "firstname",
                "maxlength" => 255,
                "width" => 6,
                "value" => $personinfo["firstname"],
                "error" => "Enter the person's first name."
            ],
            [
                "label" => "Last Name",
                "name" => "lastname",
                "width" => 6,
                "maxlength" => 255,
                "value" => $personinfo["lastname"],
                "error" => "Enter the person's last name."
            ]
        ];
        if ($type == "camper") {
            $textboxes = array_merge($textboxes, [
                [
                    "label" => "Parent/Guardian Name",
                    "name" => "parentname",
                    "width" => 12,
                    "maxlength" => 255,
                    "value" => $personinfo["parentname"],
                    "error" => "Enter the parent or guardian's full name."
                ]
            ]);
        }
        $textboxes = array_merge($textboxes, [
            [
                "label" => "Address",
                "name" => "address",
                "maxlength" => 500,
                "width" => 8,
                "value" => $personinfo["address"],
                "error" => "Enter the person's home address."
            ],
            [
                "label" => "ZIP Code",
                "name" => "zip",
                "maxlength" => 10,
                "width" => 4,
                "value" => $personinfo["zip"],
                "pattern" => "[0-9]{5}(-?[0-9]{4})?",
                "error" => "Enter a valid 5 or 9 digit ZIP code."
            ],
            [
                "label" => "Phone Number",
                "name" => "phone1",
                "type" => "tel",
                "maxlength" => 20,
                "width" => 3,
                "value" => $personinfo["phone1"],
                "pattern" => "[0-9]{10}",
                "error" => "Enter a 10-digit phone number (numbers only)."
            ]
        ]);
        if ($type == "camper") {
            $textboxes = array_merge($textboxes, [
                [
                    "label" => "Alt. Phone Number",
                    "name" => "phone2",
                    "type" => "tel",
                    "maxlength" => 20,
                    "width" => 3,
                    "optional" => true,
                    "value" => $personinfo["phone2"],
                    "pattern" => "[0-9]{10}",
                    "error" => "Enter a 10-digit phone number (numbers only)."
                ]
            ]);
        }
        $textboxes = array_merge($textboxes, [
            [
                "label" => "Email",
                "name" => "email",
                "maxlength" => 255,
                "width" => 5,
                "type" => "email",
                "value" => $personinfo["email"],
                "error" => "Enter your email address."
            ]
        ]);
        if ($type == "camper") {
            $textboxes = array_merge($textboxes, [
                [
                    "label" => "Pack #",
                    "name" => "unit",
                    "width" => 2,
                    "maxlength" => 4,
                    "pattern" => "[0-9]{3,4}",
                    "value" => $personinfo["unit"],
                    "error" => "Enter the pack number."
                ],
                [
                    "label" => "Rank",
                    "name" => "rank",
                    "type" => "select",
                    "width" => 2,
                    "value" => $personinfo["rank"],
                    "options" => [
                        "" => "Choose...",
                        "Tiger" => "Tiger",
                        "Wolf" => "Wolf",
                        "Bear" => "Bear",
                        "Webelos" => "Webelos",
                        "Arrow of Light" => "Arrow of Light",
                    ],
                    "error" => "Choose a rank."
                ]
            ]);
        }
        if ($type == "youth") {
            $textboxes = array_merge($textboxes, [
                [
                    "label" => "Parent Name",
                    "name" => "parentname",
                    "width" => 4,
                    "maxlength" => 255,
                    "value" => $personinfo["parentname"],
                    "error" => "Enter the parent or guardian's full name."
                ],
                [
                    "label" => "Parent Phone",
                    "name" => "phone2",
                    "type" => "tel",
                    "maxlength" => 20,
                    "width" => 3,
                    "value" => $personinfo["phone2"],
                    "pattern" => "[0-9]{10}",
                    "error" => "Enter a 10-digit phone number (numbers only)."
                ],
                [
                    "label" => "Unit #",
                    "name" => "unit",
                    "width" => 2,
                    "maxlength" => 4,
                    "pattern" => "[0-9]{3,4}",
                    "value" => $personinfo["unit"],
                    "optional" => true,
                    "error" => "Enter the unit number."
                ],
            ]);
        }
        if ($type == "adult" || $type == "youth") {
            if ($type == "adult") {
                $positions = [
                    "None" => "No Preference",
                    "Den Walker" => "Den Walker",
                    "Station Leader" => "Station Leader",
                    "Tot Lot" => "Tot Lot",
                    "First Aid" => "First Aid",
                    "Floater" => "Floater"
                ];
            } else {
                $positions = [
                    "None" => "No Preference",
                    "Den Chief" => "Den Chief",
                    "Station" => "Station",
                    "Tot Lot" => "Tot Lot",
                    "Floater" => "Floater"
                ];
            }
            $textboxes = array_merge($textboxes, [
                [
                    "label" => "Available Days",
                    "name" => "days",
                    "type" => "checkboxes",
                    "options" => [
                        "Tu" => "Tuesday",
                        "We" => "Wednesday",
                        "Th" => "Thursday",
                        "Fr" => "Friday",
                    ],
                    "error" => "Choose at least one day."
                ],
                [
                    "label" => "Preferred Position",
                    "name" => "position",
                    "type" => "select",
                    "width" => 5,
                    "options" => $positions,
                    "error" => "Choose a position."
                ],
                [
                    "label" => "Shirt Size",
                    "name" => "shirt",
                    "type" => "select",
                    "value" => $personinfo["shirt"],
                    "options" => [
                        "" => "Choose...",
                        "NO" => "No Shirt",
                        "YS" => "Youth Small",
                        "YM" => "Youth Medium",
                        "YL" => "Youth Large",
                        "AS" => "Adult Small",
                        "AM" => "Adult Medium",
                        "AL" => "Adult Large",
                        "AX" => "Adult Extra Large",
                        "A2" => "Adult 2X Large"
                    ],
                    "error" => "Choose a shirt size."
                ]
            ]);
        } else {
            $textboxes = array_merge($textboxes, [
                [
                    "label" => "Shirt Size",
                    "name" => "shirt",
                    "type" => "select",
                    "value" => $personinfo["shirt"],
                    "options" => [
                        "" => "Choose...",
                        "YS" => "Youth Small",
                        "YM" => "Youth Medium",
                        "YL" => "Youth Large",
                        "AS" => "Adult Small",
                        "AM" => "Adult Medium",
                        "AL" => "Adult Large",
                        "AX" => "Adult Extra Large",
                        "A2" => "Adult 2X Large"
                    ],
                    "error" => "Choose a shirt size."
                ]
            ]);
        }
        $textboxes = array_merge($textboxes, [
            [
                "label" => "Gender",
                "name" => "sex",
                "type" => "select",
                "width" => 3,
                "value" => $personinfo["sex"],
                "options" => [
                    "" => "Choose...",
                    "M" => "Male",
                    "F" => "Female"
                ],
                "error" => "Choose a gender."
            ],
        ]);

        foreach ($textboxes as $item) {
            ?>

            <div class="col-12 col-md-<?php echo (empty($item['width']) ? "4" : $item['width']); ?>">
                <div class="form-group mb-3">
                    <label class="mb-0"><?php echo $item['label']; ?>:</label>
                    <div class="input-group">
                        <?php if (empty($item['type']) || ($item['type'] != "select" && $item['type'] != "checkboxes")) { ?>
                            <input type="<?php echo (empty($item['type']) ? "text" : $item['type']); ?>"
                                   name="people[<?php echo $item['name']; ?>][<?php echo $personid; ?>]"
                                   data-name="<?php echo $item['name']; ?>"
                                   class="form-control"
                                   placeholder=""
                                   aria-label="<?php echo $item['label']; ?>"
                                   maxlength="<?php echo $item['maxlength']; ?>"
                                   <?php
                                   if (!empty($item['pattern'])) {
                                       ?>
                                       pattern="<?php echo $item['pattern']; ?>"
                                       <?php
                                   }
                                   ?>
                                   <?php
                                   if (!empty($item['value'])) {
                                       ?>
                                       value="<?php echo htmlspecialchars($item['value']); ?>"
                                       <?php
                                   }
                                   if (empty($item['optional'])) {
                                       echo "required";
                                   }
                                   ?> />
                               <?php } else if ($item['type'] == "select") { ?>
                            <select class="form-control"
                                    name="people[<?php echo $item['name']; ?>][<?php echo $personid; ?>]"
                                    data-name="<?php echo $item['name']; ?>"
                                    aria-label="<?php echo $item['label']; ?>"
                                    <?php
                                    if (empty($item['optional'])) {
                                        echo "required";
                                    }
                                    ?>>
                                        <?php
                                        foreach ($item['options'] as $value => $label) {
                                            $selected = "";
                                            if (!empty($item['value']) && $value == $item['value']) {
                                                $selected = " selected";
                                            }
                                            echo "<option value=\"$value\"$selected>$label</option>\n";
                                        }
                                        ?>
                            </select>
                            <?php
                        } else if ($item['type'] == "checkboxes") {
                            ?>
                            <div class="d-flex justify-content-left flex-wrap">
                                <?php
                                foreach ($item['options'] as $value => $label) {
                                    ?>
                                    <div class="form-check m-1">
                                        <input class="form-check-input" type="checkbox" data-name="<?php echo $item['name']; ?>" name="people[<?php echo $item['name']; ?>][<?php echo $personid; ?>][]" value="<?php echo $value; ?>">
                                        <label class="form-check-label">
                                            <?php echo $label; ?>
                                        </label>
                                    </div>
                                    <?php
                                }
                                ?>
                            </div>
                            <?php
                        }
                        ?>
                        <div class="invalid-feedback">
                            <?php echo $item['error']; ?>
                        </div>
                    </div>
                </div>
            </div>

            <?php
        }
        ?>
    </div>
</div>