<?php
/**
   * @author Govinda Kumar
   */
  require_once 'include/DB_Functions.php';
  $db = new DB_Functions(); 

if(isset($_REQUEST['uid']) && isset($_REQUEST['roll_no'])){
    
    // array for JSON response
    $response = array();
    
    // include db connect class
    $db = new Db_Connect();
    $conn = $db->connect();

    // Create Online Certificate

	include("../mpdf/mpdf.php");
	
    function base64url_encode($data) { 
    return rtrim(strtr(base64_encode($data), '+/', '-_'), '='); 
    } 

    function base64url_decode($data) { 
    return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT)); 
    } 	

    //unset($_SESSION['CountingTime']);
    $uid=md5(uniqid(rand()));

    date_default_timezone_set('Asia/Kolkata');
    
    // Update Admit Card Items for Finish Exam Section:
    $Query = $conn->query("UPDATE `admit_card_items` SET `status`= 2, `exp_date`='".date('Y-m-d H:i:s')."' WHERE `roll_no`='".$_REQUEST['roll_no']."'");
    
    // Update Result for Finish Exam Section:
    $Update = $conn->query("UPDATE `result` SET `r_status`= 1 WHERE `uid`=".$_REQUEST['uid']." AND `roll_no`='".$_REQUEST['roll_no']."'");

    $Query = $conn->query("SELECT * FROM `result` WHERE `uid`=".$_REQUEST['uid']." AND `roll_no`='".$_REQUEST['roll_no']."' GROUP BY `roll_no`");		
    $Row = $Query->fetch_assoc();

    $Id = base64url_encode($Row['id']);    
    $RollNo = base64url_encode($Row['roll_no']);    
  
    // User details:    
    
    $UQuery = $db->query("SELECT * FROM `users` WHERE `id`=".$_REQUEST['uid']."");
    $URow = $UQuery->fetch_assoc();
    
    if($URow['mode']=='1'){

        $to = $URow['email']; ////  email of the user who  fill the from.
        $subject = "EHF Online Certificate"; /// Subject of the mail
        $message = "Hi ".$URow['name'].",<br/><br/><br/>"; /// msg head line.
        $message .= "
                 Welcome to EHF<br/><br/>
                 Your Certificate link is: <a href='https://www.ehfworld.com/view-certificate.php?uid=".$uid."&did=".$Id."&rol=".$RollNo."' class='btn btn-success' target='_blank'>Download Certificate</a><br/>"; // text you want to show in the mail body which is send to user. 

        $headers[] = 'MIME-Version: 1.0';
        $headers[] = 'Content-type: text/html; charset=iso-8859-1';

        // Additional headers
        $headers[] = 'From: EHF Online Certificate <donotreply@ehfworld.com>';

        $retval = mail ($to,$subject,$message, implode("\r\n", $headers));

        if( $retval == true )
        {
            $to = "info@eduhealfoundation.org";  // on which you want enquery 
            $subject = "EHF Online Certificate"; // subject of that mail.
            ///////////Message Show to the site owner//////////		
            $message = "Exam taken";							
            ////////////////////				
            $headers[] = 'MIME-Version: 1.0';
            $headers[] = 'Content-type: text/html; charset=iso-8859-1';

            // Additional headers
            $headers[] = 'From: Olympiad Registration <donotreply@ehfworld.com>';

            $retval = mail ($to,$subject,$message, implode("\r\n", $headers));

            if( $retval == true )
            {
                $response["status"] = 1;
                $response["message"] = "Congrates! You are successfully completed test. Your Online Certificate to sent your register Email Id.";

                // echoing JSON response
                echo json_encode($response);                          
            }        
            else
            {
                $response["status"] = 0;
                $response["message"] = "Congrates! You are successfully completed test. Your Online Certificate is not sent your register Email Id. Please contact on this email id: info@eduhealfoundation.org.";

                // echoing JSON response
                echo json_encode($response);
            }
        }    
        else
        {
            $response["status"] = 0;
            $response["message"] = "Congrates! You are successfully completed test. Your Online Certificate is not sent your register Email Id. Please contact on this email id: info@eduhealfoundation.org.";

            // echoing JSON response
            echo json_encode($response);
        }
    }else{
        $response["status"] = 1;
        $response["message"] = "Congrates! You are successfully completed test.";

        // echoing JSON response
        echo json_encode($response);                              
    }
    
} else {
    // required field is missing
    $response["status"] = 2;
    $response["message"] = "Required field(s) is missing";
 
    // echoing JSON response
    echo json_encode($response);
}
?>