<?php

include "../../classes/autoload.php";

$loader = new loading();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $doc = 'quote';
    $order_types = ['SUPPLY & INSTALL', 'SUPPLY', 'SUPPLY & DELIVERY'];
    $key_map = [
        'SUPPLY & INSTALL'  => 'supply_and_install',
        'SUPPLY'            => 'supply',
        'SUPPLY & DELIVERY' => 'supply_and_delivery',
    ];
    foreach ($order_types as $order_type) {
        $key       = $key_map[$order_type];
        $terms_val = addslashes($_POST[$key . '_terms'] ?? '');
        $notes_val = addslashes($_POST[$key . '_notes'] ?? '');
        $res = $db->query("terms", "SELECT record_id FROM `terms` WHERE `document_type` = '$doc' AND `order_type` = '$order_type'");
        if ($res && $res->num_rows > 0) {
            $row = $res->fetch_assoc();
            $db->query("terms", "UPDATE `terms` SET `terms` = '$terms_val', `notes` = '$notes_val' WHERE `record_id` = '{$row['record_id']}'");
        } else {
            $db->query("terms", "INSERT INTO `terms` (`document_type`, `order_type`, `terms`, `notes`) VALUES ('$doc', '$order_type', '$terms_val', '$notes_val')");
        }
    }
    echo "<script>window.location.href='terms.php?saved=1';</script>";
    exit();
}

$html = new html("QUOTE TERMS");

$app = new inner_app();
$app->quick_bar("/app/quotes/");
$app->app_start();

$terms_res = $db->query("terms", "SELECT * FROM `terms` WHERE `document_type` = 'quote'");
$terms_by_type = [];
while ($t = $terms_res->fetch_assoc()) {
    $terms_by_type[$t['order_type']] = $t;
}
?>

<div class="column width_80 background_1 border_radius">

    <h1>EDIT QUOTE TERMS</h1>

    <?php if (isset($_GET['saved'])): ?>
    <div style="background:#1e7e4a; color:#fff; padding:10px 20px; border-radius:6px; margin-bottom:16px; width:80%;">
        &#10003; QUOTE terms saved successfully.
    </div>
    <?php endif; ?>

    <?php
    $sections = [
        'SUPPLY & INSTALL'  => 'supply_and_install',
        'SUPPLY'            => 'supply',
        'SUPPLY & DELIVERY' => 'supply_and_delivery',
    ];
    foreach ($sections as $order_type => $key):
        $row = $terms_by_type[$order_type] ?? ['terms' => '', 'notes' => ''];
    ?>
    <div class="row column_gap_2 width_80">
        <div class="column width_100">
            <h3><?php echo htmlspecialchars($order_type); ?></h3>
            <label>IMPORTANT NOTES</label>
            <textarea class="width_100" name="<?php echo $key; ?>_terms" rows="6"><?php echo htmlspecialchars($row['terms']); ?></textarea>
            <label>TERMS</label>
            <textarea class="width_100" name="<?php echo $key; ?>_notes" rows="6"><?php echo htmlspecialchars($row['notes']); ?></textarea>
        </div>
    </div>
    <br>
    <?php endforeach; ?>

    <button class="width_90" onclick="save()">SAVE</button>
    <br>

</div>

<script>
    function save() {
        var form = document.createElement('form');
        form.method = 'POST';
        form.action = 'terms.php';
        var inputs = document.querySelectorAll('textarea[name]');
        inputs.forEach(function(el) {
            var input = document.createElement('input');
            input.type = 'hidden';
            input.name = el.name;
            input.value = el.value;
            form.appendChild(input);
        });
        document.body.appendChild(form);
        form.submit();
    }
</script>