<?php 
ini_set('display_errors', 0);
$form_name = "";
$table_name = "custom_participant_registration";
$primary_key = "reg_id";

$mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : "";

include_once("includes/config.php");
include("includes/header.php");
include_once("includes/common_functions.php");

$common = new General_Functions();
$css_class = "x-gray";
// $common->SetCssClass($css_class);

$q = strtolower($_GET["q"]);
$columnname = 'reg_participant_id';
$from = isset($_REQUEST['from']) ? $_REQUEST['from'] : '';

// Vidhya - 31-10-2017  Added reg_date_of_recurit  to all the select queries below for adding in lookup

if($from == "")
{
    $auto_qry = "SELECT DISTINCT reg_id, reg_age, $columnname, sur_code, sur_title,reg_date_of_recurit FROM custom_participant_registration 
					INNER JOIN custom_survey_master ON (custom_survey_master.sur_code = custom_participant_registration.reg_survey_id)
				WHERE custom_participant_registration.status = 'Y'";
}
elseif($from == 'registration')
{
    /*$auto_qry = "SELECT DISTINCT sur_title, mem_scrn_part_id AS reg_participant_id, mem_scrn_q3 AS reg_age, mem_intr_part_type, mem_scrn_q8 
						FROM custom_msm_introduction 
					INNER JOIN custom_msm_screening ON (custom_msm_screening.mem_scrn_part_id = custom_msm_introduction.mem_intr_part_id)
					INNER JOIN custom_survey_master ON (custom_survey_master.sur_code = custom_msm_introduction.mem_intr_survey)
				WHERE mem_scrn_part_id NOT IN (SELECT reg_participant_id FROM custom_participant_registration) AND mem_scrn_q11 = 1";*/
	
    $auto_qry = "SELECT DISTINCT sur_title, mem_scrn_survey, mem_scrn_part_id AS reg_participant_id, mem_scrn_q3 AS reg_age, mem_intr_part_type, mem_scrn_q9, mem_scrn_dob, mem_intr_doi 
						FROM custom_msm_introduction 
					INNER JOIN custom_msm_screening 
						ON (custom_msm_screening.mem_scrn_part_id = custom_msm_introduction.mem_intr_part_id AND 
							custom_msm_screening.mem_scrn_survey = custom_msm_introduction.mem_intr_survey)
					INNER JOIN custom_survey_master ON (custom_survey_master.sur_code = custom_msm_introduction.mem_intr_survey)
				WHERE concat(mem_scrn_survey, ' ', mem_scrn_part_id) NOT IN (SELECT concat(reg_survey_id, ' ', reg_participant_id) as pid FROM custom_participant_registration) AND (mem_scrn_q11 = 1 OR mem_scrn_q13 = 1)";
	
	if(SURVYFXD == 'Y')
		$auto_qry .= " AND custom_msm_introduction.mem_intr_survey = '".SURVY."'";
}
elseif($from == 'loc')
{
    $auto_qry = "SELECT DISTINCT reg_id, reg_age, $columnname, sur_code, sur_title,reg_date_of_recurit FROM custom_participant_registration 
					INNER JOIN custom_survey_master ON (custom_survey_master.sur_code = custom_participant_registration.reg_survey_id)
				WHERE custom_participant_registration.status = 'Y' AND concat(sur_code, ' ', reg_participant_id) NOT IN (SELECT concat(loctr_survey_id, ' ', loctr_reg_id) as pid FROM custom_locator_information)";
}
elseif($from == 'lab')
{
    $auto_qry = "SELECT DISTINCT reg_id, reg_age, $columnname, sur_code, sur_title,reg_date_of_recurit FROM custom_participant_registration 
					INNER JOIN custom_survey_master ON (custom_survey_master.sur_code = custom_participant_registration.reg_survey_id)
				WHERE custom_participant_registration.status = 'Y' AND concat(sur_code, ' ', reg_participant_id) NOT IN (SELECT concat(lab_smple_sur_id, ' ', lab_smple_part_id) as pid FROM custom_lab_samples)";
}
elseif($from == 'reimbursement')
{
    $auto_qry = "SELECT DISTINCT reg_id, reg_age, $columnname, sur_code, sur_title,reg_date_of_recurit FROM custom_participant_registration 
					INNER JOIN custom_survey_master ON (custom_survey_master.sur_code = custom_participant_registration.reg_survey_id)
				WHERE custom_participant_registration.status = 'Y' AND reg_participant_id NOT IN (SELECT reimbrsmnt_particpnt_id FROM custom_reimbursement)";
}
elseif($from == 'reimbursement_payments')
{
    $auto_qry = "SELECT DISTINCT reg_id, reg_age, $columnname, sur_code, sur_title,reg_date_of_recurit FROM custom_participant_registration 
					INNER JOIN custom_survey_master ON (custom_survey_master.sur_code = custom_participant_registration.reg_survey_id)
				WHERE custom_participant_registration.status = 'Y' AND reg_participant_id IN (SELECT reimbrsmnt_particpnt_id FROM custom_reimbursement)";
}
elseif($from == 'incentive')
{
    $auto_qry = "SELECT DISTINCT reg_id, reg_age, $columnname, sur_code, sur_title,reg_date_of_recurit FROM custom_participant_registration
						INNER JOIN custom_app_settings ON (custom_app_settings.app_survey_id = custom_participant_registration.reg_survey_id)
						INNER JOIN custom_survey_master ON (custom_survey_master.sur_code = custom_participant_registration.reg_survey_id)
				WHERE custom_participant_registration.status = 'Y' AND custom_app_settings.app_incentive = 'Y' AND reg_participant_id NOT IN (SELECT incentive_particpnt_id FROM custom_incentive)";
}
$common->sql->query($auto_qry, SQL_ALL, SQL_ASSOC);
$rec = $common->sql->record;
?>
<link rel="stylesheet" href="includes/media/css/demo_page.css" type="text/css" />
<link rel="stylesheet" href="includes/media/css/demo_table.css" type="text/css" />
<script type="text/javascript" language="javascript" src="includes/media/js/jquery.js"></script>
<script type="text/javascript" language="javascript" src="includes/media/js/jquery.dataTables.js"></script>
<script type="text/javascript" charset="utf-8">
	var asInitVals = new Array();
	
	$(document).ready(function() {
		var oTable = $('#example').dataTable( {
			"bJQueryUI": true,
			"sPaginationType": "full_numbers",
		} );
		
		$("thead input").keyup( function () {
			oTable.fnFilter( this.value, $("thead input").index(this) );
		} );
		
		$("thead input").each( function (i) {
			asInitVals[i] = this.value;
		} );
		
		$("thead input").focus( function () {
			if ( this.className == "search_init" )
			{
				this.className = "";
				this.value = "";
			}
		} );
		
		$("thead input").blur( function (i) {
			if ( this.value == "" )
			{
				this.className = "search_init";
				this.value = asInitVals[$("thead input").index(this)];
			}
		} );
		
		$(".fg-toolbar").height(40);
		$(".dataTables_length").hide();
		$(".dataTables_filter").hide();
		$("#example_info").hide();
		
		$('#example tbody tr').live('click', function () {
			var frmval = new Array();
			var nTds = $('td', this);
			var row = $(nTds[0]).text();
			
			j = row;
			reg_id = '#reg_id_'+j;
			
			<?php
				if($from != 'registration')
				{
				?>
					sur_id = '#sur_id_'+j;
					frmval["sur_id"] = $(sur_id).val();
					
					frmval["reg_id"] = $(reg_id).val();
					parent.assign_partid(frmval);
				<?php
				}
				else
				{
				?>
					reg_age = '#reg_age_'+j;
					sur_id = '#sur_id_'+j;
					sur_title = '#sur_title_'+j;
					prt_type = '#prt_type_'+j;
					prt_gndr = '#prt_gndr_'+j;
					prt_dob = '#prt_dob_'+j;
					
					frmval["sur_id"] = $(sur_id).val();
					frmval["sur_title"] = $(sur_title).val();
					frmval["reg_id"] = $(reg_id).val();
					frmval["reg_age"] = $(reg_age).val();
					frmval["prt_type"] = $(prt_type).val();
					frmval["prt_gndr"] = $(prt_gndr).val();
					frmval["prt_dob"] = $(prt_dob).val();
					parent.assign_partid(frmval);
				<?php
				}
			?>
		});
	} );
</script>
<br /><br />
<div id="demo" style="width: 95%; margin: 0 auto;">
	<form name="view" method="post">
		<table cellpadding="0" cellspacing="0" border="0" class="display" id="example" width="100%">
			<thead>
				<tr>
					<th style="display: none;">&nbsp;</th>
					<!-- Commented by Vidhya - 31-10-2017 -->
					<!--  <th>Survey</th>--> 
					<th>Participant ID</th>
					<th>Age</th>
					<th>Date of Recruit</th>
				</tr>
			</thead>
			<tbody>
				<?php
					if($from != 'registration')
					{
						for($i=0; $i<count($rec); $i++)
						{
							echo '<tr>
									<td style="display: none;">'.$i.'
									
										<input type="hidden" name="sur_id" id="sur_id_'.$i.'" value="'.$rec[$i]['sur_code'].'" />
										<input type="hidden" name="reg_id" id="reg_id_'.$i.'" value="'.$rec[$i]['reg_participant_id'].'" />
									</td>
									<td>'.$rec[$i]['reg_participant_id'].'</td>
									<td>'.$rec[$i]['reg_age'].'</td>
									<td>'.date('d-m-Y',strtotime($rec[$i]['reg_date_of_recurit'])).' </td>
								</tr>';
							// Commented this - '.$rec[$i]['sur_title'].'  - Vidhya - 31-10-2017
						}
					}
					else
					{
						for($i=0; $i<count($rec); $i++)
						{
							echo '<tr>
									<td style="display: none;">'.$i.'
									
										<input type="hidden" name="sur_title" id="sur_title_'.$i.'" value="'.$rec[$i]['sur_title'].'" />
										<input type="hidden" name="sur_id" id="sur_id_'.$i.'" value="'.$rec[$i]['mem_scrn_survey'].'" />
										<input type="hidden" name="reg_id" id="reg_id_'.$i.'" value="'.$rec[$i]['reg_participant_id'].'" />
										<input type="hidden" name="reg_age" id="reg_age_'.$i.'" value="'.$rec[$i]['reg_age'].'" />
										<input type="hidden" name="prt_type" id="prt_type_'.$i.'" value="'.$rec[$i]['mem_intr_part_type'].'" />
										<input type="hidden" name="prt_gndr" id="prt_gndr_'.$i.'" value="'.$rec[$i]['mem_scrn_q9'].'" />
										<input type="hidden" name="prt_dob" id="prt_dob_'.$i.'" value="'.$rec[$i]['mem_scrn_dob'].'" />
									</td>
									<td>'.$rec[$i]['reg_participant_id'].'</td>
									<td>'.$rec[$i]['reg_age'].'</td>
    								<td>'.date('d-m-Y',strtotime($rec[$i]['mem_intr_doi'])).' </td>
								</tr>';
							// Commented this './/$rec[$i]['sur_title'].' - Vidhya - 31-10-2017
						}
					}
				?>
			</tbody>
			<thead>
				<tr>
					<th style="display: none;"><input type="text" name="search_engine" value="Search Particiapnt ID" class="search_init" /></th>
					<!--  <th><input type="text" name="search_engine" value="Search Survey" class="search_init" /></th>-->
					<th><input type="text" name="search_engine" value="Search Participant ID" class="search_init" /></th>
					<th><input type="text" name="search_engine" value="Search Age" class="search_init" /></th>
					<th><input type="text" name="search_engine" value="Search Date " class="search_init" /></th>
				</tr>
			</thead>
		</table>
	</form>
</div>
<?php
echo "<script>
		$(document).ready(function() {
				parent.set_size(this.body.offsetHeight + 25 + 'px', this.body.offsetWidth);  
		});
	  </script>";
exit;
$mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : "";
$act_type = isset($_REQUEST['act_type']) ? $_REQUEST['act_type'] : "";
$cancel_link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
$action = $_SERVER['SCRIPT_NAME']."?page=".$_REQUEST['page']."&mode=submit";

switch($mode)
{
	default:
		$query = "SELECT *, CASE WHEN ".$table_name.".status = 'Y' THEN 'Active' ELSE 'Inactive' END as status 
						FROM ".$table_name."
					LEFT JOIN custom_coupon_master ON (custom_coupon_master.coupn_reg_id = ".$table_name.".reg_id)
					LEFT JOIN custom_location ON (custom_location.loc_code = ".$table_name.".reg_location)
					LEFT JOIN custom_survey_master ON (custom_survey_master.sur_id = ".$table_name.".reg_survey_id)
				  GROUP BY reg_id";

		$query = "SELECT *, CASE WHEN ".$table_name.".status = 'Y' THEN 'Active' ELSE 'Inactive' END as status 
						FROM ".$table_name."
					LEFT JOIN custom_coupon_master ON (custom_coupon_master.coupn_reg_id = ".$table_name.".reg_id)
					LEFT JOIN custom_location ON (custom_location.loc_code = ".$table_name.".reg_location)
					LEFT JOIN custom_survey_master ON (custom_survey_master.sur_id = ".$table_name.".reg_survey_id)";

		$query = "SELECT *, CASE WHEN ".$table_name.".status = 'Y' THEN 'Active' ELSE 'Inactive' END as status 
						FROM ".$table_name."
					INNER JOIN custom_locator_information ON (custom_locator_information.loctr_reg_id = ".$table_name.".reg_participant_id)
					INNER JOIN custom_location ON (custom_location.loc_code = ".$table_name.".reg_location)
					INNER JOIN custom_survey_master ON (custom_survey_master.sur_code = ".$table_name.".reg_survey_id)";
		$common->sql->query($query, SQL_ALL, SQL_ASSOC);
		$rec = $common->sql->record;
		?>
		<link rel="stylesheet" href="includes/media/css/demo_page.css" type="text/css" />
		<link rel="stylesheet" href="includes/media/css/demo_table.css" type="text/css" />
		<script type="text/javascript" language="javascript" src="includes/media/js/jquery.js"></script>
		<script type="text/javascript" language="javascript" src="includes/media/js/jquery.dataTables.js"></script>
		<script type="text/javascript" charset="utf-8">
			var asInitVals = new Array();
			
			$(document).ready(function() {
				var oTable = $('#example').dataTable( {
					"bJQueryUI": true,
					"sPaginationType": "full_numbers",
				} );
				
				$("thead input").keyup( function () {
					oTable.fnFilter( this.value, $("thead input").index(this) );
				} );
				
				$("thead input").each( function (i) {
					asInitVals[i] = this.value;
				} );
				
				$("thead input").focus( function () {
					if ( this.className == "search_init" )
					{
						this.className = "";
						this.value = "";
					}
				} );
				
				$("thead input").blur( function (i) {
					if ( this.value == "" )
					{
						this.className = "search_init";
						this.value = asInitVals[$("thead input").index(this)];
					}
				} );
				
				$(".fg-toolbar").height(40);
				$(".dataTables_length").hide();
				$(".dataTables_filter").hide();
				$("#example_info").hide();
				
				$('#example tbody tr').live('click', function () {
					var frmval = new Array();
					var nTds = $('td', this);
					var row = $(nTds[0]).text();
					
					j = row;
					reg_id = '#reg_id_'+j;
					
					frmval["reg_id"] = $(reg_id).val();
                                        
					parent.frm_edit_val(frmval["reg_id"]);
				});
			} );
		</script>
        <br /><br />
        <div id="demo" style="width: 95%; margin: 0 auto;">
            <form name="view" method="post">
                <table cellpadding="0" cellspacing="0" border="0" class="display" id="example" width="100%">
                    <thead>
                        <tr>
                            <th style="display: none;">&nbsp;</th>
                            <th>Participant ID</th>
                            <th>Participant Type</th>
                            <th>Location</th>
                            <th>Survey</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                            $part_id = "";
                            
                            for($i=0; $i<count($rec); $i++)
                            {
                                if($rec[$i]['reg_participant_type'] == 'S')
                                        $type = 'Seed';
                                else
                                        $type = 'Recruit';
                                
                                echo '<tr>
                                        <td style="display: none;">'.$i.'</td>
                                        <td>
                                            '.$rec[$i]['reg_participant_id'].'
                                            <input type="hidden" name="reg_id" id="reg_id_'.$i.'" value="'.$rec[$i]['loctr_reg_id'].'" />
                                        </td>
                                        <td>'.$type.'</td>
                                        <td>'.$rec[$i]['loc_name'].'</td>
                                        <td>'.$rec[$i]['sur_title'].'</td>
                                    </tr>';
                            }
                        ?>
                    </tbody>
                    <thead>
                        <tr>
                            <th style="display: none;"><input type="text" name="search_engine" value="Search Participant ID" class="search_init" /></th>
                            <th><input type="text" name="search_engine" value="Search Participant ID" class="search_init" /></th>
                            <th><input type="text" name="search_browser" value="Search Participant Type" class="search_init" /></th>
                            <th><input type="text" name="search_platform" value="Search Location" class="search_init" /></th>
                            <th><input type="text" name="search_version" value="Search Survey" class="search_init" /></th>
                        </tr>
                    </thead>
                </table>
            </form>
        </div>
        <?php
	break;
}
?>