<?php
include("common/connection.php");
$confirm=$_REQUEST['par'];
//$lead=$_REQUEST['lid'];
$ConformAgree="update lead_master set Agreement_Confirm='1',Agreement_Confirm_Date='".date('Y-m-d')."' where Lead_ID='".$confirm."'";
$ConfirmAgree_query=mysqli_query($con,$ConformAgree);

$ConformAgr="update lead_master set Agreement_Confirm='1',Agreement_Confirm_Date='".date('Y-m-d')."' where Lead_ID='".$confirm."'";
$ConfirmAgr_query=mysqli_query($con,$ConformAgr);

$ConformAgreement="update patient_agreement set Agreement_Confirm='1',Agreement_Confirm_Date='".date('Y-m-d')."' where Lead_ID='".$confirm."'";
$ConfirmAgreement_query=mysqli_query($con,$ConformAgreement);

$ConformAgreement="update allocation_master set Agreement_Confirm='1' where Lead_ID='".$confirm."'";
$ConfirmAgreement_query=mysqli_query($con,$ConformAgreement);
 
//------------- Patient ID -------------//
 $seq_sln_sql="select max(SLN) as total_leads from patient_master";
	$seq_sln_qry=mysqli_query($con,$seq_sln_sql);
	$seq_sln=mysqli_fetch_array($seq_sln_qry);
    $Max_Sln=$seq_sln['total_leads']; 

		if($seq_sln['total_leads']!='')
		{			 
		    
			$Sql_PrjId="Select * From patient_master Where SLN='".$Max_Sln."'";
			$Qry_PrjId=mysqli_query($con,$Sql_PrjId);
			$Result_PrjId=mysqli_fetch_array($Qry_PrjId);
			$Max_PrjId=$Result_PrjId['Patient_Id'];
			$Id=explode("PID",$Max_PrjId);
			$New_PrjId=$Id[1]+1;								
			$Cln_Id="PID".$New_PrjId; 
		}
		else
		{
			$Cln_Id="PID10001";
		}

	//------------- Patient ID -------------//
		     $fetch="select * from lead_contact_details where Lead_ID='".$confirm."'";
		   $fetch_query=mysqli_query($con,$fetch);
		   $fetch_fetch=mysqli_fetch_array($fetch_query);
		   //patient_agreement//
		    $fetch2="select * from lead_master where Lead_ID='".$confirm."'";
		   $fetch2_query=mysqli_query($con,$fetch2);
		   $fetch_fetch2=mysqli_fetch_array($fetch2_query)
?>	
	
<?php
	$CheckPatientMaster_sql=mysqli_query($con,"select Lead_ID from patient_master where Lead_ID='".$confirm."'"); 
		  
		  if($CheckPatientMaster=mysqli_num_rows($CheckPatientMaster_sql)==0)
			{
		  
		   $patient_master="insert into patient_master(Lead_ID,Patient_Id,Patient_Name,Gender,DOB,Age,Mobile,Email,Adhar_No,Address,Landmark,State,Pin_Code,Reffered_Buurtzorg_From,Other_FM_Staying_With_Patient,Doctor_Name,Patient_Creation_Date,Control_Location)values('".$confirm."','".$Cln_Id."','".$fetch_fetch['Patient_Name']."','".$fetch_fetch['Patient_Gender']."','".$fetch_fetch['DOB']."','".$fetch_fetch['Patient_Age']."','".$fetch_fetch['Contactperson_Phno']."','".$fetch_fetch['Contact_Person_Email']."','".$fetch_fetch['Adhar_No']."','".$fetch_fetch['Patient_Address1']."','".$fetch_fetch['Landmark']."','".$fetch_fetch['Patient_State']."','".$fetch_fetch['Patient_Pincode']."','".$fetch_fetch2['Lead_Referred_To']."','".$fetch_fetch['Other_FM_Staying_With_Patient']."','".$fetch_fetch2['Allocated_Doctor']."','".date('Y-m-d')."','".$fetch_fetch2['Control_Location']."')";
           mysqli_query($con,$patient_master);
		   $check_roster_sql=mysqli_query($con,"select Patient_Id from roster_master where Patient_Id='".$Cln_Id."'");
			if($check_roster=mysqli_num_rows($check_roster_sql)==0)
			{
				echo $Cln_Id;
			}
			}
		   else
		   {
			   $UpdatePatMas="update patient_master set Patient_Name='".$fetch_fetch['Patient_Name']."', Gender='".$fetch_fetch['Patient_Gender']."', DOB='".$fetch_fetch['DOB']."',Age='".$fetch_fetch['Patient_Age']."',Mobile='".$fetch_fetch['Contactperson_Phno']."',Email='".$fetch_fetch['Contact_Person_Email']."',Adhar_No='".$fetch_fetch['Adhar_No']."',Address='".$fetch_fetch['Patient_Address1']."',Landmark='".$fetch_fetch['Landmark']."',State='".$fetch_fetch['Patient_State']."',Pin_Code='".$fetch_fetch['Patient_Pincode']."',Reffered_Buurtzorg_From='".$fetch_fetch2['Lead_Referred_To']."',Other_FM_Staying_With_Patient='".$fetch_fetch['Other_FM_Staying_With_Patient']."',Control_Location='".$fetch_fetch2['Control_Location']."' where Lead_ID='".$confirm."' and Patient_Creation_Date='".date('Y-m-d')."'";
				 mysqli_query($con,$UpdatePatMas);
				 //NewLead($con,$_REQUEST['Patient_Name']);
		   }
	?>
	 <div>
     <form  method="post">
    <input name="Patient_Name" id="Patient_Name" type="text" value="<?php echo $fetch_fetch['Patient_Name']; ?>">
    
     			 <select name="Gender" id="Gender" class="form-control" style="" >
                 <option value="" selected hidden>-- Select --</option>
                 <option value="Male" <?php if($fetch_fetch['Patient_Gender']=="M"){echo "selected";}?>>Male</option>
                 <option value="Female" <?php if($fetch_fetch['Patient_Gender']=="F"){echo "selected";}?>>Female</option> 
                 </select> 
                 
     <input name="DOB" id="from_Date" type="text" value="<?php echo $fetch_fetch['DOB']; ?>">
     
     <input name="Age" id="Age" type="text"  value="<?php echo $fetch_fetch['Patient_Age']; ?>"
     
     <input name="Mobile" id="Mobile" type="text" value="<?php echo $fetch_fetch['Contactperson_Phno']; ?>">
     
      <input name="Email" id="Email" type="text" value="<?php echo $fetch_fetch['Contact_Person_Email']; ?>">
      
      <input name="Adhar_No" id="Adhar_No" type="text" value="<?php echo $fetch_fetch['Adhar_No']; ?>">
      
      <textarea name="Address" id="Address"><?php echo $fetch_fetch['Patient_Address1']; ?></textarea>
      
       <input name="Landmark" id="Landmark" type="text"  value="<?php echo $fetch_fetch['Landmark']; ?>">
       
        <select name="State" class="form-control" id="pState"  data-rule-required="true" data-msg-required="Please select state">
                      <option value="">Select State</option>
                                              <?php
											   $SqlLoc="select SLN, State_Name from state_master";
                                               $RsLoc= mysqli_query($con,$SqlLoc);
                                               while($RowSt=mysqli_fetch_array($RsLoc))
											   {
											 ?>
                                              <option value="<?php echo $RowSt['State_Name']; ?>" <?php if($RowSt['State_Name']==$fetch_fetch['Patient_State']){echo "selected";} ?>><?php echo $RowSt['State_Name']; ?></option>
                                                <?php
												}
												?>
                 </select>
                 
                 <input name="Pin_Code" id="Pin_Code" value="<?php echo $fetch_fetch['Patient_Pincode']; ?>">
                 <input name="location" id="location" value="<?php echo $fetch_fetch2['Control_Location']; ?>">
                  <select name="Reffered_Buurtzorg_From" id="Reffered_Buurtzorg_From" class="form-control" onChange="Reffered_opt_function()">
                 <option value="" selected>Select reffered from</option>
                 <option value="Doctor" <?php if($fetch_fetch2['Lead_Referred_To']=="Doctor"){echo "selected";} ?>>Doctor</option>
                 <option value="Family" <?php if($fetch_fetch2['Lead_Referred_To']=="Family"){echo "selected";} ?> >Family</option>
                 <option value="Friend" <?php if($fetch_fetch2['Lead_Referred_To']=="Friend"){echo "selected";} ?>>Friend</option>  
                 <option value="Close_to_home_work" <?php if($fetch_fetch2['Lead_Referred_To']=="Close_to_home_work"){echo "selected";} ?>>Close to home/work</option>
                 <option value="Yellow_Pages" <?php if($fetch_fetch2['Lead_Referred_To']=="Yellow_Pages"){echo "selected";} ?>>Yellow Pages</option>
                 <option value="Other" <?php if($fetch_fetch2['Lead_Referred_To']=="Other"){echo "selected";} ?>>Other</option>
                 </select>
                 
                 <input name="Doctor_Name" id="Doctor_Name" value="<?php $fetch_fetch2['Allocated_Doctor']; ?>">
                 
                 <input name="Other_FM_Staying_With_Patient" id="Other_FM_Staying_With_Patient"s value="<?php echo $fetch_fetch['Other_FM_Staying_With_Patient']; ?>">
                 </form>
    </div>