<?php

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

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

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

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

// Build search query..
$search_sql = [];
if (isset($_GET['tsf'])) {
  foreach (array('tsf','pf','aff','notes','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 'pf':
        if (isset($_GET[$sK],$_GET['st']) && mswNFM($_GET[$sK]) && mswNFM($_GET['pt']) != '0.00') {
          $search_sql[] = (empty($search_sql) ? 'WHERE (' : 'AND (') . "`payamount` >= " . $DB->db_escape($_GET[$sK]) . " AND `payamount` <= " . $DB->db_escape($_GET['pt']) . ")";
        }
        break;
      case 'aff':
        if (!empty($_GET[$sK])) {
          $search_sql[] = (empty($search_sql) ? 'WHERE (' : 'AND (') . "`affid` IN(" . $DB->db_escape(implode(',',$_GET[$sK])) . "))";
        }
        break;
      case 'sort':
        if (isset($_GET[$sK]) && in_array($_GET[$sK], array_keys($msw_adm_payments_search_sort))) {
          $msw_adm_payments_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]) {
          $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;
  }
  // Order by..
  switch((in_array($msw_adm_payments_search_default_sort, array_keys($msw_adm_payments_search_sort)) ? $msw_adm_payments_search_default_sort : 'date_asc')) {
    case 'date_asc': $orderBy = '`ts`'; break;
    case 'date_desc': $orderBy = '`ts` DESC'; break;
    case 'aff_asc': $orderBy = '`affName`'; break;
    case 'aff_desc': $orderBy = '`affName` DESC'; break;
    case 'pay_asc': $orderBy = '`payamount` * 100'; break;
    case 'pay_desc': $orderBy = '`payamount` * 100 DESC'; break;
    case 'method_asc': $orderBy = '`paymethod`'; break;
    case 'method_desc': $orderBy = '`paymethod` DESC'; break;
  }
}

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

?>