<?php
$cr = mysqli_query($GLOBALS['con'], "SELECT * FROM `locations` WHERE `locnumber`='" . $_REQUEST['locnumber'] . "' ") or die(mysqli_error($GLOBALS['con']));
$cd = mysqli_fetch_array($cr); ?>

<div class="row">
    <div class="col-12  text-center text-md-end">
        <a href="javascript:void(0)" class="btn btn-secondary noprint " data-mdb-tooltip-init
           data-mdb-placement="bottom" data-mdb-trigger="hover" totitle="hover" title="Print"
           onclick="printContent('')"><i class="fad fa-print"></i></a>
    </div>
</div>
<h4><span class="text-muted">Edit Site for</span> <strong><?= $_REQUEST['clientname'] ?></strong></h4>

<form id="locationForm" class="form-horizontal">
    <fieldset>
        <?php require_once(ROOT_URL . '/forms/elements/location_form.php') ?>
        <div class="spacer"></div>
        <div class="col-12 col-md-12 text-center">
            <div class="form-group row">
                <input type="hidden" name="clientnumber" value="<?= $_REQUEST['clientnumber'] ?>">
                <input type="hidden" name="locnumber" value="<?= $_REQUEST['locnumber'] ?>">
                <input type="hidden" name="tenant" value="<?= TENANT_URL ?>">
                <input type="hidden" name="FUNCTION" value="update">
    </fieldset>
    <input id="btn_save" class="btn btn-secondary" type="submit" name="submit" value="Submit" style="display:none" />
    </div>
    </div>

</form>
<script>
document.querySelector("#locationForm input[type='submit']").addEventListener('click', function(event) {
    event.preventDefault();

    var xhr = new XMLHttpRequest();
    var form = document.getElementById('locationForm');
    var formData = new FormData(form); // FormData handles serialization of the form

    xhr.open('POST', ROOT_URL + '/includes/locations/location_functions.php', true);

    // Set the callback function for when the request completes
    xhr.onload = function() {
        if (xhr.status === 200) {
            var result = xhr.responseText;
            showResponse('success', 'Location updated successfully');
        }
    };

    // Send the FormData object directly without converting it to a string
    xhr.send(formData);
});



document.addEventListener('DOMContentLoaded', function() {
    document.getElementById('navBox').innerHTML =
        '<li class="breadcrumb-item"><a href="index.php?t=includes/master&p=client.php&view=list">Client List</a><i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i></li><li class="breadcrumb-item"><a href="index.php?t=includes/master&p=client.php&clientnumber=<?= $_REQUEST['clientnumber'] ?>&clientname=<?= addslashes($_REQUEST['clientname']) ?>&view=asset"><?= $_REQUEST['clientname'] ?></a><i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i></li><li class="breadcrumb-item"><a href="index.php?t=includes/master&p=locations.php&clientnumber=<?= $_REQUEST['clientnumber'] ?>&clientname=<?= addslashes($_REQUEST['clientname']) ?>">Locations</a><i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i></li><li class="breadcrumb-item active">Edit Location</li>'

    document.getElementById('footerControls').innerHTML =
        '<button class="btn btn-secondary" onclick="triggerAction(\'btn_save\')">SAVE CHANGES</button>'
})
</script>