<?php

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

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

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

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

// Build search query..
$search_sql = [];
if (isset($_GET['sf'])) {
  foreach (array('sf','cf','prod','aff','tsf','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 'sf':
      case 'cf':
        if ($sK == 'sf') {
          if (isset($_GET[$sK],$_GET['st']) && mswNFM($_GET[$sK]) && mswNFM($_GET['tst']) != '0.00') {
            $search_sql[] = (empty($search_sql) ? 'WHERE (' : 'AND (') . "`saletotal` >= " . $DB->db_escape($_GET[$sK]) . " AND `saletotal` <= " . $DB->db_escape($_GET['st']) . ")";
          }
        } else {
          if (isset($_GET[$sK],$_GET['ct']) && $_GET[$sK] && $_GET['ct'] && mswNFM($_GET['ct']) != '0.00') {
            $search_sql[] = (empty($search_sql) ? 'WHERE (' : 'AND (') . "`commtotal` >= " . $DB->db_escape($_GET[$sK]) . " AND `commtotal` <= " . $DB->db_escape($_GET['ct']) . ")";
          }
        }
        break;
      case 'prod':
      case 'aff':
        if ($sK == 'prod') {
          if (!empty($_GET[$sK])) {
            $search_sql[] = (empty($search_sql) ? 'WHERE (' : 'AND (') . "`product` IN(" . $DB->db_escape(implode(',',$_GET[$sK])) . "))";
          }
        } else {
          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_commissions_search_sort))) {
          $msw_adm_commissions_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;
  } else {
    $search_sql[] = "AND (`en` = 'yes' AND `approved` = 'yes')";
  }
  // Order by..
  switch((in_array($msw_adm_commissions_search_default_sort, array_keys($msw_adm_commissions_search_sort)) ? $msw_adm_commissions_search_default_sort : 'name_asc')) {
    case 'date_asc': $orderBy = '`ts`'; break;
    case 'date_desc': $orderBy = '`ts` DESC'; break;
    case 'name_asc': $orderBy = '`affName`'; break;
    case 'name_desc': $orderBy = '`affName` DESC'; break;
    case 'product_asc': $orderBy = '`productName`'; break;
    case 'product_desc': $orderBy = '`productName` DESC'; break;
    case 'sale_asc': $orderBy = '`saletotal`  * 100'; break;
    case 'sale_desc': $orderBy = '`saletotal` * 100 DESC'; break;
    case 'comm_asc': $orderBy = '`commtotal` * 100'; break;
    case 'comm_desc': $orderBy = '`commtotal` * 100 DESC'; break;
  }
}

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

?>