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

$machine = null;

if (!empty($VARS["arg"]) && Component::exists($VARS["arg"])) {
    $editing = true;
    $component = new Component($VARS["arg"]);
} else if (!empty($VARS["id"]) && Component::exists($VARS["id"])) {
    $editing = true;
    $component = new Component($VARS["id"]);
} else {
    $component = Component::create();
    $editing = false;
}

if ($editing && !empty($component->getMachineID())) {
    $machine = new Machine($component->getMachineID());
}

if (!empty($VARS['machine']) && Machine::exists($VARS['machine'])) {
    $machine = new Machine($VARS['machine']);
    $component->setMachineID($machine->getID());
}

if ($editing) {
    $form = new FormBuilder("Edit Component #" . $component->getID(), "fas fa-memory", "action.php", "POST");
} else {
    $form = new FormBuilder("Add Component", "fas fa-memory", "action.php", "POST");
}

$form->setID("editcomponent");

$form->addHiddenInput("action", "editcomponent");

$types = [
    "" => ""
];
foreach (Component::getTypes() as $k => $v) {
    $types[$k] = $v;
}

if ($editing) {
    $form->addHiddenInput("id", $component->getID());
} else {
    $form->addInput("id", $component->getID(), "text", true, null, null, "Component ID", "fas fa-memory", 4, 1, 20);
}

if (!empty($machine)) {
    $form->addHiddenInput("source", "viewmachine");
} else {
    $form->addHiddenInput("source", "components");
}

$date = date('Y-m-d');
$time = date('H:i');
if (!empty($component->getTestedDate())) {
    $date = date("Y-m-d", strtotime($component->getTestedDate()));
    $time = date("H:i:s", strtotime($component->getTestedDate()));
}
$form->addInput("machine", (is_null($machine) ? "" : $machine->getID()), "text", false, null, null, "Machine ID", "fas fa-desktop", 4, 0, 200);
$form->addInput("serial", $component->getSerial(), "text", false, null, null, "Serial", "fas fa-barcode", 4, 0, 200);
$form->addInput("type", $component->getTypeID(), "select", false, null, $types, "Type", "fas fa-list");
$form->addInput("date", $date, "date", false, null, null, "Tested Date", "fas fa-calendar");
$form->addInput("time", $time, "time", false, null, null, "Tested Time", "fas fa-clock");
$form->addInput("capacity", $component->getCapacity(), "text", false, null, null, "Capacity", "fas fa-database", 4, 0, 200);
$form->addInput("model", $component->getModel(), "text", false, null, null, "Model", "fas fa-hashtag", 4, 0, 200);
$form->addInput("manufacturer", $component->getManufacturer(), "text", false, null, null, "Manufacturer", "fas fa-industry", 4, 0, 200);
$form->addInput("price", $component->getPrice(), "number", false, null, null, "Price", "fas fa-money-bill-wave");
$form->addInput("privatenotes", $component->getPrivateNotes(), "textarea", false, null, null, "Private Notes", "fas fa-comment-dots", 6, 0, 10000);
$form->addInput("publicnotes", $component->getPublicNotes(), "textarea", false, null, null, "Public Notes", "far fa-comment-dots", 6, 0, 10000);

$form->addButton("Save", "fas fa-save", null, "submit", "savebtn");
if (!is_null($machine)) {
    $form->addButton("Back", "fas fa-arrow-left", "./app.php?page=viewmachine&id=" . $machine->getID(), "", null, null, "", "btn btn-info ml-2");
} else {
    $form->addButton("Back", "fas fa-arrow-left", "./app.php?page=components", "", null, null, "", "btn btn-info ml-2");
}

$form->generate();
