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


// Detect if loaded by the user or by PHP
if (count(get_included_files()) == 1) {
    define("LOADED", true);
} else {
    define("LOADED", false);
}

require_once __DIR__ . "/../required.php";

dieifnotloggedin();

if (LOADED) {
    if (isset($VARS['type']) && isset($VARS['format'])) {
        generateReport($VARS['type'], $VARS['format']);
        die();
    } else {
        $Strings->get("invalid parameters");
        die();
    }
}

/**
 * Get a 2d array of the families in the database.
 * @global type $database
 * @param array $filter Medoo WHERE clause.
 * @return string
 */
function getMemberReport($filter = []): Report {
    global $database, $Strings;

    if (empty($filter)) {
        $report = new Report($Strings->get("Families", false));
        $filter = ["ORDER" => ["familyname" => "ASC"]];
    } else {
        $report = new Report($Strings->get("Expiring Memberships", false));
    }

    $familyids = $database->select(
            "families", "familyid", $filter
    );

    $report->setHeader([
        $Strings->get("Name", false),
        $Strings->get("Father", false),
        $Strings->get("Mother", false),
        $Strings->get("Phone", false),
        $Strings->get("Email", false),
        $Strings->get("Address", false),
        $Strings->get("City", false),
        $Strings->get("State", false),
        $Strings->get("ZIP", false),
        $Strings->get("Photo Permission", false),
        $Strings->get("Newsletter", false),
        $Strings->get("Expires", false),
        $Strings->get("Private", false),
        $Strings->get("Children", false),
    ]);
    $families = [];
    foreach ($familyids as $id) {
        $f = (new Family())->load($id);
        $families[] = $f;
    }
    foreach ($families as $f) {
        $newsletter = "";
        switch ($f->getNewsletter()) {
            case 1:
                $newsletter = $Strings->get("Email", false);
                break;
            case 2:
                $newsletter = $Strings->get("Print", false);
                break;
            case 3:
                $newsletter = $Strings->get("Email+Print", false);
                break;
        }
        $children = [];
        foreach ($f->getChildren() as $c) {
            $children[] = $c->getName() . " (" . date("n/d", $c->getBirthday()) . ")";
        }
        $report->addDataRow([
            $f->getName(),
            $f->getFather(),
            $f->getMother(),
            $f->getPhone() . "",
            $f->getEmail(),
            $f->getAddress(),
            $f->getCity(),
            $f->getState(),
            $f->getZip() . "",
            $f->getPhotoPermission() ? $Strings->get("Yes", false) : $Strings->get("No", false),
            $newsletter,
            date("Y-m-d", $f->getExpires()),
            $f->getPrivate() ? $Strings->get("Yes", false) : $Strings->get("No", false),
            implode(", ", $children)
        ]);
    }
    return $report;
}

function getReport($type): Report {
    switch ($type) {
        case "members":
            return getMemberReport();
            break;
        case "expiring":
            return getMemberReport(["expires[<]" => date("Y-m-d", strtotime("+1 month")), "ORDER" => ["expires" => "ASC"]]);
            break;
        default:
            return new Report("error", ["ERROR"], ["Invalid report type."]);
    }
}

function generateReport($type, $format) {
    $report = getReport($type);
    $report->output($format);
}
