<?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/.
 */

redirectIfNotLoggedIn();

$editing = false;
$data = [
    "id" => null,
    "familyid" => "",
    "camperid" => null,
    "adultid" => null,
    "youthid" => null,
    "firstname" => "",
    "lastname" => "",
    "address" => "",
    "zip" => "",
    "phone1" => "",
    "phone2" => "",
    "email" => "",
    "unit" => "",
    "shirt" => "",
    "sex" => "",
    "parentname" => "",
    "rank" => "",
    "days" => "",
    "den" => "",
    "health" => "",
    "notes" => "",
    "child_care" => ""
];
$type = "camper";
if (!empty($VARS['type']) && preg_match("/(camper|adult|youth)/", $VARS['type'])) {
    $type = $VARS['type'];
}
if (!empty($VARS['id']) && $database->has('people', ['personid' => $VARS['id']])) {
    $personid = $VARS['id'];
    $data = $database->get('people', ['personid (id)',
        'familyid',
        'camperid',
        'adultid',
        'youthid',
        'firstname',
        'lastname',
        'address',
        'zip',
        'phone1',
        'phone2',
        'email',
        'unit',
        'shirt',
        'sex',
        'notes'], ['personid' => $personid]);
    if (!empty($data["camperid"])) {
        $type = "camper";
        $data = array_merge($data, $database->get('campers', ['parentname', 'rank', 'den', 'health'], ['camperid' => $data["camperid"]]));
    } else if (!empty($data["adultid"])) {
        $type = "adult";
        $data = array_merge($data, $database->get('adults', ['days', 'position', 'child_care'], ['adultid' => $data["adultid"]]));
    } else if (!empty($data["youthid"])) {
        $type = "youth";
        $data = array_merge($data, $database->get('youth', ['days', 'position', 'parentname'], ['youthid' => $data["youthid"]]));
    }
    $editing = true;
}
?>

<div class="modal fade" id="familysearchmodal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Find Family</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <input type="text" id="familysearchbox" class="form-control" placeholder="Type to Search" />
                <div class="list-group" id="familysearchresults">
                </div>
            </div>
        </div>
    </div>
</div>

<form action="action.php" method="post" id="editform">

    <div class="card">

        <h3 class="card-header d-flex">
            <div>
                <?php
                if ($editing) {
                    ?>
                    <i class="fas fa-edit"></i> <?php $Strings->build("Editing $type", ['person' => "<span id=\"name_title\">" . htmlentities($data["firstname"] . " " . $data["lastname"]) . "</span>"]); ?>
                    <?php
                } else {
                    ?>
                    <i class="fas fa-edit"></i> <?php $Strings->get("Adding $type"); ?>
                    <?php
                }
                ?>
            </div>
            <a href="./app.php?page=people" class="ml-auto btn btn-outline-info btn-sm">
                <i class="fas fa-times"></i> <?php $Strings->get("Cancel"); ?>
            </a>
        </h3>

        <div class="card-body">

            <div class="row">

                <div class="col-12 col-md-2">
                    <div class="form-group mb-3">
                        <label class="mb-0">Family ID:</label>
                        <div class="input-group">
                            <div class="input-group-prepend">
                                <span class="btn btn-default" id="familysearchbtn"><i class="fas fa-search"></i></span>
                            </div>
                            <input type="number"
                                   name="familyid"
                                   value="<?php echo $data['familyid']; ?>"
                                   class="form-control"
                                   placeholder=""
                                   aria-label="Family ID"
                                   maxlength="5" />
                            <div class="invalid-feedback">
                                Enter a family ID.
                            </div>
                        </div>
                    </div>
                </div>

                <?php
                $textboxes = [
                    [
                        "label" => "First Name",
                        "name" => "firstname",
                        "maxlength" => 255,
                        "width" => 5,
                        "value" => $data["firstname"],
                        "error" => "Enter the person's first name."
                    ],
                    [
                        "label" => "Last Name",
                        "name" => "lastname",
                        "width" => 5,
                        "maxlength" => 255,
                        "value" => $data["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" => $data["parentname"],
                            "error" => "Enter the parent or guardian's full name."
                        ]
                    ]);
                }
                $textboxes = array_merge($textboxes, [
                    [
                        "label" => "Address",
                        "name" => "address",
                        "maxlength" => 500,
                        "width" => 8,
                        "value" => $data["address"],
                        "error" => "Enter the person's home address."
                    ],
                    [
                        "label" => "ZIP Code",
                        "name" => "zip",
                        "maxlength" => 10,
                        "width" => 4,
                        "value" => $data["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" => $data["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,
                            "value" => $data["phone2"],
                            "optional" => true,
                            "pattern" => "[0-9]{10}",
                            "error" => "Enter a 10-digit phone number (numbers only)."
                        ]
                    ]);
                }
                $textboxes = array_merge($textboxes, [
                    [
                        "label" => "Email",
                        "name" => "email",
                        "maxlength" => 255,
                        "width" => 4,
                        "type" => "email",
                        "value" => $data["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" => $data["unit"],
                            "error" => "Enter the pack number."
                        ],
                        [
                            "label" => "Rank",
                            "name" => "rank",
                            "type" => "select",
                            "width" => 2,
                            "value" => $data["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" => 5,
                            "maxlength" => 255,
                            "value" => $data["parentname"],
                            "error" => "Enter the parent or guardian's full name."
                        ],
                        [
                            "label" => "Parent Phone",
                            "name" => "phone2",
                            "type" => "tel",
                            "maxlength" => 20,
                            "width" => 3,
                            "value" => $data["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" => $data["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",
                            ],
                            "selected" => str_split($data['days'], 2),
                            "error" => "Choose at least one day."
                        ],
                        [
                            "label" => "Preferred Position",
                            "name" => "position",
                            "type" => "select",
                            "width" => 5,
                            "options" => $positions,
                            "value" => $data["position"],
                            "error" => "Choose a position."
                        ]
                    ]);
                }
                $textboxes = array_merge($textboxes, [
                    [
                        "label" => "Shirt Size",
                        "name" => "shirt",
                        "type" => "select",
                        "value" => $data["shirt"],
                        "width" => 3,
                        "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",
                            "A3" => "Adult 3X Large"
                        ],
                        "error" => "Choose a shirt size."
                    ],
                    [
                        "label" => "Gender",
                        "name" => "sex",
                        "type" => "select",
                        "width" => 3,
                        "value" => $data["sex"],
                        "options" => [
                            "" => "Choose...",
                            "M" => "Male",
                            "F" => "Female"
                        ],
                        "error" => "Choose a gender."
                    ],
                ]);
                if ($type == "camper") {
                    $textboxes = array_merge($textboxes, [
                        [
                            "label" => "Den",
                            "name" => "den",
                            "width" => 3,
                            "maxlength" => 255,
                            "value" => $data["den"],
                            "optional" => true,
                            "error" => "Enter a den"
                        ],
                        [
                            "label" => "Health",
                            "name" => "health",
                            "type" => "textarea",
                            "width" => 6,
                            "value" => $data["health"],
                            "optional" => true,
                            "error" => "Error"
                        ]
                    ]);
                }
                if ($type == "adult") {
                    $textboxes = array_merge($textboxes, [
                        [
                            "label" => "Child care ages",
                            "name" => "child_care",
                            "optional" => true,
                            "width" => 6,
                            "value" => $data["child_care"]
                        ],
                    ]);
                }
                $textboxes = array_merge($textboxes, [
                    [
                        "label" => "Notes",
                        "name" => "notes",
                        "type" => "textarea",
                        "width" => 6,
                        "value" => $data["notes"],
                        "optional" => true,
                        "error" => "Error"
                    ]
                ]);

                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'] = "text";
                                }
                                switch ($item['type']) {
                                    case "select":
                                        ?>
                                        <select class="form-control"
                                                name="<?php echo $item['name']; ?>"
                                                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
                                        break;
                                    case "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="<?php echo $item['name']; ?>[]"
                                                           value="<?php echo $value; ?>"
                                                           <?php
                                                           if (in_array($value, $item['selected'])) {
                                                               echo "checked";
                                                           }
                                                           ?>>
                                                    <label class="form-check-label">
                                                        <?php echo $label; ?>
                                                    </label>
                                                </div>
                                                <?php
                                            }
                                            ?>
                                        </div>
                                        <?php
                                        break;
                                    case "textarea":
                                        ?>
                                        <textarea class="form-control"
                                                  name="<?php echo $item['name']; ?>"
                                                  data-name="<?php echo $item['name']; ?>"
                                                  aria-label="<?php echo $item['label']; ?>"
                                                  rows="4"
                                                  placeholder=""
                                                  <?php
                                                  if (empty($item['optional'])) {
                                                      echo "required";
                                                  }
                                                  ?>><?php echo $item['value']; ?></textarea>
                                                  <?php
                                                  break;
                                              default:
                                                  ?>
                                        <input type="<?php echo (empty($item['type']) ? "text" : $item['type']); ?>"
                                               name="<?php echo $item['name']; ?>"
                                               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['min'])) {
                                                   ?>
                                                   min="<?php echo $item['min']; ?>"
                                                   <?php
                                               }
                                               ?>
                                               <?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
                                       }
                                       ?>
                                <div class="invalid-feedback">
                                    <?php echo $item['error']; ?>
                                </div>
                            </div>
                        </div>
                    </div>

                    <?php
                }
                ?>

            </div>

        </div>

        <?php
        if ($editing) {
            ?>
            <input type="hidden" name="personid" value="<?php echo $data['id']; ?>" />
            <?php
        }
        ?>
        <input type="hidden" name="type" value="<?php echo $type; ?>" />
        <input type="hidden" name="source" value="editperson" />
        <input type="hidden" name="action" value="editperson" />

        <div class="card-footer d-flex">
            <button type="submit" class="btn btn-success mr-1" id="savebutton">
                <i class="fas fa-save"></i> <?php $Strings->get("Save"); ?>
            </button>
            <?php if ($editing) { ?>
                <a href="./app.php?page=confirmdelete&id=<?php echo $data['id']; ?>" class="btn btn-danger ml-auto">
                    <i class="fas fa-times"></i> <?php $Strings->get("Delete"); ?>
                </a>
            <?php } ?>
        </div>
    </div>

</form>