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

if (empty($IN_SITE)) {
    die("Access denied.");
}

$familyname = "";
$fathername = "";
$mothername = "";
$streetaddress = "";
$city = "";
$state = "";
$zip = "";
$phone = "";
$email = "";
$newsletter_method = "";

$children = [];

if (isset($_SESSION['familyid']) && $database->has('families', ['familyid' => $_SESSION['familyid']])) {
    $familyinfo = $database->get("families", ['familyname', 'phone', 'email', 'address', 'city', 'state', 'zip', 'father_name (fathername)', 'mother_name (mothername)', 'newsletter_method'], ['familyid' => $_SESSION['familyid']]);
    $children = $database->select("people", 'personid', ['familyid' => $_SESSION['familyid']]);
    $familyname = $familyinfo['familyname'];
    $fathername = $familyinfo['fathername'] ?? "";
    $mothername = $familyinfo['mothername'] ?? "";
    $streetaddress = $familyinfo['address'];
    $city = $familyinfo['city'];
    $state = $familyinfo['state'];
    $zip = $familyinfo['zip'];
    $phone = $familyinfo['phone'];
    $email = $familyinfo['email'];
    $newsletter_method = $familyinfo['newsletter_method'];
}
?>
<div class="container mt-4">
    <form action="actions/submitmembership.php" method="post" id="membershipform">
        <?php
        // Add a hidden form element, to detect if the renewal session
        // expired before we submitted the thing
        if (isset($_SESSION['familyid'])) {
            ?>
            <input type="hidden" name="renewing" value="1" />
            <?php
        }
        ?>
        <div class="card mb-4">
            <div class="card-body">

                <div class="d-flex flex-wrap justify-content-around">
                    <img class="img-fluid" style="max-height: 100px; min-width: 100px;" src="static/hachelogo.svg" alt="HACHE: Helena Area Christian Home Educators"/>
                    <div class="ml-auto mr-auto pl-4 align-self-center text-center">
                        <?php
                        if (isset($_SESSION['familyid'])) {
                            echo "<h1>Membership Renewal</h1>";
                        } else {
                            echo "<h1>Membership Application</h1>";
                        }
                        ?>
                    </div>
                </div>
            </div>
        </div>

        <?php
        if (!empty($_GET['error'])) {
            ?>
            <div class="card mb-4 bg-danger text-white">
                <div class="card-body">
                    <?php echo htmlspecialchars($_GET['error']); ?>
                </div>
            </div>
            <?php
        }
        ?>

        <div class="card mb-4">
            <div class="card-header">
                <h3><i class="fas fa-info fa-fw"></i> Basic Information</h3>
            </div>
            <div class="card-body">

                <div class="row">

                    <?php
                    $textboxes = [
                        [
                            "label" => "Family Name (Last Name)",
                            "icon" => "fas fa-users",
                            "name" => "familyname",
                            "maxlength" => 100,
                            "value" => $familyname,
                            "error" => "Enter your last name."
                        ],
                        [
                            "label" => "Father's Name",
                            "icon" => "fas fa-male",
                            "name" => "fathername",
                            "maxlength" => 255,
                            "value" => $fathername,
                            "error" => "Enter the father's name.",
                            "optional" => true
                        ],
                        [
                            "label" => "Mother's Name",
                            "icon" => "fas fa-female",
                            "name" => "mothername",
                            "maxlength" => 255,
                            "value" => $mothername,
                            "error" => "Enter the mother's name.",
                            "optional" => true
                        ],
                        [
                            "label" => "Street Address",
                            "icon" => "fas fa-home",
                            "name" => "streetaddress",
                            "maxlength" => 500,
                            "value" => $streetaddress,
                            "error" => "Enter your address."
                        ],
                        [
                            "label" => "City",
                            "icon" => "fas fa-city",
                            "name" => "city",
                            "maxlength" => 255,
                            "width" => 3,
                            "value" => $city,
                            "error" => "Enter your city."
                        ],
                        [
                            "label" => "State",
                            "icon" => "fas fa-flag",
                            "name" => "state",
                            "type" => "select",
                            "value" => $state,
                            "error" => "Choose 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" => $zip,
                            "pattern" => "[0-9]{5}(-?[0-9]{4})?",
                            "error" => "Enter a valid 5 or 9 digit ZIP code."
                        ],
                        [
                            "label" => "Phone Number",
                            "icon" => "fas fa-phone",
                            "name" => "phone",
                            "type" => "tel",
                            "maxlength" => 20,
                            "value" => $phone,
                            "pattern" => "[0-9]{10}",
                            "error" => "Enter a 10-digit phone number (numbers only)."
                        ],
                        [
                            "label" => "Email",
                            "icon" => "fas fa-at",
                            "name" => "email",
                            "maxlength" => 255,
                            "type" => "email",
                            "value" => $email,
                            "error" => "Enter your email address."
                        ],
                        [
                            "label" => "Newsletter Preference",
                            "icon" => "fas fa-newspaper",
                            "name" => "newsletter_method",
                            "type" => "select",
                            "value" => $newsletter_method,
                            "options" => [
                                "1" => "Email ($25)",
                                "2" => "Paper ($50)",
                                "3" => "Email and Paper ($50)"
                            ],
                            "error" => "Choose a newsletter option."
                        ]
                    ];

                    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>

                <div class="card-text">
                    <p>
                        The membership fees (determined by your newsletter
                        preference) cover costs of the following:
                        phone; website; postage; distribution of newsletters and
                        directories; publication of materials; library; and other
                        HACHE related activities.
                        HACHE will not restrict membership based on inability to
                        pay.   HACHE does not mandate curriculum choices or the
                        manner in which curriculum is administered.  We do encourage
                        all members to follow and adhere to MT laws governing home
                        schooling.
                </div>
            </div>
        </div>

        <div class="card mb-4">
            <div class="card-header">
                <h3><i class="fas fa-user-graduate fa-fw"></i> Children</h3>
            </div>
            <div class="card-body">
                <div class="card-text">
                    <p>
                        Please list your children's first names and birth dates.  This
                        information will appear in our members’ directory.  Members
                        agree that they will NOT make this information public.

                    <div class="list-group" id="child_list">
                        <?php
                        if (count($children) > 0) {
                            foreach ($children as $childid) {
                                include __DIR__ . "/template_child_entry.php";
                            }
                        } else {
                            include __DIR__ . "/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>

                </div>
            </div>
        </div>

        <div class="card mb-4">
            <div class="card-header">
                <h3><i class="fas fa-check-circle fa-fw"></i> Consent</h3>
            </div>
            <div class="card-body">

                <div class="card-text">
                    <p class="mb-0">HACHE members occasionally take pictures of students during
                        home school functions and activities.  These photos may be
                        used in HACHE displays, brochures, website, etc.
                    <p>I give permission for my photos to be included in such displays:
                        <span class="form-check form-check-inline">
                            <input class="form-check-input" type="radio" name="photo_permission" id="photo_permission" value="1" 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" required>
                            <label class="form-check-label" for="photo_permission">No</label>
                        </span>
                </div>

                <div class="form-check">
                    <input class="form-check-input" type="checkbox" value="1" name="agree_terms" id="agree_terms" required>
                    <label class="form-check-label" for="agree_terms">
                        I/We home school our <span id="child_ren"><?php
                            if (count($children) > 1) {
                                echo "children";
                            } else {
                                echo "child";
                            }
                            ?></span> and have read and understand this application.  We also agree to abide by HACHE’s policy of common courtesy and respect for one another.
                    </label>
                </div>

            </div>

        </div>

        <div class="card mb-4">
            <div class="card-header">
                <h3>
                    <span class="fa-layers fa-fw">
                        <i class="fas fa-basketball-ball" data-fa-transform="shrink-5 up-4.5 left-4.5"></i>
                        <i class="fas fa-book" data-fa-transform="shrink-5 right-4.5 down-4.5"></i>
                    </span>
                    Activities
                </h3>
            </div>

            <div class="card-body">

                <div class="card-text">
                    <p>HACHE is an all-volunteer organization.  Listed below are events
                        and activities that may occur throughout the year.  If you are
                        interested in helping with one or more of these events please
                        select any and all events of interest so we can get you in touch
                        with the member in charge of said event.  Please feel free to
                        contact Steering Committee members or the newsletter editor with
                        ideas for field trips and or other activities that may be
                        enjoyed by all.  (Not all of these events are specifically
                        HACHE events, but rather events HACHE supported events our
                        members have participated in and enjoyed in past years.)
                </div>

                <?php
                $events = $database->select('events', ['eventid (id)', 'event (name)']);

                $eventcount = count($events);

                $cola = [];
                $colb = [];

                for ($i = 0; $i < $eventcount; $i++) {
                    if ($i % 2 === 0) {
                        $cola[] = $events[$i];
                    } else {
                        $colb[] = $events[$i];
                    }
                }
                ?>

                <div class="row">

                    <div class="col-12 col-md-6">
                        <ul class="list-group">
                            <?php
                            foreach ($cola as $ev) {
                                ?>
                                <li class="list-group-item">
                                    <div class="form-check form-check-inline">
                                        <input class="form-check-input" type="checkbox" id="events_<?php echo $ev['id']; ?>" value="<?php echo $ev['id']; ?>" name="events[]">
                                        <label class="form-check-label" for="events_<?php echo $ev['id']; ?>"><?php echo $ev['name']; ?></label>
                                    </div>
                                </li>
                                <?php
                            }
                            ?>
                        </ul>
                    </div>

                    <div class="col-12 col-md-6">
                        <ul class="list-group">
                            <?php
                            foreach ($colb as $ev) {
                                ?>
                                <li class="list-group-item">
                                    <div class="form-check form-check-inline">
                                        <input class="form-check-input" type="checkbox" id="events_<?php echo $ev['id']; ?>" value="<?php echo $ev['id']; ?>" name="events[]">
                                        <label class="form-check-label" for="events_<?php echo $ev['id']; ?>"><?php echo $ev['name']; ?></label>
                                    </div>
                                </li>
                                <?php
                            }
                            ?>
                        </ul>
                    </div>
                </div>

            </div>
        </div>

        <div class="card mb-4">
            <div class="card-header">
                <h3><i class="fas fa-dollar-sign fa-fw"></i> Pay and Submit</h3>
            </div>
            <div class="card-body">
                <div class="card-text text-success mb-1">
                    <i class="fas fa-lock"></i> We can't see your card info; it's sent directly and securely from your computer to our payment processor.
                </div>
                <label for="card-element">
                    Credit or debit card
                </label>
                <div id="card-element" class="form-control">

                </div>
                <div id="card-errors" class="alert alert-danger d-none"></div>
                <input type="hidden" name="stripeToken" id="stripe-token" required />
            </div>
            <div class="card-footer">
                <button type="submit" class="btn btn-primary" id="savebutton">
                    <span id="savebutton-text">
                        <i class="fas fa-arrow-right"></i> Submit<span class="d-none d-sm-inline"> Application and Payment</span>
                    </span>
                    <span id="savebutton-wait" class="d-none">
                        <i class="fas fa-spinner fa-spin"></i> Working...
                    </span>
                </button>
            </div>
        </div>
    </form>
</div>

<script src="https://js.stripe.com/v3/"></script>
<script>
    var stripe_pubkey = '<?php echo $SETTINGS["stripe"]["pubkey"]; ?>';
</script>
<script src="static/signup.js"></script>