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

$errmsg='';
$sucmsg='';
$errmsg1='';

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 prob_sponsor SET memberNo='$memberNo', memberSurname='$memberSurname', memberOthernames='$memberOthernames', memberEmail='$memberEmail', memberAddress='$memberAddress', memberGrade='$memberGrade', memberRegDate='$memberRegDate' WHERE applicant_id='$applicant_idgdef'");
	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){
		if(mysqli_num_rows($qry2) > 0) {
			$row=mysqli_fetch_assoc($qry2);
			extract($row);
		}else{  $errmsg='NOT FOUND'; $errmsg=='NOT FOUND'; }
		
	}else{
		$errmsg='Not found!';
		$errmsg1='Not found!';
		$errmsgsal='Not found!';
	}

}else{

//GET DETAILS FROM DB
	
	$q = "Select * from prob_sponsor where applicant_id='$applicant_idgdef' limit 1";
	//echo "<font color='white'> Q ".$q."</fornt>";
    $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);
			$mysponsor = $memberSurname;
			
		}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&i=<?php echo $_GET['i']; ?>"> 
                <?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(empty($memberGrade)){ 
			     $errmsg='Provide valid membership number of your sponsor'; 
			     }
			}
			
			
			//check dues
			if($errmsg=='')
			{
			   $q_dues = "select * from dues where memberNo='$memberNo'";
			   $r_dues = mysqli_query($con, $q_dues);
			   	  //if(mysqli_num_rows($r_dues))
					$rowdues = mysqli_fetch_array($r_dues);
					extract($rowdues);
			   
			  $amount_owing = $outstanding + $annualSub + $advance + $upgradeCharge + $annualSub2017 + $annualSub2018 + $annualSub2019 + $annualSub2020 + $annualSub2021 + $annualSub2022;
					   
			   if($amount_owing>=1000){ $errmsg='Your intended sponsor is not financially up-to-date!'; }
			   
			}//dues check ends here
		
		}
		
		//get memberNo
		$q_appno = "SELECT * FROM prob_application WHERE id='".$_GET['i']."' LIMIT 1";
		$r_appno = mysqli_query($con, $q_appno);

		if($r_appno){
			if(mysqli_num_rows($r_appno) > 0){
				$row_appno = mysqli_fetch_array($r_appno);
				$applicant_no = $row_appno['memberNo'];

			}
		}
	
		
		//verify that applicant is not using self

		//echo "Application No: ".$applicant_no;
		
		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!';}
		  
		}
		
		 if(($errmsg!='')||($errmsgsal!='')){
                    echo '<div class="alert alert-danger">'.$errmsg.'</div>';
                }elseif($sucmsg!=''){
                    echo '<div class="alert alert-success">'.$sucmsg.'</div>';
                }
                
		
                 ?>   
                 
                 <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> 
                    
					<form method="post" name="sponsorform" action="index.php?page=sponsor&i=<?php echo $_GET['i']; ?>"> 
					<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
			if(strlen($memberSurname)>0 && ($errmsg=='')){
                    echo '<input name="update" type="submit" class="btn btn-default" value="Update"/>  ';
                        }
                 ?>		
                    
                </form> 
    		</div>
    
    </div>

</div>


<div>
<?php
echo "<a href='?page=experience&i=".$_GET['i']."'><button class='btn'>Back</button></a>";

if(!empty($mysponsor) && ($errmsg=='')){echo "<a href='?page=preview&i=".$_GET['i']."'><button class='btn'>Next</button></a> </div>"; }
?>

</div>