<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title><?= lang('invoice') ?></title>
    <style type="text/css">
        @page {
            margin: 0;
        }

        * {
            box-sizing: border-box;
        }

        body {
            margin: 0;
            background: #ffffff;
            color: #06152d;
            font-family: DejaVu Sans, Arial, Helvetica, sans-serif;
            font-size: 13px;
            line-height: 1.48;
        }

        .page {
            padding: 39px 38px 76px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            border-spacing: 0;
            table-layout: fixed;
        }

        td,
        th {
            vertical-align: top;
        }

        a {
            color: #0047d9;
            text-decoration: none;
        }

        .top-table {
            margin-bottom: 42px;
        }

        .brand-cell {
            width: 58%;
            padding-top: 0;
        }

        .invoice-cell {
            width: 42%;
            text-align: right;
            padding-top: 5px;
        }

        .brand-row {
            margin: 0;
        }

        .brand-logo {
            max-width: 188px;
            max-height: 62px;
            width: auto;
            height: auto;
        }

        .brand-title {
            margin: 0;
            color: #07152e;
            font-size: 24px;
            font-weight: 800;
            letter-spacing: .01em;
            line-height: 1;
        }

        .brand-title span {
            color: #2463df;
        }

        .brand-version {
            margin-top: 4px;
            color: #40516f;
            font-size: 12px;
            letter-spacing: .08em;
        }

        .company-name {
            margin: 0 0 5px;
            font-size: 17px;
            font-weight: 800;
            color: #06152d;
            clear: both;
        }

        .logo-spacer {
            height: 18px;
            line-height: 18px;
            font-size: 1px;
        }

        .company-line {
            margin: 4px 0;
            color: #263d60;
            font-size: 13px;
        }

        .company-contact {
            margin-top: 13px;
        }

        .contact-line {
            margin: 5px 0;
            color: #425674;
            font-size: 13px;
        }

        .invoice-word {
            margin: 0;
            color: #c5ccd9;
            font-size: 35px;
            line-height: 1;
            font-weight: 300;
            letter-spacing: .13em;
        }

        .invoice-number {
            margin: 10px 0 26px;
            font-size: 16px;
            font-weight: 800;
            color: #06152d;
        }

        .meta-card {
            margin-left: auto;
            width: 242px;
            border: 1px solid #edf1f7;
            border-radius: 14px;
            background: #f8fafc;
            padding: 14px 16px;
        }

        .meta-table {
            width: 100%;
            border-collapse: collapse;
            table-layout: fixed;
        }

        .meta-table td {
            padding: 5px 0;
            color: #50617c;
            font-size: 13px;
            border: 0;
            vertical-align: middle;
        }

        .meta-label {
            width: 108px;
            text-align: right;
            padding-right: 20px;
        }

        .meta-value {
            color: #06152d;
            font-weight: 800;
        }

        .meta-divider {
            height: 1px;
            margin: 11px -16px 9px;
            background: #e7ecf4;
        }

        .status-pill {
            display: inline-block;
            padding: 3px 10px;
            border: 1px solid #ffb4b4;
            border-radius: 16px;
            background: #fff3f3;
            color: #e21d2b;
            font-size: 12px;
            font-weight: 800;
        }

        .paid-pill {
            border-color: #a7f3d0;
            background: #ecfdf5;
            color: #047857;
        }

        .separator {
            height: 1px;
            background: #e8edf4;
            margin: 0 0 32px;
        }

        .billing-table {
            margin-bottom: 50px;
        }

        .billing-left {
            width: 50%;
        }

        .billing-right {
            width: 50%;
            text-align: right;
        }

        .section-kicker {
            color: #9aa7bb;
            font-size: 11px;
            font-weight: 800;
            letter-spacing: .07em;
            text-transform: uppercase;
            margin-bottom: 15px;
        }

        .client-name {
            margin: 0 0 4px;
            color: #06152d;
            font-size: 17px;
            font-weight: 800;
        }

        .client-line {
            color: #263d60;
            font-size: 13px;
            margin: 4px 0;
        }

        .right-contact {
            margin: 65px 0 11px;
        }

        .right-contact .contact-line {
            margin: 6px 0;
        }

        .vat-line {
            display: inline-block;
            min-width: 448px;
            padding-top: 11px;
            border-top: 1px solid #e8edf4;
            color: #06152d;
            font-size: 13px;
            font-weight: 800;
        }

        .vat-label {
            color: #9aa7bb;
            font-size: 11px;
            font-weight: 800;
            letter-spacing: .07em;
            text-transform: uppercase;
            margin-right: 6px;
        }

        .items {
            margin-bottom: 76px;
        }

        .items th {
            padding: 12px 16px 11px;
            border-bottom: 2px solid #dfe5ee;
            color: #415674;
            font-size: 11px;
            font-weight: 500;
            letter-spacing: .04em;
            text-transform: uppercase;
            text-align: left;
        }

        .items td {
            padding: 20px 16px 19px;
            color: #06152d;
            font-size: 13px;
            border-bottom: 0;
            word-wrap: break-word;
        }

        .col-description {
            width: 47%;
        }

        .col-price {
            width: 16%;
            text-align: right !important;
        }

        .col-qty {
            width: 18%;
            text-align: center !important;
        }

        .col-tax {
            width: 9%;
            text-align: center !important;
        }

        .col-total {
            width: 10%;
            text-align: right !important;
        }

        .item-title {
            color: #06152d;
            font-weight: 800;
            margin-bottom: 3px;
        }

        .item-sub {
            color: #496080;
            font-size: 12px;
        }

        .muted-unit {
            color: #8da0ba;
            font-size: 12px;
        }

        .bottom-table {
            margin-top: 0;
        }

        .instructions-cell {
            width: 54%;
        }

        .totals-cell {
            width: 46%;
            text-align: right;
        }

        .instructions {
            width: 464px;
            padding: 17px 17px;
            border: 1px solid #dfe8f7;
            border-radius: 11px;
            background: #f6f9ff;
        }

        .instructions-title {
            color: #0036b7;
            font-size: 13px;
            font-weight: 800;
            margin: 0 0 7px;
        }

        .instructions-text {
            color: #425674;
            font-size: 13px;
            line-height: 1.45;
        }

        .totals {
            display: inline-block;
            width: 309px;
            text-align: left;
        }

        .totals-row {
            width: 100%;
            margin-bottom: 11px;
            color: #263d60;
            font-size: 13px;
        }

        .totals-row .label {
            display: inline-block;
            width: 165px;
        }

        .totals-row .amount {
            display: inline-block;
            width: 140px;
            text-align: right;
            color: #06152d;
            font-weight: 700;
        }

        .total-divider {
            height: 1px;
            background: #dfe5ee;
            margin: 13px 0 16px;
        }

        .grand-total {
            color: #06152d;
            font-size: 16px;
            font-weight: 800;
        }

        .grand-total .grand-amount {
            float: right;
            font-size: 24px;
            line-height: 1;
            font-weight: 900;
        }

        .footer {
            position: fixed;
            left: 0;
            right: 0;
            bottom: 0;
            height: 44px;
            padding-top: 15px;
            border-top: 1px solid #e8edf4;
            background: #f7f9fc;
            color: #425674;
            text-align: center;
            font-size: 11px;
        }

        .footer a {
            color: #0047d9;
            font-weight: 700;
        }
    </style>
</head>
<body>
<?php
$client_info = $this->invoice_model->check_by(array('client_id' => $invoice_info->client_id), 'tbl_client');
if (!empty($client_info)) {
    $currency = $this->invoice_model->client_currency_symbol($invoice_info->client_id);
    $client_lang = $client_info->language;
} else {
    $client_lang = 'english';
    $currency = $this->invoice_model->check_by(array('code' => config_item('default_currency')), 'tbl_currencies');
}

unset($this->lang->is_loaded[5]);
$language_info = $this->lang->load('sales_lang', $client_lang, true, false, '', true);
$invoice_items = $this->invoice_model->ordered_items_by_id($invoice_info->invoices_id);
$subtotal = $this->invoice_model->calculate_to('invoice_cost', $invoice_info->invoices_id);
$refund_amount = $this->invoice_model->calculate_to('refund_amount', $invoice_info->invoices_id);
$discount_amount = $this->invoice_model->calculate_to('discount', $invoice_info->invoices_id);
$total_amount = $this->invoice_model->calculate_to('total', $invoice_info->invoices_id);
$paid_amount = $this->invoice_model->calculate_to('paid_amount', $invoice_info->invoices_id);
$invoice_due = $this->invoice_model->calculate_to('invoice_due', $invoice_info->invoices_id);
$tax_info = !empty($invoice_info->total_tax) ? json_decode($invoice_info->total_tax) : null;
$tax_total = 0;
$tax_label = 'Tax (0%)';
if (!empty($tax_info) && !empty($tax_info->tax_name) && !empty($tax_info->total_tax)) {
    $tax_labels = array();
    foreach ($tax_info->tax_name as $tax_key => $tax_name_raw) {
        $tax_parts = explode('|', $tax_name_raw);
        $tax_total += !empty($tax_info->total_tax[$tax_key]) ? (float) $tax_info->total_tax[$tax_key] : 0;
        if (!empty($tax_parts[1])) {
            $tax_labels[] = trim($tax_parts[1]) . '%';
        }
    }
    if (!empty($tax_labels)) {
        $tax_label = 'Tax (' . implode(', ', array_unique($tax_labels)) . ')';
    }
}

$company_name = config_item('company_legal_name_' . $client_lang) ? config_item('company_legal_name_' . $client_lang) : config_item('company_legal_name');
$company_name = !empty($company_name) ? $company_name : config_item('company_name');
$company_city = config_item('company_city_' . $client_lang) ? config_item('company_city_' . $client_lang) : config_item('company_city');
$company_country = config_item('company_country_' . $client_lang) ? config_item('company_country_' . $client_lang) : config_item('company_country');
$company_email = config_item('company_email');
$company_phone = config_item('company_phone');
$company_address = config_item('company_address_' . $client_lang) ? config_item('company_address_' . $client_lang) : config_item('company_address');
$rootPath = defined('ROOTPATH') ? ROOTPATH : FCPATH;
$logo_path = $rootPath . '/' . ltrim((string) config_item('invoice_logo'), '/');
if (!is_file($logo_path)) {
    $logo_path = $rootPath . '/uploads/default_logo.png';
}

$client_name = !empty($client_info->name) ? $client_info->name : '-';
$client_address = !empty($client_info->address) ? $client_info->address : '-';
$client_city = !empty($client_info->city) ? $client_info->city : '';
$client_zip = !empty($client_info->zipcode) ? $client_info->zipcode : '';
$client_country = !empty($client_info->country) ? $client_info->country : '';
$client_phone = !empty($client_info->phone) ? $client_info->phone : '-';
$client_email = !empty($client_info->email) ? $client_info->email : '-';
$client_vat = !empty($client_info->vat) ? $client_info->vat : '-';

$profile_info = !empty($invoice_info->user_id) ? $this->db->where('user_id', $invoice_info->user_id)->get('tbl_account_details')->row() : null;
$sales_agent = !empty($profile_info->fullname) ? $profile_info->fullname : '-';
$payment_status = $this->invoice_model->get_payment_status($invoice_info->invoices_id);
$is_paid = (float) $invoice_due <= 0;
$status_text = $is_paid ? lang('paid') : lang('not_paid');
if (empty($status_text) || $status_text === 'not_paid') {
    $status_text = $is_paid ? 'Paid' : 'Not Paid';
}

$format_amount = function ($value) {
    return display_money($value, false);
};
$invoice_date = !empty($invoice_info->invoice_date) ? date('m-d-Y', safe_strtotime($invoice_info->invoice_date)) : '-';
$due_date = !empty($invoice_info->due_date) ? date('m-d-Y', safe_strtotime($invoice_info->due_date)) : '-';
$instructions = trim(strip_tags((string) $invoice_info->notes));
if ($instructions === '') {
    $instructions = 'Please pay within 15 days of receiving this invoice. Mention the invoice number in your payment reference.';
}
?>
<div class="page">
    <table class="top-table">
        <tr>
            <td class="brand-cell">
                <table cellpadding="0" cellspacing="0">
                    <tr>
                        <td>
                            <div class="brand-row">
                                <img class="brand-logo" src="<?= $logo_path ?>" alt="<?= html_escape($company_name) ?>">
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td class="logo-spacer">&nbsp;</td>
                    </tr>
                    <tr>
                        <td>
                            <h2 class="company-name"><?= html_escape($company_name) ?></h2>
                            <div class="company-line"><?= html_escape($company_address) ?></div>
                            <div class="company-line"><?= html_escape(trim($company_city . ', ' . config_item('company_zip_code'), ', ')) ?></div>
                            <div class="company-line"><?= html_escape($company_country) ?></div>
                            <div class="company-contact">
                                <div class="contact-line"><?= html_escape($company_phone) ?></div>
                                <div class="contact-line"><?= html_escape($company_email) ?></div>
                            </div>
                        </td>
                    </tr>
                </table>
            </td>
            <td class="invoice-cell">
                <h1 class="invoice-word">INVOICE</h1>
                <div class="invoice-number"><?= html_escape($invoice_info->reference_no) ?></div>
                <div class="meta-card">
                    <table class="meta-table">
                        <tr>
                            <td class="meta-label">Invoice Date</td>
                            <td class="meta-value"><?= $invoice_date ?></td>
                        </tr>
                        <tr>
                            <td class="meta-label">Due Date</td>
                            <td class="meta-value"><?= $due_date ?></td>
                        </tr>
                        <tr>
                            <td class="meta-label">Sales Agent</td>
                            <td class="meta-value"><?= html_escape($sales_agent) ?></td>
                        </tr>
                    </table>
                    <div class="meta-divider"></div>
                    <table class="meta-table">
                        <tr>
                            <td class="meta-label">Status</td>
                            <td><span class="status-pill <?= $is_paid ? 'paid-pill' : '' ?>">&#9679; <?= html_escape($status_text) ?></span></td>
                        </tr>
                    </table>
                </div>
            </td>
        </tr>
    </table>

    <div class="separator"></div>

    <table class="billing-table">
        <tr>
            <td class="billing-left">
                <div class="section-kicker">Billed To</div>
                <h2 class="client-name"><?= html_escape($client_name) ?></h2>
                <div class="client-line"><?= html_escape($client_address) ?></div>
                <div class="client-line"><?= html_escape(trim($client_city . ' ' . $client_zip, ' ')) ?><?= !empty($client_country) ? ', ' . html_escape($client_country) : '' ?></div>
            </td>
            <td class="billing-right">
                <div class="right-contact">
                    <div class="contact-line"><?= html_escape($client_phone) ?></div>
                    <div class="contact-line"><?= html_escape($client_email) ?></div>
                </div>
                <div class="vat-line"><span class="vat-label">VAT Number:</span> <?= html_escape($client_vat) ?></div>
            </td>
        </tr>
    </table>

    <table class="items">
        <thead>
            <tr>
                <th class="col-description">Description</th>
                <th class="col-price">Price</th>
                <th class="col-qty">Qty</th>
                <th class="col-tax">Tax</th>
                <th class="col-total">Total</th>
            </tr>
        </thead>
        <tbody>
            <?php if (!empty($invoice_items)) { ?>
                <?php foreach ($invoice_items as $v_item) {
                    $item_name = !empty($v_item->item_name) ? $v_item->item_name : $v_item->item_desc;
                    $item_desc = !empty($v_item->item_desc) && $v_item->item_desc !== $item_name ? $v_item->item_desc : '';
                    $item_tax_name = !empty($v_item->item_tax_name) ? json_decode($v_item->item_tax_name) : array();
                    $tax_display = '-';
                    if (!empty($item_tax_name)) {
                        $tax_parts = array();
                        foreach ($item_tax_name as $tax_name_raw) {
                            $split_tax = explode('|', $tax_name_raw);
                            $tax_parts[] = !empty($split_tax[1]) ? $split_tax[1] . '%' : $split_tax[0];
                        }
                        $tax_display = implode(', ', $tax_parts);
                    }
                    ?>
                    <tr>
                        <td class="col-description">
                            <div class="item-title"><?= html_escape($item_name) ?></div>
                            <?php if ($item_desc !== '') { ?>
                                <div class="item-sub"><?= nl2br(html_escape($item_desc)) ?></div>
                            <?php } ?>
                        </td>
                        <td class="col-price"><strong><?= $format_amount($v_item->unit_cost) ?></strong></td>
                        <td class="col-qty"><?= $format_amount($v_item->quantity) ?> <span class="muted-unit"><?= html_escape($v_item->unit) ?></span></td>
                        <td class="col-tax"><?= html_escape($tax_display) ?></td>
                        <td class="col-total"><strong><?= $format_amount($v_item->total_cost) ?></strong></td>
                    </tr>
                <?php } ?>
            <?php } ?>
        </tbody>
    </table>

    <table class="bottom-table">
        <tr>
            <td class="instructions-cell">
                <div class="instructions">
                    <div class="instructions-title">Payment Instructions</div>
                    <div class="instructions-text"><?= nl2br(html_escape($instructions)) ?></div>
                </div>
            </td>
            <td class="totals-cell">
                <div class="totals">
                    <div class="totals-row"><span class="label">Subtotal</span><span class="amount"><?= $format_amount($subtotal) ?></span></div>
                    <?php if ((float) $discount_amount > 0) { ?>
                        <div class="totals-row"><span class="label">Discount</span><span class="amount">-<?= $format_amount($discount_amount) ?></span></div>
                    <?php } ?>
                    <?php if ((float) $refund_amount > 0) { ?>
                        <div class="totals-row"><span class="label">Refund</span><span class="amount">-<?= $format_amount($refund_amount) ?></span></div>
                    <?php } ?>
                    <div class="totals-row"><span class="label"><?= html_escape($tax_label) ?></span><span class="amount"><?= $format_amount($tax_total) ?></span></div>
                    <?php if ((float) $invoice_info->adjustment > 0) { ?>
                        <div class="totals-row"><span class="label">Adjustment</span><span class="amount"><?= $format_amount($invoice_info->adjustment) ?></span></div>
                    <?php } ?>
                    <?php if ((float) $paid_amount > 0) { ?>
                        <div class="totals-row"><span class="label">Paid Amount</span><span class="amount">-<?= $format_amount($paid_amount) ?></span></div>
                    <?php } ?>
                    <div class="total-divider"></div>
                    <div class="grand-total">Total Due <span class="grand-amount"><?= $format_amount($invoice_due > 0 ? $invoice_due : $total_amount) ?></span></div>
                </div>
            </td>
        </tr>
    </table>
</div>
<div class="footer">
    Thank you for your business! If you have any questions concerning this invoice, contact <a href="mailto:<?= html_escape($company_email) ?>"><?= html_escape($company_email) ?></a>.
</div>
</body>
</html>
