<?php
//initialize data
$memberNo ="";
$memberSurname ="";
$memberOthernames ="";
$memberGrade ="";
$memberEmail ="";
$memberAddress ="";
$memberRegDate ="";

$errmsg='';
$sucmsg='';
if(isset($_POST['update'])){
    extract($_POST);
    $errmsg='';
    $sucmsg='';
    
    
	$memberAddress = str_replace("'", "\'", $memberAddress);
	$memberSurname = str_replace("'", "\'", $memberSurname);
	$memberOthernames = str_replace("'", "\'", $memberOthernames);
	
		//update record
	$qry2=mysqli_query($con, "UPDATE pci_sponsor SET memberNo='$memberNo', memberSurname='$memberSurname', memberOthernames='$memberOthernames', memberEmail='$memberEmail', memberAddress='$memberAddress', memberGrade='$memberGrade', memberRegDate='$memberRegDate' WHERE applicant_id='$applicant_idpci'");
	if($qry2){
		$sucmsg='Successful!';
	}else{
		echo mysqli_error($con);
		$errmsg='Not Successful! Please ensure that your sponsor update his/her personal details.';
	}
	
    
}


if(isset($_POST['changesponsor'])){
    extract($_POST);
    $errmsg='';
    $sucmsg='';
    
		//search new sponsor
	$qry2=mysqli_query($con, "select * from members WHERE memberNo='$memberNo' limit 1");
	if($qry2){
		$row=mysqli_fetch_assoc($qry2);
		extract($row);
		
		$sucmsg='';
	}else{
		$errmsg='Not found!';
		$errmsg1='Not found!';
	}

}else{

//GET DETAILS FROM DB
	
	$q = "Select * from pci_sponsor where applicant_id='$applicant_idpci' limit 1";
    $qry=mysqli_query($con, $q);
	if($qry){
		
		if(mysqli_num_rows($qry) >0){
			$myallow = 1;  //allow next button to display
			$row=mysqli_fetch_assoc($qry);
			extract($row);
			
		}else{ $errmsg='Sponsor not found!'; $myallow = 0; }
				
	}else{
		$errmsg='Sponsor not found!'; $myallow = 0;
	}
}
?>

<div class="graph-visual tables-main">
<h2 class="inner-tittle">SPONSOR’S DETAILS</h2>
The Sponsor must be a professionally qualified Member of the Institute, a Fellow or Corporate Member of not less than 5 years. 
<br><br>The Sponsor must have prior knowledge of your intention to use him/her as your sponsor.
	<div class="graph-form">
    		<div class="form-body">
    			<form method="post" name="sponsorform" action="index.php?page=sponsor"> 
                <?php 
               
               if($errmsg1!='Not found!')
               {
                
			switch($memberGrade)
			{
				case 1: $memberGrade="FELLOW";
				break;
				
				case 2: $memberGrade="MEMBER";
				break;
				
				case 3: $memberGrade="PROBATIONER";
				break;
				
				case 4: $memberGrade="TECHNICIAN";
				break;
				
				case 5: $memberGrade="STUDENT";
				break;
				
				case 6: $memberGrade="PRACTICING FIRM";
				break;
				
				case 7: $memberGrade="GRADUATE";
				break;
				
				case 8: $memberGrade="STATE CHAPTER";
				break;
				
				case 9: $memberGrade="PAST PRESIDENT";
				break;
				
				default: $memberGrade = $memberGrade;
				
			}
		
		
			//verify condition of sponsor grade
			if($memberGrade!="Past President" && $memberGrade!="Member" && $memberGrade!="Fellow")
			{
			    //not qualified
			    
			    if(strlen($memberGrade)<=0){ 
			     $errmsg='Sponsor not found!'; 
			     }
			}
			
			
			//check dues
			if($errmsg=='')
			{
			   $q_dues = "select * from dues where memberNo='$memberNo'";
			   $r_dues = mysqli_query($con, $q_dues);
			   $rowdues = mysqli_fetch_array($r_dues);
			   extract($rowdues);
			   
			   $amount_owing = $outstanding + $annualSub + $advance + $upgradeCharge + $annualSub2017 + $annualSub2018 + $annualSub2019 + $annualSub2020 + $annualSub2021;
					   
			   if($amount_owing>=1000){ $errmsg='Your intended sponsor is not financially up-to-date!'; }
			   
			}//dues check ends here
		
		}
		
		//verify that applicant is not using self
		if($memberNo==$applicant_no)
		{
		    //not qualified
		   $errmsg='You can not sponsor yourself for this application!';
		}
		
		
		//check year of membership -- only Members
		if($memberGrade =="MEMBER")
		{
		
		  $myYear = $member_date+0;
		  $countYear = date('Y') - $myYear;
		  
		  if($countYear<5){ $errmsg='Your intended sponsor is not qualified by number of years as a member to serve as a sponsor!';}
		  
		}  ?>
		
		 <div class="form-group"> 
                    
                        <label>Sponsor Membership Number</label> 
                        <input type="text" class="form-control" name="memberNo" value="<?php echo $memberNo; ?>" placeholder="e.g 01234, 02345" maxlength="5" required="" /> 
						<input name="changesponsor" type="submit" class="btn btn-default" value="Search Sponsor"/> 
                </form> 
                    </div> 
                    
                    
                    <?php
		
		 if($errmsg!=''){
                    echo '<div class="alert alert-danger">'.$errmsg.'</div>';
                }elseif($sucmsg!=''){
                   $errmsg='';
                    echo '<div class="alert alert-success">'.$sucmsg.'</div>';
                    }
                    
                    
                    if($errmsg==''){
               
		
                 ?>   
                 
                
                    
					<form method="post" name="sponsorform" action="index.php?page=sponsor"> 
					<input type="hidden" class="form-control" name="memberNo" value="<?php echo $memberNo; ?>" required="" /> 
						
					<div class="form-group"> 
                        <label>Sponsor Surname</label> 
                        <input type="text" class="form-control" name="memberSurname" value="<?php echo strtoupper($memberSurname); ?>" required="" readonly/> 
                    </div> 
                    
					<div class="form-group"> 
                        <label>Sponsor Othernames</label> 
                        <input type="text" class="form-control" name="memberOthernames" value="<?php echo strtoupper($memberOthernames); ?>" required="" readonly/> 
                    </div> 
                    
					
					<div class="form-group"> 
                        <label>Sponsor Email Address</label> 
                        <input type="text" class="form-control" name="memberEmail" value="<?php echo $memberEmail; ?>" required="" readonly/> 
                    </div> 
                    
					<div class="form-group"> 
                        <label>Sponsor Contact Address</label> 
                        <input type="text" class="form-control" name="memberAddress" value="<?php echo strtoupper($memberAddress); ?>" required="" readonly/> 
                    </div> 
                    
					<div class="form-group"> 
                        <label>Sponsor Membership Grade</label> 
						
                        <input type="text" class="form-control" name="memberGrade" value="<?php echo strtoupper($memberGrade); ?>" required="" readonly/> 
                    </div> 
                    
					<div class="form-group"> 
                        <label>Sponsor Registration Date</label> 
                        <input type="text" class="form-control" name="memberRegDate" value="<?php echo $memberRegDate; ?>" required="" readonly/> 
                    </div> 
                    
					
		<?php
			     // success closure
                
		
			
                    echo '<input name="update" type="submit" class="btn btn-default" value="Update"/>  ';
                        }
                 ?>		
                    
                </form> 
    		</div>
    
    </div>

</div>

<div>
<a href='?page=employer'><button class='btn'>Back</button></a>

<?php
if(($errmsg=='') && ($myallow != 0)){
	echo "<a href='?page=preview'><button class='btn'>Next</button></a>";
}
?>
</div>