<?php
date_default_timezone_set("Asia/Kolkata");
require_once 'web_constant.php';
// sync_and_show.php
// Source and destination (update if you need)
//$source = 'F:\\webserver\\UwAmp\\www\\scanner\\uploads';
//$destination = 'E:\\backup';

$source = web_constant::$document_path;
$destination = web_constant::$document_bakup_path;

$homeUrl = 'home'; // change to your desired home link

// --- HEAD (send immediately) ---
ob_implicit_flush(true);
while (ob_get_level() > 0) ob_end_flush();
header("Content-Type: text/html; charset=utf-8");
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Below files are copied</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<style>
    :root{
        --bg:#f7fbff;
        --card:#ffffff;
        --accent:#2563eb;
        --muted:#6b7280;
        --success:#10b981;
        --mono:'Courier New',Courier,monospace;
    }
    body{
        margin:0;
        font-family:Inter,system-ui,-apple-system,"Segoe UI",Roboto,"Helvetica Neue",Arial;
        background:linear-gradient(180deg,#f0f8ff 0%,#fff 100%);
        color:#0f172a;
        padding:40px 16px;
    }
    .wrap{max-width:940px;margin:0 auto;}
    .card{
        background:var(--card);
        border-radius:12px;
        box-shadow:0 6px 18px rgba(15,23,42,0.08);
        padding:28px;
        border:1px solid rgba(15,23,42,0.04);
    }
    h1{
        margin:0;
        font-size:22px;
        text-align:center;
        color:var(--accent);
        letter-spacing:0.3px;
    }
    .topbar{
        display:flex;
        justify-content:space-between;
        align-items:center;
        margin-bottom:16px;
    }
    .home-btn{
        display:inline-flex;
        align-items:center;
        justify-content:center;
        background:var(--accent);
        color:white;
        text-decoration:none;
        font-size:40px;
        width:42px;
        height:42px;
        border-radius:50%;
        transition:0.25s ease;
        box-shadow:0 4px 10px rgba(37,99,235,0.25);
    }
    .home-btn:hover{
        background:#1e40af;
        transform:scale(1.1);
    }
    .meta{
        text-align:center;
        color:var(--muted);
        font-size:13px;
        margin-bottom:18px;
    }
    .list{
        background:linear-gradient(180deg,rgba(10,20,40,0.02),rgba(255,255,255,0.02));
        border-radius:8px;
        padding:14px;
        max-height:520px;
        overflow:auto;
        border:1px dashed rgba(37,99,235,0.08);
        font-family:var(--mono);
        font-size:13px;
    }
    .path{
        padding:6px 8px;
        margin:4px 0;
        border-radius:6px;
        background:rgba(16,185,129,0.05);
        color:#064e3b;
        display:block;
        white-space:pre-wrap;
        word-break:break-all;
        animation:fadeIn 0.5s ease;
    }
    @keyframes fadeIn{
        from{opacity:0;transform:translateY(6px);}
        to{opacity:1;transform:translateY(0);}
    }
    .summary{
        margin-top:16px;
        font-size:14px;
        color:var(--muted);
        display:flex;
        justify-content:space-between;
        align-items:center;
        gap:12px;
    }
    .count{
        background:rgba(37,99,235,0.08);
        color:var(--accent);
        padding:6px 10px;
        border-radius:999px;
        font-weight:600;
    }
</style>
</head>
<body>
<div class="wrap">
    <div class="card" role="main" aria-live="polite">
        <div class="topbar">
            <a href="<?php echo htmlspecialchars($homeUrl, ENT_QUOTES); ?>" class="home-btn" title="Go Home">&#8962;</a>
            <h1>Below files are copied</h1>
            <div style="width:42px;"></div>
        </div>
        <div class="meta">Source: <strong><?php echo htmlspecialchars($source); ?></strong><br>
        Destination: <strong><?php echo htmlspecialchars($destination); ?></strong></div>

        <div class="list" id="output"></div>

        <div class="summary" id="summary">
            <div class="count">0 copied</div>
            <div class="meta">Started: <?php echo date('Y-m-d H:i:s'); ?></div>
        </div>
    </div>
</div>

<script>
let count=0;
function addPath(p){
  const list=document.getElementById('output');
  const div=document.createElement('div');
  div.className='path';
  div.textContent=p;
  list.appendChild(div);
  list.scrollTop=list.scrollHeight;
  count++;
  document.querySelector('.count').textContent=count+" copied";
}
</script>
<?php
flush();

/**
 * Recursively copy missing files and stream UI updates.
 */
function streamCopy($src,$dst){
    $dh=@opendir($src);
    if(!$dh) return;
    if(!is_dir($dst)) @mkdir($dst,0777,true);
    while(false!==($file=readdir($dh))){
        if($file=='.'||$file=='..') continue;
        $srcPath=$src.DIRECTORY_SEPARATOR.$file;
        $dstPath=$dst.DIRECTORY_SEPARATOR.$file;
        if(is_dir($srcPath)){
            streamCopy($srcPath,$dstPath);
        }else{
            if(!file_exists($dstPath)){
                if(@copy($srcPath,$dstPath)){
                    echo "<script>addPath(".json_encode($dstPath).");</script>";
                    flush();
                }
            }
        }
    }
    closedir($dh);
}

// Start copying and streaming output
streamCopy($source,$destination);

// End summary
echo "<script>document.getElementById('summary').insertAdjacentHTML('beforeend', '<div class=\"meta\">Completed: ".date('Y-m-d H:i:s')."</div>');</script>";
flush();
?>
</body>
</html>

