<?php
$data = urldecode($_REQUEST['data']);
$d = explode("|", $data);
if (isset($_REQUEST['forwarder']) && $_REQUEST['forwarder'] == "standalone") {
    $d[0] = $_REQUEST['clientnumber'];
    $d[4] = $_REQUEST['clientname'];
    $cd['code'] = $_REQUEST['type'];
    $cd['locnumber'] = $_REQUEST['site'];
    $cd['statuslive'] = "1";
} else {
    $cd['code'] = $_REQUEST['type'];
}
$saveFunction = "insert";
?>
<script>
    document.addEventListener('DOMContentLoaded', function() {
        const navBox = document.getElementById('navBox');
        const breadcrumbHTML = `
    <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=<?= $d[0] ?>&view=edit"><?= getClientName($d[0]) ?></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=<?= $d[0] ?>&view=asset">Schedule</a><i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i></li>
    <li class="breadcrumb-item active">New Asset</li>
  `;

        navBox.innerHTML = breadcrumbHTML;
    });
</script>
<form id="assetForm" class="form-horizontal">
    <fieldset>
        <div class="row">
            <?php require_once(ROOT_URL . '/forms/elements/personnel.frm'); ?>
            <?php require_once(ROOT_URL . '/forms/elements/asset_controls.frm'); ?>
            <?php require_once(ROOT_URL . '/forms/elements/asset_details.frm'); ?>
            <?php if (isset($_REQUEST['forwarder']) && $_REQUEST['forwarder'] == "standalone") { ?>
                <input type="hidden" name="locnumber" id="locnumber" value="<?= $cd['locnumber'] ?>" />
            <?php } ?>
            <input type="hidden" name="clientnumber" id="clientnumber" value="<?= $d[0] ?>" />
            <input type="hidden" name="itemnumber" id="locnumber" value="<?= $cd['itemnumber'] ?>" />
            <input type="hidden" name="FUNCTION" id="FUNCTION" value="insert" />
            <input type="hidden" name="tenant" id="tenant" value="<?= TENANT_URL ?>" />
            <div class="col-md-12 text-center"><input id="btn_save" type="submit" value="save" style="display:none" />
            </div>
        </div>
    </fieldset>

</form>
<script>
    document.addEventListener('DOMContentLoaded', function() {

        // Submit Button Click Handler
        document.getElementById('assetForm').addEventListener('submit', function(event) {

            event.preventDefault();
            const actionButton = document.getElementById("actionButton");
            actionButton.disabled = true;

            const locNumber = document.getElementById('locnumber').value;
            if (locNumber === "") {
                showResponse('warning', 'Please specify a site before proceeding.');
                actionButton.disabled = false;
            } else {
                fetch(ROOT_URL + '/includes/asset/asset_functions.php', {
                        method: 'POST',
                        body: new FormData(document.getElementById('assetForm'))
                    })
                    .then(response => response.text())
                    .then(result => {

                        showResponse('success', 'Your changes have been saved successfully');
                       //debug(result);
                        window.location.href = '<?= $_SESSION['returnLanding'] ?>'
                    });
            }
        });

        const footerControls = document.getElementById('footerControls');
        const buttonHTML =
            `<button id="actionButton" class="btn btn-secondary" onclick="triggerAction('btn_save')">SAVE</button>`;

        footerControls.innerHTML = buttonHTML;
    });
</script>