<!DOCTYPE html>
<html>
<head>
    <title>Payments PDF</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        html, body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; color: #1e293b; background: #ffffff; }
        body { font-size: 12px; line-height: 1.6; padding: 20px; }
        table { width: 100%; border-collapse: collapse; }
        .pdf-header { border-bottom: 3px solid #0066cc; padding-bottom: 20px; margin-bottom: 30px; }
        .company-logo { width: 70px; height: auto; margin-right: 15px; display: inline-block; }
        .company-name { font-size: 20px; font-weight: 700; color: #0066cc; margin-bottom: 3px; }
        .company-address { font-size: 11px; color: #64748b; margin-bottom: 2px; }
        .receipt-number { text-align: right; font-size: 11px; color: #64748b; }
        .receipt-number-value { font-size: 14px; font-weight: 600; color: #0066cc; }
        .receipt-title { text-align: center; margin: 30px 0; }
        .receipt-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.1em; color: #64748b; margin-bottom: 8px; }
        .receipt-heading { font-size: 28px; font-weight: 700; color: #0066cc; margin-bottom: 10px; }
        .payment-status { font-size: 12px; color: #16a34a; font-weight: 600; }
        .amount-box { background: linear-gradient(135deg, #0066cc 0%, #0052a3 100%); color: white; padding: 20px; margin: 25px 0; text-align: center; border-radius: 4px; box-shadow: 0 2px 8px rgba(0, 102, 204, 0.15); }
        .amount-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.1em; opacity: 0.9; margin-bottom: 5px; }
        .amount-value { font-size: 32px; font-weight: 700; margin: 10px 0; }
        .details-section { margin-bottom: 30px; }
        .section-title { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.1em; color: #0066cc; padding-bottom: 8px; border-bottom: 2px solid #e2e8f0; margin-bottom: 12px; }
        .detail-item { margin-bottom: 12px; }
        .detail-label { font-size: 10px; text-transform: uppercase; letter-spacing: 0.08em; color: #64748b; font-weight: 600; margin-bottom: 3px; }
        .detail-value { font-size: 13px; color: #1e293b; font-weight: 500; }
        .invoice-table { margin-top: 20px; }
        .invoice-table thead { background: #f8fafc; border-top: 2px solid #0066cc; border-bottom: 2px solid #0066cc; }
        .invoice-table th { padding: 10px; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.08em; color: #475569; text-align: right; border: none; }
        .invoice-table th:first-child, .invoice-table td:first-child { text-align: left; }
        .invoice-table td { padding: 10px; font-size: 12px; color: #1e293b; border-bottom: 1px solid #e2e8f0; text-align: right; }
        .invoice-table tbody tr:last-child td { border-bottom: 2px solid #0066cc; }
        .notes { color: #64748b; min-height: 20px; padding: 15px; margin-top: 20px; background-color: #f8fafc; border-left: 3px solid #0066cc; }
        .notes-title { font-weight: 600; color: #1e293b; margin-bottom: 5px; }
        .pdf-footer { margin-top: 40px; padding-top: 20px; border-top: 1px solid #e2e8f0; text-align: center; font-size: 10px; color: #64748b; }
        .print-toolbar { background: #0f172a; padding: 12px 20px; text-align: right; }
        .print-toolbar button { background: #0066cc; border: 0; border-radius: 6px; color: #fff; cursor: pointer; font-size: 13px; font-weight: 700; padding: 8px 14px; }
        @media print { .print-toolbar { display: none; } }
    </style>
</head>
<body>
<?php if (!empty($print_mode)) { ?>
<div class="print-toolbar">
    <button type="button" onclick="window.print()"><?= lang('print') ?: 'Print' ?></button>
</div>
<?php } ?>
<?php
$purchase_info = $this->purchase_model->check_by(array('purchase_id' => $payments_info->purchase_id), 'tbl_purchases');
if (empty($purchase_info)) {
    $purchase_info = new stdClass();
    $purchase_info->reference_no = '-';
    $purchase_info->purchase_date = null;
}
$supplier_info = $this->purchase_model->check_by(array('supplier_id' => $payments_info->paid_to), 'tbl_suppliers');
if (is_numeric($payments_info->payment_method)) {
    $payment_methods = $this->purchase_model->check_by(array('payment_methods_id' => $payments_info->payment_method), 'tbl_payment_methods');
} else {
    $payment_methods = (object) array('method_name' => $payments_info->payment_method);
}
$role = $this->session->userdata('user_type');
if ($role == 1 && $payments_info->account_id != 0) {
    $account_info = $this->purchase_model->check_by(array('account_id' => $payments_info->account_id), 'tbl_accounts');
}
$currency = $this->purchase_model->check_by(array('code' => config_item('default_currency')), 'tbl_currencies');
$img = base_url() . config_item('invoice_logo');
$purchase_due = $this->purchase_model->calculate_to('purchase_due', $payments_info->purchase_id);
$render_payment_note = function ($note) {
    $note = strip_html_tags($note, '<p><br><strong><b><em><i><u><ul><ol><li><a>');
    $note = preg_replace('/\s+href\s*=\s*(["\'])\s*javascript:[^"\']*\1/i', '', $note);
    return nl2br($note);
};
?>
<div class="pdf-header">
    <table style="width: 100%;">
        <tr>
            <td style="width: 80px; vertical-align: top; border: none; padding: 0;">
                <img class="company-logo" src="<?= $img ?>" alt="Company Logo">
            </td>
            <td style="vertical-align: top; border: none; padding: 0 15px;">
                <div class="company-name"><?= config_item('company_name') ?></div>
                <div class="company-address"><?= $this->config->item('company_address') ?></div>
            </td>
            <td style="text-align: right; vertical-align: top; border: none; padding: 0;">
                <div class="receipt-number">
                    <div><?= lang('receipt_no') ?></div>
                    <div class="receipt-number-value">#<?= $payments_info->payments_id ?></div>
                </div>
            </td>
        </tr>
    </table>
</div>

<div class="receipt-title">
    <div class="receipt-label"><?= lang('payment_confirmation') ?></div>
    <div class="receipt-heading"><?= lang('purchase') . ' ' . lang('payment') ?></div>
    <div class="payment-status">✓ <?= lang('payment_successful') ?></div>
</div>

<div class="amount-box">
    <div class="amount-label"><?= lang('amount_paid') ?></div>
    <div class="amount-value"><?= display_money($payments_info->amount, $currency->symbol); ?></div>
</div>

<div class="details-section">
    <div class="section-title"><?= lang('payment_details') ?></div>
    <table style="width: 100%; border: none;">
        <tr style="border: none;">
            <td style="width: 50%; vertical-align: top; border: none; padding: 10px 10px 10px 0;">
                <div class="detail-item">
                    <div class="detail-label"><?= lang('payment_date') ?></div>
                    <div class="detail-value"><?= display_date($payments_info->payment_date); ?></div>
                </div>
                <div class="detail-item">
                    <div class="detail-label"><?= lang('transaction_id') ?></div>
                    <div class="detail-value"><?= $payments_info->trans_id ?></div>
                </div>
                <div class="detail-item">
                    <div class="detail-label"><?= lang('payment_mode') ?></div>
                    <div class="detail-value"><?= !empty($payment_methods->method_name) ? $payment_methods->method_name : '-' ?></div>
                </div>
            </td>
            <td style="width: 50%; vertical-align: top; border: none; padding: 10px 0 10px 10px;">
                <div class="detail-item">
                    <div class="detail-label"><?= lang('paid') . ' ' . lang('to') ?></div>
                    <div class="detail-value"><strong><?= !empty($supplier_info->name) ? ucfirst($supplier_info->name) : '-' ?></strong></div>
                </div>
                <?php if ($role == 1 && !empty($account_info)) { ?>
                <div class="detail-item">
                    <div class="detail-label"><?= lang('deduct_from') ?></div>
                    <div class="detail-value"><strong><?= $account_info->account_name ?></strong></div>
                </div>
                <?php } ?>
                <?php if (config_item('amount_to_words') == 'Yes') { ?>
                <div class="detail-item">
                    <div class="detail-label"><?= lang('num_word') ?></div>
                    <div class="detail-value"><?= number_to_word('', $payments_info->amount); ?></div>
                </div>
                <?php } ?>
            </td>
        </tr>
    </table>
</div>

<?php if (!empty($payments_info->notes)) { ?>
<div class="notes">
    <div class="notes-title"><?= lang('notes') ?></div>
    <div><?= $render_payment_note($payments_info->notes) ?></div>
</div>
<?php } ?>

<div class="details-section" style="margin-top: 35px;">
    <div class="section-title"><?= lang('payment_for') ?> - <?= lang('purchase') . ' ' . lang('details') ?></div>
    <table class="invoice-table">
        <thead>
            <tr>
                <th style="text-align: left; padding-left: 10px;"><?= lang('reference_no') ?></th>
                <th style="text-align: right; padding-right: 10px;"><?= lang('purchase_date') ?></th>
                <th style="text-align: right; padding-right: 10px;"><?= lang('purchase') . ' ' . lang('amount') ?></th>
                <th style="text-align: right; padding-right: 10px;"><?= lang('paid_amount') ?></th>
                <?php if ($purchase_due > 0) { ?>
                <th style="text-align: right; padding-right: 10px; color: #cf2127;"><?= lang('due_amount') ?></th>
                <?php } ?>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td style="text-align: left; padding-left: 10px;"><?= $purchase_info->reference_no ?></td>
                <td style="text-align: right; padding-right: 10px;"><?= display_date($purchase_info->purchase_date) ?></td>
                <td style="text-align: right; padding-right: 10px;"><?= display_money($this->purchase_model->calculate_to('total', $payments_info->purchase_id), $currency->symbol); ?></td>
                <td style="text-align: right; padding-right: 10px;"><strong><?= display_money($payments_info->amount, $currency->symbol); ?></strong></td>
                <?php if ($purchase_due > 0) { ?>
                <td style="text-align: right; padding-right: 10px; color: #cf2127; font-weight: 600;"><?= display_money($purchase_due, $currency->symbol); ?></td>
                <?php } ?>
            </tr>
        </tbody>
    </table>
</div>

<div class="pdf-footer">
    <div style="margin-top: 40px; font-size: 11px; color: #64748b;">
        <p><?= lang('thank_you') ?></p>
        <p>Generated on <?= date('F d, Y') ?> at <?= date('H:i:s') ?></p>
    </div>
</div>
<?php if (!empty($print_mode)) { ?>
<script>
window.addEventListener('load', function () {
    window.setTimeout(function () {
        window.print();
    }, 350);
});
</script>
<?php } ?>
</body>
</html>
