<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<?php
$direction = $this->session->userdata('direction');
$RTL = (!empty($direction) && $direction == 'rtl') ? 'on' : config_item('RTL');

$client_info = $this->estimates_model->check_by(array('client_id' => $estimates_info->client_id), 'tbl_client');
$client_lang = !empty($client_info->language) ? $client_info->language : 'english';
unset($this->lang->is_loaded[5]);
$language_info = $this->lang->load('sales_lang', $client_lang, TRUE, FALSE, '', TRUE);
$currency = $this->estimates_model->client_currency_symbol($estimates_info->client_id);
$currency_symbol = !empty($currency->symbol) ? $currency->symbol : '';
$currency_code = !empty($currency->code) ? $currency->code : config_item('default_currency');

$t = function ($key) use ($language_info) {
    return !empty($language_info[$key]) ? $language_info[$key] : lang($key);
};

$rootPath = defined('ROOTPATH') ? ROOTPATH : FCPATH;
$img = $rootPath . '/' . config_item('invoice_logo');
if (!file_exists($img)) {
    $img = base_url() . config_item('invoice_logo');
}
if (!file_exists($img)) {
    $img = $rootPath . '/uploads/default_logo.png';
}

$client_name = !empty($client_info->name) ? $client_info->name : '-';
$address = !empty($client_info->address) ? $client_info->address : '-';
$city = !empty($client_info->city) ? $client_info->city : '';
$zipcode = !empty($client_info->zipcode) ? $client_info->zipcode : '';
$country = !empty($client_info->country) ? $client_info->country : '';
$phone = !empty($client_info->phone) ? $client_info->phone : '-';
$email = !empty($client_info->email) ? $client_info->email : '-';
$invoice_view = config_item('invoice_view');
$show_hsn = !empty($invoice_view) && $invoice_view == '2';
$summary_colspan = $show_hsn ? 4 : 3;
$invoice_items = $this->estimates_model->ordered_items_by_id($estimates_info->estimates_id);
$sub_total = $this->estimates_model->estimate_calculation('estimate_cost', $estimates_info->estimates_id);
$discount = $this->estimates_model->estimate_calculation('discount', $estimates_info->estimates_id);
$total_amount = $this->estimates_model->estimate_calculation('total', $estimates_info->estimates_id);
$tax_info = !empty($estimates_info->total_tax) ? json_decode($estimates_info->total_tax) : null;
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title><?= html_escape($t('estimates')) ?></title>
    <style type="text/css">
        @font-face {
            font-family: "Source Sans Pro", sans-serif;
        }

        body {
            margin: 0;
            color: #263244;
            background: #ffffff;
            font-size: 11px;
            line-height: 1.5;
            font-family: DejaVu Sans, "Source Sans Pro", sans-serif;
            <?php if (!empty($RTL)) { ?>text-align: right;<?php } ?>
        }

        .page-shell {
            padding: 18px 20px 28px;
        }

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

        .document-frame {
            border: 1px solid #d9e2ef;
            border-top: 4px solid #0f172a;
            padding: 18px;
            background: #ffffff;
        }

        .brand-table td,
        .meta-table td,
        .party-table td,
        .totals-table td {
            border: 0;
        }

        .brand-logo {
            width: 58px;
            height: 58px;
            border: 1px solid #d9e2ef;
            background: #ffffff;
            padding: 5px;
            object-fit: contain;
        }

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

        .brand-subtitle {
            margin: 0 0 8px;
            color: #64748b;
            font-size: 9px;
            letter-spacing: 0.18em;
            text-transform: uppercase;
        }

        .muted {
            color: #64748b;
        }

        .doc-title {
            margin: 0 0 8px;
            color: #0f172a;
            font-size: 24px;
            font-weight: 400;
            letter-spacing: 0.22em;
            text-transform: uppercase;
            text-align: right;
        }

        .meta-table td {
            padding: 3px 0;
            font-size: 10.5px;
        }

        .meta-table .label {
            color: #64748b;
            text-transform: uppercase;
            letter-spacing: 0.06em;
        }

        .meta-table .value {
            color: #0f172a;
            font-weight: 700;
            text-align: right;
        }

        .status-chip {
            display: inline-block;
            padding: 3px 9px;
            border-radius: 999px;
            background: #fff1f2;
            color: #be123c;
            border: 1px solid #fecdd3;
            font-size: 9px;
            font-weight: 700;
            text-transform: uppercase;
        }

        .status-accepted {
            background: #ecfdf5;
            color: #047857;
            border-color: #a7f3d0;
        }

        .status-sent {
            background: #eff6ff;
            color: #1d4ed8;
            border-color: #bfdbfe;
        }

        .party-table {
            margin-top: 20px;
        }

        .party-box {
            background: #f8fafc;
            padding: 14px 15px;
        }

        .section-label {
            margin: 0 0 9px;
            color: #64748b;
            font-size: 9px;
            font-weight: 700;
            letter-spacing: 0.16em;
            text-transform: uppercase;
        }

        .party-name {
            margin: 0 0 5px;
            color: #0f172a;
            font-size: 13px;
            font-weight: 700;
        }

        .items-table {
            margin-top: 20px;
            border: 1px solid #d9e2ef;
        }

        .items-table th {
            padding: 10px 9px;
            background: #0f172a;
            color: #ffffff;
            font-size: 9.5px;
            font-weight: 700;
            letter-spacing: 0.06em;
            text-align: left;
            text-transform: uppercase;
            border-bottom: 1px solid #0f172a;
        }

        .items-table td {
            padding: 10px 9px;
            border-bottom: 1px solid #e5edf7;
            vertical-align: top;
        }

        .items-table tbody tr:nth-child(even) td {
            background: #fbfdff;
        }

        .item-name {
            color: #0f172a;
            font-size: 11px;
            font-weight: 700;
        }

        .item-desc {
            margin-top: 3px;
            color: #64748b;
            font-size: 10px;
        }

        .right {
            text-align: right;
        }

        .totals-table {
            margin-top: 14px;
        }

        .totals-table td {
            padding: 5px 0;
        }

        .totals-box {
            border-top: 1px solid #d9e2ef;
            padding-top: 9px;
        }

        .total-label {
            color: #64748b;
        }

        .total-value {
            color: #0f172a;
            font-weight: 700;
            text-align: right;
        }

        .grand-total td {
            padding-top: 10px;
            border-top: 1px solid #c7d2fe;
            color: #0f172a;
            font-size: 13px;
            font-weight: 700;
        }

        .amount-words {
            margin-top: 14px;
            padding: 11px 13px;
            border: 1px solid #d9e2ef;
            background: #f8fafc;
            color: #334155;
            font-size: 10.5px;
        }

        .notes-box {
            margin-top: 18px;
            padding: 13px 15px;
            border-left: 4px solid #0f172a;
            background: #f8fafc;
            color: #475569;
        }

        .thanks {
            margin: 18px 0 8px;
            color: #0f172a;
            font-size: 14px;
            font-weight: 700;
        }

        .summary-panel {
            margin-top: 18px;
            border: 1px solid #d9e2ef;
        }

        .summary-heading {
            padding: 10px 12px;
            background: #f8fafc;
            border-bottom: 1px solid #d9e2ef;
            color: #0f172a;
            font-size: 11px;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: 0.08em;
        }

        footer {
            position: absolute;
            bottom: 0;
            left: 0;
            right: 0;
            height: 24px;
            padding: 7px 20px 0;
            border-top: 1px solid #d9e2ef;
            color: #64748b;
            font-size: 9px;
            text-align: center;
        }
    </style>
</head>
<body>
<div class="page-shell">
    <div class="document-frame">
        <table class="brand-table">
            <tr>
                <td style="width: 58%; vertical-align: top;">
                    <table>
                        <tr>
                            <td style="width: 72px; vertical-align: top;">
                                <img class="brand-logo" src="<?= $img ?>" alt="<?= html_escape(config_item('company_name')) ?>">
                            </td>
                            <td style="vertical-align: top;">
                                <h1 class="brand-name"><?= html_escape(config_item('company_name')) ?></h1>
                                <div><?= html_escape(config_item('company_legal_name_' . $client_lang) ?: config_item('company_legal_name')) ?></div>
                                <div class="muted"><?= html_escape(config_item('company_address_' . $client_lang) ?: config_item('company_address')) ?></div>
                                <div class="muted"><?= html_escape(config_item('company_city_' . $client_lang) ?: config_item('company_city')) ?> <?= html_escape(config_item('company_zip_code')) ?></div>
                                <div class="muted"><?= html_escape(config_item('company_country_' . $client_lang) ?: config_item('company_country')) ?></div>
                            </td>
                        </tr>
                    </table>
                </td>
                <td style="width: 42%; vertical-align: top;">
                    <h2 class="doc-title"><?= html_escape($t('estimates')) ?></h2>
                    <table class="meta-table">
                        <tr>
                            <td class="label"><?= html_escape(lang('reference_no')) ?></td>
                            <td class="value"><?= html_escape($estimates_info->reference_no) ?></td>
                        </tr>
                        <tr>
                            <td class="label"><?= html_escape($t('estimate_date')) ?></td>
                            <td class="value"><?= display_date($estimates_info->estimate_date) ?></td>
                        </tr>
                        <tr>
                            <td class="label"><?= html_escape($t('due_date')) ?></td>
                            <td class="value" style="color:#be123c;"><?= display_date($estimates_info->due_date) ?></td>
                        </tr>
                        <tr>
                            <td class="label"><?= html_escape($t('estimate_status')) ?></td>
                            <td class="value">
                                <?php
                                $status_class = $estimates_info->status == 'accepted' ? ' status-accepted' : ($estimates_info->status == 'sent' ? ' status-sent' : '');
                                ?>
                                <span class="status-chip<?= $status_class ?>"><?= html_escape(lang($estimates_info->status)) ?></span>
                            </td>
                        </tr>
                        <?php if (!empty($estimates_info->user_id)) { ?>
                            <tr>
                                <td class="label"><?= html_escape(lang('sales') . ' ' . lang('agent')) ?></td>
                                <td class="value"><?= html_escape(fullname($estimates_info->user_id)) ?></td>
                            </tr>
                        <?php } ?>
                        <?php
                        $show_custom_fields = custom_form_label(10, $estimates_info->estimates_id);
                        if (!empty($show_custom_fields)) {
                            foreach ($show_custom_fields as $c_label => $v_fields) {
                                if (!empty($v_fields)) { ?>
                                    <tr>
                                        <td class="label"><?= html_escape($c_label) ?></td>
                                        <td class="value"><?= html_escape($v_fields) ?></td>
                                    </tr>
                                <?php }
                            }
                        }
                        ?>
                    </table>
                </td>
            </tr>
        </table>

        <table class="party-table">
            <tr>
                <td style="width: 50%; padding-right: 8px; vertical-align: top;">
                    <div class="party-box">
                        <div class="section-label"><?= html_escape(lang('our_info')) ?></div>
                        <div class="party-name"><?= html_escape(config_item('company_legal_name_' . $client_lang) ?: config_item('company_legal_name')) ?></div>
                        <div><?= html_escape(config_item('company_address_' . $client_lang) ?: config_item('company_address')) ?></div>
                        <div><?= html_escape(config_item('company_city_' . $client_lang) ?: config_item('company_city')) ?>, <?= html_escape(config_item('company_zip_code')) ?></div>
                        <div><?= html_escape(config_item('company_country_' . $client_lang) ?: config_item('company_country')) ?></div>
                        <div><?= html_escape(config_item('company_phone')) ?></div>
                        <div><?= html_escape(config_item('company_email')) ?></div>
                        <?php if (config_item('company_vat')) { ?>
                            <div><?= html_escape(config_item('company_vat')) ?></div>
                        <?php } ?>
                    </div>
                </td>
                <td style="width: 50%; padding-left: 8px; vertical-align: top;">
                    <div class="party-box">
                        <div class="section-label"><?= html_escape(lang('customer')) ?></div>
                        <div class="party-name"><?= html_escape($client_name) ?></div>
                        <div><?= html_escape($address) ?></div>
                        <div><?= html_escape(trim($city . ', ' . $zipcode . ', ' . $country, ', ')) ?></div>
                        <div><?= html_escape($phone) ?></div>
                        <div><?= html_escape($email) ?></div>
                        <?php if (!empty($client_info->vat)) { ?>
                            <div><?= html_escape(lang('vat_number')) ?>: <?= html_escape($client_info->vat) ?></div>
                        <?php } ?>
                    </div>
                </td>
            </tr>
        </table>

        <table class="items-table">
            <thead>
            <tr>
                <th><?= html_escape($t('items')) ?></th>
                <?php if ($show_hsn) { ?><th><?= html_escape(lang('hsn_code')) ?></th><?php } ?>
                <th class="right"><?= html_escape($t('qty')) ?></th>
                <th class="right"><?= html_escape($t('price')) ?></th>
                <th><?= html_escape($t('tax')) ?></th>
                <th class="right"><?= html_escape($t('total')) ?></th>
            </tr>
            </thead>
            <tbody>
            <?php if (!empty($invoice_items)) {
                foreach ($invoice_items as $v_item) {
                    $item_name = !empty($v_item->item_name) ? $v_item->item_name : $v_item->item_desc;
                    $item_tax_name = !empty($v_item->item_tax_name) ? json_decode($v_item->item_tax_name) : array(); ?>
                    <tr>
                        <td>
                            <div class="item-name"><?= html_escape($item_name) ?></div>
                            <?php if (!empty($v_item->item_desc)) { ?>
                                <div class="item-desc"><?= nl2br(html_escape($v_item->item_desc)) ?></div>
                            <?php } ?>
                        </td>
                        <?php if ($show_hsn) { ?><td><?= html_escape($v_item->hsn_code) ?></td><?php } ?>
                        <td class="right"><?= html_escape($v_item->quantity . ' ' . $v_item->unit) ?></td>
                        <td class="right"><?= display_money($v_item->unit_cost, $currency_symbol) ?></td>
                        <td>
                            <?php
                            if (!empty($item_tax_name)) {
                                foreach ($item_tax_name as $v_tax_name) {
                                    $i_tax_name = explode('|', $v_tax_name);
                                    $tax_percent = !empty($i_tax_name[1]) ? (float) $i_tax_name[1] : 0;
                                    echo html_escape($i_tax_name[0] . ' (' . $tax_percent . '%)') . '<br>';
                                    echo '<span class="muted">' . display_money($v_item->total_cost / 100 * $tax_percent, $currency_symbol) . '</span><br>';
                                }
                            } else {
                                echo '-';
                            }
                            ?>
                        </td>
                        <td class="right"><?= display_money($v_item->total_cost, $currency_symbol) ?></td>
                    </tr>
                <?php }
            } ?>
            </tbody>
        </table>

        <table class="totals-table">
            <tr>
                <td style="width: 55%; vertical-align: top;">
                    <?php if (config_item('amount_to_words') == 'Yes') { ?>
                        <div class="amount-words">
                            <strong><?= html_escape(lang('num_word')) ?>:</strong>
                            <?= html_escape(number_to_word($currency_code, $total_amount)) ?>
                        </div>
                    <?php } ?>
                </td>
                <td style="width: 45%; vertical-align: top;">
                    <div class="totals-box">
                        <table>
                            <tr>
                                <td class="total-label"><?= html_escape($t('sub_total')) ?></td>
                                <td class="total-value"><?= display_money($sub_total, $currency_symbol) ?></td>
                            </tr>
                            <?php if ($estimates_info->discount_total > 0) { ?>
                                <tr>
                                    <td class="total-label"><?= html_escape($t('discount')) ?> (<?= html_escape($estimates_info->discount_percent) ?>%)</td>
                                    <td class="total-value">-<?= display_money($discount, $currency_symbol) ?></td>
                                </tr>
                            <?php } ?>
                            <?php
                            $tax_total = 0;
                            if (!empty($tax_info) && !empty($tax_info->tax_name)) {
                                $tax_name = $tax_info->tax_name;
                                $total_tax = !empty($tax_info->total_tax) ? $tax_info->total_tax : array();
                                foreach ($tax_name as $t_key => $v_tax_info) {
                                    $tax = explode('|', $v_tax_info);
                                    $tax_value = !empty($total_tax[$t_key]) ? (float) $total_tax[$t_key] : 0;
                                    $tax_total += $tax_value; ?>
                                    <tr>
                                        <td class="total-label"><?= html_escape($tax[0] . ' (' . (!empty($tax[1]) ? $tax[1] : 0) . '%)') ?></td>
                                        <td class="total-value"><?= display_money($tax_value, $currency_symbol) ?></td>
                                    </tr>
                                <?php }
                            }
                            ?>
                            <?php if ($tax_total > 0) { ?>
                                <tr>
                                    <td class="total-label"><?= html_escape($t('total') . ' ' . $t('tax')) ?></td>
                                    <td class="total-value"><?= display_money($tax_total, $currency_symbol) ?></td>
                                </tr>
                            <?php } ?>
                            <?php if ($estimates_info->adjustment > 0) { ?>
                                <tr>
                                    <td class="total-label"><?= html_escape($t('adjustment')) ?></td>
                                    <td class="total-value"><?= display_money($estimates_info->adjustment, $currency_symbol) ?></td>
                                </tr>
                            <?php } ?>
                            <tr class="grand-total">
                                <td><?= html_escape($t('total')) ?></td>
                                <td class="right"><?= display_money($total_amount, $currency_symbol) ?></td>
                            </tr>
                        </table>
                    </div>
                </td>
            </tr>
        </table>

        <div class="thanks"><?= html_escape(lang('thanks')) ?>!</div>
        <div class="notes-box"><?= strip_html_tags($estimates_info->notes, true) ?></div>

        <?php if (!empty($invoice_view) && $invoice_view > 0) { ?>
            <div class="summary-panel">
                <div class="summary-heading"><?= html_escape(lang('tax_summary')) ?></div>
                <table class="items-table" style="margin-top:0; border:0;">
                    <thead>
                    <tr>
                        <th><?= html_escape($t('items')) ?></th>
                        <?php if ($show_hsn) { ?><th><?= html_escape(lang('hsn_code')) ?></th><?php } ?>
                        <th class="right"><?= html_escape($t('qty')) ?></th>
                        <th><?= html_escape($t('tax')) ?></th>
                        <th class="right"><?= html_escape($t('total_tax')) ?></th>
                        <th class="right"><?= html_escape($t('tax_excl_amt')) ?></th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php
                    $summary_tax = 0;
                    $summary_cost = 0;
                    if (!empty($invoice_items)) {
                        foreach ($invoice_items as $v_item) {
                            $item_tax_name = !empty($v_item->item_tax_name) ? json_decode($v_item->item_tax_name) : array();
                            $tax_amount = 0; ?>
                            <tr>
                                <td><?= html_escape($v_item->item_name) ?></td>
                                <?php if ($show_hsn) { ?><td><?= html_escape($v_item->hsn_code) ?></td><?php } ?>
                                <td class="right"><?= html_escape($v_item->quantity . ' ' . $v_item->unit) ?></td>
                                <td>
                                    <?php
                                    if (!empty($item_tax_name)) {
                                        foreach ($item_tax_name as $v_tax_name) {
                                            $i_tax_name = explode('|', $v_tax_name);
                                            $tax_percent = !empty($i_tax_name[1]) ? (float) $i_tax_name[1] : 0;
                                            $line_tax = $v_item->total_cost / 100 * $tax_percent;
                                            $tax_amount += $line_tax;
                                            echo html_escape($i_tax_name[0] . ' (' . $tax_percent . '%)') . '<br>';
                                        }
                                    } else {
                                        echo '-';
                                    }
                                    $summary_cost += $v_item->total_cost;
                                    $summary_tax += $tax_amount;
                                    ?>
                                </td>
                                <td class="right"><?= display_money($tax_amount, $currency_symbol) ?></td>
                                <td class="right"><?= display_money($v_item->total_cost, $currency_symbol) ?></td>
                            </tr>
                        <?php }
                    } ?>
                    </tbody>
                    <tfoot>
                    <tr class="grand-total">
                        <td colspan="<?= $summary_colspan ?>"></td>
                        <td class="right"><?= display_money($summary_tax, $currency_symbol) ?></td>
                        <td class="right"><?= display_money($summary_cost, $currency_symbol) ?></td>
                    </tr>
                    </tfoot>
                </table>
            </div>
        <?php } ?>
    </div>
</div>
<footer><?= config_item('estimate_footer') ?></footer>
</body>
</html>
