<?php
if (core::ifGet("id") == FALSE and access::ifAccesss("add-document") == FALSE) {
    get::page("unknow");
    exit();
}

if (core::getGet("id") == NULL) {
    $titre = "Ajouter un document";
    $id_form = '<input type="hidden" name="id" value="add">';
    $submit = "Ajouter ce nouveau document";
    $document = session::getTemp("document");
} else {
    $datas = document::get(core::getGet("id"));
    $document = $datas["document"];
    $files = $datas["files"];

    if (empty($document["id"]) OR access::ifLimitAccessException("salaire") AND isset($document["id_type"]) AND $document["id_type"] == 3) {
        get::page("unknow");
        exit();
    }

    $id_form = '<input type="hidden" name="id" value="' . $document["id"] . '">';
    $submit = "Modifier ce document";
    $badgeCSS = " font-size:0.4em; margin-top:-5px;";
    $titre = "[#" . $document["id"] . "] " . $document["titre"];
}

if (debug::isFile("debug")) {
    (!empty($document)) ? debug::log($document, "Données brutes document") : NULL;
    (!empty($files)) ? debug::log($files, "Données brutes files") : NULL;
}

$userTags = user::getUserById(session::getId())["tags"];

?>
<header class="d-flex flex-column flex-md-row align-items-md-center p-3 bg-light ">
    <h2 class="bd-title" id="content">
        <span><?php echo $titre ?></span>
    </h2>
</header>
<?php
if (core::ifGet("add")) {
    $labelFil = "Ajouter un document";
    $lienFil = "/add-document.html";
} else {
    $labelFil = "[#" . $document["id"] . "] " . $document["titre"];
    $lienFil = "/document-" . core::getGet("id") . ".html";
}

echo core::filAriane(array(
    "current" => $labelFil,
    "arbo" => array(
        "Factures" => NULL,
        "Listes des documents" => "/documents.html",
        $labelFil => $lienFil
    )
));

if (isset($document["id"])) {
?>
    <div style="float:right; margin-top: -60px;">
        <a href="/submit.php?from=document-delete&id=<?php echo $document["id"] ?>" style="color: #dc3545; text-decoration:none;" onclick="return confirm('Voulez-vous supprimer ce document ?')"><button type="submit" class="btn btn-outline-danger btn-sm"><?php icon::getFont(["icon" => "bi-trash-fill"]) ?></i> Supprimer</button></a>
    </div>
<?php
}
?>

<link rel="stylesheet" href="css/dragAndDrop.css">

<form id="form-document" method="post" action="/submit.php" enctype="multipart/form-data" onsubmit="return(false);">

    <div class="row">
        <div class="col">
            <input type="hidden" name="from" value="document">

            <?php echo $id_form; ?>


            <?php $_titre = (isset($document["titre"])) ? $document["titre"] : NULL; ?>
            <div class="form-group">
                <label>Titre</label>
                <input type="text" class="form-control" value="<?php core::printFormValue($_titre) ?>" name="titre" placeholder="" required>
            </div>

            <br />
            <?php
            $_type_document = (isset($document["id_type"])) ? $document["id_type"] : NULL;
            $_label_type_document = document::getTypes();
            ?>
            <div class="form-group">
                <label>Type de document</label>
                <select name="id_type" class="form-select">
                    <option value=""></option>
                    <option value="1" <?php core::printFormSelectOption($_type_document, 1) ?>><?php echo $_label_type_document[1] ?></option>
                    <option value="2" <?php core::printFormSelectOption($_type_document, 2) ?>><?php echo $_label_type_document[2] ?></option>
                    <?php if (!access::ifLimitAccessException("salaire")) { ?>
                        <option value="3" <?php core::printFormSelectOption($_type_document, 3) ?>><?php echo $_label_type_document[3] ?></option>
                    <?php } ?>
                    <option value="0" <?php core::printFormSelectOption($_type_document, 0) ?>><?php echo $_label_type_document[0] ?></option>
                </select>
            </div>
            <br />

            <?php $_montant = (isset($document["montant"])) ? $document["montant"] : NULL; ?>
            <div class="form-group">
                <label>Montant associé</label>
                <input type="text" class="form-control" value="<?php core::printFormValue($_montant) ?>" name="montant" placeholder="" onkeypress="return (event.charCode >= 48 && event.charCode <= 57) || event.charCode == 46 || event.charCode == 0 ">
            </div>
            <br />

            <?php $_tagsSupplier = (isset($document["tagsSupplier"])) ? $document["tagsSupplier"] : NULL; ?>
            <div class="form-group">
                <label>Tags associés</label>
                <input type="text" value="<?php core::printFormValue($_tagsSupplier) ?>" name="tagsSupplier" id="tagsSupplier" />
            </div>
            <br />

            <?php $_date = (isset($document["date"])) ? $document["date"] : NULL; ?>
            <div class="form-group">
                <label>Date associée à ce document</label>
                <input type="date" class="form-control" name="date" value="<?php core::printFormValue($_date) ?>" placeholder="" required>
            </div>
            <br />

            <?php $_deadline = (isset($document["deadline"])) ? $document["deadline"] : NULL; ?>
            <div class="form-group">
                <label>Date limite de traitement</label>
                <input type="date" class="form-control" name="deadline" value="<?php core::printFormValue($_deadline) ?>" placeholder="" required>
            </div>
            <br />

            <?php $_description = (isset($document["description"])) ? $document["description"] : NULL; ?>
            <div class="form-group">
                <label>Description et commentaires</label>
                <textarea class="form-control" name="description" style="height:100%;"><?php core::printFormValue($_description) ?></textarea>
            </div>
            <br />

            <?php $_tagsUser = (isset($document["tagsUser"])) ? $document["tagsUser"] : NULL; ?>
            <div class="form-group">
                <label>Attribution</label>
                <input type="text" value="<?php core::printFormValue($_tagsUser) ?>" name="tagsUser" id="tagsUser" />
            </div>
            <br />

            <?php if (isset($files["principal"]["id"])) { ?>

                <div style="border:1px #CCCCCC solid; background-color:#F6F6F6; padding:20px; border-radius:.375rem;">

                    <?php

                    if ((isset($document) and tags::compareUserDocument($userTags, $document["tagsUser"]) == TRUE) or (isset($document["date_done"]))) {
                        $checkDone = (isset($document["id_user_done"])) ? " disabled" : NULL;
                        $checkColor = (isset($document["id_user_done"])) ? " style=\"background-color:#d1e7dd;\"" : NULL;
                        $checkText = (isset($document["id_user_done"])) ? "Document traité par <span style=\"font-weight: bold;\">" . $document["doneUser"] . "</span> le" : "Document traité le";
                        $_dateDone = (isset($document["date_done"])) ? $document["date_done"] : NULL;
                        echo '  <div class="form-group">
                                <label>' . $checkText . '</label>
                                <input type="date" class="form-control"' . $checkColor . ' name="date_done" value="' . core::getFormValue($_dateDone) . '"' . $checkDone . '>
                                </div><br />';
                    }

                    document::printAttachement($files);
                    ?>
                    <div class="file-drop-area">
                        <span class="choose-file-button">Ajouter une pièce jointe</span>
                        <span class="file-message">ou drag & drop</span>
                        <input id="attachement-document" class="import-excel" name="attachement-document" type="file" onchange="dargAndDropAttachment()" accept="image/jpeg,image/png,application/pdf,text/xml,application/xml">
                    </div>
                </div>
                <br />
            <?php } ?>
        </div>

        <div class="col">
            <?php if (empty($files["principal"]["id"])) { ?>
                <div class="file-drop-area" style="margin-top:22px;" id="file-document-import">
                    <span class="choose-file-button">Choisissez votre document</span>
                    <span class="file-message">ou drag & drop</span>
                    <input id="document-import" class="import-excel" name="document-import" type="file" onchange="dargAndDrop()" accept="image/jpeg,image/png,application/pdf,text/xml,application/xml" required>
                </div>
                <br />
            <?php } else {
                $getMime = mime_content_type(file::download($files["principal"]["id"], DIR_DATAS_DOCS));
                $heigh = ($getMime == "application/pdf" OR $getMime == "text/xml" OR $getMime == "application/xml") ? "height:110vh;" : NULL;
                $marginTop = ($getMime == "text/xml" OR $getMime == "application/xml") ? "margin-top:4px;" : "margin-top:22px;";
                echo '  <div style="' . $marginTop . '">
                        <embed src="/document.php?id=' . $files["principal"]["id"] . '" style="width:100%; margin-top:10px;' . $heigh . '" /></embed>
                    </div><br />
                    ';
            }
            ?>
        </div>
    </div>

    <input class="btn btn-primary btn-lg" style="width: 100%;" type="button" value="<?php echo $submit ?>" onclick="validateAndSubmit()">
    <br /><br />
</form>

<?php
    get::javascript("document");
?>