<?php if(!defined('PARENT')) { exit; }
$Q  = $DB->db_query("SELECT SQL_CALC_FOUND_ROWS *,
      (SELECT ROUND(SUM(`commtotal`),2) FROM `" . DB_PREFIX . "affiliates_commissions` WHERE `" . DB_PREFIX . "affiliates_commissions`.`affid` = `" . DB_PREFIX . "affiliates`.`id` AND `" . DB_PREFIX . "affiliates_commissions`.`en` = 'yes' AND `" . DB_PREFIX . "affiliates_commissions`.`approved` = 'yes') AS `affCommissions`,
      (SELECT ROUND(SUM(`payamount`),2) FROM `" . DB_PREFIX . "affiliates_payments` WHERE `" . DB_PREFIX . "affiliates_payments`.`affid` = `" . DB_PREFIX . "affiliates`.`id`) AS `affPayments`,
      (SELECT SUM(`count`) FROM `" . DB_PREFIX . "affiliates_tracking` WHERE `" . DB_PREFIX . "affiliates_tracking`.`affid` = `" . DB_PREFIX . "affiliates`.`id`) AS `affReferrals`
      FROM `" . DB_PREFIX . "affiliates`
      WHERE `approved` = 'yes'
      ORDER BY `fname`,`lname`
      LIMIT $limit," . PER_PAGE);
$NR = $DB->db_foundrows($Q);
?>

    <div class="row">
      <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 text-right">
        <div class="btn-group">
          <button type="button" class="btn btn-sm btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            <i class="fa fa-chevron-down fa-fw"></i>
          </button>
          <ul class="dropdown-menu dropdown-menu-right">
            <li><a href="?p=aff-add"><i class="fa fa-plus fa-fw"></i> <?php echo $msw_adm_aff_manage2; ?></a></li>
            <?php
            if ($NR > 0) {
            ?>
            <li><a href="#" onclick="mswExp('<?php echo (isset($_GET['q']) && $_GET['q'] ? '&amp;q=' . urlencode(mswSH($_GET['q'])) : ''); ?>','aff-export');return false"><i class="fa fa-save fa-fw"></i> <?php echo $msw_adm_aff_manage18; ?></a></li>
            <li><a href="?p=aff-search"><i class="fa fa-search fa-fw"></i> <?php echo $msw_adm_aff_manage6; ?></a></li>
            <?php
            }
            ?>
          </ul>
        </div>
      </div>
    </div>

    <div class="managepanel" style="margin-top:10px">
      <form method="post" action="?p=<?php echo $cmd; ?>">
      <div class="panel panel-default managearea">
        <div class="panel-heading">
          <?php echo $msw_adm_aff_manage; ?> (<?php echo mswNFM($NR); ?>)
        </div>
        <div class="panel-body">
          <div class="table-responsive">
           <table class="table table-striped table-hover" id="sortTable">
             <thead>
               <tr>
                 <th class="text-center"><input type="checkbox" name="c" value="yes" onclick="mswSC('.managearea .table td','delb')"></th>
                 <th data-sort="string"><?php echo $msw_adm_aff_manage15; ?></th>
                 <th data-sort="float"><?php echo $msw_adm_aff_manage12; ?></th>
                 <th data-sort="float"><?php echo $msw_adm_aff_manage13; ?></th>
                 <th data-sort="float"><?php echo $msw_adm_aff_manage21; ?></th>
                 <th data-sort="int"><?php echo $msw_adm_aff_manage14; ?></th>
                 <th data-sort="string"><?php echo $msw_common18; ?></th>
                 <th>&nbsp;</th>
               </tr>
             </thead>
             <tbody>
               <?php
               if ($NR > 0) {
                 while ($AF = $DB->db_object($Q)) {
                   $due = mswFPCE($AF->affCommissions - $AF->affPayments);
                   if ($due < 1) {
                     $due = '0.00';
                   }
                   include(PATH . 'content/list-screens/aff-manage.php');
                 }
               } else {
                 $colspan = 8;
                 include(PATH . 'content/no-data.php');
               }
               ?>
             </tbody>
           </table>
         </div>
         <?php
         // Pagination..
         if ($NR > PER_PAGE) {
           $PGS = new pages(array(
             'count' => $NR,
             'text' => $msw_pages,
             'page' => $page,
             'admin' => 'yes',
             'flag' => '',
             'limit' => PER_PAGE,
             's' => $SETTINGS
           ));
           echo $PGS->display();
         }
         ?>
        </div>
      </div>
      <?php
      if ($NR > 0) {
      ?>
      <div class="act-button">
        <div class="text-center delselected" id="delb_span" style="display:none"><span>0</span> <?php echo $msw_public_11_global[0]; ?></div>
        <button type="button" class="btn btn-danger disabled" id="delb" onclick="if(jQuery(this).attr('class')!='btn btn-danger disabled'){mswAction('<?php echo $cmd; ?>-del','.managepanel','no','yes')}"><i class="fa fa-times fa-fw"></i> <span class="hidden-xs"><?php echo $msw_common3; ?></span></button>
      </div>
      <?php
      }
      ?>
      </form>
    </div>