<?php switch($_REQUEST['view']){
default:?>
<?php break;
case 'new':?>
<script>$('#navBox').html('<li class="breadcrumb-item"><a href="index.php?t=includes/master&p=agent.php&view=list">Agents</a></li><i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i></li><li class="breadcrumb-item active"><a href="javascript:void(0)">New Agent</a></li>')</script>
<form id="agentForm" class="form-horizontal">
<fieldset>
<?php require_once('includes/forms/elements/agent_details.frm');?>
<div class="spacer"></div>
<input id="function" type="hidden" name="FUNCTION" value="insert">
</fieldset>

<div class="col-12 col-md-12 text-center">
<input id="btn_save" class="btn btn-info" type="submit" name="submit" value="Submit" style="display: none" />
</div>
</form>
<script>
$( document ).ready(function() {
$('#footerControls').html('<button class="btn btn-secondary" onclick="triggerAction(\'btn_save\')">SAVE</button>')
})
</script>
<?php 
break;


case 'edit':
$stmt = $GLOBALS['con']->prepare("SELECT * FROM `agents` WHERE `agent_number`=?")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$_REQUEST['agent_number']);$stmt->execute();$r=$stmt->get_result();$stmt->close();
$cd=mysqli_fetch_array($r);?>
<script>$('#navBox').html('<li class="breadcrumb-item"><a href="index.php?t=includes/master&p=agent.php&view=list">Agents</a><i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i></li><li class="breadcrumb-item active"><a href="javascript:void(0)">Edit Agent</a><i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i></li><a href="javascript:void(0)"><?= $cd['agent_name']?></a></li>')</script>
<div class="row mb-4">
<div class="col-12 col-md-12">
<div class="float-end"><a id="phone" href="tel:<?= $cd['phone1']?>" class="btn btn-secondary noprint"><i class="fad fa-phone"></i></a> <a href="index.php?t=includes/master&p=agent.php&agent_number=<?= $_REQUEST['agent_number']?>&view=load" class="btn btn-secondary"><i class="fad fa-info-circle"></i></a> <a href="#" class="btn btn-secondary"><i class="fad fa-envelope"></i></a> <a href="#" class="btn btn-secondary"><i class="fad fa-archive"></i></a></div>
</div>
</div>
<form id="agentForm" class="form-horizontal">
<fieldset>
<?php require_once('includes/forms/elements/agent_details.frm');?>

<div class="spacer clearfix"></div>
<div class="row">
<div class="col-12 col-md-4">
<div class="card">
<div class="card-header">Access</div>
<?php $stmt2 = $GLOBALS['con']->prepare("SELECT AES_DECRYPT(password,email) AS pass FROM `agents` WHERE `agent_number`=?")or die(mysqli_error());
$stmt2->bind_param('i',$_REQUEST['agent_number']);$stmt2->execute();$p=$stmt2->get_result();$stmt2->close();
$data=mysqli_fetch_array($p);
$cd['password']=$data['pass'];
?>
<div id="pcont" class="card-body text-center">
<div class="form-group row">
<div class="col-12 col-md-3"><label class="control-label" for="forename">Password</label></div>
<div class="col-12 col-md-9"><input class="form-control form-control-sm" type="password" name="password" id="password" value="<?= $cd['password']?>" autocomplete="new-password" required  /></div>
</div>

<div class="col-md-12"><div class="pwstrength_viewport_progress"></div></div>
<div class="clearfix"></div>
<div class="row"><div id="messages" class="col-sm-12"></div></div>
</div>

</div></div>


<div class="col-12 col-md-4 text-center">
<div class="card">
<div class="card-header">Active</div>
<div class="card-body">
<div class="form-group row">
<?php if($cd['statuslive']=="1" || $_REQUEST['view']=="new"){$checked="checked";}if($cd['statuslive']=="0"){$checked="";}?>
<label class="col-12 col-md-2 control-label" for="statuslive">Live Agent</label>
<div class="col-12 col-md-9">
<div class="form-check">
<input type="checkbox" class="form-check-input" name="statuslive" id="statuslive" <?= $checked?>>
<label class="form-check-label" for="statuslive"></label>
</div>
</div>
</div>

</div></div></div>

<div class="col-12 col-md-12 text-center">
<div class="form-group text-center">
<input type="hidden" name="agent_number" value="<?= $_REQUEST['agent_number']?>">
<input type="hidden" name="FUNCTION" value="update">
<input id="btn_update" type="submit" name="submit" value="Update" style="display:none" />
</div>
</div>
</fieldset>
</form>
<div class="clearfix"></div>
<div class="spacer"></div>
<script type="text/javascript" src="packages/jqpass/pwstrength-bootstrap.min.js"></script>
<script>
$( document ).ready(function() {
$('#footerControls').html('<button class="btn btn-secondary" onclick="triggerAction(\'btn_update\')">UPDATE</button>')
})
$(document).ready(function () {
"use strict";
var options = {};
options.ui = {
bootstrap4: true,
container: "#pcont",
viewports: {
progress: ".pwstrength_viewport_progress"
},
showVerdictsInsideProgressBar: false
};
options.common = {
debug: true,
onLoad: function () {
$('#messages').text('mixed case , numbers and special chars work best');
}
};
$(':password').pwstrength(options);
});
</script>
<?php

break;

case"load":
$cr=mysqli_query($GLOBALS['con'],"SELECT * FROM `agents` WHERE `agent_number`='".$_REQUEST['agent_number']."'")or die(mysqli_error());
$cd=mysqli_fetch_array($cr);?>
<div id="printbox">
<div class="row mb-4">
<script>$('#navBox').html('<li class="breadcrumb-item"><a href="index.php?t=includes/master&p=agent.php&view=list>Agents</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=agent.php&agent_number=<?= $_REQUEST['agent_number']?>&view=load">View Agent</a><i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i></li><li class="breadcrumb-item active"><a href="javascript:void(0)"><?= $cd['agent_name']?></a></li>')</script>
<div class="col-12 col-md-12">
<div class="float-end"><a id="phone" href="tel:<?= $cd['tel']?>" class="btn btn-secondary noprint"><i class="fad fa-phone"></i></a> <a href="index.php?t=includes/master&p=agent.php&agent_number=<?= $_REQUEST['agent_number']?>&view=edit" class="btn btn-secondary noprint"><i class="fad fa-edit"></i></a> <a href="#" class="btn btn-secondary noprint emailButton" data-values="<?= $cd['agent_number']?>|<?= $cd['pri_contact']?>|<?= $cd['email']?>|<?= $cd['agent_name']?>" data-href="<?=ROOT_URL?>/includes/master/mailer.php"><i class="fad fa-envelope"></i></a> <a id="print" href="#" class="btn btn-secondary noprint"><i class="fad fa-print"></i></a> <a href="#" class="btn btn-secondary noprint"><i class="fad fa-archive"></i></a></div>
</div>
</div>


<div class="col-12 col-md-12">

<div class="col-12 col-md-12">
<div class="card">
<div class="card-header">Agent Details</div>
<div class="card-body">
<div class="col-12 col-md-6">
<div class="row row-striped"><div class="col-12 col-md-3 title">ID</div><div class="col-12 col-md-9"><?= $cd['agent_number']?></div></div>
<div class="row row-striped"><div class="col-12 col-md-3 title">Name</div><div class="col-12 col-md-9"><?= $cd['agent_name']?></div></div>
<div class="row row-striped"><div class="col-12 col-md-3 title">Address1</div><div class="col-12 col-md-9"><?= $cd['address1']?></div></div>
<div class="row row-striped"><div class="col-12 col-md-3 title">Address2</div><div class="col-12 col-md-9"><?= $cd['address2']?></div></div>
<div class="row row-striped"><div class="col-12 col-md-3 title">town</div><div class="col-12 col-md-9"><?= $cd['town']?></div></div>
<div class="row row-striped"><div class="col-12 col-md-3 title">County</div><div class="col-12 col-md-9"><?= $cd['county']?></div></div>
<div class="row row-striped"><div class="col-12 col-md-3 title">Post Code</div><div class="col-12 col-md-9"><?= $cd['post']?></div></div>
</div>
<div class="col-12 col-md-6">
<div class="row row-striped"><div class="col-12 col-md-3 title">Tel</div><div class="col-12 col-md-9"><?= $cd['tel']?></div></div>
<div class="row row-striped"><div class="col-12 col-md-3 title">Fax</div><div class="col-12 col-md-9"><?= $cd['fax']?></div></div>
<div class="row row-striped"><div class="col-12 col-md-3 title">Email</div><div class="col-12 col-md-9"><?= $cd['email']?></div></div>
<div class="row row-striped"><div class="col-12 col-md-3 title">Commission Rate</div><div class="col-12 col-md-9"><?= $cd['commission_rate']?></div></div>
</div>
</div></div></div>

</div>



<div class="clearfix"></div>

<?php break;

case "list":
if($_REQUEST['filter']=="" || $_REQUEST['filter']=="current"){$filter="WHERE `statuslive`='1'";}
if($_REQUEST['filter']=="archive"){$filter="WHERE `statuslive`='0'";}	
$stmt = $GLOBALS['con']->prepare("SELECT `agent_number`,`agent_name`,`pri_contact`,`tel`,`email` FROM `agents` $filter")or die(mysqli_error("GETAGENT: ".$GLOBALS['con']));
$stmt->execute();$cc=$stmt->get_result();$stmt->close();
?>
<script>
$('#navBox').html('<li class="breadcrumb-item"><a href="index.php?t=includes/master&p=agent.php&view=list">Agents</a><i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i></li><li class="breadcrumb-item active">Agent List</li>')</script>

<div class="row">
<div class="col text-end">

<?php if($_REQUEST['filter']=="" || $_REQUEST['filter']=="current"){?>
<div id="exportTitle" style="display:none">
<p style="font-size:14px; font-weight:bold">EnRep Reporting System</p>
<p style="font-size:12px"><a href="">Agent List</a> : <a href="">Active Agents</a></p>
</div>

<div class="col text-end">
<a href="index.php?t=includes/master&p=agent.php&view=new" class="btn btn-secondary btn-sm"><i class="fad fa-plus" aria-hidden="true"></i> New Agent</a>
<a href="index.php?t=includes/master&p=agent.php&view=list&filter=archive" class="btn btn-secondary btn-sm">View Archived Agents</a>
<script>$('#navBox').html('<li class="breadcrumb-item"><a href="index.php?t=includes/master&p=agent.php&view=list">Agents</a><i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i></li><li class="breadcrumb-item"><a href="javascript:void(0)">Agent List</a><i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i></li><li class="breadcrumb-item active">Active Agents</li>')</script>
</div>
<?php }

if($_REQUEST['filter']=="archive"){?>
<div id="exportTitle" style="display:none">
<p style="font-size:14px; font-weight:bold">EnRep Reporting System</p>
<p style="font-size:12px"><a href="">Agent List</a> : <a href="">Archived Agents</a></p>
</div>

<div class="col text-end">
<a href="index.php?t=includes/master&p=agent.php&view=list&filter=current" class="btn btn-secondary btn-sm">View Active Agents</a>
<script>$('#navBox').html('<li class="breadcrumb-item"><a href="index.php?t=includes/master&p=agent.php&view=list">Agents</a><i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i></li><li class="breadcrumb-item"><a href="javascript:void(0)">Agent List</a><i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i></li><li class="breadcrumb-item active">Archived Agents</li>')</script>
</div>
<?php }?>

</div>
</div>
<style>#datatable>tbody>tr>td{vertical-align:middle}</style>
<table id="datatable" class="table table-striped table-sm table-bordered table-hover responsive" width="100%">
<thead>
<tr><th>Name</th><th>Primary Contact</th><th>Phone</th><th>Email</th><th></th></tr>
</thead>
<tbody>
<?php while($cd=mysqli_fetch_array($cc)){?>
<tr>
<td><?= $cd['agent_name']?></td>
<td><?= $cd['pri_contact']?></td>
<td><?= $cd['tel']?></td>
<td><?= $cd['email']?></td>
<td align="right">
<div class="btn-group" role="group">
<?php if($cd['tel']!=""){?><a id="phone" href="tel:<?= $cd['tel']?>" class="btn btn-secondary noprint"><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" data-values="<?= $cd['agent_number']?>|<?= $cd['pri_contact']?>|<?= $cd['email']?>|<?= $cd['agent_name']?>" data-href="includes/master/mailer.php"><i class="fad fa-envelope"></i></a><?php }?>
<a href="index.php?t=includes/master&p=agent.php&agent_number=<?= $cd['agent_number']?>&view=load" class="btn btn-secondary noprint"><i class="fad fa-info-circle"></i></a>
<a href="index.php?t=includes/master&p=agent.php&agent_number=<?= $cd['agent_number']?>&agent_name=<?= $cd['agent_name']?>&view=clientlist" class="btn btn-secondary noprint"><i class="fad fa-th-list"></i></a>
<a href="index.php?t=includes/finance&amp;p=agent.php&amp;&agent_number=<?= $cd['agent_number']?>" class="btn btn-secondary noprint ml-3" data-mdb-tooltip-init data-mdb-placement="top" data-mdb-trigger="hover" title="Agent Finances"><i class="fad fa-file-chart-line"></i>

</a>
</div>
</td>
</tr>
<?php }?>
</tbody>
</table>

<div class="clearfix"></div>
<?php 
$table="datatable";
require_once('includes/single_table.engine');?>
<?php break;

case "clientlist":

$stmt = $GLOBALS['con']->prepare("SELECT `clientnumber`,`clientname`,`address1`,`address2`,`town`,`county`,`postcode` FROM `clientdetails` WHERE `agent_number`='".$_REQUEST['agent_number']."' AND `statuslive`='1'")or die(mysqli_error($GLOBALS['con']));
$stmt->execute();$cc=$stmt->get_result();$stmt->close();
?>
<script>
$('#navBox').html('<li class="breadcrumb-item"><a href="index.php?t=includes/master&p=agent.php&view=list">Agents</a><i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i></li><li class="breadcrumb-item active">Agent List</li>')</script>

<div class="row">
<div class="col text-end">

<div id="exportTitle" style="display:none">
<p style="font-size:14px; font-weight:bold">EnRep Reporting System</p>
<p style="font-size:12px"><a href="">Agent Client List</a> : <?= $_REQUEST['agent_name']?></p>
</div>

</div>
</div>


<style>#datatable>tbody>tr>td{vertical-align:middle}</style>

<table id="datatable" class="table table-striped table-sm table-bordered table-hover responsive" width="100%">
<thead>
<tr><th>Due Date</th><th>Client Name</th><th>Address 1</th><th>Address 2</th><th>Town</th><th>County</th><th>Post Code</th></tr>
</thead>
<tbody>
<?php while($cd=mysqli_fetch_array($cc)){
$stmt2 = $GLOBALS['con']->prepare("SELECT `duedate` FROM `itemdetails` WHERE `clientnumber`='".$cd['clientnumber']."' AND `statuslive`='1' AND `duedate`!='' ORDER BY abs(duedate) DESC LIMIT 1")or die(mysqli_error($GLOBALS['con']));
$stmt2->execute();$ics=$stmt2->get_result();$stmt2->close();
$lid=mysqli_fetch_array($ics);
?>
<tr>
<td><?php if($lid['duedate']!=""){?><span style="display:none"><?= date('Y-m-d',$lid['duedate'])?></span><?= date('d-m-Y',$lid['duedate']); }?></td>
<td><?= $cd['clientname']?></td>
<td><?= $cd['address1']?></td>
<td><?= $cd['address2']?></td>
<td><?= $cd['town']?></td>
<td><?= $cd['county']?></td>
<td><?= $cd['postcode']?></td>
</tr>
<?php }?>
</tbody>
</table>

<div class="clearfix"></div>
<?php 
$table="datatable";
require_once('includes/single_table.engine');?>
<?php break;



}
?>
<script>
function inlineFilter(i){
var table = $('#datatable').DataTable();
table.search(i).draw();   
};


$("#agentForm").submit(function(event){
event.preventDefault();
$.ajax({
url:'includes/master/agent_functions.php',
type:'POST',
data:$('#agentForm').serialize(),
success:function(result){debug(result)
if($('#function').val()=="insert"){
document.location.href='index.php?t=includes/master&p=agent.php&agent_number='+result+'&view=edit'
}
else{
if($.trim(result)=="success"){showResponse('success','Your changes have been saved successfully');}
else{showResponse('danger','There was a problem, please check your details and try again<br><br>'+result)}
}}
});
});
</script>

