<?php
// check user is loged in or not
if($_SESSION['seas_user_id'] <=0) {
	header("location: home");
}

error_reporting(0);

/*
require_once($application_path . "class/survey.class.php");
$SEAS_Survey = new survey();
*/
?>
<div id="progress_area" style="width:100%; text-align:center">
<!-- Progress bar holder -->
	<br><br>
    <br><br>
	<img src="view\images\seas-logo.png" style="text-align:center">
    <div style="clear:both"></div>
    <div id="progress" style="display: inline-block;width:500px;border:1px solid #ccc;"></div>
    <div style="clear:both"></div>
    <!-- Progress information -->
    <br>
    <img src="view\images\loading.gif" style="text-align:center">
    <br>
    <div style="clear:both"></div>
    <br>
    <div id="information" style="width; color:#999"></div>
</div>
<?php

// Progress Bar
    echo '<script language="javascript">
    document.getElementById("progress").innerHTML="<div style=\"width:10%;background-color:#ddd;\">&nbsp;</div>";
    document.getElementById("information").innerHTML="Loading Dashboard - Main Statistics";
    </script>';
	echo str_repeat('        ',1024*8);
	ob_end_flush();
	ob_flush();
	flush();
// End Progress Bar
	
require_once($application_path . "model/survey.mdl.php");
$SEAS_Survey_Model = new survey_Model();

$tv_resp_statistics = $SEAS_Survey_Model->data_statistics(1);
//$rd_resp_statistics = $SEAS_Survey_Model->data_statistics(2);  // Disabled for temporary
//$pr_resp_statistics = $SEAS_Survey_Model->data_statistics(3);
//$mu_resp_statistics = $SEAS_Survey_Model->data_statistics(4);

// Progress Bar
    echo '<script language="javascript">
    document.getElementById("progress").innerHTML="<div style=\"width:20%;background-color:#ddd;\">&nbsp;</div>";
    document.getElementById("information").innerHTML="Loading Dashboard - Survey Details";
    </script>';
	echo str_repeat('        ',1024*8);
	ob_end_flush();
	ob_flush();
	flush();
// End Progress Bar


$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($_REQUEST['selected_survey']) {
	if($_REQUEST['selected_survey']>0) {
		$_SESSION['seas_srv_id'] = $_REQUEST['selected_survey'];
	}
	else {
		$_SESSION['seas_srv_id'] = $survey_list[0]['srv_id'];
		$_SESSION['seas_target_id'];
	}
}

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

// Progress Bar
    echo '<script language="javascript">
    document.getElementById("progress").innerHTML="<div style=\"width:40%;background-color:#ddd;\">&nbsp;</div>";
    document.getElementById("information").innerHTML="Loading Dashboard - Survey Calculations";
    </script>';
	echo str_repeat('        ',1024*8);
	ob_end_flush();
	ob_flush();
	flush();
// End Progress Bar
// Calculations
require_once($application_path . "model/survey_calc.mdl.php");
$SEAS_Survey_Calc_Model = new survey_calc_Model();

// Client profile
$client_fav_channel = array();  

$sql2  = "select chid,forder from mst_user_fav where user_id = " . $_SESSION['seas_user_id'] . " order by forder";
$rslt2 = mysql_query($sql2);
if(mysql_num_rows($rslt2) >0) {
	while($rwrs2 = mysql_fetch_assoc($rslt2)) {
		$client_fav_channel[] = $rwrs2['chid']; 
	}
}

$client_fav_list = $SEAS_Survey_Calc_Model->extract_grp_reach($survey_detail,0,1,$client_fav_channel,0);

// Calculate GRP & Reach
$top5_chnls_id_lst =array();
if($survey_detail['type'] == 1 || $survey_detail['type'] == 2) { // Applicable only for TV and Radio
	$channel_list = array();
	$channel_list = $SEAS_Survey_Calc_Model->extract_grp_reach($survey_detail,0,1);
	
	$typology_list = array();
	$typology_list = $SEAS_Survey_Calc_Model->extract_grp_reach($survey_detail,0,2); 
	
	$cx=0;
	$data_txt = "";
	$top5_channels = array();
	foreach($channel_list as $chid=>$chlst) {
		if($chlst['chname'] != "Total") {
			$top5_channels[$chlst['chid']] = $chlst['chname'];
			$top5_chnls_id_lst[] = $chlst['chid'];
			$cx++;
		}
		if($cx >=5) break;
	}
	
	$top5_channel_list = array();
	$top5_channel_list = $SEAS_Survey_Calc_Model->extract_dp_grp_reach($survey_detail,0,$top5_channels,1);  // Fetch top5 channels data for PARC DayPart ID: 1
	
}

// Progress Bar
    echo '<script language="javascript">
    document.getElementById("progress").innerHTML="<div style=\"width:50%;background-color:#ddd;\">&nbsp;</div>";
    document.getElementById("information").innerHTML="Loading Dashboard - Survey Socio Composition";
    </script>';
	echo str_repeat('        ',1024*8);
	ob_end_flush();
	ob_flush();
	flush();
// End Progress Bar
	
// Socio Composition
require_once($application_path . "model/survey_socio.mdl.php");
$SEAS_Socio_Model = new seas_socio_Model();

$socio_composition = $SEAS_Socio_Model->socio_composition($survey_detail);


// Progress Bar
    echo '<script language="javascript">
    document.getElementById("progress").innerHTML="<div style=\"width:80%;background-color:#ddd;\">&nbsp;</div>";
    document.getElementById("information").innerHTML="Loading Dashboard - Survey Program details";
    </script>';
	echo str_repeat('        ',1024*8);
	ob_end_flush();
	ob_flush();
	flush();
// End Progress Bar
// Program Data
require_once($application_path . "model/program.mdl.php");
$SEAS_Program_Model = new program_data();

$program_data = $SEAS_Program_Model->fetchdata($survey_detail['srv_mrkt'],$survey_detail['type'],$top5_chnls_id_lst,$survey_detail['rw_srv_stdt'],$survey_detail['rw_srv_eddt'],"0000","3000",$SEAS_Survey_Calc_Model->filter_resp_sql,8,1,0,"");

/*
echo "<table border=1>";
foreach($program_data as $ky=>$pdata) {
	echo "<tr>";
	foreach($pdata as $ky=>$pgdata) {
		echo "<td>";	
		echo $ky;
		echo "</td>";	
	}
	echo "</tr>";
	break;
}
foreach($program_data as $ky=>$pdata) {
	echo "<tr>";
	foreach($pdata as $ky=>$pgdata) {
		echo "<td>";	
		echo $pgdata;
		echo "</td>";	
	}
	echo "</tr>";
}
echo "</table>";
exit;
*/

// Progress Bar
    echo '<script language="javascript">
    document.getElementById("progress").innerHTML="<div style=\"width:90%;background-color:#ddd;\">&nbsp;</div>";
    document.getElementById("information").innerHTML="Loading Dashboard - Survey Average Program Ratings";
    </script>';
	echo str_repeat('        ',1024*8);
	ob_end_flush();
	ob_flush();
	flush();
// End Progress Bar


$avg_program_data = $SEAS_Program_Model->Average_PeriodWise($program_data);

// Sort Program Data by Average rating
$ch_program_data = array();
foreach ($avg_program_data as $ki=>$avg_program_data_lst) {    
	$ch_program_data[] = $avg_program_data_lst['Avg_Rat'];
}

array_multisort($ch_program_data, SORT_DESC, $avg_program_data);



// Progress Bar
    echo '<script language="javascript">
    document.getElementById("progress").innerHTML="<div style=\"width:100%;background-color:#ddd;\">&nbsp;</div>";
    document.getElementById("information").innerHTML="Loading Dashboard - Brand Analysis";
    </script>';
	echo str_repeat('        ',1024*8);
	ob_end_flush();
	ob_flush();
	flush();
// End Progress Bar

// Brand Analysis 
require_once($application_path . "controller/brandanalysis.php");


// Progress Bar
    echo '<script language="javascript">
	var element = document.getElementById("progress_area");
	element.outerHTML = "";
	element.innerHTML = "";
	delete element;	
    </script>';
	echo str_repeat('        ',1024*8);
	ob_end_flush();
	ob_flush();
	flush();
// End Progress Bar

?>