  <?php 
$r=mysqli_query($GLOBALS['con'],"SELECT * FROM `locations` WHERE `locnumber`='".$_REQUEST['locnumber']."' AND `statuslive`='1'");
    $rData=mysqli_fetch_array($r);
$cr=mysqli_query($GLOBALS['con'],"SELECT `itemnumber`,`desc`,`plantnumber`,`serialnumber` FROM `itemdetails` WHERE `locnumber`='".$_REQUEST['locnumber']."' AND `statuslive`='1'")or die(mysqli_error($GLOBALS['con']));
$assetList=array();
$ic=mysqli_num_rows($cr);
while($cd=mysqli_fetch_array($cr)){$assetList[]=$cd;}
if($ic==0){?>
  <div class="container text-center">
      <div class="row">
          <div class="col-12 text-center">
              <h2 class="text-center">You have 0 Assets at this location <strong>[<?= $rData['locname']?>]</strong></h2>
              <a class="btn btn-info mt-5 me-4" href="javascript:void(0)" onclick="confirmRemove()">Remove location</a>
              <a class="btn btn-secondary mt-5 ml-5" href="?t=includes/master&p=locations.php&clientnumber=<?= $_REQUEST['clientnumber']?>&clientname=<?= $_REQUEST['clientname']?>"><i class="fad fa-chevrons-left"></i> Back</a>
          </div>
      </div>
  </div>
  <script>
function confirmRemove() {
    var dest = '<p>Are you sure you wish to remove this location?</p>'
    loadModalContent(dest, "popupBox", "modal-md", 1060, "true", "Remove location", "cancelConfirm", clearLoc);

}

function clearLoc() {
    // Hide the modal
    var confirmBox = document.getElementById('confirmBox');
    killModal('popupBox')

    var oldloc = '<?= $_REQUEST['locnumber']?>';
    var client = '<?= $_REQUEST['clientnumber']?>';

    var xhr = new XMLHttpRequest();
    xhr.open('POST', ROOT_URL + '/includes/locations/location_functions.php', true);
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    xhr.onload = function() {
        if (xhr.status === 200) {
            var ret = xhr.showResponseText;
            // Check if the showResponse is "s"
            
                window.location.href = 'index.php?t=includes/master&p=locations.php&clientnumber=<?= $_REQUEST['clientnumber'] ?>&clientname=<?= $_REQUEST['clientname'] ?>';
            
        }
    };

    // Sending the POST request with parameters
    xhr.send('FUNCTION=dellocEmpty&clientnumber=' + encodeURIComponent(client) + '&oldloc=' + encodeURIComponent(oldloc) + '&tenant=' + encodeURIComponent(TENANT_URL));
}

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">Remove Location</li>'

})
  </script>



  <?php }
else{
    
    
    $clientList='';
    $stmt = $GLOBALS['con']->prepare("SELECT `clientnumber`, `clientname` FROM clientdetails WHERE statuslive='1' ORDER BY `clientname` ASC")or die(mysqli_error($GLOBALS['con']));
    $stmt->execute();$cc=$stmt->get_result();$stmt->close();
    while($cn=mysqli_fetch_array($cc)){
        $cn['clientname']=preg_replace("/[^A-Za-z0-9 ]/", ' ', $cn['clientname']);
        $clientList.='<option value="'.$cn['clientnumber'].'" data-clientName="'.$cn['clientname'].'">'.$cn['clientname'].'</option>';
    }
   
    ?>
  <div class="row mb-4">
      <div class="note note-secondary"><strong>Location Management:</strong> This system gives you the option move assets to another location en-mass, move items to another location and delete the current location, archive the assets and
          location for later retrieval or move all assets and location to another client.</div>
  </div>
  <h4 class="text-center">You have <strong><?= $ic?></strong> Assets at this location <strong>[<?= $rData['locname']?>]</strong></h4>

  <div style="max-height:250px; overflow:auto; position:relative">
      <table class="table table-striped table-sm">
          <thead class="table-dark">
              <tr>
                  <th width="33%">Description</th>
                  <th width="33%">Plant Number</th>
                  <th width="33%">Serial Number</th>
              </tr>
          </thead>
          <tbody>
              <?php foreach($assetList as $cd){?>
              <tr>
                  <td width="33%"><?= $cd['desc']?></td>
                  <td width="33%"><?= $cd['plantnumber']?></td>
                  <td width="33%"><?= $cd['serialnumber']?></td>
              </tr>
              <?php }?>
          </tbody>
      </table>
  </div>
  <?php $r=mysqli_query($GLOBALS['con'],"SELECT * FROM `locations` WHERE `clientnumber`='".$_REQUEST['clientnumber']."'");?>
  <div class="alert-dark mt-3 mb-3 p-2">
      <div class="row ">
          <div class="col-12 col-md-3 mt-1">Select a new location for the items</div>
          <div class="col-12 col-md-3">
              <select id="newloc" class="form-control">
                  <option value="NULL">SELECT</option>
                  <?php while($cd=mysqli_fetch_array($r)){?>
                  <?php if($cd['locnumber']!=$_REQUEST['locnumber']){?><option value="<?= $cd['locnumber']?>"><?= $cd['locname']?></option><?php }?>
                  <?php }?>
              </select>
          </div>
          <div class="col-12 col-md-3 text-center">
              <a class="btn btn-secondary" onclick="confirmAction('move')">Move all assets</a>
          </div>
          <div class="col-12 col-md-3 text-center">
              <a class="btn btn-secondary" onclick="confirmAction('delete')">Move assets and Delete</a>
          </div>
      </div>
  </div>

  <div class="row mt-5 mb-5">
      <div class="col-12 col-md-6 text-center"><a class="btn btn-secondary" onclick="confirmArchive()">Archive location and assets</a></div>
      <div class="col-12 col-md-6 text-center"><a class="btn btn-danger" onclick="confirmTransfer()"><i class="fad fa-arrows-left-right-to-line"></i> Move assets to new client</a></div>
  </div>

  <div id="detailsBox" class="hidden"></div>
  <script>
function confirmAction(i) {
    newloc = $('#newloc').val();
    if (newloc == "NULL") {
        showResponse("warning", "Please select a location before proceeding");
    } else {
        switch (i) {
            case "move":
                var dest = '<p>Are you sure you wish to relocate all assets listed to the selected location?</p>'
                loadModalContent(dest, "popupBox", "modal-md", 1060, "true", "Confirm Move all items", "cancelConfirm", moveAssets);
                break;
            case "delete":
                var dest = '<p>Are you sure you wish to remove this location and relocate all assets listed to the selected location?</p>'
                loadModalContent(dest, "popupBox", "modal-md", 1060, "true", "Confirm Move all items and Delete Location", "cancelConfirm", deleteLoc);

                break;
        }
    }
}


function confirmChange() {
    var dest = '<p>Are you sure you wish to remove this location and relocate all items listed to the selected location?</p>'
    loadModalContent(dest, "popupBox", "modal-md", 1060, "true", "Confirm Delete Location", "cancelConfirm", makeChange);
}

function confirmNone() {
    var dest = '<p>Are you sure you wish to remove this location?</p>'
    loadModalContent(dest, "popupBox", "modal-md", 1060, "true", "Confirm Delete Location", "cancelConfirm", clearLoc);
}


function confirmArchive() {
    var dest = '<p>Are you sure you wish to archive this location and all assets listed at this location?</p>'
    loadModalContent(dest, "popupBox", "modal-md", 1060, "true", "Confirm Archive Location & Assets", "cancelConfirm", archiveLoc);

}

function confirmTransfer() {

    var dest =
        '<p>Are you sure you wish to move this location and relocate all assets listed to the selected client?<br><small class="text-danger">THIS WILL MOVE ALL ASSETS ASSOCIATED WITH THIS LOCATION TO THE SELECTED CLIENT</small></p>' +
        '<select id="newCLient" class="autocomplete mt-3"><option value="">SELECT CLIENT</option><?= $clientList?></select>' +
        '<br>' +
        '<div class="form-check">' +
        '<input type="checkbox" class="form-check-input" id="switchClient"><label class="form-check-label" for="switchClient">Switch to selected client when complete</label>' +
        '</div>'
    loadModalContent(dest, "popupBox", "modal-lg", 1060, "true", "Confirm Transfer Location and Assets", "cancelConfirm", makeChange);
    resetAC()
}

function moveAssets() {
    killModal('popupBox')
    var newloc = $('#newloc').val();
    var oldloc = '<?= $_REQUEST['locnumber']?>';
    var client = '<?= $_REQUEST['clientnumber']?>';
    $.post(ROOT_URL + '/includes/locations/location_functions.php', {
        FUNCTION: 'moveAssets',
        clientnumber: client,
        oldloc: oldloc,
        newloc: newloc,
        tenant: TENANT_URL
    }, function(ret) {
        //debug(ret)
        if ($.trim(ret) == "s") {
            document.location.href = 'index.php?t=includes/master&p=locations.php&clientnumber=<?= $_REQUEST['clientnumber']?>&clientname=<?= $_REQUEST['clientname']?>'
        }
    })
}

function deleteLoc() {
    killModal('popupBox')
    var newloc = $('#newloc').val();
    var oldloc = '<?= $_REQUEST['locnumber']?>';
    var client = '<?= $_REQUEST['clientnumber']?>';
    $.post(ROOT_URL + '/includes/locations/location_functions.php', {
        FUNCTION: 'delloc',
        clientnumber: client,
        oldloc: oldloc,
        newloc: newloc,
        tenant: TENANT_URL
    }, function(ret) {
        //debug(ret)
        if ($.trim(ret) == "s") {
            document.location.href = 'index.php?t=includes/master&p=locations.php&clientnumber=<?= $_REQUEST['clientnumber']?>&clientname=<?= $_REQUEST['clientname']?>'
        }
    })
}

function archiveLoc() {
    killModal('popupBox')
    var newloc = $('#newloc').val();
    var oldloc = '<?= $_REQUEST['locnumber']?>';
    var client = '<?= $_REQUEST['clientnumber']?>';
    $.post(ROOT_URL + '/includes/locations/location_functions.php', {
        FUNCTION: 'archiveLoc',
        clientnumber: client,
        locnumber: oldloc,
        tenant: TENANT_URL
    }, function(ret) {
        //debug(ret)
        if ($.trim(ret) == "s") {
            document.location.href = 'index.php?t=includes/master&p=locations.php&clientnumber=<?= $_REQUEST['clientnumber']?>&clientname=<?= $_REQUEST['clientname']?>'
        }
    })
}

function makeChange() {
    killModal('popupBox')
    var newClient = $('#newCLient').val();
    var client = '<?= $_REQUEST['clientnumber']?>';
    var locnumber = '<?= $_REQUEST['locnumber']?>';
    var clientName = $('#newCLient').find(':selected').attr('data-clientName')
    $.post(ROOT_URL + '/includes/locations/location_functions.php', {
        FUNCTION: 'moveClient',
        clientnumber: client,
        locnumber: locnumber,
        newClient: newClient,
        tenant: TENANT_URL
    }, function(ret) {
        //debug(ret)
        if ($.trim(ret) == "s") {
            if ($('#switchClient').is(':checked')) {
                document.location.href = 'index.php?t=includes/master&p=locations.php&clientnumber=' + newClient + '&clientname=' + clientName;
            } else {
                document.location.href = 'index.php?t=includes/master&p=locations.php&clientnumber=<?= $_REQUEST['clientnumber']?>&clientname=<?= $_REQUEST['clientname']?>';
            }
        }
    })
}



function remove(c, l) {
    alert(c)
}

$('#navBox').html(
    '<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=<?= $_REQUEST['clientname']?>&view=asset"><?= $_REQUEST['clientname']?></a><i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i><a href="index.php?t=includes/master&p=locations.php&clientnumber=<?= $_REQUEST['clientnumber']?>&clientname=<?= $_REQUEST['clientname']?>">Locations</a></li><i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i></li><li class="breadcrumb-item active"><strong>Manage</strong> Location</li>'
);
  </script>
  <?php }?>