<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <?php
    $direction = $this->session->userdata('direction');
    if (!empty($direction) && $direction == 'rtl') {
        $RTL = 'on';
    } else {
        $RTL = config_item('RTL');
    }

    $account_info = $this->db->where(array('account_id' => $expense_info->account_id))->get('tbl_accounts')->row();
    $currency = $this->db->where(array('code' => config_item('default_currency')))->get('tbl_currencies')->row();
    $category_info = $this->db->where('expense_category_id', $expense_info->category_id)->get('tbl_expense_category')->row();
    $category = !empty($category_info) ? $category_info->expense_category : lang('undefined_category');
    $client_name = $this->db->where('client_id', $expense_info->paid_by)->get('tbl_client')->row();
    $payment_methods = null;
    if (!empty($expense_info->payment_methods_id)) {
        $payment_methods = $this->db->where('payment_methods_id', $expense_info->payment_methods_id)->get('tbl_payment_methods')->row();
    }
    $project = null;
    if (!empty($expense_info->project_id)) {
        $project = $this->db->where('project_id', $expense_info->project_id)->get('tbl_project')->row();
    }

    $invoice_info = null;
    $payment_status = null;
    $paid_amount = 0;
    if (!empty($expense_info->invoices_id)) {
        $payment_status = $this->invoice_model->get_payment_status($expense_info->invoices_id);
        $invoice_info = $this->db->where('invoices_id', $expense_info->invoices_id)->get('tbl_invoices')->row();
        $paid_amount = $this->invoice_model->calculate_to('paid_amount', $expense_info->invoices_id);
    }

    $status_key = '';
    if ($expense_info->type == 'Expense') {
        if (!empty($expense_info->project_id)) {
            $status_key = $expense_info->billable == 'No' ? 'not_billable' : 'billable';
        } else {
            $status_key = $expense_info->status;
        }
    } elseif (!empty($payment_status)) {
        $status_key = strtolower(str_replace(' ', '_', $payment_status));
    }

    $show_custom_fields = custom_form_label($expense_info->type == 'Income' ? 1 : 2, $expense_info->transactions_id);
    $detail_rows = array(
        array(lang('expense') . ' ' . lang('prefix'), !empty($expense_info->transaction_prefix) ? $expense_info->transaction_prefix : '-'),
        array(lang('name') . '/' . lang('title'), !empty($expense_info->name) ? $expense_info->name : '-'),
        array(lang('date'), display_date($expense_info->date)),
        array(lang('account'), !empty($account_info->account_name) ? $account_info->account_name : '-'),
        array(lang('categories'), $category),
        array(lang('paid_by'), !empty($client_name->name) ? $client_name->name : '-'),
        array(lang('payment_method'), !empty($payment_methods->method_name) ? $payment_methods->method_name : '-'),
    );
    $summary_title = $expense_info->type == 'Income' ? lang('deposit') : lang('expense');
    ?>
    <style>
        body {
            margin: 0;
            color: #172033;
            background: #ffffff;
            font: 10px/1.4 DejaVu Sans, Arial, Helvetica, sans-serif;
            min-width: 100%;
            min-height: 100%;
        <?php if (!empty($RTL)) { ?> text-align: right;
        <?php } ?>
        }

        table {
            width: 100%;
            border-collapse: collapse;
            border-spacing: 0;
        <?php if (!empty($RTL)) { ?> text-align: right;
        <?php } ?>
        }

        .page-wrap {
            padding: 12px 14px 14px;
        }

        .brand-shell {
            border: 1px solid #d7deea;
            padding: 12px 14px;
            background: #f8fafc;
            margin-bottom: 12px;
        }

        .brand-logo {
            width: 46px;
            height: 46px;
            background: #ffffff;
            border: 1px solid #d7deea;
            padding: 4px;
        }

        .brand-title {
            font-size: 18px;
            font-weight: 500;
            color: #0f172a;
            margin: 0 0 2px;
        }

        .brand-text {
            font-size: 10px;
            color: #64748b;
            margin: 0;
        }

        .header-title {
            font-size: 16px;
            font-weight: 700;
            color: #0f172a;
            margin: 0 0 2px;
            line-height: 1.15;
        }

        .header-subtitle {
            font-size: 9px;
            color: #64748b;
            margin: 0;
            line-height: 1.25;
        }

        .report-meta {
            width: 220px;
        <?php if (!empty($RTL)) { ?> text-align: left;
        <?php } else { ?> text-align: right;
        <?php } ?>
        }

        .range-chip {
            display: inline-block;
            padding: 2px 0 0;
            background: transparent;
            color: #475569;
            font-size: 8px;
            font-weight: 700;
            letter-spacing: .08em;
            text-transform: uppercase;
        }

        .metrics {
            margin: 0 0 12px;
        }

        .metric-card {
            width: 23.5%;
            border: 1px solid #d7deea;
            background: #ffffff;
            padding: 10px 12px;
            vertical-align: top;
        }

        .metric-gap {
            width: 2%;
        }

        .metric-label {
            font-size: 9px;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: .12em;
            color: #64748b;
            margin-bottom: 4px;
        }

        .metric-value {
            font-size: 16px;
            font-weight: 700;
            color: #0f172a;
            margin: 0;
        }

        .metric-note {
            font-size: 9px;
            color: #94a3b8;
            margin-top: 2px;
        }

        .table-shell {
            border: 1px solid #d7deea;
            overflow: hidden;
            background: #ffffff;
            margin-bottom: 12px;
        }

        .table-shell table {
            border-collapse: separate;
            border-spacing: 0;
        }

        .table-head td {
            background: #1e293b;
            color: #f8fafc;
            font-size: 9px;
            font-weight: 700;
            letter-spacing: .06em;
            text-transform: uppercase;
            padding: 9px 10px;
            border-bottom: 1px solid #1e293b;
        }

        .row td {
            padding: 8px 10px;
            border-bottom: 1px solid #e2e8f0;
            color: #1e293b;
            vertical-align: top;
        }

        .row-alt td {
            background: #f8fafc;
        }

        .key {
            width: 38%;
            font-size: 9px;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: .06em;
            color: #64748b;
        }

        .val {
            font-size: 10px;
            color: #0f172a;
            line-height: 1.3;
        }

        .number {
        <?php if (!empty($RTL)) { ?> text-align: left;
        <?php } else { ?> text-align: right;
        <?php } ?>
        }

        .strong {
            font-weight: 700;
            color: #0f172a;
        }

        .muted {
            color: #64748b;
        }

        .pill {
            display: inline-block;
            padding: 3px 6px;
            border-radius: 999px;
            font-size: 8px;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: .08em;
        }

        .pill-success,
        .pill-billable,
        .pill-fully_paid,
        .pill-paid {
            background: #dcfce7;
            color: #166534;
        }

        .pill-warning,
        .pill-unpaid,
        .pill-partially_paid,
        .pill-draft {
            background: #fef3c7;
            color: #92400e;
        }

        .pill-danger,
        .pill-non_approved,
        .pill-cancelled {
            background: #fee2e2;
            color: #991b1b;
        }

        .pill-primary,
        .pill-not_billable {
            background: #dbeafe;
            color: #1d4ed8;
        }

        .total-row td {
            padding: 10px;
            background: #eef2ff;
            border-top: 1px solid #c7d2fe;
            font-weight: 700;
            color: #1e1b4b;
        }

        .note-box {
            border: 1px solid #d7deea;
            background: #ffffff;
            padding: 10px 12px;
        }

        .note-title {
            font-size: 9px;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: .08em;
            color: #475569;
            margin-bottom: 6px;
        }

        .note-body {
            border: 1px solid #e2e8f0;
            background: #f8fafc;
            padding: 8px 10px;
            font-size: 10px;
            color: #334155;
            line-height: 1.35;
            min-height: 28px;
        }

        .footer {
            border-top: 1px solid #e2e8f0;
            padding-top: 6px;
            font-size: 9px;
            color: #64748b;
            text-align: center;
        }
    </style>
</head>
<body>
    <?php
    $rootPath = defined('ROOTPATH') ? ROOTPATH : FCPATH;
    $img = $rootPath . '/' . config_item('company_logo');
    if (!file_exists($img)) {
        $img = $rootPath . '/uploads/default_logo.png';
    }
    ?>
    <div class="page-wrap">
        <div class="brand-shell">
            <table>
                <tr>
                    <td style="width: 84px; border: 0;">
                        <img class="brand-logo" src="<?= $img ?>" alt="<?= html_escape(config_item('company_name')) ?>">
                    </td>
                    <td style="border: 0;">
                        <p class="brand-title"><?= html_escape(config_item('company_name')) ?></p>
                        <p class="brand-text"><?= html_escape($title) ?></p>
                    </td>
                    <td class="report-meta" style="border:0;">
                        <p class="header-title"><?= html_escape($title) ?></p>
                        <p class="header-subtitle"><?= html_escape($summary_title) ?> <?= lang('details') ?> <?= lang('for') ?> <?= !empty($expense_info->name) ? html_escape($expense_info->name) : lang('transaction') ?></p>
                        <span class="range-chip"><?= lang('generated_on') ?> <?= display_date(date('Y-m-d')) ?></span>
                    </td>
                </tr>
            </table>
        </div>

        <table class="metrics">
            <tr>
                <td class="metric-card">
                    <div class="metric-label"><?= lang('amount') ?></div>
                    <p class="metric-value"><?= display_money($expense_info->amount, $currency->symbol) ?></p>
                    <div class="metric-note"><?= lang('date') ?>: <?= display_date($expense_info->date) ?></div>
                </td>
                <td class="metric-gap"></td>
                <td class="metric-card">
                    <div class="metric-label"><?= lang('status') ?></div>
                    <p class="metric-value" style="font-size:15px;">
                        <?php if (!empty($status_key)) { ?>
                            <span class="pill pill-<?= html_escape($status_key) ?>"><?= lang($status_key) ?></span>
                        <?php } else { ?>
                            -
                        <?php } ?>
                    </p>
                    <div class="metric-note"><?= html_escape($summary_title) ?></div>
                </td>
                <td class="metric-gap"></td>
                <td class="metric-card">
                    <div class="metric-label"><?= lang('payment_method') ?></div>
                    <p class="metric-value" style="font-size:15px;"><?= !empty($payment_methods->method_name) ? html_escape($payment_methods->method_name) : '-' ?></p>
                    <div class="metric-note"><?= lang('account') ?>: <?= !empty($account_info->account_name) ? html_escape($account_info->account_name) : '-' ?></div>
                </td>
                <td class="metric-gap"></td>
                <td class="metric-card">
                    <div class="metric-label"><?= lang('paid_amount') ?></div>
                    <p class="metric-value"><?= display_money($paid_amount, $currency->symbol) ?></p>
                    <div class="metric-note"><?= lang('project_name') ?>: <?= !empty($project->project_name) ? html_escape($project->project_name) : '-' ?></div>
                </td>
            </tr>
        </table>

        <div class="table-shell">
            <table>
                <thead>
                    <tr class="table-head">
                        <td colspan="2"><?= lang('transaction') ?> <?= lang('overview') ?></td>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($detail_rows as $index => $row) : ?>
                        <tr class="row<?= ($index % 2 !== 0) ? ' row-alt' : '' ?>">
                            <td class="key"><?= html_escape($row[0]) ?></td>
                            <td class="val"><?= $row[1] ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>

        <div class="table-shell">
            <table>
                <thead>
                    <tr class="table-head">
                        <td colspan="2"><?= lang('linked_records') ?></td>
                    </tr>
                </thead>
                <tbody>
                    <tr class="row">
                        <td class="key"><?= lang('project_name') ?></td>
                        <td class="val"><?= !empty($project->project_name) ? html_escape($project->project_name) : '-' ?></td>
                    </tr>
                    <tr class="row row-alt">
                        <td class="key"><?= lang('reference_no') ?></td>
                        <td class="val"><?= !empty($invoice_info->reference_no) ? html_escape($invoice_info->reference_no) : '-' ?></td>
                    </tr>
                    <tr class="row">
                        <td class="key"><?= lang('payment_status') ?></td>
                        <td class="val">
                            <?php if (!empty($payment_status)) { ?>
                                <span class="pill pill-<?= html_escape(strtolower(str_replace(' ', '_', $payment_status))) ?>"><?= html_escape($payment_status) ?></span>
                            <?php } else { ?>
                                -
                            <?php } ?>
                        </td>
                    </tr>
                    <tr class="total-row">
                        <td><?= lang('total') ?></td>
                        <td class="number"><?= display_money($expense_info->amount, $currency->symbol) ?></td>
                    </tr>
                </tbody>
            </table>
        </div>

        <?php
        $has_custom_fields = false;
        if (!empty($show_custom_fields)) {
            foreach ($show_custom_fields as $v_fields) {
                if (!empty($v_fields)) {
                    $has_custom_fields = true;
                    break;
                }
            }
        }
        ?>

        <?php if ($has_custom_fields) { ?>
            <div class="table-shell">
                <table>
                    <thead>
                        <tr class="table-head">
                            <td colspan="2"><?= lang('custom_fields') ?></td>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $custom_index = 0;
                        $custom_total = 0;
                        foreach ($show_custom_fields as $v_fields) {
                            if (!empty($v_fields)) {
                                $custom_total++;
                            }
                        }
                        foreach ($show_custom_fields as $c_label => $v_fields) :
                            if (!empty($v_fields)) :
                                $custom_index++;
                        ?>
                                <tr class="row<?= ($custom_index % 2 !== 0) ? '' : ' row-alt' ?>">
                                    <td class="key"><?= html_escape($c_label) ?></td>
                                    <td class="val"><?= $v_fields ?></td>
                                </tr>
                        <?php
                            endif;
                        endforeach;
                        ?>
                    </tbody>
                </table>
            </div>
        <?php } ?>

        <?php if (!empty(strip_html_tags($expense_info->notes, true))) { ?>
            <div class="note-box">
                <div class="note-title"><?= lang('notes') ?></div>
                <div class="note-body"><?= nl2br(strip_html_tags($expense_info->notes, true)); ?></div>
            </div>
        <?php } ?>

        <div class="footer">
            <?= html_escape(config_item('company_name')) ?> | <?= lang('generated_on') ?> <?= display_date(date('Y-m-d')) ?>
        </div>
    </div>
</body>
</html>
