<?php 
require './inn/connect.php'; 
require './inn/function.php'; 
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $input = json_decode(file_get_contents('php://input'), true);
    if (json_last_error() === JSON_ERROR_NONE) {
        //echo $input['email'];
        if(!empty($input['email'])){
            $otp = rand(100000,999999);
            $vtoken = md5($input['email'].$otp.time());
            $insertData = ['email'=>$input['email'],'otp'=>$otp, 'vtoken'=>$vtoken,'created_at'=>date('Y-m-d H:i:s')];
            $stmt = "INSERT INTO verify_email (email, otp, vtoken, created_at) VALUES (:email, :otp, :vtoken, :created_at) ";
            $stmt = $conn->prepare($stmt);
            $stmt->execute($insertData);     
            sendmail($input['email'],$otp);      
            $response = [
                'status' => 'true',             
            ];
            echo json_encode($response);
        }
        if(!empty($input['otp'])){
            $stmt = $conn->prepare("SELECT * FROM verify_email where email = :email and otp = :otp");
            $stmt->execute(['email'=>trim($input['vemail']),'otp'=>$input['otp']]);
            $data = $stmt->fetch();
            if(!empty($data)){
                $stmt = $conn->prepare("UPDATE verify_email set status = 1 where id = :id");
                $stmt->execute(['id'=>$data['id']]);
                $response = [
                    'status' => 'true',
                    'token' =>   $data['vtoken']           
                ];
            }else{
                $response = [
                    'status' => 'false',             
                ];
            }
            echo json_encode($response);
        }
    }
}