<?php 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();
?>

<div class="row mb-3">
    <div class="col-12 col-md-3 order-md-2 order-1 text-end mb-3">

        <?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 me-2"><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"><i class="fad fa-archive" aria-hidden="true"></i> View Archived Agents</a>
            <script>
            document.getElementById('navBox').innerHTML =
                '<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"><i class="fad fa-wave-pulse"></i> View Active Agents</a>
            <script>
            document.getElementById('navBox').innerHTML =
                '<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 class="col-12 col-md-9 order-md-1 order-2 mb-3">
        <div class="note note-secondary mb-3">
            <strong>Agents:</strong> Manage your registered agents, access their details for editing or contact them directly via the <b>EnRep</b> system.
        </div>
    </div>
</div>


<div id="loadingObject">
    <h1 class="text-center"><i class="fad fa-flux-capacitor fa-spin fa-3x fa-fw" style="--fa-primary-color: #0055A6; --fa-secondary-color: gray;"></i><br>Loading Agents <br><small>Please wait</small><span
              class="sr-only">Loading...</span>
    </h1>
</div>


<div class="row">
    <div class="col-12">
        <table id="datatable" class="table table-striped table-sm table-bordered table-hover responsive" width="100%" style="display: none" ;>
            <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)){
             $email_values = [
                0 => '0', //client
                1 => $cd['agent_name'], //type
                2 => $cd['email'], //recipient
                3 => '', //subject
                4 => '', //message
                5 => '', //attachment
                6 => '' //source
            ];
            $email64_values = base64_encode(json_encode($email_values));
            ?>
                <tr id="R<?= $cd['agent_number']?>">
                    <td><?= $cd['agent_name']?></td>
                    <td><?= $cd['pri_contact']?></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 me-2 waves-effect waves-light 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 me-2 waves-effect waves-light noprint emailButton"
                               data-values="<?= $email64_values ?>" data-href="<?=ROOT_URL?>/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=edit" class="btn btn-secondary me-2 waves-effect waves-light 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 me-2 waves-effect waves-light noprint"><i
                                   class="fad fa-th-list"></i></a>

                            <a href="index.php?t=includes/finance&p=agent.php&agent_number=<?= $cd['agent_number']?>" class="btn btn-secondary ml-2 waves-effect waves-light noprint" data-mdb-tooltip-init data-mdb-placement="top"
                               data-mdb-trigger="hover" title="Agent Finances"><i class="fad fa-file-chart-line"></i></a>

                            <a href="javascript:void(0)" onclick="convert('<?= $cd['agent_number']?>')" class="btn btn-secondary ml-2 waves-effect waves-light noprint" data-mdb-tooltip-init data-mdb-placement="top" data-mdb-trigger="hover"
                               title="Convert to Broker"><i class="fad fa-shuffle"></i></a>
                        </div>
                    </td>
                </tr>
                <?php }?>
            </tbody>
        </table>

    </div>
</div>
<?php 
$table="datatable";

        $dateTarget="";
        $sortTarget="1";
        $length="25";
        $sortCell="nth-child(2)";
        $logo="yes";
        $scroller="";
        require (ROOT_URL."/includes/tables/single_table.php");
        ?>