<?php
/**
 * modules/token/print_dispatch.php
 * Premium 4-Part Multi-Print: Tax Slip, Invoice, Urdu DO, Gate Pass
 */

ob_clean(); // Prevent admin layout

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($id == 0) die("Invalid ID");

$sql = "SELECT w.*, c.CONTRACTOR_NAME, c.CONTRACTOR_CODE, m.MINE_NUMBER, b.BLOCK_NAME 
        FROM TBL_WEIGHBRIDGE_TICKETS w
        LEFT JOIN TBL_CONTRACTORS c ON w.CONTRACTOR_ID = c.CONTRACTOR_ID
        LEFT JOIN TBL_MINES m ON w.MINE_ID = m.MINE_ID
        LEFT JOIN TBL_BLOCKS b ON m.BLOCK_ID = b.BLOCK_ID
        WHERE w.TICKET_ID = $id AND w.STATUS = 'COMPLETED'";
$result = $db->query($sql);
$data = mysqli_fetch_assoc($result);

if (!$data) die("Dispatch record not found or not completed.");

// Assets & Data
$sindh_logo_url = "assets/img/sindh-gov-logo.png";
$slcmc_logo_url = "assets/img/slmc-logo.png";
$ur_date = date('d/m/Y', strtotime($data['SECOND_WEIGHT_TIME']));
$ur_time = date('h:i A', strtotime($data['SECOND_WEIGHT_TIME']));

// Generate a secure QR Code containing Token, Vehicle, and Net Weight
$qr_data = urlencode("TK:" . $data['TICKET_NO'] . "|VH:" . $data['VEHICLE_NO'] . "|NW:" . $data['NET_WEIGHT'] . "T");
$qr_url = "https://api.qrserver.com/v1/create-qr-code/?size=80x80&data=" . $qr_data . "&margin=0";
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Dispatch Challans - <?php echo $data['TICKET_NO']; ?></title>
    <style>
        body { margin: 0; padding: 0; font-family: 'Arial', sans-serif; background: #555; }
        .print-btn { display: block; text-align: center; padding: 20px; background: #fff; margin-bottom: 20px; }
        .print-btn button { font-size: 18px; padding: 10px 30px; cursor: pointer; font-weight: bold; background: #007bff; color: white; border: none; border-radius: 5px; }
        
        /* Individual Page Settings */
        .page { 
            width: 210mm; min-height: 148mm; /* A5 Size for Slips */
            margin: 0 auto 30px auto; padding: 25px 35px; 
            background: #fff; border: 1px solid #ccc; box-shadow: 0 5px 15px rgba(0,0,0,0.3);
            position: relative; overflow: hidden; box-sizing: border-box;
        }
        .page-break { page-break-after: always; }
        
        /* Unified Official Header */
        .header-section { display: flex; justify-content: space-between; align-items: center; border-bottom: 3px double #000; padding-bottom: 10px; margin-bottom: 15px; position: relative; z-index: 2; }
        .header-logos { display: flex; align-items: center; gap: 15px; }
        .logo-img { height: 60px; width: auto; object-fit: contain; }
        .qr-img { height: 60px; width: 60px; border: 1px solid #000; padding: 2px; }
        .header-title-wrapper { text-align: center; flex-grow: 1; padding: 0 15px; }
        .company-name { font-size: 22px; font-weight: 900; text-transform: uppercase; font-family: 'Times New Roman', serif; margin: 0 0 5px 0; color: #000; letter-spacing: 1px; }
        .challan-type { font-size: 16px; font-weight: bold; background: #000; color: #fff; display: inline-block; padding: 4px 15px; border-radius: 4px; letter-spacing: 1px; -webkit-print-color-adjust: exact !important; print-color-adjust: exact !important; }
        
        /* Security Watermark & Content Layering */
        .watermark { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); opacity: 0.08; width: 280px; pointer-events: none; z-index: 1; -webkit-print-color-adjust: exact !important; print-color-adjust: exact !important; }
        .content-z { position: relative; z-index: 2; }

        /* General Layout helpers */
        .flex-row { display: flex; justify-content: space-between; margin-bottom: 15px; }
        .underline { border-bottom: 1px solid #000; display: inline-block; min-width: 150px; text-align: center; font-weight: bold; }
        
        /* Tables */
        table { width: 100%; border-collapse: collapse; margin-top: 15px; background: rgba(255,255,255,0.8); }
        table th, table td { border: 1px solid #000; padding: 6px; text-align: center; font-size: 14px; }
        
        /* Urdu Styling */
        .urdu-text { font-family: 'Jameel Noori Nastaleeq', 'Nafees Web Naskh', 'Arial', serif; text-align: right; direction: rtl; }
        
        @media print {
            body { background: #fff; }
            .print-btn { display: none; }
            .page { margin: 0; border: none; box-shadow: none; width: 100%; padding: 15px; }
        }
    </style>
</head>
<body onload="window.print()">

    <div class="print-btn">
        <button onclick="window.print()">Print All 4 Dispatch Challans</button>
    </div>

    <!-- PAGE 1: GOVT TAX / EXCISE SLIP -->
    <div class="page page-break">
        <img src="<?php echo $sindh_logo_url; ?>" class="watermark" alt="Watermark">
        
        <div class="header-section">
            <div class="header-logos">
                <img src="<?php echo $sindh_logo_url; ?>" class="logo-img">
                <img src="<?php echo $slcmc_logo_url; ?>" class="logo-img">
            </div>
            <div class="header-title-wrapper">
                <h1 class="company-name">SINDH LAKHRA COAL MINING COMPANY</h1>
                <div class="challan-type">APPROVED GOVT. CONTRACTOR SEAL</div>
            </div>
            <div>
                <img src="<?php echo $qr_url; ?>" class="qr-img">
            </div>
        </div>

        <div class="content-z">
            <div class="flex-row" style="margin-top: 10px; font-size: 16px;">
                <div>Serial No: <span class="underline"><?php echo $data['DAILY_SERIAL']; ?></span></div>
                <div>Date: <span class="underline"><?php echo date('d/m/Y', strtotime($data['SECOND_WEIGHT_TIME'])); ?></span></div>
            </div>
            <div style="margin-bottom: 15px; font-size: 16px;">
                Name of Contractor: <span class="underline" style="min-width: 300px;"><?php echo htmlspecialchars($data['CONTRACTOR_NAME']); ?></span>
            </div>
            <div style="margin-bottom: 10px; font-size: 16px;">
                Truck No: <span class="underline"><?php echo htmlspecialchars($data['VEHICLE_NO']); ?></span>
            </div>

            <table>
                <thead style="background: #f4f4f4; -webkit-print-color-adjust: exact;">
                    <tr>
                        <th style="text-align: left;">DESCRIPTION</th>
                        <th>RATE / TON</th>
                        <th>QTY (TONS)</th>
                        <th>AMOUNT (RS)</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td style="text-align: left; font-weight: bold;">ROYALTY</td>
                        <td>150.00</td>
                        <td><?php echo $data['NET_WEIGHT']; ?></td>
                        <td><?php echo number_format($data['ROYALTY_AMOUNT'], 2); ?></td>
                    </tr>
                    <tr>
                        <td style="text-align: left; font-weight: bold;">COAL DEVELOPMENT CESS</td>
                        <td>50.00</td>
                        <td><?php echo $data['NET_WEIGHT']; ?></td>
                        <td><?php echo number_format($data['CESS_AMOUNT'], 2); ?></td>
                    </tr>
                    <tr>
                        <td style="text-align: left; font-weight: bold;">MINE EXCISE DUTY</td>
                        <td>30.00</td>
                        <td><?php echo $data['NET_WEIGHT']; ?></td>
                        <td><?php echo number_format($data['EXCISE_AMOUNT'], 2); ?></td>
                    </tr>
                    <tr style="border-top: 2px solid #000; font-size: 16px;">
                        <th colspan="3" style="text-align: right;">TOTAL RS.</th>
                        <th><?php echo number_format($data['ROYALTY_AMOUNT'] + $data['CESS_AMOUNT'] + $data['EXCISE_AMOUNT'], 2); ?></th>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>

    <!-- PAGE 2: FINANCIAL INVOICE -->
    <div class="page page-break">
        <img src="<?php echo $slcmc_logo_url; ?>" class="watermark" alt="Watermark" style="opacity: 0.05;">
        
        <div class="header-section">
            <div class="header-logos">
                <img src="<?php echo $sindh_logo_url; ?>" class="logo-img">
                <img src="<?php echo $slcmc_logo_url; ?>" class="logo-img">
            </div>
            <div class="header-title-wrapper">
                <h1 class="company-name">SINDH LAKHRA COAL MINING COMPANY</h1>
                <div class="challan-type">FINANCIAL INVOICE</div>
            </div>
            <div>
                <img src="<?php echo $qr_url; ?>" class="qr-img">
            </div>
        </div>

        <div class="content-z">
            <div class="flex-row" style="margin-top: 10px; font-size: 16px;">
                <div><strong>Token No:</strong> <span class="underline"><?php echo $data['TICKET_NO']; ?></span></div>
                <div><strong>Contractor:</strong> <span class="underline"><?php echo htmlspecialchars($data['CONTRACTOR_CODE']); ?></span></div>
            </div>
            
            <table>
                <thead style="background: #f4f4f4; -webkit-print-color-adjust: exact;">
                    <tr>
                        <th>Particulars</th>
                        <th>Net Tons</th>
                        <th>Rate / Ton</th>
                        <th>Gross Amount</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td style="text-align: left; font-weight: bold;">Coal Dispatch (Mine <?php echo $data['MINE_NUMBER']; ?>)</td>
                        <td><?php echo $data['NET_WEIGHT']; ?></td>
                        <td>Rs. <?php echo number_format($data['RATE_APPLIED'], 2); ?></td>
                        <td>Rs. <?php echo number_format($data['TOTAL_AMOUNT'], 2); ?></td>
                    </tr>
                    <tr>
                        <td colspan="3" style="text-align: right;">Less: Income Tax / WHT (2%)</td>
                        <td>( Rs. <?php echo number_format($data['WHT_AMOUNT'], 2); ?> )</td>
                    </tr>
                    <tr>
                        <td colspan="3" style="text-align: right;">Less: Govt Duties (Royalty/Cess)</td>
                        <td>( Rs. <?php echo number_format($data['ROYALTY_AMOUNT'] + $data['CESS_AMOUNT'] + $data['EXCISE_AMOUNT'], 2); ?> )</td>
                    </tr>
                    <tr style="border-top: 2px solid #000; font-size: 16px;">
                        <th colspan="3" style="text-align: right;">NET PAYABLE</th>
                        <th>Rs. <?php echo number_format($data['NET_PAYABLE'], 2); ?></th>
                    </tr>
                </tbody>
            </table>
            
            <div style="margin-top: 60px; display: flex; justify-content: space-between; font-weight: bold;">
                <div style="border-top: 1px solid #000; padding-top: 5px; width: 200px; text-align: center;">Accountant</div>
                <div style="border-top: 1px solid #000; padding-top: 5px; width: 200px; text-align: center;">Authorized Signature</div>
            </div>
        </div>
    </div>

    <!-- PAGE 3: URDU DELIVERY ORDER (BILTY) -->
    <div class="page page-break urdu-text" dir="rtl">
        <img src="<?php echo $sindh_logo_url; ?>" class="watermark" alt="Watermark">
        
        <!-- Header is LTR to keep logos on correct sides -->
        <div class="header-section" dir="ltr">
            <div class="header-logos">
                <img src="<?php echo $sindh_logo_url; ?>" class="logo-img">
                <img src="<?php echo $slcmc_logo_url; ?>" class="logo-img">
            </div>
            <div class="header-title-wrapper">
                <h1 class="company-name">SINDH LAKHRA COAL MINING COMPANY</h1>
                <div class="challan-type" style="font-family: 'Jameel Noori Nastaleeq', Arial; font-size: 20px; padding: 0px 20px;">سندھ لاکھڑا کول مائننگ پراجیکٹ</div>
            </div>
            <div>
                <img src="<?php echo $qr_url; ?>" class="qr-img">
            </div>
        </div>

        <div class="content-z">
            <div style="display: flex; justify-content: space-between; margin-top: 10px; font-size: 18px;">
                <div>سیریل نمبر: <span class="underline" style="font-family: Arial;"><?php echo $data['DAILY_SERIAL']; ?></span></div>
                <div>تاریخ: <span class="underline" style="font-family: Arial;"><?php echo $ur_date; ?></span></div>
            </div>
            <div style="display: flex; justify-content: space-between; margin-top: 10px; font-size: 18px;">
                <div>چالان / ڈیلیوری آرڈر نمبر: <span class="underline" style="font-family: Arial;"><?php echo $data['TICKET_NO']; ?></span></div>
                <div>وقت: <span class="underline" style="font-family: Arial;"><?php echo $ur_time; ?></span></div>
            </div>

            <div style="margin-top: 20px; font-size: 20px;">
                جناب: <span class="underline" style="min-width: 300px; font-family: Arial;"><?php echo htmlspecialchars($data['CONTRACTOR_NAME']); ?></span>
            </div>
            <div style="margin-top: 15px; font-size: 18px;">
                کوئلہ بمطابق ڈیلیوری آرڈر مذکورہ بذریعہ ٹرک ترسیل کر دی گئی ہے۔
            </div>

            <div style="margin-top: 20px; font-size: 18px; line-height: 2;">
                <div>نام ڈرائیور: <span class="underline" style="font-family: Arial;"><?php echo htmlspecialchars($data['DRIVER_NAME']); ?></span></div>
                <div>ٹرک نمبر: <span class="underline" style="font-family: Arial; text-transform: uppercase;"><?php echo htmlspecialchars($data['VEHICLE_NO']); ?></span></div>
                <div>ٹرک کی پیمائش: <span class="underline" style="font-family: Arial;" dir="ltr"><?php echo htmlspecialchars($data['TRUCK_MEASUREMENT']); ?></span></div>
                <div>وزن (میٹرک ٹن): <span class="underline" style="font-family: Arial;"><?php echo $data['NET_WEIGHT']; ?></span></div>
                <div>مائن نمبر: <span class="underline" style="font-family: Arial;"><?php echo htmlspecialchars($data['MINE_NUMBER']); ?></span></div>
            </div>

            <div style="display: flex; justify-content: space-between; margin-top: 40px; font-size: 18px; font-weight: bold;">
                <div style="text-align: center;">
                    <div style="border-bottom: 1px solid #000; width: 150px; margin-bottom: 5px;"></div>
                    ڈرائیور وصول کنندہ
                </div>
                <div style="text-align: center;">
                    <div style="border-bottom: 1px solid #000; width: 150px; margin-bottom: 5px;"></div>
                    سائیٹ انچارج
                </div>
            </div>
        </div>
    </div>

    <!-- PAGE 4: GATE PASS -->
    <div class="page" style="background-color: #f4fcf4; -webkit-print-color-adjust: exact;">
        <img src="<?php echo $slcmc_logo_url; ?>" class="watermark" alt="Watermark" style="opacity: 0.05;">
        
        <div class="header-section">
            <div class="header-logos">
                <img src="<?php echo $sindh_logo_url; ?>" class="logo-img">
                <img src="<?php echo $slcmc_logo_url; ?>" class="logo-img">
            </div>
            <div class="header-title-wrapper">
                <h1 class="company-name">LAKHRA ASSOCIATION</h1>
                <div class="challan-type" style="background: #2e7d32;">GATE PASS (OUTBOUND)</div>
            </div>
            <div>
                <img src="<?php echo $qr_url; ?>" class="qr-img" style="border-color: #2e7d32;">
            </div>
        </div>

        <div class="content-z">
            <div class="flex-row" style="font-size: 18px;">
                <div>S.No: <span class="underline"><?php echo $data['DAILY_SERIAL']; ?></span></div>
                <div>Date: <span class="underline"><?php echo date('d/m/Y', strtotime($data['SECOND_WEIGHT_TIME'])); ?></span></div>
            </div>

            <div style="font-size: 18px; margin-top: 20px;">
                Name: <span class="underline" style="min-width: 300px;"><?php echo htmlspecialchars($data['CONTRACTOR_NAME']); ?></span>
            </div>
            
            <div style="font-size: 18px; margin-top: 20px;">
                Truck No: <span class="underline" style="min-width: 200px; font-weight: bold; font-family: monospace; font-size: 20px;"><?php echo htmlspecialchars($data['VEHICLE_NO']); ?></span>
            </div>

            <div style="font-size: 18px; margin-top: 20px;">
                Net Tons: <span class="underline" style="min-width: 150px; font-weight: bold; font-size: 20px; color: #2e7d32;"><?php echo $data['NET_WEIGHT']; ?></span>
            </div>

            <div style="display: flex; justify-content: space-between; margin-top: 60px; font-size: 16px; font-weight: bold;">
                <div style="text-align: center;">
                    <div style="border-bottom: 1px solid #000; width: 150px; margin-bottom: 5px; height: 30px;"></div>
                    Association Stamp
                </div>
                <div style="text-align: center;">
                    <div style="border-bottom: 1px solid #000; width: 150px; margin-bottom: 5px; height: 30px;"></div>
                    Authorized Signature
                </div>
            </div>
        </div>
    </div>

</body>
</html>
<?php exit; ?>