<?php
session_start();
require_once("../config/config.php");

$dc_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($dc_id <= 0) {
    die("<h2 style='color:red;text-align:center;padding:100px;'>Invalid Delivery Challan ID</h2>");
}

// Fetch Delivery Challan Header
$query = "
    SELECT 
        dc.*,
        p.project_code,
        p.project_name,
        u.full_name AS issued_by_name
    FROM delivery_challan dc
    LEFT JOIN project_master p ON dc.project_id = p.id
    LEFT JOIN users u ON dc.issued_by = u.id
    WHERE dc.id = ?
";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $dc_id);
$stmt->execute();
$dc = $stmt->get_result()->fetch_assoc();

if (!$dc) {
    die("<h2 style='color:red;text-align:center;padding:100px;'>Delivery Challan not found.</h2>");
}

// Fetch Items
$items_query = "
    SELECT 
        dci.*,
        i.name AS item_name,
        i.model_no AS item_model,
        i.description AS item_description,
        i.hsn_sac AS hsn_code
    FROM delivery_challan_items dci
    INNER JOIN inventory_master i ON dci.item_id = i.id
    WHERE dci.dc_id = ?
    ORDER BY i.name ASC
";
$items_stmt = $conn->prepare($items_query);
$items_stmt->bind_param("i", $dc_id);
$items_stmt->execute();
$items = $items_stmt->get_result()->fetch_all(MYSQLI_ASSOC);
?>

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>DC-<?= str_pad($dc_id, 5, '0', STR_PAD_LEFT) ?> | Technocool HVAC</title>
    <style>
    @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');

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

    body {
        font-family: 'Inter', Arial, sans-serif;
        background: #f8f9fa;
        color: #1f2937;
        line-height: 1.6;
        padding: 20px;
    }

    .print-container {
        max-width: 1000px;
        margin: 0 auto;
        background: white;
        border: 1px solid #e5e7eb;
        box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        overflow: hidden;
    }

    .header {
        /* background: linear-gradient(135deg, #8b5cf6, #6d28d9); */
        color: white;
        padding: 9px 86px;
        text-align: center;
        display: flex;
        justify-content: space-between;
        align-items: center;
        flex-wrap: wrap;
        gap: 15px;
        border-bottom: 1px solid #2f2f81;
    }
    .logo-container {
        display: flex;
        align-items: center;
        gap: 15px;
    }
    .company-name {
        font-size: 28px;
        font-weight: 700;
        letter-spacing: 1px;
        text-transform: uppercase;
        color: #2f2f81;
    }

    .subtitle {
        font-size: 15px;
        opacity: 0.95;
        margin-top: 8px;
        letter-spacing: 1px;
        text-transform: uppercase;
        color: black;
    }

    .challan-info {
        padding: 2px 40px;
        /* background: #1e2937; */
        color: #1e2937;
        border-bottom:1px solid #2f2f81;
        display: flex;
        justify-content: space-between;
        align-items: center;
        flex-wrap: wrap;
        gap: 15px;
    }

    .challan-no {
        font-size: 18px;
        font-weight: 700;
        color: #1e2937;
    }

    .info-section {
        display: grid;
        grid-template-columns: 1fr 1fr 1fr;
        gap: 25px;
        padding: 8px 40px;
        background: white;
    }

    .info-item {
        margin-bottom: 18px;
    }

    .info-label {
        font-size: 13px;
        font-weight: 600;
        color: #64748b;
        margin-bottom: 4px;
    }

    .info-value {
        font-weight: 500;
        font-size: 15px;
    }

    table {
        width: 100%;
        border-collapse: collapse;
        margin: 10px 0 10px;
    }
    thead {
        border-top: 1px solid #2f2f81;
        border-bottom: 1px solid #2f2f81;
           
    }
    th {
        /* background: #1e2937; */
        color: #1e2937;
        padding: 16px 12px;
        font-weight: 600;
        text-align: left;
        
    }

    td {
        padding: 14px 12px;
        border-bottom: 1px solid #e2e8f0;
         
    }

    tr:last-child td {
        border-bottom: none;
    }

    .text-center {
        text-align: center;
    }

    .text-right {
        text-align: right;
    }

    .total-row {
        background: #f1f5f9;
        font-weight: 700;
        font-size: 1.1rem;
    }

    .footer {
        padding: 30px 40px;
        text-align: center;
        font-size: 13px;
        color: #64748b;
        border-top: 1px solid #e2e8f0;
    }

    .signature-area {
        display: flex;
        justify-content: space-between;
        padding: 35px 40px 30px;
        margin-top: 20px;
    }

    .signature-box {
        text-align: center;
        width: 45%;
    }

    .signature-line {
        border-top: 2px solid #334155;
        width: 200px;
        margin: 0 auto 12px;
    }

    @media print {
        body {
            background: white;
            padding: 0 !important;
        }
        .header {
            padding: 9px 20px;
        }
        
        .print-container {
            box-shadow: none;
            border: none;
            max-width: 100%;
        }
        .company-name {
            font-size: 24px;
        }
        .subtitle {
            font-size: 11px;
        }
        th{
            padding: 8px 2px;
            font-size: 12px;
            
        }
        td{
            padding: 8px 2px;
            font-size: 12px;
            
        }
        .footer {
            padding: 10px 20px;
        }   
        .no-print {
            display: none;
        }
    }

    
    </style>
</head>

<body>

    <div class="print-container">

        <!-- Header -->

        <div class="header">
            <div class="logo-container">
                <div><img src="../technocool-logo.png" alt="Technocool Logo"></div>
                <div class="logo-text">
                    <div class="company-name">TECHNOCOOL HVAC ENGINEERS</div>
                    <div class="subtitle">
                        Office: 66/7 Vidya Apt., Pratap Nagar, Ring Road, Nagpur-440022<br>
                        Showroom: Suraj Place, Shop No. 2, Ravi Nagar, Nagpur-440010<br>
                        Mob: +91 9766751640 | +91 9226267309 | GSTIN: 27AAHFT7601F1Z7
                    </div>
                </div>
            </div>
        </div>

        <!-- Challan Info -->
        <div class="challan-info">
            <div>
                <div class="challan-no">DELIVERY CHALLAN</div>
                <div style="font-size:16px; font-weight:700; color:#1e2937;">
                    DC-<?= str_pad($dc['id'], 5, '0', STR_PAD_LEFT) ?>
                </div>
            </div>
            <div style="text-align:right;">
                <strong>Date:</strong> <?= date('d M Y', strtotime($dc['dc_date'])) ?><br>
                <strong>Status:</strong> <?= htmlspecialchars($dc['status']) ?>
            </div>
        </div>

        <!-- Information Section -->
        <div class="info-section">
            <div>
                <div class="info-item">
                    <div class="info-label">RECEIVER NAME</div>
                    <div class="info-value"><?= htmlspecialchars($dc['receiver_name']) ?></div>
                </div>
                <div class="info-item">
                    <div class="info-label">MOBILE</div>
                    <div class="info-value"><?= htmlspecialchars($dc['receiver_mobile'] ?: '—') ?></div>
                </div>
                 <div class="info-item">
                    <div class="info-label">VEHICLE NO.</div>
                    <div class="info-value"><?= htmlspecialchars($dc['vehicle_no'] ?: '—') ?></div>
                </div>
               
            </div>

           

            <div>
                <div class="info-item">
                    <div class="info-label">PROJECT / SITE</div>
                    <div class="info-value">
                        <?= $dc['project_code'] 
                        ? htmlspecialchars($dc['project_code']) . ' - ' . htmlspecialchars($dc['project_name']) 
                        : 'Central / General Delivery' ?>
                    </div>
                </div>
                <div class="info-item">
                    <div class="info-label">CLIENT GST NO.</div>
                    <div class="info-value"><?= htmlspecialchars($dc['client_gst_number'] ?: '—') ?></div>
                </div>
                 <div class="info-item">
                    <div class="info-label">DELIVERY ADDRESS</div>
                    <div class="info-value"><?= nl2br(htmlspecialchars($dc['delivery_address'] ?: '—')) ?></div>
                </div>
                
            </div>


            <div>
                
                <div class="info-item">
                    <div class="info-label">PO NO</div>
                    <div class="info-value"><?= htmlspecialchars($dc['po_no'] ?: '—') ?></div>
                </div>
                <div class="info-item">
                    <div class="info-label">PO Date</div>
                    <div class="info-value"><?= htmlspecialchars($dc['po_date'] ?: '—') ?></div>
                </div>
                <div class="info-item">
                    <div class="info-label">Eway Bill No.</div>
                    <div class="info-value"><?= htmlspecialchars($dc['eway_bill_no'] ?: '—') ?></div>
                </div>
            </div>
        </div>

        <!-- Items Table -->
        <table>
            <thead>
                <tr>
                    <th style="width:2%">Sr.No</th>
                    <th style="width:5%">HSN/SAC</th>
                    <th style="width:20%">Item Model</th>
                    <th style="width:15%">Serial No</th>
                    <th style="width:20%">Item Description</th>
                    <th style="width:15%">Billing Type</th>
                    <th style="width:10%">Qty</th>
                    <th style="width:8%">Unit</th>
                    <th style="width:10%">Rate</th>
                    <th style="width:18%">Amount</th>
                </tr>
            </thead>
            <tbody>
                <?php 
            $grand_total = 0;
            $sr = 1;
            foreach ($items as $item): 
                $line_total = $item['issued_qty'] * ($item['rate'] ?? 0);
                $grand_total += $line_total;
            ?>
                <tr>
                    <td class="text-center"><?= $sr++ ?></td>
                    <td class="text-center"><?= htmlspecialchars($item['hsn_code'] ?: '—') ?></td>
                    <td><?= htmlspecialchars($item['item_model'])?></td>
                    <td><?= htmlspecialchars($item['serial_number'] ?: '—') ?></td>
                    <td><?= htmlspecialchars($item['item_description']) ?></td>
                    <td><?= htmlspecialchars($item['billing_type'] ?? 'Billable') ?></td>
                    <td class="text-center"><?= number_format($item['issued_qty'], 3) ?></td>
                    <td class="text-center"><?= htmlspecialchars($item['unit']) ?></td>
                    <td class="text-right"><?= number_format($item['rate'] ?? 0, 2) ?></td>
                    <td class="text-right"><?= number_format($line_total, 2) ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
            <tfoot>
                <tr class="total-row">
                    <td colspan="8" style="text-align:right; padding-right:20px;">Grand Total</td>
                    <td class="text-right"colspan="2" style="padding-right:20px;"><?= number_format($grand_total, 2) ?></td>
                </tr>
            </tfoot>
        </table>

        <?php if (!empty($dc['remarks'])): ?>
        <div style="margin:12px 40px; padding:12px; background:#f8fafc; border-left:4px solid #1e2937;">
            <strong>Remarks:</strong><br>
            <?= nl2br(htmlspecialchars($dc['remarks'])) ?>
        </div>
        <?php endif; ?>

        <!-- Signature -->
        <div class="signature-area">
            <div class="signature-box">
                <div class="signature-line"></div>
                <strong>Receiver's Signature & Seal</strong><br>
                <!-- <small><?= htmlspecialchars($dc['receiver_name']) ?></small> -->
            </div>
            <div class="signature-box">
                <div class="signature-line"></div>
                <strong>Authorized Signatory</strong><br>
                <small>For TECHNOCOOL HVAC ENGINEERS</small>
            </div>
        </div>

        <div class="footer">
            This is a computer-generated Delivery Challan • Original copy for receiver<br>
            Generated on <?= date('d M Y h:i A') ?> | Thank you for choosing Technocool
        </div>

    </div>

</body>

</html>

<?php
$stmt->close();
$items_stmt->close();
$conn->close();
?>