document.title = "内容审核";

// var link = document.createElement('link');
// link.rel = 'shortcut icon';
// link.type = 'image/x-icon';
// link.href = '/favicon.ico'

// var existingLink = document.querySelector('link[rel="shortcut icon"]');
// if (existingLink) {
//   existingLink.parentNode.removeChild(existingLink);
// }

// document.head.appendChild(link);
function appendAgreement() {
    if(document.querySelectorAll('input[type="password"]').length >= 3){
        const existingContainer = document.querySelector('.agreement-container');
        if (existingContainer) {
            existingContainer.remove();
        }
        return;
    }
    const element = document.querySelector('.relative.rounded.p-10.sm\\:shadow-md.bg-white.w-screen.max-w-md');
    if (element && !element.querySelector('.agreement-container')) {
        // 创建容器 div
        const container = document.createElement('div');
        container.classList.add('agreement-container');

        // 创建复选框
        const checkbox = document.createElement('input');
        checkbox.type = 'checkbox';
        checkbox.id = 'agreement';

        // 创建标签
        const label = document.createElement('label');
        label.htmlFor = 'agreement';
        label.style.marginLeft = '5px'; // 增加间距
        label.innerHTML = '我已阅读并同意';

        // 创建链接
        // const userAgreementLink = document.createElement('a');
        // userAgreementLink.href = '#';
        // userAgreementLink.style.color = 'blue';
        // userAgreementLink.textContent = '用户协议';

        const privacyPolicyLink = document.createElement('a');
        privacyPolicyLink.href = '/privacy-policy/index.html';
        privacyPolicyLink.style.color = 'blue';
        // privacyPolicyLink.style.marginLeft = '5px'; // 增加间距
        privacyPolicyLink.textContent = '隐私政策';
        privacyPolicyLink.target = '_blank';

        // const serviceAgreementLink = document.createElement('a');
        // serviceAgreementLink.href = '#';
        // serviceAgreementLink.style.color = 'blue';
        // serviceAgreementLink.style.marginLeft = '5px'; // 增加间距
        // serviceAgreementLink.textContent = '产品服务协议';

        // 拼接标签和链接
        // label.appendChild(userAgreementLink);
        // label.innerHTML += '、';
        label.appendChild(privacyPolicyLink);
        // label.innerHTML += '、';
        // label.appendChild(serviceAgreementLink);


        const privacyMessage = document.createElement('div');
        privacyMessage.classList.add('privacy-message', 'shake');
        privacyMessage.id = 'privacyMessage'; // 增加间距
        privacyMessage.textContent = '请阅读并同意隐私政策';


        // 将复选框和标签添加到容器中
        container.appendChild(checkbox);
        container.appendChild(label);
        container.appendChild(privacyMessage);
        

        // 在提交按钮前插入容器
        element.appendChild(container);

        const ssoBtn = document.querySelector('.w-full.border.border-gray-300.justify-center.hover\\:bg-gray-50.text-gray-800.py-2.px-4.rounded.inline-flex.items-center')

        if(ssoBtn){
            ssoBtn.addEventListener('click', function(event) {
                var privacyPolicyCheckbox = document.getElementById('agreement');
                var privacyMessage = document.getElementById('privacyMessage');
                if (!privacyPolicyCheckbox.checked) {
                    privacyMessage.classList.remove('show');
                    void privacyMessage.offsetWidth;
                    privacyMessage.classList.add('show');
                    event.preventDefault(); // 阻止默认行为
                }else{
                    privacyMessage.classList.remove('show');
                }
            });
        }

        
        document.querySelector('.group.relative.w-full.justify-center.rounded-md.py-2.px-4.border-transparent.text-sm.leading-5.font-medium.text-white.bg-blue-600.hover\\:bg-blue-500.focus\\:outline-none.focus\\:border-blue-700.focus\\:shadow-outline-blue.active\\:bg-blue-700.transition.duration-150.ease-in-out').addEventListener('click', function(event) {
            var privacyPolicyCheckbox = document.getElementById('agreement');
            var privacyMessage = document.getElementById('privacyMessage');
            if (!privacyPolicyCheckbox.checked) {
                privacyMessage.classList.remove('show');
                void privacyMessage.offsetWidth;
                privacyMessage.classList.add('show');
                event.preventDefault(); // 阻止默认行为
            }else{
                privacyMessage.classList.remove('show');
            }
        });

        document.querySelector('span.px-2.bg-white.text-gray-500').textContent = '外部用户请使用以下登录方式';
        document.getElementById('loginId').placeholder = '外部邮箱';
        document.getElementById('password').placeholder = '密码';
        document.querySelector('button[type="submit"]').textContent = '非百事员工登录';
    }
}

const observer = new MutationObserver(function() {
    appendAgreement();
});

function startObserver() {
    if (document.body) {
        observer.observe(document.body, { childList: true, subtree: true });
        appendAgreement();
    } else {
        setTimeout(startObserver, 100);
    }
}

startObserver();


function showUpdatePasswordTitle(){
    // 判断方式是页面有3个input password 框

    if(document.querySelectorAll('input[type="password"]').length >= 3){
        // 将class 为 mt-2 text-left text-2xl leading-9 font-extrabold text-gray-900 的h2设置为显示
        const titleElement = document.querySelector('h2.mt-2.text-left.text-2xl.leading-9.font-extrabold.text-gray-900');
        if(titleElement){
            titleElement.style.display = 'block';
            titleElement.textContent = '请更新您的密码';
        }
    }else{
        setTimeout(showUpdatePasswordTitle, 100);
    }
}

setTimeout(showUpdatePasswordTitle, 100);
