<!DOCTYPE html>
<html>
<head>
    <title><?= lang('proposals_report') ?></title>
    <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');
    }

    $cur = $this->report_model->check_by(array('code' => config_item('default_currency')), 'tbl_currencies');
    $discount_total = 0;
    $proposal_total = 0;
    $total_tax = 0;
    $proposal_count = !empty($all_proposals) ? count($all_proposals) : 0;
    ?>
    <style>
        body {
            margin: 0;
            color: #172033;
            background: #ffffff;
            font: 12px/1.55 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: 18px 22px 24px;
        }

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

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

        .brand-title {
            font-size: 24px;
            font-weight: 700;
            color: #0f172a;
            margin: 0 0 4px;
        }

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

        .report-shell {
            border: 1px solid #d7deea;
            padding: 18px 20px;
            margin-bottom: 18px;
            background: linear-gradient(180deg, #ffffff 0%, #f8fafc 100%);
        }

        .eyebrow {
            display: inline-block;
            padding: 5px 12px;
            border: 1px solid #dbe4ff;
            border-radius: 999px;
            background: #eef2ff;
            color: #4338ca;
            font-size: 10px;
            font-weight: 700;
            letter-spacing: .16em;
            text-transform: uppercase;
            margin-bottom: 10px;
        }

        .report-title {
            font-size: 26px;
            font-weight: 700;
            color: #0f172a;
            margin: 0 0 6px;
        }

        .report-subtitle {
            font-size: 12px;
            color: #64748b;
            margin: 0;
        }

        .range-chip {
            margin-top: 12px;
            display: inline-block;
            padding: 8px 14px;
            border: 1px solid #d7deea;
            background: #ffffff;
            color: #334155;
            font-size: 11px;
            font-weight: 600;
        }

        .metrics {
            margin: 0 0 18px;
        }

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

        .metric-gap {
            width: 2%;
        }

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

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

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

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

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

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

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

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

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

        .muted {
            color: #64748b;
        }

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

        .total-row td {
            padding: 14px;
            background: #eef2ff;
            border-top: 1px solid #c7d2fe;
            font-weight: 700;
            color: #1e1b4b;
        }
    </style>
</head>
<body>
<?php
$rootPath = defined('ROOTPATH') ? ROOTPATH : FCPATH;
$img = $rootPath . '/' . config_item('invoice_logo');
$a = file_exists($img);
if (empty($a)) {
    $img = base_url() . config_item('invoice_logo');
}
if (!file_exists($img)) {
    $img = $rootPath . '/' . 'uploads/default_logo.png';
}

if (!empty($range[0])) {
    $start_date = display_date($range[0]);
    $end_date = display_date($range[1]);
}

if (!empty($all_proposals)) {
    foreach ($all_proposals as $v_proposals) {
        $proposal_total += $this->proposal_model->proposal_calculation('total', $v_proposals->proposals_id);
        $total_tax += $this->proposal_model->get_proposal_tax_amount($v_proposals->proposals_id);
        $discount_total += $this->proposal_model->get_proposal_discount($v_proposals->proposals_id);
    }
}
?>
<div class="page-wrap">
    <div class="brand-shell">
        <table>
            <tr>
                <td style="width: 84px; border: 0;">
                    <img class="brand-logo" src="<?= $img ?>" alt="<?= config_item('company_name') ?>">
                </td>
                <td style="border: 0;">
                    <p class="brand-title"><?= config_item('company_name') ?></p>
                    <p class="brand-text"><?= $this->config->item('company_address') ?></p>
                </td>
            </tr>
        </table>
    </div>

    <div class="report-shell">
        <span class="eyebrow"><?= lang('finance') ?></span>
        <p class="report-title"><?= lang('proposals_report') ?></p>
        <p class="report-subtitle">Snapshot of proposal values, tax exposure, and discount distribution by module.</p>
        <?php if (!empty($start_date)) { ?>
            <span class="range-chip"><?= lang('FROM') ?> <?= $start_date ?> <?= lang('TO') ?> <?= $end_date ?></span>
        <?php } ?>
    </div>

    <table class="metrics">
        <tr>
            <td class="metric-card">
                <div class="metric-label"><?= lang('proposal') ?></div>
                <p class="metric-value"><?= $proposal_count ?></p>
                <div class="metric-note"><?= lang('records') ?></div>
            </td>
            <td class="metric-gap"></td>
            <td class="metric-card">
                <div class="metric-label"><?= lang('amount') ?></div>
                <p class="metric-value"><?= display_money($proposal_total, $cur->symbol) ?></p>
                <div class="metric-note"><?= lang('gross_value') ?></div>
            </td>
            <td class="metric-gap"></td>
            <td class="metric-card">
                <div class="metric-label"><?= lang('tax') . ' ' . lang('amount') ?></div>
                <p class="metric-value"><?= display_money($total_tax, $cur->symbol) ?></p>
                <div class="metric-note"><?= lang('tax_summary') ?></div>
            </td>
            <td class="metric-gap"></td>
            <td class="metric-card">
                <div class="metric-label"><?= lang('discount') ?></div>
                <p class="metric-value"><?= display_money($discount_total, $cur->symbol) ?></p>
                <div class="metric-note"><?= lang('discount_total') ?></div>
            </td>
        </tr>
    </table>

    <div class="table-shell">
        <table cellpadding="0" cellspacing="0" border="0">
            <thead>
            <tr class="table-head">
                <td><?= lang('estimate') ?></td>
                <td class="number"><?= lang('proposal') . ' ' . lang('date') ?></td>
                <td class="number"><?= lang('expire') . ' ' . lang('date') ?></td>
                <td><?= lang('module') ?>#</td>
                <td class="number"><?= lang('amount') ?></td>
                <td class="number"><?= lang('tax') . ' ' . lang('amount') ?></td>
                <td class="number"><?= lang('discount') ?></td>
            </tr>
            </thead>
            <tbody>
            <?php if (!empty($all_proposals)) {
                foreach ($all_proposals as $index => $v_proposals) {
                    if ($v_proposals->module == 'client') {
                        $client_info = $this->proposal_model->check_by(array('client_id' => $v_proposals->module_id), 'tbl_client');
                        if (!empty($client_info)) {
                            $client_name = $client_info->name;
                        } else {
                            $client_name = '-';
                        }
                    } else if ($v_proposals->module == 'leads') {
                        $client_info = $this->proposal_model->check_by(array('leads_id' => $v_proposals->module_id), 'tbl_leads');
                        if (!empty($client_info)) {
                            $client_name = $client_info->lead_name;
                        } else {
                            $client_name = '-';
                        }
                    } else {
                        $client_name = '-';
                    }

                    $proposal_amount = $this->proposal_model->proposal_calculation('total', $v_proposals->proposals_id);
                    $proposal_tax = $this->proposal_model->get_proposal_tax_amount($v_proposals->proposals_id);
                    $proposal_discount = $this->proposal_model->get_proposal_discount($v_proposals->proposals_id);
                    ?>
                    <tr class="row<?= ($index % 2 !== 0) ? ' row-alt' : '' ?>">
                        <td><div class="strong"><?= $v_proposals->reference_no ?></div></td>
                        <td class="number"><?= display_date($v_proposals->proposal_date) ?></td>
                        <td class="number"><?= display_date($v_proposals->due_date) ?></td>
                        <td><div class="strong"><?= $client_name ?></div></td>
                        <td class="number strong"><?= display_money($proposal_amount, $cur->symbol); ?></td>
                        <td class="number"><?= display_money($proposal_tax, $cur->symbol); ?></td>
                        <td class="number"><?= display_money($proposal_discount, $cur->symbol); ?></td>
                    </tr>
                <?php } ?>
                <tr class="total-row">
                    <td colspan="4"><?= lang('total') ?></td>
                    <td class="number"><?= display_money($proposal_total, $cur->symbol) ?></td>
                    <td class="number"><?= display_money($total_tax, $cur->symbol) ?></td>
                    <td class="number"><?= display_money($discount_total, $cur->symbol) ?></td>
                </tr>
            <?php } else { ?>
                <tr class="row">
                    <td colspan="7" style="text-align:center;" class="muted"><?= lang('nothing_to_display') ?></td>
                </tr>
            <?php } ?>
            </tbody>
        </table>
    </div>
</div>
</body>
</html>
