<?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;
$data = [
    "id" => "",
    "family" => "",
    "amount" => 1.0,
    "date" => date("Y-m-d"),
    "type" => "",
    "paid" => true
];

if (!empty($_GET['id']) && $database->has('payments', ['paymentid' => $_GET['id']])) {
    $editing = true;
    $payment = $database->get("payments", ['paymentid (id)', "familyid (family)", "amount", "date", "type", "paid"], ["paymentid" => $_GET['id']]);
    $payment["date"] = date("Y-m-d", strtotime($payment["date"]));
    $payment["paid"] = ($payment["paid"] == 1 ? true : false);
    $data = $payment;
}
?>

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

    <div class="card">

        <h3 class="card-header d-flex">
            <div>
                <i class="fas fa-edit"></i> <?php
                if ($editing) {
                    $Strings->get("Edit Payment");
                } else {
                    $Strings->get("Add Payment");
                }
                ?>
            </div>
            <a href="./app.php?page=payments" 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">

                <?php
                $families = $database->select("families", ["familyid (id)", "familyname (name)", "mother_name", "father_name"]);
                $familylist = [
                    "" => $Strings->get("Choose...", false)
                ];
                foreach ($families as $f) {
                    if (!empty($f["father_name"]) && !empty($f["mother_name"])) {
                        $familylist[$f['id']] = "$f[name], $f[father_name] and $f[mother_name]";
                    } else if (!empty($f["father_name"])) {
                        $familylist[$f['id']] = "$f[name], $f[father_name]";
                    } else if (!empty($f["mother_name"])) {
                        $familylist[$f['id']] = "$f[name], $f[mother_name]";
                    }
                }
                $textboxes = [
                    [
                        "label" => "Family",
                        "icon" => "fas fa-users",
                        "name" => "familyid",
                        "type" => "select",
                        "value" => $data["family"],
                        "options" => $familylist,
                        "error" => "Choose a family."
                    ],
                    [
                        "label" => "Amount",
                        "icon" => "fas fa-dollar-sign",
                        "name" => "amount",
                        "type" => "number",
                        "maxlength" => 5,
                        "value" => $data["amount"],
                        "width" => 2,
                        "error" => "Enter a dollar amount."
                    ],
                    [
                        "label" => "Date",
                        "icon" => "fas fa-calendar",
                        "name" => "date",
                        "type" => "date",
                        "value" => $data["date"],
                        "width" => 3,
                        "error" => "Choose a date for the payment."
                    ],
                    [
                        "label" => "Type",
                        "icon" => "fas fa-money-bill",
                        "name" => "type",
                        "type" => "select",
                        "value" => $data["type"],
                        "options" => [
                            "" => $Strings->get("Choose...", false),
                            "Online" => "Online",
                            "Cash" => "Cash",
                            "Check" => "Check",
                            "Free" => "Free",
                            "Other" => "Other"
                        ],
                        "width" => 3,
                        "error" => "Select a payment type."
                    ]
                ];

                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
                                           }
                                           ?>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 class="col-12 col-md-4">
                    <div class="form-check">
                        <input class="form-check-input" type="checkbox" value="1" name="paid" id="paid" <?php
                        if ($data["paid"]) {
                            echo "checked";
                        }
                        ?>>
                        <label class="form-check-label" for="paid">
                            <?php $Strings->get("Mark as paid"); ?>
                        </label>
                    </div>
                </div>

                <?php if (!$editing) { ?>
                    <div class="col-12 col-md-4">
                        <div class="form-check">
                            <input class="form-check-input" type="checkbox" value="1" name="extendmembership" id="extendmembership" checked>
                            <label class="form-check-label" for="extendmembership">
                                <?php $Strings->get("This payment is a membership renewal (automatically add one year to the family's membership)"); ?>
                            </label>
                        </div>
                    </div>
                <?php } ?>

            </div>
        </div>

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

        <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>
        </div>
    </div>

</form>