<?php
require_once __DIR__ . '/vendor/autoload.php';
use Mpdf\Mpdf;

try {
    // Data Retrieval
    require(realpath(__DIR__ . '/../../services/Model.php'));
    $obj = new Model();
    $token = isset($_GET['token']) ? $_GET['token'] : NULL;
    $url = $baseUrl . '/';

    $account = $obj->details_by_cond('tbl_account', "acc_id='$token'");
    $cus_billing = $obj->details_by_cond('customer_billing', "agid='$account[agent_id]'");
    
    $dueAdvance = $cus_billing["dueadvance"];
    if ($dueAdvance < 0) {
        $label = "Advance Amount:";
        $value = abs($dueAdvance); // minus remove kore positive kore dibe
    } else {
        $label = "Due Amount:";
        $value = $dueAdvance;
    }

    // Date format
    $datetime = new DateTime($account['entry_date']);
    $month = $datetime->format('F');
    $year = $datetime->format('Y');
    
    $logo = $obj->getSettingValue('logo', 'logo');
		$CompanyName = $obj->getSettingValue('excel', 'name');
		$address = $obj->getSettingValue('address', 'address');
		$mobile = $obj->getSettingValue('mobile', 'mobile');
		$email = $obj->getSettingValue('email', 'email');

    $uniqueReceiptNumber = date('YmdHis') . '-' . mt_rand(10, 99);

    $agentDetails = $obj->details_by_cond('tbl_agent', "ag_id='$account[agent_id]'");

    // Dynamic logo (fallback if not found)
    $logoPath = !empty($agentDetails["logo"]) ? $agentDetails["logo"] : __DIR__ . "/logo.png";

    // POS printer friendly HTML with Logo
    $html = '
    <html>
    <head>
        <style>
    body { 
        font-family: monospace; 
        font-size: 13px; 
        font-weight: bold;
    }

    h2 { 
        text-align: center; 
        margin: 5px 0; 
        font-size: 16px; 
        font-weight: bold;
    }

    table { 
        width: 100%; 
        border-collapse: collapse; 
        margin-bottom: 5px; 
    }

    th, td { 
        padding: 3px; 
        text-align: left; 
        font-size: 13px; 
        font-weight: bold;
    }

    th {
        border-bottom: 1px dashed #000;
    }

    .center { 
        text-align: center; 
        font-weight: bold;
    }

    .right { 
        text-align: right; 
        font-weight: bold;
    }

    .line { 
        border-top: 1px dashed #000; 
        margin: 5px 0; 
    }
</style>
    </head>
    <body>
        <div class="center">
            
          
        </div>
        <h2>RECEIPT</h2>
        <div class="line"></div>
        <table>
            <tr>
                <td>Invoice:</td>
                <td class="right">' . $uniqueReceiptNumber . '</td>
            </tr>
            <tr>
                <td>Date:</td>
                <td class="right">' . date("Y-m-d") . '</td>
            </tr>
        </table>
        <div class="line"></div>
        <table>
            <tr><td>Customer ID:</td><td>' . $agentDetails["ip"] . '</td></tr>
            <tr><td>Name:</td><td>' . $agentDetails["ag_name"] . '</td></tr>
            <tr><td>Mobile:</td><td>' . (!empty($agentDetails["ag_mobile_no"]) ? $agentDetails["ag_mobile_no"] : "N/A") . '</td></tr>
            <tr><td>Address:</td><td>' . (!empty($agentDetails["ag_office_address"]) ? $agentDetails["ag_office_address"] : "N/A") . '</td></tr>
        </table>
        <div class="line"></div>
        <table>
            <tr>
                <th>SL</th>
                <th>Desc</th>
                
                <th class="right">Amount</th>
            </tr>
            <tr>
                <td>1</td>
                <td>' . $account["acc_description"] . '</td>
                
                <td class="right">' . $account["acc_amount"] . '</td>
            </tr>
        </table>
        <div class="line"></div>
        <table>
            <tr>
                <td class="right"><strong>Total Paid:</strong></td>
                <td class="right"><strong>' . number_format($account["acc_amount"], 2) . '</strong></td>
            </tr>
            <tr>
                <td class="right">' . $label . '</td>
                <td class="right">' . number_format($value, 2) . '</td>
            </tr>
        </table>
        <div class="line"></div>
        <p class="center">Thank you for your payment!</p>
        <p class="right" style="margin-top:40px;">..................<br/>Signature</p>
    </body>
    </html>
    ';

    // mPDF Initialization with POS printer width (80mm)
    $mpdf = new Mpdf([
        'mode' => 'utf-8',
        'format' => [80, 200], // width=80mm
        'margin_left' => 2,
        'margin_right' => 2,
        'margin_top' => 2,
        'margin_bottom' => 2,
    ]);

    $mpdf->WriteHTML($html);
    $mpdf->Output();
} catch (\Mpdf\MpdfException $e) {
    echo "PDF Generation Error: " . $e->getMessage();
}
exit;
