<?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("MACHINEMANAGER_EDIT")) {
    header("Location: ./app.php?msg=no_permission");
    die();
}

$editing = false;

if (!empty($_GET['arg']) && Client::exists($_GET['arg'], Clients::areLocal())) {
    $editing = true;
    $client = new Client($_GET['arg'], Clients::areLocal());
} else {
    $client = new Client();
}

if ($editing) {
    $form = new FormBuilder("Edit " . htmlspecialchars($client->getName()), "fas fa-user", "action.php", "POST");
} else {
    $form = new FormBuilder("Add Client", "fas fa-user", "action.php", "POST");
}

$form->setID("editclient");

$form->addHiddenInput("action", "editclient");
$form->addHiddenInput("source", "editclient");

if ($editing) {
    $form->addHiddenInput("id", $client->getID());
}

$form->addInput("name", $client->getName(), "text", true, null, null, "Name", "fas fa-user");
$form->addInput("phone", $client->getPhone(), "tel", false, null, null, "Phone", "fas fa-phone");
$form->addInput("email", $client->getEmail(), "email", false, null, null, "Email", "fas fa-envelope");
$form->addInput("billingaddress", $client->getBillingAddress(), "textarea", false, null, null, "Billing Address", "fas fa-file-invoice", 6);
$form->addInput("mailingaddress", $client->getMailingAddress(), "textarea", false, null, null, "Mailing Address", "fas fa-mail-bulk", 6);
$form->addInput("privatenotes", $client->getPrivateNotes(), "textarea", false, null, null, "Private Notes", "fas fa-comment-dots", 6, 0, 10000);
$form->addInput("publicnotes", $client->getPublicNotes(), "textarea", false, null, null, "Public Notes", "far fa-comment-dots", 6, 0, 10000);

$form->addButton("Save", "fas fa-save", null, "submit", "savebtn");

$form->generate();
