<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Ultra Fast Mass SMTP Checker</title>
<style>
body{background:#0f172a;color:#e2e8f0;font-family:'Segoe UI',sans-serif;padding:20px;}
.container{max-width:1000px;margin:auto;background:#1e293b;padding:25px;border-radius:12px;box-shadow:0 5px 25px rgba(0,0,0,0.5);}
h1{color:#22c55e;text-align:center;}
textarea,input[type=file]{padding:12px;border-radius:8px;border:none;background:#0f172a;color:#fff;width:100%;box-sizing:border-box;}
button{padding:12px;border:none;border-radius:8px;font-weight:bold;color:#000;background:#22c55e;cursor:pointer;transition:0.3s;}
button:hover{background:#16a34a;}
.bar-wrap{background:#334155;height:22px;border-radius:12px;overflow:hidden;margin:20px 0;}
.bar{height:100%;width:0%;background:linear-gradient(90deg,#22c55e,#06b6d4);}
.log{background:#020617;padding:15px;height:400px;overflow:auto;border-radius:10px;font-family:monospace;margin-top:10px;}
.ok{color:#22c55e;}
.fail{color:#ef4444;}
.filter-btn{padding:6px 12px;margin:5px;border:none;border-radius:6px;cursor:pointer;}
.filter-btn:hover{opacity:0.8;}
.current-check{font-weight:bold;color:#facc15;margin-top:10px;}
</style>
</head>
<body>
<div class="container">
<h1>⚡ Ultra Fast Mass SMTP Checker</h1>

<textarea id="manual_input" placeholder="host|port|user|pass|secure TLS/SSL per line"></textarea>
<input type="file" id="file_input">
<button id="start_btn">Start Check</button>

<div class="current-check" id="current">Belum memulai pengecekan</div>
<div class="bar-wrap"><div class="bar" id="bar"></div></div>
<div>
<button class="filter-btn" onclick="filterLog('all')">All</button>
<button class="filter-btn" onclick="filterLog('ok')">SUCCESS</button>
<button class="filter-btn" onclick="filterLog('fail')">FAIL</button>
</div>
<div class="log" id="log"></div>
</div>

<script>
let tasks = [];
let total = 0;
let processed = 0;

document.getElementById('start_btn').addEventListener('click',()=>{
    const manual = document.getElementById('manual_input').value.split("\n").map(l=>l.trim()).filter(l=>l);
    tasks = [];
    manual.forEach(l=>{
        const p=l.split('|');
        if(p.length<4) return;
        tasks.push({host:p[0],port:p[1],user:p[2],pass:p[3],secure:p[4]||''});
    });

    const fileElem = document.getElementById('file_input');
    if(fileElem.files.length>0){
        const fr = new FileReader();
        fr.onload=function(e){
            e.target.result.split("\n").map(l=>l.trim()).filter(l=>l).forEach(l=>{
                const p=l.split('|'); if(p.length<4) return;
                tasks.push({host:p[0],port:p[1],user:p[2],pass:p[3],secure:p[4]||''});
            });
            startChecking();
        };
        fr.readAsText(fileElem.files[0]);
    } else {
        startChecking();
    }
});

function startChecking(){
    total = tasks.length;
    processed = 0;
    document.getElementById('log').innerHTML='';
    runNext();
}

let maxSimultaneous = 20; // 20 pseudo threads
let running = 0;

function runNext(){
    while(running < maxSimultaneous && tasks.length>0){
        const t = tasks.shift();
        running++;
        checkSMTP(t);
    }
    if(running===0 && tasks.length===0){
        document.getElementById('current').innerText='✅ Semua pengecekan selesai!';
    }
}

function checkSMTP(t){
    document.getElementById('current').innerText=`🔄 Mengecek ${t.host} (${t.user}) ...`;
    const form = new FormData();
    form.append('host',t.host);
    form.append('port',t.port);
    form.append('user',t.user);
    form.append('pass',t.pass);
    form.append('secure',t.secure);

    fetch('check_smtp_ajax.php',{method:'POST',body:form})
.then(r=>r.json())
.then(res=>{
    processed++;
    let line = document.createElement('div');
    if(res.success){
        line.innerHTML=`[${processed}] ${t.host}:${t.port} (${t.user}) → <span class='ok'>SUCCESS ✅</span> (${res.message})`;
        line.dataset.status='ok';
    }else{
        line.innerHTML=`[${processed}] ${t.host}:${t.port} (${t.user}) → <span class='fail'>FAIL ❌</span> (${res.message})`;
        line.dataset.status='fail';
    }
    document.getElementById('log').appendChild(line);
    document.getElementById('log').scrollTop=document.getElementById('log').scrollHeight;
    document.querySelector('.bar').style.width=Math.round((processed/total)*100)+'%';
    running--;
    runNext();
})
    .catch(err=>{
        processed++;
        let line = document.createElement('div');
        line.innerHTML=`[${processed}] ${t.host}:${t.port} (${t.user}) → <span class='fail'>ERROR ❌</span>`;
        line.dataset.status='fail';
        document.getElementById('log').appendChild(line);
        document.querySelector('.bar').style.width=Math.round((processed/total)*100)+'%';
        running--;
        runNext();
    });
}

function filterLog(status){
    document.querySelectorAll('#log div').forEach(d=>{
        if(status==='all'){ d.style.display='block'; }else{ d.style.display=(d.dataset.status===status)?'block':'none'; }
    });
}
</script>
</body>
</html>