<?php 
function listClients($c, $n, $t){
    
    switch($t){
    default:break;
    
    case "select":
    $ca=array();
    $stmt = $GLOBALS['con']->prepare("SELECT `clientnumber`,`clientname` FROM `clientdetails` WHERE `statuslive`='1'  ORDER BY `clientname` ASC")or die(mysqli_error("listClients A: ".$GLOBALS['con']));
    $stmt->execute();$cc=$stmt->get_result();$stmt->close();	
    while($pd=mysqli_fetch_array($cc)){$ca[]=$pd;}
    $ret='<select class="form-control form-control-sm autocomplete" id="'.$n.'" name="'.$n.'" onchange="confirmAssign()">'; 
    $ret.='<option value="NULL">Select Client</option>';//.PHP_EOL;	
    foreach($ca as $px){
    $name=$px['clientname'];
    if($c==$px['clientnumber']){$s="SELECTED";}else{$s="";}
    $ret.='<option value="'.$px['clientnumber'].'|'.$px['clientname'].'" '.$s.'>'.$name.'</option>';//.PHP_EOL;
    }
    $ret.='</select>';
    return($ret.$error);
    break;
    
    case "name":
    $stmt = $GLOBALS['con']->prepare("SELECT `clientnumber`,`clientname` FROM `clientdetails` WHERE `statuslive`='1'  ORDER BY `clientname` ASC")or die(mysqli_error("listClients B: ".$GLOBALS['con']));
    $stmt->execute();$cc=$stmt->get_result();$stmt->close();	
    while($pd=mysqli_fetch_array($cc)){$ca[]=$pd;}
    $ret='<select class="form-control form-control-sm" id="'.$n.'" name="'.$n.'">';
    $ret.='<option value="NULL">Select Client</option>';//.PHP_EOL;	
    foreach($ca as $px){
    $name=$px['clientname'];
    if($c==$name){$s="SELECTED";}else{$s="";}
    $ret.='<option value="'.$name.'" '.$s.'>'.$name.'</option>';//.PHP_EOL;
    }
    $ret.='</select>';
    return($ret.$error);
    break;
    }
}

    
switch($_REQUEST['view']){

default:
$cc=mysqli_query($GLOBALS['con'],"SELECT * FROM `contractors` WHERE `statuslive`='1' $filter")or die(mysqli_error($GLOBALS['con']));
?>

<div class="row"><div class="col-12"><p class="blockquote">Select contractor to view currrent status and edit assignments</small></p></div></div>
<table id="datatable" class="table table-striped table-sm table-bordered table-hover responsive nowrap" width="100%">
<thead>
<tr><th>Contractor Name</th><th>Phone</th><th>Email</th><th class="no-sort noExport"></th></tr>
</thead>
<tbody>
<?php while($cd=mysqli_fetch_array($cc)){?>
<tr>
<td><?= $cd['contractor_name']?></td>
<td><?= $cd['tel']?></td>
<td><?= $cd['email']?></td>
<td class="text-end">
<div class="btn-group" role="group">
<?php if($cd['tel']!=""){?>
<a id="phone" href="tel:<?= $cd['tel']?>" class="btn btn-secondary noprint me-2"><i class="fad fa-phone"></i></a>
<?php }?>

<?php if(!filter_var($cd['email'], FILTER_VALIDATE_EMAIL) === false) {?>
<a href="#" class="btn btn-secondary noprint emailButton me-2" data-values="<?= $cd['id']?>|<?= $cd['forename']?> <?= $cd['surname']?>|<?= $cd['email']?>|<?= $cd['clientname']?>" data-href="<?=ROOT_URL?>/includes/master/mailer.php"><i class="fad fa-envelope"></i></a>
<?php }?>

<a href="index.php?t=includes/master&p=contractor_assign.php&id=<?= $cd['contractor_number']?>&view=edit" class="btn btn-secondary noprint"><i class="fad fa-info-circle"></i></a>
</div>
</td>
</tr>
<?php }?>
</tbody>
</table>
<?php 
$table="datatable";
require_once('includes/single_table.engine');?>

<script>$('#navBox').html('<li class="breadcrumb-item active">Contractors: Assign</li>')</script>

<?php break;

case "edit":
$contractorCall=mysqli_query($GLOBALS['con'],"SELECT * FROM `contractors` WHERE `contractor_number`='".$_REQUEST['id']."' $filter")or die(mysqli_error($GLOBALS['con']));
$contractor_data=mysqli_fetch_array($contractorCall);

$cc=mysqli_query($GLOBALS['con'],"SELECT * FROM `clientdetails` WHERE `contractor`='".$_REQUEST['id']."' $filter")or die(mysqli_error($GLOBALS['con']));
?>
<div class="row">
<div id="exportTitle" class="col text-end">
<?= listClients('','clientList','select')?>
</div>
</div>
<table id="datatable" class="table table-striped table-sm table-bordered table-hover responsive nowrap" width="100%">
<thead>
<tr><th>Client</th><th class="no-sort noExport"></th></tr>
</thead>
<tbody>
<?php while($cd=mysqli_fetch_array($cc)){?>
<tr id="R<?= $cd['clientnumber']?>">
<td><?= $cd['clientname']?></td>
<td class="text-end">
<div class="btn-group" role="group">
<a href="javascript:void(0)" class="btn btn-danger noprint" onclick="confirmRemove('<?= $cd['clientnumber']?>')"><i class="fad fa-trash"></i></a>
</div>
</td>
</tr>
<?php }?>
</tbody>
</table>
<?php 
$table="datatable";
require_once('includes/single_table.engine');?>

<script>$('#navBox').html('<li class="breadcrumb-item"><a href="index.php?t=includes/master&p=client.php&view=list">Contractors: Assign</a><i class="fad fa-angle-double-right mx-2 mt-1" aria-hidden="true"></i></li><li class="breadcrumb-item active">Managing:&nbsp;<strong><?= $contractor_data['contractor_name']?></strong></li>')</script>
<?php break;
}?>

<script>

function confirmRemove(i){
confirmBox('Unassign Client','Are you sure you wish to unassign this client from this contractor?','removeClient('+i+')');
}

function removeClient(i){
var activeContractor='<?= $_REQUEST['id']?>';
$.post('includes/master/contractor_functions.php',{FUNCTION:'remove',clientnumber:i,contractor_number:activeContractor},function(result){
if($.trim(result)=="success"){
$('#confirmBox').modal('hide')
$('#R'+i).fadeOut() 
showResponse('success','Contractor removed from client successfully')   
}
})
}

function confirmAssign(){
var selectedClient=$('#clientList').val();
var clientData=selectedClient.split('|');
confirmBox('Assign Client','Are you sure you wish to assign this client to this contractor?','assignClient('+clientData[0]+',\''+clientData[1]+'\')');
}

function assignClient(i,n){
var activeContractor='<?= $_REQUEST['id']?>';
$.post('includes/master/contractor_functions.php',{FUNCTION:'add',clientnumber:i,contractor_number:activeContractor},function(result){
if($.trim(result)=="success"){
$('#confirmBox').modal('hide')
$('#datatable tbody').append('<tr id="R'+i+'"><td>'+n+'</td><td class="text-end"><div class="btn-group" role="group"><a href="javascript:void(0)" class="btn btn-danger noprint" onclick="confirmRemove(\''+i+'\')"><i class="fad fa-trash"></i></a></div></td></tr>')    
showResponse('success','Contractor added to client successfully')   
}

})
}
</script>

