<?php 
if(file_exists('../../../'.$_REQUEST['tenant'].'/config.php')){require_once('../../../'.$_REQUEST['tenant'].'/config.php'); }
if(file_exists('/config.php')){require_once('/config.php'); }
$d=explode("|",$_REQUEST['data']);

function getSurveyorsName($esname){
    $stmt = $GLOBALS['con']->prepare("SELECT `surname`,`forename` FROM `surveyor` WHERE `id`=?")or die(mysqli_error($GLOBALS['con']));
    $stmt->bind_param('i',$esname);$stmt->execute();$rc=$stmt->get_result();$stmt->close();	
    $cd=mysqli_fetch_array($rc);	
    return $cd['forename']." ".$cd['surname'];
    }


$items=explode(",",$_REQUEST['items']);
$iCount=count($items);
?>
<p class="lead">You have chosen to allocate the following <b><?= $iCount?></b> assets to <b><?=getSurveyorsName($_REQUEST['surveyor'])?></b></p>
<div class="row" style="max-height:200px; overflow:auto">
    <?php 
foreach($items as $item){
$item=str_replace("C","",$item);
?>
    <div class="col-md-4">
        <div class="card me-2 mb-2 p-2"><?= $item?> - <?= getItemDetails($item,'name')?></div>
    </div>

    <?php }?>
    <input id="itemList" type="hidden" value="<?= $_REQUEST['items']?>">
</div>
<div class="row mt-5">
    <div class="col-6 text-center"><a id="btnMassConfirm" href="javascript:void(0)" class="btn btn-success">Confirm</a></div>
    <div class="col-6 text-center"><a id="btnCancel" href="javascript:void(0)" class="btn btn-danger">Cancel</a></div>
</div>
<input id="itemList" type="hidden" value="<?= $_REQUEST['items']?>">
<input id="clientnumber" type="hidden" value="<?= $_REQUEST['clientnumber']?>">
<input id="surveyor" type="hidden" value="<?= $_REQUEST['surveyor']?>">
<input id="locnumber" type="hidden" value="<?= $_REQUEST['locnumber']?>">
<script>
$('#popupBox .modal-header').html('<a href="javascript:void(0)" class="sa">Assign assets</a></div>');
</script>

<div id="erm" class="text-center"></div>
<script>
document.getElementById("btnMassConfirm").addEventListener("click", function() {
    var items = document.getElementById('itemList').value;
    var clientnumber = document.getElementById('clientnumber').value;
    var locnumber = document.getElementById('locnumber').value;
    var surveyor = document.getElementById('surveyor').value;

    var arr = items.split(",");

    arr.forEach(function(value) {
        var i = value.replace('C', '');

        var xhr = new XMLHttpRequest();
        xhr.open("POST", ROOT_URL + '/includes/surveyor/surveyor_functions.php', true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4 && xhr.status === 200) {
                // handle response if needed
            }
        };
        xhr.send("FUNCTION=setAssetSurveyor&clientnumber=" + clientnumber +
            "&locnumber=" + locnumber +
            "&surveyor=" + surveyor +
            "&tenant=" + TENANT_URL +
            "&itemnumber=" + i);
    });

    setTimeout(function() {
        killModal('assetBox');
        getAssigned();
        document.getElementById('assetList').innerHTML = '';
        document.getElementById('locationSelector').value = '';
        document.getElementById('clientSelector').value = '';
    }, 300);
});

document.getElementById("btnCancel").addEventListener("click", function() {
    killModal('assetBox');
});
</script>