<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title>Login</title>
        <link rel="stylesheet" href="/api/public/styles.css">
        <link rel="stylesheet"  type='text/css' href="/api/public/Akkurat-Font/Akkurat.ttf" >
		<script type="text/javascript" src="https://code.jquery.com/jquery-latest.min.js"></script>

        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>

        <link rel="stylesheet" href="/api/public/bootstrap-select.min.css">
		<script src="/api/public/bootstrap-select.min.js"></script>
		<script src="https://code.jquery.com/ui/1.14.1/jquery-ui.js"></script>
		<link rel="stylesheet" href="https://code.jquery.com/ui/1.14.1/themes/base/jquery-ui.css">
	</head>
    <body>
        <div class="container-fluid">
            <nav class="navbar navbar-expand-lg navbar-light bg-light">
                <div class="container-fluid" style="display: unset;">
					<div class="row">
						<div class="col-sm-6">
							<a class="navbar-brand" href="/admin"><img src="/api/public/Img/logo.png" alt="">&nbsp;<img src="/api/public/Img/staymaster.png" alt=""></a>
							<span>Staymaster <img src="/api/public/Img/Maskgroup.png" alt=""></span>
						</div>
					</div>
                </div>
            </nav>
            <div class="container-fluid">
                <div class="row flex-nowrap">
                    <div class="col py-3">
                        <br/><br/><br/>
                        <form id="resetPasswordForm" method="POST">
                            <div class="bookingdetails">
                                <div class="row">
                                    <div class="col-sm-4"></div>
                                    <div class="col-sm-4"><h1>Reset you password</h1></div>
                                    <div class="col-sm-4"></div>
                                </div>
                                <hr>
                                <div class="row">
                                    <div class="col-sm-4"></div>
                                    <div class="col-sm-4">
                                        <div class="logindetails">
                                            <p>Please Login</p>
                                            <hr>
                                            <table>
                                                <tr>
                                                    <td scope="col"><span class="titlehead">&nbsp;</span></td>
                                                    <td scope="col">
                                                        <input type="email" name="email" id="email" value="" placeholder="Email" required>
                                                        <button class="btn btn-outline-success" id="get-otp" type="button">Get OTP</button>
                                                    </td>
                                                </tr>
                                                
                                                <tr>
                                                    <td scope="col"><span class="titlehead">Enter OTP : &nbsp;&nbsp;&nbsp;</span></td>
                                                    <td scope="col">
                                                        <input type="number" name="otp" id="otp" value="" placeholder="OTP" required> 
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td scope="col"><span class="titlehead">Password : &nbsp;&nbsp;&nbsp;</span></td>
                                                    <td scope="col">
                                                        <input type="password" id="new_password" name="new_password" value="" placeholder="Password" required> 
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td scope="col"><span class="titlehead">Confirm : &nbsp;&nbsp;&nbsp;</span></td>
                                                    <td scope="col">
                                                        <input type="password" id="confirm_password" name="confirm_password" value="" placeholder="Confirm Password" required> 
                                                    </td>
                                                </tr>
                                                <tr><td>&nbsp;</td><td>&nbsp;</td></tr>
                                                <tr><td>&nbsp;</td><td scope="col">
                                                    <div class="col-sm-12">
                                                        <button type="button" name="login" class="greenbutton" id="upload-button">Reset</button>&nbsp;&nbsp;
                                                        <a href="#"><button type="button" class="greybutton">Cancel</button></a>
                                                    </div>
                                                </td></tr>
                                            </table>
                                        </div>
                                    </div>
                                    <div class="col-sm-4"></div>
                                </div>
                            </div>
                        </form>
                        <br/><br/><br/><br/><br/><br/><br/><br/>
                    </div>
                </div>
            </div>
        </div>
    </body>
    <script>
        document.addEventListener('DOMContentLoaded', function () {
            const uploadButton = document.getElementById('upload-button');
            const getOtpButton = document.getElementById('get-otp');
            const resetPasswordForm = document.getElementById('resetPasswordForm');
            const email = document.getElementById('email');
            const otp = document.getElementById('otp');
            const newPassword = document.getElementById('new_password');
            const confirmPassword = document.getElementById('confirm_password');
    
            getOtpButton.addEventListener('click', async () => {
                if(email.value == ''){
                    alert("Please enter email");
                    return;
                }
                try {
                    getOtpButton.disabled = true;
                    const formData = new FormData(resetPasswordForm);
                    console.log(formData);
                    // Send formData to backend
                    const response = await fetch('/admin/generateOTP', {
                        method: 'POST',
                        body: formData,
                    });
    
                    if (!response.ok) {
                        console.log('Failed to reset password:', response.json());
                        getOtpButton.disabled=false;
                        throw new Error('Failed to reset password');
                    }
    
                    const result = await response.json();
                    alert(result.message);
                    getOtpButton.disabled = false;
                } catch (error) {
                    console.error('Error with Password reset:', error);
                    alert('Failed to reset password');
                }
            });

            uploadButton.addEventListener('click', async () => {
                if (!resetPasswordForm.checkValidity()) {
                    resetPasswordForm.reportValidity();
                    return;
                }
                if(confirmPassword.value != newPassword.value){
                    alert("Password and Confirm Password do not match");
                    return;
                }
                try {
                    uploadButton.disabled=true;
                    const formData = new FormData(resetPasswordForm);
                    console.log(formData);
                    // Send formData to backend
                    const response = await fetch('/admin/resetPassword', {
                        method: 'POST',
                        body: formData,
                    });
    
                    if (!response.ok) {
                        console.log('Failed to reset password:', response.json());
                        uploadButton.disabled=false;
                        throw new Error('Failed to reset password');
                    }
    
                    const result = await response.json();
                    if(result.success){
                        window.location.href = `/admin/login/${result.message}`;
                    }else{
                        alert(result.message);
                    }
                    uploadButton.disabled=false;
                } catch (error) {
                    console.error('Error with Password reset:', error);
                    alert('Failed to reset password 1');
                }
            });
        });
    </script>
</html>