<?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();

$regdata = [
    'id' => '',
    'name' => ''
];
$cash = [];

$editing = false;

if (!empty($VARS['id'])) {
    if ($database->has('registers', ['registerid' => $VARS['id']])) {
        $editing = true;
        $regdata = $database->get(
                'registers', [
            'registerid (id)',
            'registername (name)'
                ], [
            'registerid' => $VARS['id']
        ]);
    } else {
        // customer id is invalid, redirect to a version of the page that won't
        // cause an error when pressing Save
        header('Location: app.php?page=editregister');
        die();
    }
}

if ($editing) {
    $cash = $database->select('cash_drawer', ['cashid', 'open', 'close'], ['AND' => ['registerid' => $regdata['id'], 'open[!]' => null, 'close[!]' => null], 'ORDER' => 'cashid', 'LIMIT' => 30]);
}
?>

<form role="form" action="action.php" method="POST">
    <div class="card border-green">
        <h3 class="card-header text-green">
            <?php
            if ($editing) {
                ?>
                <i class="fas fa-edit"></i> <?php lang2("editing register", ['name' => "<span id=\"name_title\">" . htmlspecialchars($regdata['name']) . "</span>"]); ?>
                <?php
            } else {
                ?>
                <i class="fas fa-edit"></i> <?php lang("adding register"); ?>
                <?php
            }
            ?>
        </h3>
        <div class="card-body row">
            <div class="form-group col-12">
                <label for="name"><i class="fas fa-font"></i> <?php lang("name"); ?></label>
                <input type="text" class="form-control" id="name" name="name" placeholder="Register #1" required="required" value="<?php echo htmlspecialchars($regdata['name']); ?>" />
            </div>
        </div>

        <div class="card-body row">
            <div class="form-group col-12 col-md-6">
                <label for="zreport"><i class="fas fa-receipt"></i> <?php lang("z report"); ?></label>
                <div class="input-group">
                    <select id="zreport" class="form-control">
                        <option value=""><?php lang("pick cash") ?></option>
                        <?php
                        for ($i = count($cash) - 1; $i >= 0; $i--) {
                            $c = $cash[$i];
                            echo "<option value=\"$c[cashid]\">"
                            . date(DATETIME_FORMAT, strtotime($c['open']))
                            . ' - '
                            . date(DATETIME_FORMAT, strtotime($c['close']))
                            . "</option>";
                        }
                        ?>
                    </select>
                    <div class="input-group-append">
                        <button type="button" id="printzreportbtn" class="btn btn-primary"><i class="fas fa-print"></i> <?php lang("print"); ?></button>
                    </div>
                </div>
                <iframe id="zframe" class="w-100 shadow-lg"></iframe>
            </div>
        </div>
        <input type="hidden" name="id" value="<?php echo htmlspecialchars($regdata['id']); ?>" />
        <input type="hidden" name="action" value="editregister" />
        <input type="hidden" name="source" value="registers" />

        <div class="card-footer d-flex">
            <button type="submit" class="btn btn-success mr-auto"><i class="fas fa-save"></i> <?php lang("save"); ?></button>
        </div>
    </div>
</form>