<?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']);

switch($_REQUEST['mode']){
default:?>
<div class="row">
    <div class="form-group col-12">
        <p><strong>Current location:</strong> <?= getLocation($d[1])?></p>
        <div id="transferRow" >
        <?= listClientLocations($d[0],$d[1],$d[2],$d[3])?>
    </div></div>
</div>

<?php break;


case "confirm":?>

<div class="row">
    <div class="col-12 text-center">
        <h1>Confirm moving asset from<br></h1>
        <span class="lead"><?= getLocation($d[1])?></span>&nbsp;&nbsp;&nbsp;<i class="fa fa-angle-double-right fa-2x" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;<span class="lead">
            <?= getLocation($d[2])?></span>
    </div>
    <div class="col-12"> <br></div>
    <div class="col-6 text-center"><a id="btnConfirm" 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>
<?php break;

case "update":
$r=mysqli_query($GLOBALS['con'],"UPDATE `itemdetails` SET `locnumber`='".$_REQUEST['itemlocationNew']."' WHERE `locnumber`='".$_REQUEST['itemlocationOld']."' AND `itemnumber`='".$_REQUEST['itemnumber']."'");
$e=mysqli_error($GLOBALS['con']);
if(empty($e)){
//genSchedule($_REQUEST['clientnumber']);
echo "done";
exit;	
}

break;


case "mass";
$items=explode(",",$_REQUEST['items']);
$iCount=count($items);
?>
<p class="lead">You have chosen to mass relocate the following <b><?= $iCount?></b> items</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 border borer-info"><?= $item?> - <?= getItemDetails($item,'name')?></div>
    </div>

    <?php }?>
    <input id="itemList" type="hidden" value="<?= $_REQUEST['items']?>">
</div>
<div class="row p-3">
    <div class="col-12">
        <span class="lead">Select a location to relocate the items to</span>
    </div>
    <div class="col-12 mt-3 mb-4">
        <?= listClientLocationsMass($_REQUEST['clientnumber'])?>
    </div>
</div>
<script>
$('#popupBox .modal-header').html('<a href="javascript:void(0)" class="sa">Mass Asset Transfer</a><div class="col-md-7 pull-right text-end" style="margin-top:30px"><h6>[ <?= $d[3]?> ]</h6></div></div>');
</script>
<?php break;


case "massConfirm":?>
<?= $_REQUEST['data'];?>
<script>
$('#popupBox .modal-header').html('<a href="javascript:void(0)" class="sa">Mass Asset Transfer</a><div class="col-md-7 pull-right text-end" style="margin-top:30px"><h6>[ <?= $d[4]?> ]</h6></div></div>');
</script>
<div class="row">
    <div class="col-12 text-center">
        <h1>Confirm moving assets to<br></h1>
        <h1 class="badge badge-pill badge-primary"><span class="lead"><?= getLocation($_REQUEST['dest'])?></span></h1>
    </div>
    <div class="col-12"> <br></div>
    <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>
    <input id="itemList" type="hidden" value="<?= $_REQUEST['items']?>">
    <input id="itemDest" type="hidden" value="<?= $_REQUEST['dest']?>">
</div>
<?php break;


case "updateMass":
$r=mysqli_query($GLOBALS['con'],"UPDATE `itemdetails` SET `locnumber`='".$_REQUEST['itemlocationNew']."' WHERE `itemnumber`='".$_REQUEST['itemnumber']."'");
$e=mysqli_error($GLOBALS['con']);
if(empty($e)){
//genSchedule($_REQUEST['clientnumber']);
echo "done";
exit;	
}

break;
}?>

<div id="erm" class="text-center"></div>
<script>
$(".singleTransferOption").click(function() {
    killModal('popupBox');
    var target = ROOT_URL + '/includes/master/transfer.php';
    var string = $(this).data('string');
    var dest = encodeURI(target + "?mode=confirm&data=" + string + "&clientnumber=<?= $d[0]?>&tenant=" + TENANT_URL)
    loadModalContent(dest, "popupBox", "modal-xl", 1060, "true", 'Transfer Single Item');
})

$(".transferOption").change(function() {
    killModal('popupBox');
    var target = ROOT_URL + '/includes/master/transfer.php';
    var string = $(this).find(':selected').data('string');
    var dest = encodeURI(target + "?mode=confirm&data=" + string + "&clientnumber=<?= $d[0]?>&tenant=" + TENANT_URL)
    loadModalContent(dest, "popupBox", "modal-xl", 1060, "true", 'Transfer Single Item');
})

$(".massTransferOption").change(function() {
    killModal('popupBox');
    var target = ROOT_URL + '/includes/master/transfer.php';
    var dest = $(this).find(':selected').data('string');
    var items = $('#itemList').val();
    var dest = encodeURI(target + "?mode=massConfirm&items=" + items + "&dest=" + dest + "&clientnumber=<?= $_REQUEST['clientnumber']?>&tenant=" + TENANT_URL)
    loadModalContent(dest, "popupBox", "modal-xl", 1060, "true", 'Transfer Single Item');
})

$(".massTransferSingle").click(function() {
    killModal('popupBox');
    var target = ROOT_URL + '/includes/master/transfer.php';
    var dest = $(this).data('string');
    var items = $('#itemList').val();
    var dest = encodeURI(target + "?mode=massConfirm&items=" + items + "&dest=" + dest + "&clientnumber=<?= $_REQUEST['clientnumber']?>&tenant=" + TENANT_URL)
    loadModalContent(dest, "popupBox", "modal-xl", 1060, "true", 'Transfer Single Item');
})




$("#btnCancel").click(function() {
    $('#popupBox').modal('hide');
})

$("#btnConfirm").click(function() {

    $.post(ROOT_URL + '/includes/master/transfer.php', {
        mode: "update",
        clientnumber: "<?= $_REQUEST['clientnumber']?>",
        itemlocationOld: "<?= $d['1']?>",
        itemlocationNew: "<?= $d['2']?>",
        itemnumber: "<?= $d['3']?>",
        tenant: TENANT_URL
    }, function(data) {
        if ($.trim(data) == "error") {
            $("#erm").fadeIn(1000);
            $("#erm").addClass('alert-danger');
            $("#erm").html('<p>There has been an error</p>');
            $("#erm").fadeOut(8000)
        }
        if ($.trim(data) == "done") {
            $("#erm").fadeIn(1000);
            $("#erm").removeClass('alert-danger');
            $("#erm").addClass('alert-success');
            $("#erm").html('<p>Item moved successfully</p>');
            setTimeout(function() {
                location.reload();
            }, 300)
        }
    })

})

$("#btnMassConfirm").click(function() {
    var items = $('#itemList').val();
    var dest = $('#itemDest').val();
    var arr = new Array();
    arr = items.split(",")
    $.each(arr, function(index, value) {
        var i = value.replace('C', '')

        $.post(ROOT_URL + '/includes/master/transfer.php', {
            mode: "updateMass",
            clientnumber: "<?= $_REQUEST['clientnumber']?>",
            itemlocationNew: dest,
            itemnumber: i,
            tenant: TENANT_URL
        }, function(data) {})

    })
    setTimeout(function() {
        location.reload();
    }, 300)
})

var mySelect = new mdb.Select(document.getElementById('transferSelect'), {
    filter: true,
    container: "#transferBox"
});
</script>