<?php
$tenant = $_REQUEST['tenant'];
if (file_exists('/home/enrepco' . $_REQUEST['tenant'] . '/config.php')) {
    require_once('/home/enrepco' . $_REQUEST['tenant'] . '/config.php');
}
?>

<form id="newFormHandler" class="form-horizontal" method="POST">
    <div class="card p-0 me-3">
        <div class="card-body px-lg-5 pt-0">
            <div class="row">
                <?php if ($_REQUEST['type'] === "plant") { ?>
                <div class="form-group col-3">
                    <input type="text" id="code" name="code" class="form-control form-control-sm" required />
                    <label for="code">Code</label>
                </div>
                <div class="form-group col-9">
                    <input type="text" id="desc" name="desc" class="form-control form-control-sm" required />
                    <label for="desc">Description</label>
                </div>
                <div class="form-group col-4">
                    <input type="text" id="freq" name="freq" class="form-control form-control-sm" required />
                    <label for="freq">Frequency</label>
                </div>
                <div class="form-group col-4">
                    <input type="text" id="size" name="size" class="form-control form-control-sm" required />
                    <label for="size">Size</label>
                </div>
                <div class="form-group col-4">
                    <input type="text" id="unit" name="unit" class="form-control form-control-sm" required />
                    <label for="unit">Units</label>
                </div>
                <input type="hidden" name="mode" value="plant">
                <input type="hidden" name="FUNCTION" value="save">
                <?php }


                if ($_REQUEST['type'] === "phrases") { ?>
                <div class="form-group col-3">
                    <input type="text" id="codeP" name="code" class="form-control form-control-sm" />
                    <label for="code">Code</label>
                </div>
                <div class="form-group col-12">
                    <textarea id="phraseP" name="phrase" class="form-control form-control-sm" rows="5"></textarea>
                    <label for="phrase">Phrase</label>
                </div>
                <input type="hidden" name="mode" value="phrase">
                <input type="hidden" name="FUNCTION" value="save">
                <?php }

                if ($_REQUEST['type'] === "elecphrases") { ?>
                <div class="form-group col-3">
                    <input type="text" id="elecCodeP" name="code" class="form-control form-control-sm" />
                    <label for="elecCodeP">Code</label>
                </div>
                <div class="form-group col-12">
                    <textarea id="elecPhraseP" name="phrase" class="form-control form-control-sm" rows="5"></textarea>
                    <label for="elecPhraseP">Phrase</label>
                </div>
                <input type="hidden" name="mode" value="elecPhrase">
                <input type="hidden" name="FUNCTION" value="save">
                <?php }

                if ($_REQUEST['type'] === "surveyorSkills") { ?>
                <div class="form-group col-3">
                    <input type="text" id="disciplineP" name="discipline" class="form-control form-control-sm" />
                    <label for="disciplineP">Discipline</label>
                </div>
                <div class="form-group col-12">
                    <textarea id="descriptionP" name="description" class="form-control form-control-sm"
                        rows="5"></textarea>
                    <label for="descriptionP">Description</label>
                </div>
                <input type="hidden" name="mode" value="surveyorSkills">
                <input type="hidden" name="FUNCTION" value="save">
                <?php } ?>

                <input type="hidden" name="tenant" value="<?= $tenant ?>">
            </div>
        </div>
        <div class="card-footer">
            <button class="btn btn-outline-success me-3" type="submit" onclick="saveItem()">
                <i class="fad fa-save" aria-hidden="true"></i> Save Entry
            </button>
            <button class="btn btn-outline-danger float-end" type="button" onclick="killModal('popupBox')">
                <i class="fad fa-times" aria-hidden="true"></i> Cancel
            </button>
        </div>
    </div>
</form>



<script>
function saveItem() {

    var t = '<?= $_REQUEST['type'] ?>';
    var m = '<?= $_REQUEST['format'] ?>';
    var l = '<?= $_REQUEST['l'] ?>';
    event.preventDefault();

    // Get the form element
    var form = document.getElementById('newFormHandler');

    if (!form || !(form instanceof HTMLFormElement)) {
        showResponse('danger', 'Form element not found or invalid');
        return;
    }

    // Check conditions based on value of t
    if (t === "plant") {
        var code = document.getElementById('code').value.trim();
        var desc = document.getElementById('desc').value.trim();
        var freq = document.getElementById('freq').value.trim();

        if (!code || !desc || !freq) {
            showResponse('danger', 'Please fill in code, description, and frequency');
            return;
        }
    }
    if (t === "phrases") {
        var code = document.getElementById('codeP').value.trim();
        var phrase = document.getElementById('phraseP').value.trim();

        if (!code || !phrase) {
            showResponse('danger', 'Please fill in code and phrase');
            return;
        }
    }
    if (t === "elecphrases") {
        var code = document.getElementById('elecCodeP').value.trim();
        var phrase = document.getElementById('elecPhraseP').value.trim();

        if (!code || !phrase) {
            showResponse('danger', 'Please fill in code and phrase');
            return;
        }
    }

    // Create a new FormData object
    var formData = new FormData(form);

    // Use XMLHttpRequest to submit the form
    var xhr = new XMLHttpRequest();
    xhr.open("POST", ROOT_URL + '/includes/jump/jump_functions.php', true);

    // Remove manually setting the content-type header
    // xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    xhr.onreadystatechange = function() {
        if (xhr.readyState === 4 && xhr.status === 200) {
            var result = xhr.responseText.trim();
            //debug(result);

            if (result === "s") {
                showResponse('success', 'Your entry has been saved successfully');
                loadItems(t, m, l);
                killModal('newForm'); // Assuming this closes the modal after saving
            } else {
                showResponse('danger', 'The code is already in the system');
            }
        }
    };

    // Send the form data
    xhr.send(formData);
}
</script>