<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>My Contract Summary - Safe Step Properties Limited</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <style>
        :root {
            --brand-blue: #125db8;
      --brand-blue-dark: #07356f;
      --brand-blue-soft: #eaf3ff;
      --brand-green: #3baa15;
      --brand-green-dark: #257b0b;
      --ink: #0b1b33;
      --muted: #5d6a7c;
      --line: #dbe4ef;
      --surface: #f6f9fc;
      --surface-strong: #eef5fb;
      --white: #ffffff;
      --footer: #06172d;
      --shadow: 0 18px 50px rgba(7, 53, 111, 0.12);
      --shadow-soft: 0 10px 28px rgba(7, 53, 111, 0.08);
      --radius: 8px;
      --container: 1180px;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
            background:
                radial-gradient(circle at top left, rgba(59, 170, 21, 0.10), transparent 24%),
                radial-gradient(circle at bottom right, rgba(18, 93, 184, 0.07), transparent 26%),
                var(--surface);
            padding: 20px;
            color: var(--ink);
        }

        .page-wrapper {
            max-width: 1050px;
            margin: 0 auto;
            position: relative;
        }

        .watermark {
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 70vw;
            max-width: 760px;
            opacity: 0.045;
            z-index: 0;
            pointer-events: none;
        }

        .watermark img {
            width: 100%;
            height: auto;
            display: block;
        }

        .controls {
            text-align: right;
            margin-bottom: 14px;
            position: relative;
            z-index: 2;
        }

        .btn {
            display: inline-block;
            border: 1px solid var(--brand-blue);
            background: linear-gradient(135deg, var(--brand-blue), var(--brand-green));
            color: #ffffff;
            padding: 9px 18px;
            font-size: 12px;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: 0.08em;
            border-radius: 10px;
            cursor: pointer;
            margin-left: 8px;
            box-shadow: 0 10px 22px rgba(18, 93, 184, 0.14);
        }

        .btn.secondary {
            background: #ffffff;
            color: var(--brand-blue);
            border: 1px solid var(--line);
            box-shadow: none;
        }

        .btn:hover {
            opacity: 0.96;
        }

        .card {
            background: rgba(255, 255, 255, 0.96);
            border: 1px solid var(--line);
            border-radius: 24px;
            padding: 28px 30px 24px 30px;
            position: relative;
            z-index: 1;
            box-shadow: 0 20px 50px rgba(18, 93, 184, 0.08);
            overflow: hidden;
        }

        .card::before {
            content: "";
            position: absolute;
            inset: 0;
            pointer-events: none;
            background:
                radial-gradient(circle at top right, rgba(59, 170, 21, 0.06), transparent 18%),
                radial-gradient(circle at bottom left, rgba(18, 93, 184, 0.04), transparent 18%);
        }

        .header {
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
            border-bottom: 1px solid #dbe4ef;
            padding-bottom: 18px;
            margin-bottom: 22px;
            gap: 20px;
        }

        .company-block {
            display: flex;
            align-items: flex-start;
            gap: 16px;
            min-width: 0;
        }

        .company-logo {
            width: 120px;
            height: auto;
            object-fit: contain;
            flex-shrink: 0;
            display: block;
        }

        .company-text h1 {
            font-size: 20px;
            font-weight: 800;
            letter-spacing: 0.08em;
            text-transform: uppercase;
            color: var(--ink);
            margin-bottom: 6px;
            line-height: 1.2;
        }

        .company-text p {
            font-size: 11px;
            line-height: 1.65;
            color: var(--muted);
        }

        .meta-top {
            text-align: right;
            font-size: 11px;
            min-width: 220px;
        }

        .meta-top h2 {
            font-size: 14px;
            text-transform: uppercase;
            letter-spacing: 0.14em;
            color: var(--brand-blue);
            margin-bottom: 10px;
            font-weight: 800;
        }

        .meta-top span {
            display: block;
            color: var(--muted);
            line-height: 1.7;
        }

        .meta-id {
            font-weight: 700;
            font-size: 13px;
            color: var(--ink) !important;
        }

        .section {
            margin-bottom: 20px;
        }

        .section-title {
            font-size: 11px;
            text-transform: uppercase;
            letter-spacing: 0.14em;
            color: var(--muted);
            margin-bottom: 8px;
            font-weight: 700;
        }

        .summary-grid {
            display: grid;
            grid-template-columns: repeat(4, minmax(0, 1fr));
            gap: 14px;
            margin-bottom: 4px;
        }

        .summary-card {
            border: 1px solid var(--line);
            border-radius: 16px;
            padding: 12px 14px;
            font-size: 12px;
            background: rgba(255, 255, 255, 0.96);
        }

        .summary-label {
            font-size: 10px;
            color: var(--muted);
            text-transform: uppercase;
            letter-spacing: 0.08em;
            margin-bottom: 5px;
            font-weight: 700;
        }

        .summary-value {
            font-size: 18px;
            font-weight: 800;
            color: var(--ink);
        }

        .summary-value.outstanding {
            color: #b42318;
        }

        .summary-value.positive {
            color: var(--brand-green-dark);
        }

        .plan-grid {
            display: grid;
            grid-template-columns: repeat(3, minmax(0, 1fr));
            gap: 14px;
            font-size: 12px;
        }

        .plan-grid > div {
            border: 1px solid var(--line);
            border-radius: 14px;
            padding: 12px 14px;
            background: rgba(255, 255, 255, 0.95);
        }

        .plan-label {
            color: var(--muted);
            margin-bottom: 5px;
            font-size: 10px;
            text-transform: uppercase;
            letter-spacing: 0.08em;
            font-weight: 700;
        }

        .plan-value {
            font-weight: 700;
            font-size: 14px;
            color: var(--ink);
        }

        .table-title {
            font-size: 11px;
            text-transform: uppercase;
            letter-spacing: 0.14em;
            color: var(--muted);
            margin: 20px 0 8px 0;
            font-weight: 700;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            font-size: 12px;
            margin-bottom: 8px;
            background: rgba(255, 255, 255, 0.96);
            border-radius: 14px;
            overflow: hidden;
        }

        thead th {
            border: 1px solid #dbe4ef;
            background: #eaf3ff;
            padding: 9px 10px;
            text-align: left;
            font-weight: 700;
            color: var(--muted);
            font-size: 10px;
            text-transform: uppercase;
            letter-spacing: 0.08em;
        }

        tbody td {
            border: 1px solid #dbe4ef;
            padding: 9px 10px;
            color: var(--ink);
        }

        .amount-cell {
            text-align: right;
            white-space: nowrap;
            font-weight: 700;
        }

        .plot-code {
            font-weight: 700;
            color: var(--brand-blue);
        }

        .status-badge {
            display: inline-block;
            padding: 4px 10px;
            border-radius: 999px;
            font-size: 10px;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: 0.04em;
        }

        .status-active {
            background: #e8f7ee;
            color: #2e7d32;
        }

        .status-completed {
            background: #e8f3ff;
            color: #1565c0;
        }

        .footer {
            border-top: 1px solid #dbe4ef;
            padding-top: 12px;
            margin-top: 14px;
            font-size: 10px;
            color: var(--muted);
            background: rgba(255, 255, 255, 0.96);
        }

        .footer p {
            margin-bottom: 4px;
            line-height: 1.6;
        }

        @media (max-width: 900px) {
            .header {
                flex-direction: column;
            }

            .meta-top {
                text-align: left;
                min-width: 0;
            }

            .summary-grid {
                grid-template-columns: repeat(2, minmax(0, 1fr));
            }

            .plan-grid {
                grid-template-columns: 1fr;
            }

            .company-logo {
                width: 95px;
            }
        }

        @media (max-width: 640px) {
            body {
                padding: 12px;
            }

            .card {
                padding: 20px 16px 18px 16px;
                border-radius: 18px;
            }

            .controls {
                text-align: left;
            }

            .btn {
                margin: 0 8px 8px 0;
            }

            .company-block {
                flex-direction: column;
                gap: 10px;
            }

            .summary-grid {
                grid-template-columns: 1fr;
            }
        }

        @media print {
            body {
                background: #ffffff;
                padding: 0;
            }

            .controls {
                display: none;
            }

            .page-wrapper {
                margin: 0;
                max-width: none;
            }

            .card {
                border-radius: 0;
                border: none;
                box-shadow: none;
                padding: 18px 22px;
            }

            .table-title {
                page-break-after: avoid;
            }

            table {
                page-break-inside: avoid;
            }

            .watermark {
                position: fixed;
            }
        }
    </style>
</head>

<body>
    <div class="page-wrapper">
        <div class="watermark">
            <img src="logo-safe-step.png" alt="Safe Step Properties Limited Watermark" />
        </div>

        <div class="controls">
            <button class="btn secondary" onclick="window.print()">Print</button>
            <button class="btn" onclick="handleDownloadPdf()">Download PDF</button>
        </div>

        <div class="card">
            <div class="header">
                <div class="company-block">
                    <img src="logo-safe-step.png" alt="Safe Step Properties Limited" class="company-logo" />
                    <div class="company-text">
                        <h1>Safe Step Properties Limited</h1>
                        <p>Sales, Investment &amp; Property Management Report</p>
                        <p>
                            Suite 02, Jahi Mall, Valentine Nwabuaeze Crescent, Jahi, FCT Abuja.
                        </p>
                        <p>
                            Contact: +234 814 241 1113<br />
                            WhatsApp: +234 704 646 6653<br />
                            Email: info@safestepproperties.com
                        </p>
                    </div>
                </div>

                <div class="meta-top">
                    <h2>My Contract Summary</h2>
                    <span class="meta-id" id="reportId">REP-00000000</span>
                    <span>Generated: <span id="generatedDate">N/A</span></span>
                </div>
            </div>

            <!-- Client info -->
            <div class="section">
                <div class="section-title">Client Information</div>
                <div style="font-size:12px;">
                    <table style="width:100%;border-collapse:collapse;">
                        <tbody>
                            <tr>
                                <td style="width:110px;padding:4px 0;color:var(--muted);">Full Name</td>
                                <td style="padding:4px 0;font-weight:700;color:var(--ink);" id="clientName">Client Name
                                </td>
                                <td style="width:110px;padding:4px 0;color:var(--muted);">Client ID</td>
                                <td style="padding:4px 0;font-weight:700;color:var(--ink);" id="clientId">CLI-000000</td>
                            </tr>
                            <tr>
                                <td style="padding:4px 0;color:var(--muted);">Phone</td>
                                <td style="padding:4px 0;font-weight:700;color:var(--ink);" id="clientPhone">N/A</td>
                                <td style="padding:4px 0;color:var(--muted);">Email</td>
                                <td style="padding:4px 0;font-weight:700;color:var(--ink);" id="clientEmail">N/A</td>
                            </tr>
                            <tr>
                                <td style="padding:4px 0;color:var(--muted);">Registration Date</td>
                                <td style="padding:4px 0;font-weight:700;color:var(--ink);" id="registeredDate">N/A</td>
                                <td></td>
                                <td></td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>

            <!-- Summary -->
            <div class="section">
                <div class="section-title">Financial Summary</div>
                <div class="summary-grid">
                    <div class="summary-card">
                        <div class="summary-label">Total Plots</div>
                        <div class="summary-value" id="totalPlots">0</div>
                    </div>
                    <div class="summary-card">
                        <div class="summary-label">Total Value (₦)</div>
                        <div class="summary-value positive" id="totalValue">0.00</div>
                    </div>
                    <div class="summary-card">
                        <div class="summary-label">Amount Paid (₦)</div>
                        <div class="summary-value positive" id="amountPaid">0.00</div>
                    </div>
                    <div class="summary-card">
                        <div class="summary-label">Outstanding (₦)</div>
                        <div class="summary-value outstanding" id="outstanding">0.00</div>
                    </div>
                </div>
            </div>

            <!-- Plan -->
            <div class="section">
                <div class="section-title">Payment Plan</div>
                <div style="font-size:12px;">
                    <div class="plan-grid">
                        <div>
                            <div class="plan-label">Plan Type</div>
                            <div class="plan-value" id="planType">Full</div>
                        </div>
                        <div>
                            <div class="plan-label">Duration</div>
                            <div class="plan-value" id="planDuration">N/A</div>
                        </div>
                        <div>
                            <div class="plan-label">Suggested Monthly Payment (₦)</div>
                            <div class="plan-value" id="monthlyAmount">0.00</div>
                        </div>
                    </div>
                    <p style="margin-top:10px;font-size:11px;color:var(--muted); line-height:1.6;">
                        You can pay any amount to Safe Step Properties Limited at any time and it will be recorded.
                        The monthly amount shown above is only a guide.
                    </p>
                </div>
            </div>

            <!-- Plots -->
            <div class="section">
                <div class="table-title">Purchased Plots</div>
                <table id="plotsTable">
                    <thead>
                        <tr>
                            <th>Area</th>
                            <th>Plot Code</th>
                            <th>Price (₦)</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody id="plotsBody">
                        <tr>
                            <td colspan="4" style="text-align:center;color:#888888;">No plots found</td>
                        </tr>
                    </tbody>
                </table>
            </div>

            <!-- Payments -->
            <div class="section">
                <div class="table-title">Payment History</div>
                <table id="paymentsTable">
                    <thead>
                        <tr>
                            <th>Date</th>
                            <th>Amount (₦)</th>
                            <th>Method</th>
                            <th>Reference</th>
                        </tr>
                    </thead>
                    <tbody id="paymentsBody">
                        <tr>
                            <td colspan="4" style="text-align:center;color:#888888;">No payments found</td>
                        </tr>
                    </tbody>
                </table>
            </div>

            <div class="footer">
                <p>This page is provided by Safe Step Properties Limited for your information.</p>
                <p>If any information appears incorrect, please contact our office immediately.</p>
                <p>Address: Suite 02, Jahi Mall, Valentine Nwabuaeze Crescent, Jahi, FCT Abuja. · Phone: +234 814 241 1113 / +234 704 646 6653 · Email: info@safestepproperties.com</p>
            </div>
        </div>
    </div>

    <script>
        function handleDownloadPdf() {
            window.print();
        }

        function getQueryParam(name){
            const params = new URLSearchParams(window.location.search);
            return params.get(name);
        }

        function setText(id,value,fallback){
            const el = document.getElementById(id);
            if(!el) return;
            el.textContent = (value !== undefined && value !== null && value !== '') ? value : fallback;
        }

        async function loadClientSummary(){
            const clientId = getQueryParam('clientId');
            if(!clientId){
                console.error('No clientId provided in link.');
                return;
            }

            try{
                const res = await fetch(`../../api/get_client_summary_report.php?clientId=${encodeURIComponent(clientId)}`);
                const data = await res.json();

                if(!res.ok || data.error){
                    console.error('Failed to load client summary', data);
                    return;
                }

                setText('reportId', data.reportId, 'N/A');
                setText('generatedDate', data.generatedDate, 'N/A');
                setText('clientName', data.clientName, 'N/A');
                setText('clientId', data.clientId, 'N/A');
                setText('clientPhone', data.clientPhone, 'N/A');
                setText('clientEmail', data.clientEmail, 'N/A');
                setText('registeredDate', data.registeredDate, 'N/A');

                setText('totalPlots', data.totalPlots, '0');
                setText('totalValue', data.totalValue, '0.00');
                setText('amountPaid', data.amountPaid, '0.00');
                setText('outstanding', data.outstanding, '0.00');

                setText('planType', data.planType, 'N/A');
                setText('planDuration', data.planDuration, 'N/A');
                setText('monthlyAmount', data.monthlyAmount, '0.00');

                const plotsBody = document.getElementById('plotsBody');
                if(data.plots && data.plots.length > 0){
                    plotsBody.innerHTML = '';
                    data.plots.forEach(plot => {
                        const row = document.createElement('tr');
                        const statusType = (plot.statusType || '').toLowerCase();
                        let statusClass = 'status-badge status-active';
                        if(statusType === 'completed'){
                            statusClass = 'status-badge status-completed';
                        }
                        row.innerHTML = `
                            <td>${plot.area || ''}</td>
                            <td class="plot-code">${plot.code || ''}</td>
                            <td class="amount-cell">${plot.price || '0.00'}</td>
                            <td><span class="${statusClass}">${plot.status || ''}</span></td>
                        `;
                        plotsBody.appendChild(row);
                    });
                }

                const paymentsBody = document.getElementById('paymentsBody');
                if(data.payments && data.payments.length > 0){
                    paymentsBody.innerHTML = '';
                    data.payments.forEach(p => {
                        const row = document.createElement('tr');
                        row.innerHTML = `
                            <td>${p.date || ''}</td>
                            <td class="amount-cell">${p.amount || '0.00'}</td>
                            <td>${p.method || ''}</td>
                            <td>${p.reference || ''}</td>
                        `;
                        paymentsBody.appendChild(row);
                    });
                }
            }catch(err){
                console.error('Error loading client summary:', err);
            }
        }

        window.onload = loadClientSummary;
    </script>
</body>

</html>
