<?php
// Survey
require_once($application_path . "model/survey.mdl.php");
$SEAS_Survey_Model = new survey_Model();
$survey_list = $SEAS_Survey_Model->list_survey($_SESSION['seas_user_id']);


if($_SESSION['seas_srv_id'] == 0) {
	$_SESSION['seas_srv_id'] = $survey_list[0]['srv_id'];
	$_SESSION['seas_target_id']=0;
}

if(isset($_REQUEST['selected_survey'])) {
	if($_REQUEST['selected_survey']>0) {
		$_SESSION['seas_srv_id'] = $_REQUEST['selected_survey'];
		$_SESSION['seas_target_id']=0;
	}
	else {
		$_SESSION['seas_srv_id'] = $survey_list[0]['srv_id'];
		$_SESSION['seas_target_id']=0;
	}
}

$survey_detail =  $SEAS_Survey_Model->get_survey_details($_SESSION['seas_srv_id']);


// Target
require_once($application_path . "model/target.mdl.php");
$SEAS_Target_Model = new target_Model();
$list_targets = $SEAS_Target_Model->list_target($_SESSION['seas_user_id'],$survey_detail['srv_mrkt']);

if(!isset($_SESSION['seas_target_id'])) {
	$_SESSION['seas_target_id'] =0;
}
if(isset($_REQUEST['selected_target'])) {
	$_SESSION['seas_target_id'] = $_REQUEST['selected_target'];
}


// Calculations
require_once($application_path . "model/survey_calc.mdl.php");
$SEAS_Survey_Calc_Model = new survey_calc_Model();

if(!isset($_SESSION['sel_chnls_list'])) {
	$_SESSION['sel_chnls_list'] = array();
}
if(!isset($_SESSION['sel_analysis_by'])) {
	$_SESSION['sel_analysis_by'] = array();
}


$report_view = 0;

if(!isset($_SESSION['sel_compare_by'])) {
	$_SESSION['sel_compare_by'] = 1;
}
if(isset($_REQUEST['sel_compare'])) {
	$_SESSION['sel_compare_by'] = $_REQUEST['sel_compare'];
}

$compare_lst = array();
if($_SESSION['sel_compare_by']==1) { //List surveys selected survey's market
	foreach($survey_list as $srvls) {
		if($srvls['srv_mrkt'] == $survey_detail['srv_mrkt']) {
			$id = $srvls['srv_stdt'] . "_" . $srvls['srv_eddt'];
			$compare_lst[$id] = $survey_detail['name'];
		}
	}
}
elseif($_SESSION['sel_compare_by']==2) { //List months
	$msql = 'select distinct 
					DATE_FORMAT(srv_dt,"%c-%Y") AS mid,
					DATE_FORMAT(srv_dt,"%b-%Y") AS my ,
					MIN(srv_dt) as srv_stdt,
					MAX(srv_dt) as srv_eddt
				from survey_resp 
				where srv_mrkt='. $survey_detail['srv_mrkt'] . 
					' and srv_type=' . $survey_detail['type'] .
					' GROUP BY 1';
					
	$mrslt = mysql_query($msql);
	while($srw=mysql_fetch_assoc($mrslt)) {
		$id = $srw['srv_stdt'] . "_" . $srw['srv_eddt'];
		$compare_lst[$id] = $srw['my'];
	}
}
elseif($_SESSION['sel_compare_by']==3) { //List Weeks
	$msql = 'select distinct DATE_FORMAT(srv_dt,"%U-%Y") AS mid,
					MIN(srv_dt) as srv_stdt,
					MAX(srv_dt) as srv_eddt
				from survey_resp 
				where srv_mrkt='. $survey_detail['srv_mrkt'] . 
					' and srv_type=' . $survey_detail['type'] .
					' GROUP BY 1';
					
	$mrslt = mysql_query($msql);
	while($srw=mysql_fetch_assoc($mrslt)) {
		$id = $srw['srv_stdt'] . "_" . $srw['srv_eddt'];
		$compare_lst[$id] = "Week - " .$srw['mid'];
	}
}
elseif($_SESSION['sel_compare_by']==4) { //List Dates
	$msql = 'select distinct srv_dt,DATE_FORMAT(srv_dt,"%d-%m-%Y") AS sdt
				from survey_resp 
				where srv_mrkt='. $survey_detail['srv_mrkt'] . ' and srv_type=' . $survey_detail['type'];
	$mrslt = mysql_query($msql);
	while($srw=mysql_fetch_assoc($mrslt)) {
		$id = $srw['srv_dt'] . "_" . $srw['srv_dt'];
		$compare_lst[$id] = $srw['sdt'];
	}
}


if(isset($_REQUEST['btn_gen'])) {
	$chnls_list = array();
	$tsarray  = array();
	$_SESSION['sel_chnls_list'] = array();
	$_SESSION['sel_analysis_by'] = array();
	
	$chnls_list[0]['chid'] = 0;
	$chnls_list[0]['chname'] = "Total TV";	
	
	if(isset($_REQUEST['chnl_list'])) {
		$cid=1;
		foreach($_REQUEST['chnl_list'] as $chl) {
			$chl_ary = explode("_",$chl);
			$chnls_list[$cid]['chid'] = $chl_ary[0];
			$chnls_list[$cid]['chname'] = $chl_ary[1];
			$cid++;
			
			$_SESSION['sel_chnls_list'][] = $chl_ary[0];
		}		
	}
	
	if(isset($_REQUEST['compare_slots'])) {
		foreach($_REQUEST['compare_slots'] as $cs) {
			$_SESSION['sel_analysis_by'][] = $cs;
		}
	}

	

	
	$rv_report = array();
		
	foreach($_SESSION['sel_analysis_by'] as $dtary) {
		
		$dummy_survey = $survey_detail;
		
		$dt_ary = explode("_",$dtary);
		
		$dummy_survey['starting_date'] = $dt_ary[0];
		$dummy_survey['ending_date']   = $dt_ary[1];
		$dummy_survey['rw_srv_stdt']   = $dt_ary[0];
		$dummy_survey['rw_srv_eddt']   = $dt_ary[1];
		
		$SEAS_Survey_Calc_Model->filter_resp($dummy_survey);
		
		$rv_report[$dtary] = array();

		$grp = $SEAS_Survey_Calc_Model->get_grp(0,1);
		$reach = $SEAS_Survey_Calc_Model->get_reach(0,1);
		$rv_report[$dtary][0] = array($grp,$reach);


		foreach($_SESSION['sel_chnls_list'] as $chlst) {
			$grp = $SEAS_Survey_Calc_Model->get_grp($chlst,1);
			$reach = $SEAS_Survey_Calc_Model->get_reach($chlst,1);
			
			$rv_report[$dtary][$chlst] = array($grp,$reach);
		}
	}
	
	$report_view = 1;
}
else {
	if(!isset($_SESSION['msort'])) {
		$_SESSION['msort'] = "grp";	
	}
	if(isset($_REQUEST['sort'])) {
		$_SESSION['msort'] = $_REQUEST['sort'];
	}

	
	$channel_list = $SEAS_Survey_Calc_Model->extract_grp_reach($survey_detail,$_SESSION['seas_target_id'],1,array(),1,$_SESSION['msort']);
}
?>