<?php

include "../../classes/autoload.php";

// handle save before any output
if (isset($_POST['record_ids'])) {
    $record_ids = $_POST['record_ids'];
    $notes_arr  = $_POST['notes'];
    $terms_arr  = $_POST['terms'];

    foreach ($record_ids as $i => $record_id) {
        $notes = str_replace(["'", "\""], "", $notes_arr[$i]);
        $terms = str_replace(["'", "\""], "", $terms_arr[$i]);
        $db->query("terms", "UPDATE terms SET notes = '$notes', terms = '$terms' WHERE record_id = '$record_id'");
    }

    header("Location: terms.php");
    exit;
}

$html = new html("TERMS");

$app = new inner_app();
$app->quick_bar("/app/invoices/");
$app->app_start();

$res = $db->query("terms", "SELECT * FROM terms WHERE document_type = 'INVOICE' ORDER BY record_id ASC");

?>

<div class="column width_90 background_1 border_radius">
    <h1>INVOICE TERMS</h1>

    <?php while ($term = $res->fetch_assoc()) { ?>

        <h3><?php echo $term['order_type']; ?></h3>

        <input type="text" hidden name="record_ids[]" class="term_record_id" value="<?php echo $term['record_id']; ?>">

        <div class="row column_gap_2 width_80">
            <div class="column width_100">
                <label>IMPORTANT NOTES</label>
                <textarea name="notes[]" class="term_notes width_100" rows="10"><?php echo $term['notes']; ?></textarea>
            </div>
        </div>

        <div class="row column_gap_2 width_80">
            <div class="column width_100">
                <label>TERMS</label>
                <textarea name="terms[]" class="term_terms width_100" rows="12"><?php echo $term['terms']; ?></textarea>
            </div>
        </div>

        <br>

    <?php } ?>

    <button class="width_90" onclick="save()">SAVE ALL</button>
    <br>
</div>

<script>
    function save() {
        let record_ids = document.querySelectorAll('[name="record_ids[]"]');
        let notes      = document.querySelectorAll('[name="notes[]"]');
        let terms      = document.querySelectorAll('[name="terms[]"]');

        let form = document.createElement("form");
        form.method = "POST";
        form.action = "terms.php";

        record_ids.forEach(el => {
            let input = document.createElement("input");
            input.type = "hidden";
            input.name = "record_ids[]";
            input.value = el.value;
            form.appendChild(input);
        });

        notes.forEach(el => {
            let input = document.createElement("input");
            input.type = "hidden";
            input.name = "notes[]";
            input.value = el.value;
            form.appendChild(input);
        });

        terms.forEach(el => {
            let input = document.createElement("input");
            input.type = "hidden";
            input.name = "terms[]";
            input.value = el.value;
            form.appendChild(input);
        });

        document.body.appendChild(form);
        form.submit();
    }
</script>