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

$stmt = $GLOBALS['con']->prepare("SELECT * FROM `dd_hse` WHERE `id`='".$_REQUEST['id']."'")or die(mysqli_error($GLOBALS['con']));
$stmt->execute();$iv=$stmt->get_result();$stmt->close();
$d=mysqli_fetch_array($iv);
?>
<div class="card">
    <div class="card-header">
        <h4>Update 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="hseEditForm" 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" value="<?= $d['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" value="<?= $d['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" rows="5"><?= $d['address']?></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" value="<?= $d['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" value="<?= $d['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" value="<?= $d['email']?>"></div>
                    </div>
                </div>

                <div class="col-md-12 text-center mt-4">
                    <input class="btn btn-info" type="submit" value="Update">

                </div>
            </div>
            <input type="hidden" name="id" value="<?= $d['id']?>">
            <input id="function" type="hidden" name="FUNCTION" value="update">
            <input type="hidden" name="tenant" value="<?=TENANT_URL?>">
        </form>

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

    var form = document.getElementById('hseEditForm');
    var formData = new FormData(form);

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

    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>' + result);
            }
        }
    };

    // Send FormData directly without converting to URLSearchParams
    xhr.send(formData);
});
</script>