<?php	 		
	require_once('list_func.php');

	$page   		= 0;
	$offset 		= 0;
	$outputField 	= 'data_list'; //default
	$params 		= '';

	if (empty($_POST))
	{
		//['argv'][0] is the session id used in list.func.php
		if ($_SERVER['argc'] > 2)
		{
			$params = [];
			for ($x = 1; $x < ($_SERVER['argc'] - 2); $x++)
			{
				$params[] = $_SERVER['argv'][$x];
			}
		}

		$sid = $_SESSION['caller']['args'][count($_SERVER['argv'])-1] ?? 0;//session id is always the last entry in the array
		if (isset($_SESSION['caller']['args'][2]))
		{
			$page 			= $_SESSION['caller']['args'][1];
			$offset 		= $_SESSION['caller']['args'][2];
			if (isset($_SESSION['caller']['args'][3]))
				$orderField = $_SESSION['caller']['args'][3];
			else
			{
				if (isset($_SESSION['listParams']['orderField']))
					$orderField = $_SESSION['listParams']['orderField'];
				else
					$orderField = 0; //default
			}
			$orderDirection	= $_SESSION['caller']['args'][4] ?? 0;
		}
		else
		{
			$page 			= $_SESSION['listParams']['page'];
			$offset 		= $_SESSION['listParams']['offset'];			
			$orderField		= $_SESSION['listParams']['orderField'];
			$orderDirection = $_SESSION['listParams']['orderDirection'];
		}	
	}
	else
	{
		$sid			= $_POST['sId']				?? 0;
		//Session values should remain when posting to the same page, for example when searching:
		$page			= $_POST['page']			?? 0;//$_SESSION['listParams']['page'];
		$offset 		= $_POST['offset']			?? SABLE_DATALIST_ROWS;//$_SESSION['listParams']['offset'];
		$orderField		= $_POST['orderField']		?? 0;//$_SESSION['listParams']['orderField'];
		$orderDirection	= $_POST['orderDirection']	?? 0;//$_SESSION['listParams']['orderDirection'];
		$outputField 	= $_POST['outputField']		?? 0;
		//$params 		= $_POST['params'];

		$_SESSION['listParams']['searchString'] = null;

		$p = 0;
		//build up the search parameters (if any)
		$d = '';
		for ($x = 0; $x < count($_POST); $x++) //use sizeof($_POST)-1 to exclude the sId.
		{
			if (isset($_POST['input'.$x]))
			{
				$d = $x ? '|' : '';
				$_SESSION['listParams']['searchString'] .= $d.$_POST['input'.$x];
			} 
			if  (isset($_POST['params'.$x]))
			{
				$d = $p ? ',' : '';
				$params .= $d.$_POST['params'.$x];
				$p++;
			}
			//else break;
		}
	}

	include_once('data_list.php');
	$_data_list = new DataList; // a new instance of the class
	$_data_list->sid			= $sid				?? session_id()								?? 0;
	$_data_list->page			= $page				?? $_SESSION['listParams']['page']			?? 0;
	$_data_list->offset			= $offset			?? $_SESSION['listParams']['offset']		?? 0;
	$_data_list->orderField		= $orderField		?? $_SESSION['listParams']['orderField']	?? 0;
	$_data_list->orderDirection = $orderDirection	?? $_SESSION['listParams']['orderDirection']?? 0;
	$_data_list->ouputField 	= $outputField		? $outputField	: 'data_list'; 
	$_data_list->params 		= $_SESSION['listParams']['searchString'] ?? ''; //$params;
	$_data_list->resetList		= intval($_POST['resetList']	?? 0);
	$_data_list->refinedList	= intval($_POST['refinedList']	?? 0);

	$_data_list->showDataList();
	unset($_data_list); //destoys the instance created and frees it from memory.
	$_SESSION['module'] = null; //clear to avoid confusion on other lists linking to the wrong pages.		  