<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Skyonn Recruiting Portal</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css" integrity="sha512-z3gLpd7yknf1YoNbCzqRKc4qyor8gaKU1qmn+CShxbuBusANI9QpRohGBreCFkKxLhei6S9CQXFEbbKuqLg0DA==" crossorigin="anonymous" referrerpolicy="no-referrer" />
    <style>
        body {
            font-family: Arial;
        }

        .container {
            width: 20rem;
            margin: auto;
            border: 1px solid black;
            padding: 20px;
            border-radius: 10px;
            margin-top: 50px;
            position: relative;
        }

        .input-group {
            margin-bottom: 10px;
        }

        .input-group input[type="text"],
        .input-group input[type="password"],
        .input-group input[type="email"],
        .input-group select {
            width: calc(100% - 22px);
            border-radius: 5px;
            padding: 10px;
            box-sizing: border-box;
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }

        button {
            background-color: white;
            color: black;
            padding: 5px;
            border-radius: 5px;
            margin: 3px 0;
            width: 100%;
            font-size: 15px;
            background-color:#66D3FA;
        }

        h2 {
            color: black;
            text-align: center;
            margin-top: -5px;
            margin-bottom: 20px;
        }

        .toggle-form {
            text-align: center;
            margin-top: 20px;
        }

        .toggle-form a {
            color: #3C99DC;
            text-decoration: none;
        }

        .footer-text {
            position: absolute;
            bottom: 0;
            width: 100%;
            text-align: center;
            color: #3C99DC;
            margin-bottom: 10px;
        }
     .eye-icon {
    position: absolute;
    right: 50px;
    top: 48%;
    transform: translateY(-50%);
    cursor: pointer;
    font-size: 15px;
    color: #999;
}
    </style>
</head>
<body>
<div class="container">
    <h2>Skyonn Recruiting Portal</h2>

    <div id="login-form">
        <form id="loginForm" action="{% url 'user_login' %}" method="post">
            {% csrf_token %}
            <div class="input-group">
                <input type="text" placeholder="Email" id="PhoneField" name="phone"  required>
            </div>
           <div class="input-group">
     <input type="password" placeholder="Password" name="password" id="passwordField" required>
      <span id="togglePassword" class="eye-icon" style="color:black;"><i class="fa fa-eye"></i></span>
      </div>
            <button type="button" id="submitButton" onclick="userLogin()">Login</button>
        </form>
        <div class="toggle-form">
            <p>New user? <a href="#" onclick="toggleForms('signup-form')">Sign Up</a></p>
        </div>
    </div>

    <div id="signup-form" style="display: none;">
        <form method="post" action="{% url 'create_user' %}" enctype="multipart/form-data">
            {% csrf_token %}
            <div class="input-group">
                <label for="first_name">First Name*:</label>
                <input type="text" id="first_name" name="first_name" required>
            </div>
            <div class="input-group">
                <label for="last_name">Last Name*:</label>
                <input type="text" id="last_name" name="last_name" required>
            </div>
            <div class="input-group">
                <label for="phone">Phone no.*:</label>
                <input type="text" id="phone" name="phone" required>
            </div>
            <div class="input-group">
                <label for="phone_2">Phone no. 2:</label>
                <input type="text" id="phone_2" name="phone_2">
            </div>
            <div class="input-group">
                <label for="company_email">Company Email*:</label>
                <input type="email" id="company_email" name="company_email" required>
            </div>
            <div class="input-group">
                <label for="personal_email">Personal Email*:</label>
                <input type="email" id="personal_email" name="personal_email" required>
            </div>
            <div class="input-group">
                <label for="address">Address*:</label>
                <input type="text" id="address" name="address" required>
            </div>
            <div class="input-group">
                <label for="work_location">Work Location*:</label>
                <input type="text" id="work_location" name="work_location" required>
            </div>
            <div class="input-group">
                <label for="role">Role*:</label>
                <select id="role" name="role" required>
                    <option value="recruiter">Recruiter</option>
                    <option value="manager">Manager</option>
                </select>
            </div>
            <div class="input-group">
                <label for="password">Password*:</label>
                <input type="password" id="password" name="password" required>
            </div>
            <div class="input-group">
                <label for="picture">Upload Picture *</label>
                <input type="file" id="picture" name="picture" accept=".svg, .jpg, .jpeg, .png">
            </div>
            <input type="submit" value="Create Account" name="signup-form">
        </form>

        <div class="toggle-form">
            <p>Already have an account? <a href="#" onclick="toggleForms('login-form')">Login</a></p>
        </div>
    </div>
</div>
<script>

    const togglePassword = document.querySelector('#togglePassword');
const passwordField = document.querySelector('#password');

togglePassword.addEventListener('click', function () {
    const type = passwordField.getAttribute('type') === 'password' ? 'text' : 'password';
    passwordField.setAttribute('type', type);
    const icon = this.querySelector('i');
    icon.classList.toggle('fa-eye');
    icon.classList.toggle('fa-eye-slash');
});


    function toggleForms(formId) {
        var signupForm = document.getElementById('signup-form');
        var loginForm = document.getElementById('login-form');

        if (formId === 'signup-form') {
            signupForm.style.display = 'block';
            loginForm.style.display = 'none';
        } else {
            signupForm.style.display = 'none';
            loginForm.style.display = 'block';
        }
    }


document.addEventListener('DOMContentLoaded', function () {

    const form = document.getElementById('loginForm');
    const phoneField = document.getElementById('PhoneField');
    const passwordField = document.getElementById('passwordField');

    const clearValidation = (field) => {
        field.setCustomValidity('');
        field.style.borderColor = '';
    };

    phoneField.addEventListener('input', () => {
        clearValidation(phoneField);
    });

    passwordField.addEventListener('input', () => {
        clearValidation(passwordField);
    });
                                                  // Function to perform AJAX validation
    const validateField = (field, fieldName) => {
        const formData = new FormData();
        formData.append(fieldName, field.value.trim());

        fetch(form.action, {                              // Replace with your actual login URL
            method: 'POST',
            body: formData,
            headers: {
                'X-CSRFToken': '{{csrf_token}}'
            }
        })
        .then(response => response.json())
        .then(data => {
            clearValidation(field);                                     // Clear previous validation
            if (data.error) {
                field.setCustomValidity(data.error);
                field.reportValidity();
            } else {
                field.style.borderColor = 'green';                     // Show success feedback
                field.setCustomValidity(''); // Clear any errors
            }
        })
        .catch(error => {
            console.error('Error:', error);
        });
    };

                                                                    // Trigger validation on blur for phone number
    phoneField.addEventListener('blur', () => {
        if (phoneField.value.trim()) {
            validateField(phoneField, 'phone');
        }
    });

                                                               // Trigger validation on blur for password (only if phone is valid)
    passwordField.addEventListener('blur', () => {
        if (phoneField.value.trim() && passwordField.value.trim()) {
            const formData = new FormData();
            formData.append('phone', phoneField.value.trim());
            formData.append('password', passwordField.value.trim());

            fetch(form.action, {  // Replace with your actual login URL
                method: 'POST',
                body: formData,
                headers: {
                    'X-CSRFToken': '{{csrf_token}}'
                }
            })
            .then(response => response.json())
            .then(data => {
                clearValidation(passwordField); // Clear previous validation
                if (data.error) {
                    passwordField.setCustomValidity(data.error);
                    passwordField.reportValidity();
                } else {
                    passwordField.setCustomValidity('');
                }
            })
            .catch(error => {
                console.error('Error:', error);
            });
        }
    });

    window.userLogin = function() {
        const form = document.getElementById('loginForm');
        const formData = new FormData(form);

        fetch(form.action, {
            method: 'POST',
            body: formData,
            headers: {
                'X-CSRFToken': '{{csrf_token}}'
            }
        })
        .then(response => response.json())
        .then(data => {
            if (data.error) {
                if (data.error.includes('phone number')) {
                    phoneField.setCustomValidity(data.error);
                    phoneField.reportValidity();
                } else if (data.error.includes('password')) {
                    passwordField.setCustomValidity(data.error);
                    passwordField.reportValidity();
                }
            } else if (data.success) {
                window.location.href = data.redirect_url;
            }
        })
        .catch(error => {
            console.error('Error:', error);
        });
    }
});




</script>
</body>
</html>
