{% extends "base.html" %}
{% block content %}
<div class="flex justify-center mt-10">
  <div class="w-full max-w-md">
    <div class="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4">
      <h2 class="text-2xl font-bold text-irish-green mb-6 text-center">Set New Password</h2>
      
      {% if error %}
      <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4">
        {{ error }}
      </div>
      {% endif %}
      
      <form method="POST" action="/auth/reset-password" id="password-form" onsubmit="return validateForm()">
        <input type="hidden" name="token" value="{{ token }}">
        
        <div class="mb-4">
          <label class="block text-gray-700 text-sm font-bold mb-2" for="new_password">
            New Password
          </label>
          <input 
            class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" 
            id="new_password" 
            name="new_password" 
            type="password" 
            placeholder="Enter your new password"
            required
            oninput="checkPasswordStrength()"
          >
          <div class="mt-2">
            <div class="w-full bg-gray-200 rounded-full h-2.5">
              <div class="bg-red-600 h-2.5 rounded-full" id="password-strength-meter" style="width: 0%"></div>
            </div>
            <p class="text-xs mt-1" id="password-strength-text">Password strength: Too weak</p>
          </div>
          <ul class="text-xs text-gray-600 mt-2 list-disc pl-5">
            <li id="length-check" class="text-red-500">At least 8 characters</li>
            <li id="lowercase-check" class="text-red-500">At least one lowercase letter</li>
            <li id="uppercase-check" class="text-red-500">At least one uppercase letter</li>
            <li id="number-check" class="text-red-500">At least one number</li>
            <li id="special-check" class="text-red-500">At least one special character</li>
          </ul>
        </div>
        
        <div class="mb-6">
          <label class="block text-gray-700 text-sm font-bold mb-2" for="confirm_password">
            Confirm New Password
          </label>
          <input 
            class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" 
            id="confirm_password" 
            name="confirm_password" 
            type="password" 
            placeholder="Confirm your new password"
            required
            oninput="checkPasswordMatch()"
          >
          <p id="password-match" class="text-xs mt-1 hidden text-red-500">Passwords do not match</p>
        </div>
        
        <div class="flex items-center justify-center">
          <button 
            class="bg-irish-green hover:bg-opacity-90 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline w-full" 
            type="submit"
            id="submit-button"
          >
            Reset Password
          </button>
        </div>
      </form>
    </div>
  </div>
</div>

<script>
  function checkPasswordStrength() {
    const password = document.getElementById('new_password').value;
    const meter = document.getElementById('password-strength-meter');
    const strengthText = document.getElementById('password-strength-text');
    
    // Check requirements
    const hasLength = password.length >= 8;
    const hasLower = /[a-z]/.test(password);
    const hasUpper = /[A-Z]/.test(password);
    const hasNumber = /\d/.test(password);
    const hasSpecial = /[!@#$%^&*(),.?":{}|<>]/.test(password);
    
    // Update requirement indicators
    document.getElementById('length-check').className = hasLength ? 'text-green-500' : 'text-red-500';
    document.getElementById('lowercase-check').className = hasLower ? 'text-green-500' : 'text-red-500';
    document.getElementById('uppercase-check').className = hasUpper ? 'text-green-500' : 'text-red-500';
    document.getElementById('number-check').className = hasNumber ? 'text-green-500' : 'text-red-500';
    document.getElementById('special-check').className = hasSpecial ? 'text-green-500' : 'text-red-500';
    
    // Calculate strength percentage (20% for each criteria)
    let strength = 0;
    if (hasLength) strength += 20;
    if (hasLower) strength += 20;
    if (hasUpper) strength += 20;
    if (hasNumber) strength += 20;
    if (hasSpecial) strength += 20;
    
    // Update meter
    meter.style.width = `${strength}%`;
    
    // Set color based on strength
    if (strength < 40) {
      meter.className = 'bg-red-600 h-2.5 rounded-full';
      strengthText.textContent = 'Password strength: Too weak';
      strengthText.className = 'text-xs mt-1 text-red-600';
    } else if (strength < 80) {
      meter.className = 'bg-yellow-500 h-2.5 rounded-full';
      strengthText.textContent = 'Password strength: Medium';
      strengthText.className = 'text-xs mt-1 text-yellow-600';
    } else {
      meter.className = 'bg-green-500 h-2.5 rounded-full';
      strengthText.textContent = 'Password strength: Strong';
      strengthText.className = 'text-xs mt-1 text-green-600';
    }
  }
  
  function checkPasswordMatch() {
    const password = document.getElementById('new_password').value;
    const confirmPassword = document.getElementById('confirm_password').value;
    const matchMessage = document.getElementById('password-match');
    
    if (confirmPassword) {
      if (password !== confirmPassword) {
        matchMessage.classList.remove('hidden');
      } else {
        matchMessage.classList.add('hidden');
      }
    }
  }
  
  function validateForm() {
    const password = document.getElementById('new_password').value;
    const confirmPassword = document.getElementById('confirm_password').value;
    
    // Check if passwords match
    if (password !== confirmPassword) {
      alert('Passwords do not match.');
      return false;
    }
    
    // Check password requirements
    const hasLength = password.length >= 8;
    const hasLower = /[a-z]/.test(password);
    const hasUpper = /[A-Z]/.test(password);
    const hasNumber = /\d/.test(password);
    const hasSpecial = /[!@#$%^&*(),.?":{}|<>]/.test(password);
    
    if (!hasLength || !hasLower || !hasUpper || !hasNumber || !hasSpecial) {
      alert('Password does not meet the strength requirements.');
      return false;
    }
    
    return true;
  }
  
  // Initial check on page load
  window.onload = function() {
    if (document.getElementById('new_password').value) {
      checkPasswordStrength();
    }
    if (document.getElementById('confirm_password').value) {
      checkPasswordMatch();
    }
  };
</script>
{% endblock %}
