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

require_once __DIR__ . '/../required.php';

redirectifnotloggedin();

if (false) {
    ?>
    <div class="alert alert-danger"><?php lang("missing permission") ?></div>
    <?php
} else {
    ?>
    <form action="lib/reports.php" method="GET" target="_BLANK">
        <div class="row">
            <div class="col-12 col-md-6">
                <div class="row">
                    <div class="col-12 col-sm-6 col-md-12 col-lg-6">
                        <div class="card mb-4">
                            <div class="card-body">
                                <h4 class="card-title"><label for="type"><i class="fas fa-list"></i> <?php lang("report type"); ?></label></h4>
                                <select name="type" id="type" class="form-control" required>
                                    <option value="cashflow"><?php lang("cashflow") ?></option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="col-12 col-sm-6 col-md-12 col-lg-6">
                        <div class="card mb-4">
                            <div class="card-body">
                                <h4 class="card-title"><label for="format"><i class="fas fa-file"></i> <?php lang("format"); ?></label></h4>
                                <select name="format" class="form-control" required>
                                    <option value="csv"><?php lang("csv file") ?></option>
                                    <option value="ods"><?php lang("ods file") ?></option>
                                    <option value="html"><?php lang("html file") ?></option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="col-12">
                        <button type="submit" class="btn btn-success btn-block d-none d-lg-block genrptbtn"><i class="fas fa-download"></i> <?php lang("generate report"); ?></button>
                    </div>
                </div>
            </div>
            <div class="col-12 col-md-6">
                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-body">
                                <h4 class="card-title"><label><i class="fas fa-filter"></i> <?php lang("filter"); ?></label></h4>
                                <div id="date-filter">
                                    <label><i class="fas fa-calendar"></i> <?php lang("date range") ?></label><br />
                                    <div class="input-group">
                                        <input type="text" id="startdate" name="startdate" data-toggle="datetimepicker" data-target="#startdate" class="form-control" />
                                        <span class="input-group-text"><i class="fas fa-chevron-right"></i></span>
                                        <input type="text" id="enddate" name="enddate" data-toggle="datetimepicker" data-target="#enddate" class="form-control" />
                                    </div>
                                </div>
                                <div id="register-filter" class="mt-2">
                                    <label for="register"><i class="fas fa-store-alt"></i> <?php lang("register"); ?></label>
                                    <select name="register" class="form-control">
                                        <option value=""><?php lang("all"); ?></option>
                                        <?php
                                        $registers = $database->select('registers', ['registerid (id)', 'registername (name)']);
                                        foreach ($registers as $r) {
                                            echo '<option value="' . $r['id'] . '">' . $r['name'] . '</option>';
                                        }
                                        ?>
                                    </select>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-12 mt-4">
                        <?php
                        $code = uniqid(rand(10000000, 99999999), true);
                        $database->insert('report_access_codes', ['code' => $code, 'expires' => date("Y-m-d H:i:s", strtotime("+5 minutes"))]);
                        ?>
                        <input type="hidden" name="code" value="<?php echo $code; ?>" />

                        <button type="submit" class="btn btn-success btn-block d-lg-none genrptbtn"><i class="fas fa-download"></i> <?php lang("generate report"); ?></button>
                    </div>
                </div>
            </div>
        </div>
    </form>
    <?php
}
?>