<?php
if(file_exists('../../../'.$_REQUEST['tenant'].'/config.php')){require_once('../../../'.$_REQUEST['tenant'].'/config.php'); }
if(file_exists('/config.php')){require_once('/config.php');}
?>
<div class="card mb-5">
    <div class="card-header">
        <h4>Create New HSE entry <div class="float-end"><a class="btn btn-secondary btn-sm" href="javascript:void(0)" onClick="killtoggle()"><i class="fa fa-times"></i></a></div>
        </h4>
    </div>
    <div class="card-body">
        <form id="hseForm" class="form-horizontal">
            <div class="row">
                <div class="col-md-6">
                    <div class="row form-group mb-3">
                        <div class="col-md-2"><label class="control-label" for="location">Location / Name</label></div>
                        <div class="col-md-5"><input class="form-control" type="text" name="location"></div>
                    </div>
                    <div class="row form-group mb-3">
                        <div class="col-md-2"><label class="control-label" for="contact">Contact Name</label></div>
                        <div class="col-md-5"><input class="form-control" type="text" name="contact"></div>
                    </div>
                    <div class="row form-group mb-3">
                        <div class="col-md-2"><label class="control-label" for="address">Address</label></div>
                        <div class="col-md-8"><textarea class="form-control" name="address" ros="5"></textarea></div>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="row form-group mb-3">
                        <div class="col-md-2"><label class="control-label" for="phone">Phone</label></div>
                        <div class="col-md-5"><input class="form-control" type="tel" name="phone"></div>
                    </div>
                    <div class="row form-group mb-3">
                        <div class="col-md-2"><label class="control-label" for="fax">Fax</label></div>
                        <div class="col-md-5"><input class="form-control" type="tel" name="fax"></div>
                    </div>
                    <div class="row form-group mb-3">
                        <div class="col-md-2"><label class="control-label" for="email">Email</label></div>
                        <div class="col-md-5"><input class="form-control" type="email" name="email"></div>
                    </div>
                </div>
                <div class="col-md-12 text-center mt-4">
                    <input class="btn btn-info" type="submit" value="Create">
                    <input id="function" type="hidden" name="FUNCTION" value="insert">
                    <input type="hidden" name="tenant" value="<?=TENANT_URL?>">
                </div>
            </div>
        </form>
    </div>
</div>

<script>
document.getElementById('hseForm').addEventListener('submit', function(event) {
    event.preventDefault(); // Prevent default form submission

    // Collect the form data manually
    var form = document.getElementById('hseForm');
    var formData = new URLSearchParams(new FormData(form)).toString(); // Serialize the form data

    var xhr = new XMLHttpRequest();
    xhr.open('POST', ROOT_URL + '/includes/hse/hse_functions.php', true);

    // Set content type for URL-encoded data
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    xhr.onreadystatechange = function() {
        if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
            var result = xhr.responseText.trim(); // Trim the result to avoid issues with whitespaces

            if (result === "s") {
                showResponse('success', 'Your entry has been saved successfully');
                document.getElementById('new_item').innerHTML = ''; // Clear the new item form
                loadData(); // Reload the data after the form is submitted
            } else {
                showResponse('danger', 'There was a problem, please check your details and try again<br><br>' + result);
            }
        }
    };

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