<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>パスワードのリセット</title>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
            background-color: #f0f2f5;
            margin: 0;
            padding: 20px;
            line-height: 1.6;
        }
        .container {
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            padding: 20px;
            max-width: 400px;
            margin: 0 auto;
        }
        h1 {
            color: #1a1a1a;
            text-align: center;
            margin-bottom: 20px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            color: #4a4a4a;
        }
        input[type="password"] {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        button {
            width: 100%;
            padding: 10px;
            background-color: #0056b3;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #003d82;
        }
        .message {
            margin-top: 15px;
            padding: 10px;
            border-radius: 4px;
            text-align: center;
        }
        .message.error {
            background-color: #ffe6e6;
            color: #d8000c;
        }
        .message.success {
            background-color: #e6ffe6;
            color: #006400;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>岐阜ナビ：パスワードのリセット</h1>
        <form id="reset-form">
            <div class="form-group">
                <label for="password">新しいパスワード</label>
                <input type="password" id="password" name="password" required>
            </div>
            <div class="form-group">
                <label for="confirm-password">パスワードの確認</label>
                <input type="password" id="confirm-password" name="confirm-password" required>
            </div>
            <button type="submit">パスワードをリセット</button>
        </form>
        <div id="message" class="message"></div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const currentUrl = window.location.href;
            
            function extractParams(url) {
                const regex = /\/reset-password\/([^\/]+)\/([^\/]+)\/?/;
                const match = url.match(regex);
                if (match) {
                    return { uidb64: match[1], token: match[2] };
                }
                const urlParams = new URLSearchParams(new URL(url).search);
                return {
                    uidb64: urlParams.get('uidb64') || '',
                    token: urlParams.get('token') || ''
                };
            }

            const { uidb64, token } = extractParams(currentUrl);

            document.getElementById('reset-form').addEventListener('submit', async function(e) {
                e.preventDefault();
                const password = document.getElementById('password').value;
                const confirmPassword = document.getElementById('confirm-password').value;
                const messageElement = document.getElementById('message');

                if (password !== confirmPassword) {
                    messageElement.textContent = 'パスワードが一致しません。';
                    messageElement.className = 'message error';
                    return;
                }

                if (!uidb64 || !token) {
                    messageElement.textContent = '無効なリセットリンクです。';
                    messageElement.className = 'message error';
                    return;
                }

                try {
                    const response = await fetch(`/api/reset-password/${uidb64}/${token}/`, {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json',
                        },
                        body: JSON.stringify({
                            new_password: password,
                            confirm_password: confirmPassword
                        }),
                    });

                    const data = await response.json();

                    if (response.ok) {
                        messageElement.textContent = 'パスワードが正常にリセットされました。';
                        messageElement.className = 'message success';
                    } else {
                        messageElement.textContent = `パスワードのリセットに失敗しました。エラー: ${data.error || response.statusText}`;
                        messageElement.className = 'message error';
                    }
                } catch (error) {
                    console.error('Error:', error);
                    messageElement.textContent = 'エラーが発生しました。もう一度お試しください。';
                    messageElement.className = 'message error';
                }
            });
        });
    </script>
</body>
</html>
