<?php

/* ADMIN SYSTEM MODULE
----------------------------------*/

if (!defined('PARENT') || !isset($mswUser)) {
  include(PATH . 'control/modules/header/403.php');
}

include(MSW_ADM_LANG . 'aff-manage.php');
include(MSW_ADM_LANG . basename(__file__));

$fjs[] = 'stupid-table';
$fjs[] = 'datepicker';
$mswPick = array(
  'lang' => $DT->jslang($msw_cal),
  'time' => 'false'
);
$pagePrefix = $msw_adm_nav33;

// Build search query..
$search_sql = [];
if (isset($_GET['fname'])) {
  foreach (array('fname','lname','email','tsf','payinfo','notes','commtier','ip','code','sort','show') AS $sK) {
    switch($sK) {
      case 'tsf':
        if (isset($_GET[$sK],$_GET['tst']) && $DT->jstots($_GET[$sK]) > 0 && $DT->jstots($_GET['tst']) > 0) {
          $search_sql[] = (empty($search_sql) ? 'WHERE (' : 'AND (') . "DATE(FROM_UNIXTIME(`ts`)) >= '" . date('Y-m-d', $DT->jstots($_GET[$sK])) . "' AND DATE(FROM_UNIXTIME(`ts`)) <= '" . date('Y-m-d', $DT->jstots($_GET['tst'])) . "')";
        }
        break;
      case 'code':
        if (isset($_GET[$sK])) {
          $search_sql[] = (empty($search_sql) ? 'WHERE (' : 'AND (') . "`code` LIKE '%" . $DB->db_escape($_GET[$sK]) . "%')";
        }
        break;
      case 'sort':
        if (isset($_GET[$sK]) && in_array($_GET[$sK], array_keys($msw_adm_aff_search_sort))) {
          $msw_adm_aff_search_default_sort = $_GET[$sK];
        }
        break;
      case 'show':
        if (isset($_GET[$sK]) && (int) $_GET[$sK] > 0) {
          define('PER_PAGE_SET', (int) $_GET[$sK]);
          $limit = $page * PER_PAGE_SET - (PER_PAGE_SET);
        }
        break;
      default:
        if (isset($_GET[$sK]) && $_GET[$sK]) {
          // If dashboard search, include last name and email..
          if (isset($_GET['dash'])) {
            $search_sql[] = "WHERE (`fname` LIKE '%" . $DB->db_escape($_GET[$sK]) . "%' OR `lname` LIKE '%" . $DB->db_escape($_GET[$sK]) . "%' OR `email` LIKE '%" . $DB->db_escape($_GET[$sK]) . "%')";
          } else {
            $search_sql[] = (empty($search_sql) ? 'WHERE (' : 'AND (') . "`" . $sK . "` LIKE '%" . $DB->db_escape($_GET[$sK]) . "%')";
          }
        }
        break;
    }
  }
  if (empty($search_sql)) {
    header("Location: ?p=" . substr(basename(__file__), 0, -4));
    exit;
  } else {
    $search_sql[] = "AND (`approved` = 'yes')";
  }
  // Order by..
  switch((in_array($msw_adm_aff_search_default_sort, array_keys($msw_adm_aff_search_sort)) ? $msw_adm_aff_search_default_sort : 'name_asc')) {
    case 'date_asc': $orderBy = '`ts`'; break;
    case 'date_desc': $orderBy = '`ts` DESC'; break;
    case 'name_asc': $orderBy = '`fname`,`fname`'; break;
    case 'name_desc': $orderBy = '`fname` DESC,`lname` DESC'; break;
    case 'email_asc': $orderBy = '`email`'; break;
    case 'email_desc': $orderBy = '`email` DESC'; break;
    case 'comm_asc': $orderBy = '`affCommissions` * 100'; break;
    case 'comm_desc': $orderBy = '`affCommissions` * 100 DESC'; break;
    case 'pay_asc': $orderBy = '`affPayments`  * 100'; break;
    case 'pay_desc': $orderBy = '`affPayments` * 100 DESC'; break;
    case 'referrals_asc': $orderBy = '`affReferrals`'; break;
    case 'referrals_desc': $orderBy = '`affReferrals` DESC'; break;
  }
}

include(PATH . 'content/_header.php');
include(PATH . 'content/' . basename(__file__));
include(PATH . 'content/_footer.php');

?>