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

$custdata = [
    'id' => '',
    'name' => '',
    'email' => '',
    'phone' => '',
    'address' => '',
    'notes' => ''
];

$editing = false;

if (!empty($VARS['id']) && !is_empty($VARS['id'])) {
    if ($database->has('customers', ['customerid' => $VARS['id']])) {
        $editing = true;
        $custdata = $database->get(
                'customers', [
            'customerid (id)',
            'name',
            'email',
            'phone',
            'address',
            'notes'
                ], [
            'customerid' => $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=editcustomer');
        die();
    }
}

$pricing = [];

if ($editing) {
    $pricing = $database->select('customer_pricing', ['itemid', 'price'], ['customerid' => $custdata['id']]);
    for ($i = 0; $i < count($pricing); $i++) {
        $pricing[$i]['item'] = $binstack->get('items', ['itemid', 'name', 'code1', 'code2', 'cost', 'price'], ['itemid' => $pricing[$i]['itemid']]);
    }
}
?>

<div class="modal fade" tabindex="-1" role="dialog" id="pricemodal">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title"><i class="fas fa-user-tag"></i> <?php lang("add customer price"); ?></h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <div class="input-group">
                    <div class="input-group-prepend">
                        <span class="input-group-text px-2"><i class="fas fa-barcode"></i></span>
                    </div>
                    <input type="text" class="form-control" id="pricemodalitem" placeholder="<?php lang("barcode or search"); ?>" />
                    <div class="input-group-append">
                        <button class="btn btn-link" type="button" id="pricemodalsearch"><i class="fas fa-search"></i></button>
                    </div>
                </div>

                <div class="card mt-2">
                    <div class="card-body">
                        <h5 class="card-title"><i class="fas fa-box"></i> <?php lang("item"); ?></h5>
                        <p>
                            <?php lang("name"); ?>: <span id="pricemodalitemname">---</span><br />
                            <?php lang("cost"); ?>: <span id="pricemodalitemcost">---</span><br />
                            <?php lang("price"); ?>: <span id="pricemodalitemprice">---</span><br />
                        </p>
                    </div>
                    <div class="card-body">
                        <h5 class="card-title"><i class="fas fa-user-tag"></i> <?php lang("customer price"); ?></h5>
                        <div class="input-group">
                            <div class="input-group-prepend">
                                <span class="input-group-text">$</span>
                            </div>
                            <input class="form-control" id="pricemodalcustomerprice" type="money" />
                        </div>
                    </div>
                </div>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal"><?php lang("cancel"); ?></button>
                <button type="button" class="btn btn-primary" id="pricemodalsave"><?php lang("save"); ?></button>
            </div>
        </div>
    </div>
</div>

<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 customer", ['name' => "<span id=\"name_title\">" . htmlspecialchars($custdata['name']) . "</span>"]); ?>
                <?php
            } else {
                ?>
                <i class="fas fa-edit"></i> <?php lang("adding customer"); ?>
                <?php
            }
            ?>
        </h3>
        <div class="card-body row">
            <div class="form-group col-12">
                <label for="name"><i class="fas fa-user"></i> <?php lang("name"); ?></label>
                <input type="text" class="form-control" id="name" name="name" placeholder="Foo Bar" required="required" value="<?php echo htmlspecialchars($custdata['name']); ?>" />
            </div>
            <div class="form-group col-sm-6">
                <label for="email"><i class="fas fa-at"></i> <?php lang("email"); ?></label>
                <input type="email" class="form-control" id="email" name="email" placeholder="user@example.com" value="<?php echo htmlspecialchars($custdata['email']); ?>" />
            </div>
            <div class="form-group col-sm-6">
                <label for="phone"><i class="fas fa-phone"></i> <?php lang("phone"); ?></label>
                <input type="phone" class="form-control" id="phone" name="phone" placeholder="" value="<?php echo htmlspecialchars($custdata['phone']); ?>" />
            </div>
            <div class="form-group col-sm-6">
                <label for="address"><i class="fas fa-map-marker"></i> <?php lang("address"); ?></label>
                <textarea rows="3" class="form-control" id="address" name="address" placeholder=""><?php echo htmlspecialchars($custdata['address']); ?></textarea>
            </div>
            <div class="form-group col-sm-6">
                <label for="notes"><i class="fas fa-sticky-note"></i> <?php lang("notes"); ?></label>
                <textarea rows="3" class="form-control" id="notes" name="notes" placeholder=""><?php echo htmlspecialchars($custdata['notes']); ?></textarea>
            </div>
        </div>

        <hr />

        <div class="card-body">
            <h5 class="card-title"><?php lang("customer pricing"); ?></h5>
            <div class="btn-toolbar">
                <div class="btn btn-success" id="addcustomerpricebtn">
                    <i class="fas fa-plus"></i> <?php lang("add price"); ?>
                </div>
            </div>
            <table class="table table-hover table-sm" id="pricingtable">
                <thead>
                    <tr>
                        <th data-priority="0"></th>
                        <th data-priority="1"><?php lang('actions'); ?></th>
                        <th data-priority="1"><?php lang("item"); ?></th>
                        <th data-priority="4"><?php lang("cost"); ?></th>
                        <th data-priority="3"><?php lang("normal price"); ?></th>
                        <th data-priority="2"><?php lang("customer price"); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $i = 0;
                    foreach ($pricing as $p) {
                        ?>
                        <tr data-itemid="<?php echo $p['itemid']; ?>">
                            <td></td>
                            <td>
                                <div class="btn btn-sm btn-danger deletepricebtn" data-itemid="<?php echo $p['itemid']; ?>"><i class="fas fa-trash"></i> <?php lang("delete"); ?></div>
                            </td>
                            <td>
                                <input type="hidden" name="pricing[<?php echo $i; ?>][item]" value="<?php echo $p['itemid']; ?>" />
                                <?php echo $p['item']['name']; ?>
                            </td>
                            <td><?php echo $p['item']['cost']; ?></td>
                            <td><?php echo $p['item']['price']; ?></td>
                            <td>
                                <input type="hidden" name="pricing[<?php echo $i; ?>][price]" value="<?php echo $p['price']; ?>" />
                                <?php echo $p['price']; ?>
                            </td>
                        </tr>
                        <?php
                        $i++;
                    }
                    ?>
                </tbody>
            </table>
        </div>

        <input type="hidden" name="id" value="<?php echo htmlspecialchars($custdata['id']); ?>" />
        <input type="hidden" name="action" value="editcustomer" />
        <input type="hidden" name="source" value="customers" />

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