<?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();
$user = new User($_SESSION['uid']);
if (!$user->hasPermission("HACHEPORTAL_EDIT")) {
    header("Location: ./app.php?msg=no_permission");
    die();
}

$editing = false;
if (empty($VARS['id']) || !$database->has('families', ['familyid' => $VARS['id']])) {
    $family = new Family();
    $family->setExpires(strtotime("+1 year"));
} else {
    $famid = $VARS['id'];
    $family = (new Family())->load($famid);
    $editing = true;
}
?>

<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 Family", ['family' => "<span id=\"name_title\">" . htmlentities($family->getName()) . "</span>"]); ?>
                    <?php
                } else {
                    ?>
                    <i class="fas fa-edit"></i> <?php $Strings->get("Adding Family"); ?>
                    <?php
                }
                ?>
            </div>
            <a href="./app.php?page=families" 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">

            <h4><i class="fas fa-info fa-fw"></i> Basic Information</h4>
            <div class="row">

                <?php
                $textboxes = [
                    [
                        "label" => "Family Name (Last Name)",
                        "icon" => "fas fa-users",
                        "name" => "familyname",
                        "maxlength" => 100,
                        "value" => $family->getName(),
                        "error" => "Enter a last name."
                    ],
                    [
                        "label" => "Father's Name",
                        "icon" => "fas fa-male",
                        "name" => "fathername",
                        "maxlength" => 255,
                        "value" => $family->getFather(),
                        "error" => "Enter the father's name.",
                        "optional" => true
                    ],
                    [
                        "label" => "Mother's Name",
                        "icon" => "fas fa-female",
                        "name" => "mothername",
                        "maxlength" => 255,
                        "value" => $family->getMother(),
                        "error" => "Enter the mother's name.",
                        "optional" => true
                    ],
                    [
                        "label" => "Street Address",
                        "icon" => "fas fa-home",
                        "name" => "streetaddress",
                        "maxlength" => 500,
                        "value" => $family->getAddress(),
                        "error" => "Enter an address."
                    ],
                    [
                        "label" => "City",
                        "icon" => "fas fa-city",
                        "name" => "city",
                        "maxlength" => 255,
                        "width" => 3,
                        "value" => $family->getCity(),
                        "error" => "Enter a city."
                    ],
                    [
                        "label" => "State",
                        "icon" => "fas fa-flag",
                        "name" => "state",
                        "type" => "select",
                        "value" => $family->getState(),
                        "error" => "Select a state.",
                        "options" => [
                            'MT' => 'Montana',
                            'AL' => 'Alabama',
                            'AK' => 'Alaska',
                            'AZ' => 'Arizona',
                            'AR' => 'Arkansas',
                            'CA' => 'California',
                            'CO' => 'Colorado',
                            'CT' => 'Connecticut',
                            'DE' => 'Delaware',
                            'DC' => 'District of Columbia',
                            'FL' => 'Florida',
                            'GA' => 'Georgia',
                            'HI' => 'Hawaii',
                            'ID' => 'Idaho',
                            'IL' => 'Illinois',
                            'IN' => 'Indiana',
                            'IA' => 'Iowa',
                            'KS' => 'Kansas',
                            'KY' => 'Kentucky',
                            'LA' => 'Louisiana',
                            'ME' => 'Maine',
                            'MD' => 'Maryland',
                            'MA' => 'Massachusetts',
                            'MI' => 'Michigan',
                            'MN' => 'Minnesota',
                            'MS' => 'Mississippi',
                            'MO' => 'Missouri',
                            'MT' => 'Montana',
                            'NE' => 'Nebraska',
                            'NV' => 'Nevada',
                            'NH' => 'New Hampshire',
                            'NJ' => 'New Jersey',
                            'NM' => 'New Mexico',
                            'NY' => 'New York',
                            'NC' => 'North Carolina',
                            'ND' => 'North Dakota',
                            'OH' => 'Ohio',
                            'OK' => 'Oklahoma',
                            'OR' => 'Oregon',
                            'PA' => 'Pennsylvania',
                            'RI' => 'Rhode Island',
                            'SC' => 'South Carolina',
                            'SD' => 'South Dakota',
                            'TN' => 'Tennessee',
                            'TX' => 'Texas',
                            'UT' => 'Utah',
                            'VT' => 'Vermont',
                            'VA' => 'Virginia',
                            'WA' => 'Washington',
                            'WV' => 'West Virginia',
                            'WI' => 'Wisconsin',
                            'WY' => 'Wyoming'
                        ],
                        "width" => 2
                    ],
                    [
                        "label" => "ZIP/Postal Code",
                        "icon" => "fas fa-mail-bulk",
                        "name" => "zip",
                        "maxlength" => 10,
                        "width" => 3,
                        "value" => $family->getZip(),
                        "error" => "Enter a valid 5 or 9 digit ZIP code.",
                        "pattern" => "[0-9]{5}(-?[0-9]{4})?"
                    ],
                    [
                        "label" => "Phone Number",
                        "icon" => "fas fa-phone",
                        "name" => "phone",
                        "width" => 3,
                        "maxlength" => 10,
                        "type" => "tel",
                        "pattern" => "[0-9]{10}",
                        "value" => $family->getPhone(),
                        "error" => "Enter a valid ten-digit phone number."
                    ],
                    [
                        "label" => "Email",
                        "icon" => "fas fa-at",
                        "name" => "email",
                        "maxlength" => 255,
                        "type" => "email",
                        "value" => $family->getEmail(),
                        "error" => "Enter an email address."
                    ],
                    [
                        "label" => "Newsletter Preference",
                        "icon" => "fas fa-newspaper",
                        "name" => "newsletter_method",
                        "type" => "select",
                        "value" => $family->getNewsletter(),
                        "error" => "Choose an option.",
                        "options" => [
                            "1" => "Email ($25)",
                            "2" => "Paper ($50)",
                            "3" => "Email and Paper ($50)"
                        ]
                    ],
                    [
                        "label" => "Membership Expiration",
                        "icon" => "fas fa-calendar",
                        "name" => "date",
                        "type" => "date",
                        "width" => 3,
                        "value" => date("Y-m-d", $family->getExpires()),
                        "error" => "Choose a valid date."
                    ]
                ];

                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">
                                <div class="input-group-prepend">
                                    <span class="input-group-text"><i class="<?php echo $item['icon']; ?>"></i></span>
                                </div>
                                <?php if (empty($item['type']) || $item['type'] != "select") { ?>
                                    <input type="<?php echo (empty($item['type']) ? "text" : $item['type']); ?>"
                                           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="<?php echo $item['name']; ?>"
                                            aria-label="<?php echo $item['label']; ?>"
                                            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
                                }
                                ?>
                                <div class="invalid-feedback">
                                    <?php echo $item['error']; ?>
                                </div>
                            </div>
                        </div>
                    </div>

                    <?php
                }
                ?>

            </div>



            <hr />



            <h4><i class="fas fa-user-graduate fa-fw"></i> Children</h4>
            <p class="card-text ml-2 mb-1">
                <i class="fas fa-info-circle"></i> <?php $Strings->get("To remove a child, delete the contents of the Name box."); ?>

            <div class="list-group" id="child_list">
                <?php
                if (count($family->getChildren()) > 0) {
                    foreach ($family->getChildren() as $child) {
                        $childid = $child->getID();
                        include __DIR__ . "/../lib/template_child_entry.php";
                    }
                } else {
                    include __DIR__ . "/../lib/template_child_entry.php";
                }
                ?>
            </div>

            <div class="btn btn-sm btn-primary mt-1" id="add_child_row">
                <i class="fas fa-plus"></i> Add another
            </div>



            <hr />



            <h4><i class="fas fa-check-circle fa-fw"></i> Consent</h4>
            <div>
                <div>
                    <?php $Strings->get("Okay to use photos?"); ?>
                    <br />
                    <span class="form-check form-check-inline">
                        <input class="form-check-input" type="radio" name="photo_permission" id="photo_permission" value="1" <?php
                        if ($family->getPhotoPermission()) {
                            echo "checked";
                        }
                        ?> required>
                        <label class="form-check-label" for="photo_permission">Yes</label>
                    </span>
                    <span class="form-check form-check-inline">
                        <input class="form-check-input" type="radio" name="photo_permission" id="photo_permission" value="0" <?php
                        if (!$family->getPhotoPermission()) {
                            echo "checked";
                        }
                        ?> required>
                        <label class="form-check-label" for="photo_permission">No</label>
                    </span>
                </div>

                <div class="mt-2">
                    <span class="form-check form-check-inline">
                        <input class="form-check-input" type="checkbox" name="private" id="private" value="1" <?php
                        if ($family->getPrivate()) {
                            echo "checked";
                        }
                        ?>>
                        <label class="form-check-label" for="private"><?php $Strings->get("Member wishes to be remain private (excluded from member directory)"); ?></label>
                    </span>
                </div>
            </div>

        </div>

        <?php
        if ($editing) {
            ?>
            <input type="hidden" name="familyid" value="<?php echo $family->getID(); ?>" />
            <?php
        }
        ?>
        <input type="hidden" name="source" value="editfamily" />
        <input type="hidden" name="action" value="editfamily" />

        <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 $family->getID(); ?>" class="btn btn-danger ml-auto">
                    <i class="fas fa-times"></i> <?php $Strings->get("Delete"); ?>
                </a>
            <?php } ?>
        </div>
    </div>

</form>