<?php if(!defined('PARENT') || !isset($_GET['id'])) { exit; }
$SQL = 'WHERE (`affid` = \'' . $AF->id . '\') ';
if (isset($_GET['q']) && $_GET['q']) {
  $SQL .= "AND (`referrer` LIKE '%" . $DB->db_escape($_GET['q']) . "%')";
}
$Q  = $DB->db_query("SELECT SQL_CALC_FOUND_ROWS *,
      (SELECT `name` FROM `" . DB_PREFIX . "products` WHERE `" . DB_PREFIX . "products`.`id` = `" . DB_PREFIX . "affiliates_tracking`.`product`) AS `productName`
      FROM `" . DB_PREFIX . "affiliates_tracking`
      $SQL
      ORDER BY `count` DESC
      LIMIT $limit," . PER_PAGE);
$NR = $DB->db_foundrows($Q);
?>

    <?php
    if ($NR > 0) {
    ?>
    <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="#" onclick="mswExp('<?php echo (isset($_GET['q']) && $_GET['q'] ? '&amp;q=' . urlencode(mswSH($_GET['q'])) : '') . '&amp;id=' . $AF->id; ?>','referrals-export');return false"><i class="fa fa-plus fa-fw"></i> <?php echo $msw_adm_aff_referral11; ?></a></li>
            <li<?php echo (isset($_GET['q']) ? ' class="active"' : ''); ?>><a href="#" onclick="mswSB('.searchpanel','show');return false"><i class="fa fa-search fa-fw"></i> <?php echo $msw_adm_aff_referral3; ?></a></li>
          </ul>
        </div>
      </div>
    </div>
    <?php
    }
    ?>

    <div class="managepanel searchpanel" style="margin-top:10px<?php echo (!isset($_GET['q']) ? ';display:none' : ''); ?>">
      <form method="get" action="index.php">
      <div class="panel panel-default managearea">
        <input type="hidden" name="p" value="<?php echo $cmd; ?>">
        <input type="hidden" name="id" value="<?php echo $AF->id; ?>">
        <div class="panel-heading">
          <i class="fa fa-search fa-fw"></i> <?php echo $msw_adm_aff_referral3; ?>
        </div>
        <div class="panel-body">
          <div class="form-group">
            <input type="text" class="form-control" name="q" value="<?php echo (isset($_GET['q']) ? mswSH($_GET['q']) : ''); ?>" placeholder="<?php echo mswSH($msw_adm_aff_referral5); ?>">
          </div>
          <div class="text-center" style="padding-bottom:20px">
            <button class="btn btn-success" type="submit"><i class="fa fa-check fa-fw"></i><span class="hidden-xs"> <?php echo $msw_adm_aff_referral6; ?></span></button>
            <button class="btn btn-default" type="button" onclick="mswWin('<?php echo $cmd; ?>&amp;id=<?php echo $AF->id; ?>')"><i class="fa fa-times fa-fw"></i><span class="hidden-xs">  <?php echo $msw_adm_aff_referral4; ?></span></button>
          </div>
        </div>
      </div>
      </form>
    </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_referral; ?> (<span class="highlight notexttrans"><?php echo mswSH($AF->fname . ' ' . $AF->lname); ?></span>, <?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_referral7; ?></th>
                 <th data-sort="string"><?php echo $msw_adm_aff_referral8; ?></th>
                 <th data-sort="int"><?php echo $msw_adm_aff_referral9; ?></th>
                 <th>&nbsp;</th>
               </tr>
             </thead>
             <tbody>
               <?php
               if ($NR > 0) {
                 while ($RF = $DB->db_object($Q)) {
                 ?>
                 <tr id="idr_<?php echo $RF->id; ?>">
                   <td><input type="checkbox" name="fm[id][]" value="<?php echo $RF->id; ?>" onclick="mswSCG('.managearea .table td','delb')"></td>
                   <td><b><?php echo mswSH($RF->referrer); ?></b> <a href="<?php echo mswSH($RF->referrer); ?>" onclick="window.open(this);return false"><i class="fa fa-external-link fa-fw"></i></a></td>
                   <td><?php echo mswSH($RF->productName); ?></td>
                   <td data-sort-value="<?php echo $RF->count; ?>"><?php echo mswNFM($RF->count); ?></td>
                   <td><a href="?p=referral&amp;id=<?php echo $RF->id; ?>" title="<?php echo mswSH($msw_common4); ?>"><i class="fa fa-pencil-square fa-2x fa-fw"></i></a></td>
                 </tr>
                 <?php
                 }
               } else {
                 $colspan = 5;
                 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>
        <?php
        if (isset($AF->id)) {
        ?>
        <button type="button" class="btn btn-default left_20" onclick="mswWin('aff-manage')"><i class="fa fa-arrow-left fa-fw"></i> <span class="hidden-xs"><?php echo $msw_common6; ?></span></button>
        <?php
        }
        ?>
      </div>
      <?php
      } else {
      ?>
      <div class="act-button">
        <button type="button" class="btn btn-default left_20" onclick="mswWin('aff-manage')"><i class="fa fa-arrow-left fa-fw"></i> <span class="hidden-xs"><?php echo $msw_common6; ?></span></button>
      </div>
      <?php
      }
      ?>
      </form>
    </div>