<?php
require_once 'config/db.php';

if (!isset($_GET['id'])) {
    header("Location: enquiries.php");
    exit;
}

$id = (int)$_GET['id'];

try {
    $stmt = $pdo->prepare("SELECT * FROM enquiries WHERE id = ?");
    $stmt->execute([$id]);
    $enq = $stmt->fetch();
    
    if (!$enq) {
        $error = "Enquiry not found.";
    }
} catch (PDOException $e) {
    $error = "Error fetching enquiry: " . $e->getMessage();
}

require_once 'includes/header.php';

// Format the phone number to be WhatsApp friendly
$clean_phone = '';
if (isset($enq['phone'])) {
    $clean_phone = preg_replace('/[^0-9]/', '', $enq['phone']);
    if (strlen($clean_phone) == 10) {
        $clean_phone = '91' . $clean_phone; // add India country code if it's 10 digits
    }
}
?>

<div class="dashboard-content">
    <div class="welcome-banner" style="margin-bottom: 20px; padding: 20px; display: flex; align-items: center; justify-content: space-between;">
        <div>
            <h2><i class="fa-solid fa-file-invoice"></i> Enquiry Details #<?= isset($enq['id']) ? str_pad($enq['id'], 4, '0', STR_PAD_LEFT) : '0000' ?></h2>
            <p>Full review of the submitted quote request.</p>
        </div>
        <div>
            <a href="enquiries.php" style="color: var(--text-main); text-decoration: none; font-weight: 500; margin-right: 15px;"><i class="fa-solid fa-arrow-left"></i> Back to Enquiries</a>
            <?php if (isset($enq['id'])): ?>
                <a href="lead_add_edit.php?enquiry_id=<?= $enq['id'] ?>" style="display:inline-flex; align-items:center; padding: 8px 15px; background:var(--dark); color:var(--white); border-radius:6px; text-decoration: none; font-weight: 600; font-size: 0.9rem;"><i class="fa-solid fa-arrow-right-arrow-left" style="margin-right:8px;"></i> Convert To Lead</a>
            <?php endif; ?>
        </div>
    </div>

    <?php if (isset($error)): ?>
        <div class="badge badge-pending" style="display:block; padding:15px; margin-bottom: 20px; background-color: #fee2e2; color: #ef4444; font-size:1rem;"><?= htmlspecialchars($error) ?></div>
    <?php elseif (isset($enq)): ?>

        <div class="data-card" style="max-width: 800px; padding: 30px;">
            <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 25px; border-bottom: 1px solid #e2e8f0; padding-bottom: 15px;">
                <h3 style="margin: 0; color: #0f172a; font-size: 1.4rem; font-weight: 700;"><?= htmlspecialchars($enq['full_name']) ?></h3>
                <span style="color:#64748b; font-size:0.9rem;">
                    <i class="fa-regular fa-calendar" style="margin-right: 5px;"></i> <?= date('F d, Y - h:i A', strtotime($enq['created_at'])) ?>
                </span>
            </div>

            <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-bottom: 30px;">
                <div style="background: #f8fafc; padding: 20px; border-radius: 8px; border: 1px solid #e2e8f0;">
                    <p style="margin: 0 0 5px 0; color: #64748b; font-size: 0.85rem; font-weight: 600; text-transform: uppercase;">Service Needed</p>
                    <p style="margin: 0; font-size: 1.1rem; font-weight: 600; color: var(--primary);"><i class="fa-solid fa-tags" style="margin-right: 5px; font-size: 0.9rem;"></i> <?= htmlspecialchars($enq['interest']) ?></p>
                </div>
                
                <div style="background: #f8fafc; padding: 20px; border-radius: 8px; border: 1px solid #e2e8f0;">
                    <p style="margin: 0 0 5px 0; color: #64748b; font-size: 0.85rem; font-weight: 600; text-transform: uppercase;">Contact Info</p>
                    <div style="margin-bottom: 8px;">
                        <i class="fa-solid fa-phone" style="color:#94a3b8; width: 20px;"></i> 
                        <span style="font-weight: 500;"><?= htmlspecialchars($enq['phone']) ?></span>
                    </div>
                    <div>
                        <i class="fa-solid fa-envelope" style="color:#94a3b8; width: 20px;"></i>
                        <span style="font-weight: 500;"><?= htmlspecialchars($enq['email']) ?></span>
                    </div>
                </div>
            </div>

            <div style="margin-bottom: 30px;">
                <p style="margin: 0 0 10px 0; color: #64748b; font-size: 0.85rem; font-weight: 600; text-transform: uppercase;">Client Message</p>
                <div style="background: #fff; padding: 20px; border-radius: 8px; border: 1px solid #cbd5e1; font-size: 1.05rem; line-height: 1.6; color: #334155; box-shadow: inset 0 2px 4px rgba(0,0,0,0.02);">
                    <?= nl2br(htmlspecialchars($enq['message'])) ?>
                </div>
            </div>

            <div style="border-top: 1px solid #e2e8f0; padding-top: 20px; display: flex; gap: 15px; flex-wrap: wrap;">
                <a href="tel:<?= htmlspecialchars($clean_phone) ?>" style="display: flex; align-items: center; justify-content: center; gap: 8px; flex: 1; background: #0f172a; color: #fff; text-decoration: none; padding: 12px 20px; border-radius: 6px; font-weight: 600; transition: 0.3s; min-width: 150px;">
                    <i class="fa-solid fa-phone-volume"></i> Click to Call
                </a>
                
                <a href="https://wa.me/<?= htmlspecialchars($clean_phone) ?>" target="_blank" style="display: flex; align-items: center; justify-content: center; gap: 8px; flex: 1; background: #25D366; color: #fff; text-decoration: none; padding: 12px 20px; border-radius: 6px; font-weight: 600; transition: 0.3s; min-width: 150px;">
                    <i class="fa-brands fa-whatsapp"></i> WhatsApp Chat
                </a>
                
                <a href="mailto:<?= htmlspecialchars($enq['email']) ?>?subject=Reply%to%Your%Enquiry%Request%-%MK%Construction" style="display: flex; align-items: center; justify-content: center; gap: 8px; flex: 1; background: var(--primary); color: #fff; text-decoration: none; padding: 12px 20px; border-radius: 6px; font-weight: 600; transition: 0.3s; min-width: 150px;">
                    <i class="fa-solid fa-paper-plane"></i> Send Email
                </a>
            </div>
        </div>

    <?php endif; ?>
</div>

<?php require_once 'includes/footer.php'; ?>
