<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <template id="pdf_export_main">
         <html>
             <head>
                 <base t-att-href="base_url"/>
                 <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
                 <t t-call-assets="odex30_account_reports.assets_pdf_export" t-js="False"/>
             </head>
             <body>
                 <div t-att-class="'o_content ' + options['css_custom_class']">
                     <header>
                         <div class="o_title">
                             <t t-if="report.filter_show_draft and options['all_entries']">[Draft]</t>
                             <t t-out="report_title"/>
                         </div>
                         <div class="row o_header_font">
                             <div class="col-8">
                                 <!-- All company information (name, address, vat, ...) -->
                                 <t t-call="{{custom_templates.get('company_information', 'odex30_account_reports.company_information')}}"/>
                             </div>
                             <div class="col-4">
                                 <!-- All filters and options -->
                                 <t t-call="{{custom_templates.get('pdf_export_filters', 'odex30_account_reports.pdf_export_filters')}}"/>
                             </div>
                         </div>
                     </header>

                     <div class="d-flex align-items-start">
                         <t t-foreach="options.get('horizontal_split') and ['left', 'right'] or [None]" t-as="split_side">
                             <table t-attf-class="o_table  #{options.get('horizontal_split') and 'horizontal_split_page'}">
                                 <!-- Header -->
                                 <t t-call="{{custom_templates.get('pdf_export_main_table_header', 'odex30_account_reports.pdf_export_main_table_header')}}"/>

                                 <!-- Body -->
                                 <tbody>
                                     <t t-if="lines">
                                         <t t-call="{{custom_templates.get('pdf_export_main_table_body', 'odex30_account_reports.pdf_export_main_table_body')}}">
                                             <t t-set="lines" t-value="filter(lambda x: not split_side or split_side == x.get('horizontal_split_side', 'left'), lines)"/>
                                         </t>
                                     </t>
                                 </tbody>
                             </table>
                         </t>
                     </div>

                     <!-- Annotations -->
                     <ol class="o_annotation">
                        <t t-foreach="annotations" t-as="annotation">
                            <li>
                                <t t-out="annotation.get('number')"/>.
                                <t t-if="annotation.get('date')"><t t-out="annotation['date']"/> -</t>
                                <t t-out="annotation.get('text')"/>
                            </li>
                        </t>
                     </ol>
                 </div>
             </body>
         </html>
    </template>

    <template id="company_information">
        <t t-set="company_names" t-value="[company['name'] for company in options['companies']]"/>
        <div class="row">
            <div class="col-10" t-out="', '.join(company_names)"/>
        </div>

        <address class="mb-0 o_text_muted" t-field="env.company.partner_id" t-options='{"widget": "contact", "fields": ["address"], "no_marker": True}'/>

        <t t-if="options.get('tax_unit', 'company_only') == 'company_only'">
            <t t-if="env.company.account_fiscal_country_id.vat_label" t-out="env.company.account_fiscal_country_id.vat_label+':'"/>
            <t t-else="">Tax ID:</t>
            <t t-out="env.company.vat"/>
        </t>
        <t t-else="">
            Tax ID: <t t-out="env['account.tax.unit'].browse(options.get('tax_unit')).vat"/>
        </t>
    </template>

    <template id="pdf_export_filters">
        <!-- Journals -->
        <t t-if="options.get('journals')">
            <div class="row" name="filter_info_template_journals">
                <t t-set="journal_group_selected" t-value="options.get('selected_journal_groups')"/>
                <t t-if="journal_group_selected">
                    <div class="col-3">Multi-Ledger: </div>
                    <div class="col-9 o_text_muted" t-out="journal_group_selected['title']"/>
                </t>
                <t t-else="">
                    <t t-set="journal_value" t-value="[journal.get('title') for journal in options['journals'] if journal.get('selected')]"/>
                    <t t-if="journal_value">
                        <div class="col-3">Journals: </div>
                        <div class="col-9 o_text_muted" t-out="', '.join(journal_value)"/>
                    </t>
                </t>
            </div>
        </t>

        <!-- Partners -->
        <t t-if="options.get('partner_ids') != None">
            <div class="row">
                <t t-set="partner_value" t-value="[partner for partner in options['selected_partner_ids']]"/>
                <t t-if="partner_value">
                    <div class="col-3">Partners:</div>
                    <div class="col-9 o_text_muted" t-out="', '.join(partner_value)"/>
                </t>
            </div>
        </t>

        <!-- Partners categories -->
        <t t-if="options.get('partner_categories') != None">
            <div class="row">
                <t t-set="partner_category_value" t-value="[partner for partner in options['selected_partner_categories']]"/>
                <t t-if="partner_category_value">
                    <div class="col-3">Partners Categories:</div>
                    <div class="col-9 o_text_muted" t-out="', '.join(partner_category_value)"/>
                </t>
            </div>
        </t>

        <!-- Horizontal -->
        <t t-if="options.get('selected_horizontal_group_id')">
            <div class="row">
                <t t-set="horizontal_group" t-value="[hg['name'] for hg in options['available_horizontal_groups'] if hg['id'] == options.get('selected_horizontal_group_id')]"/>
                <t t-if="horizontal_group">
                    <div class="col-3">Horizontal:</div>
                    <div class="col-9 o_text_muted" t-out="horizontal_group[0]"/>
                </t>
            </div>
        </t>

        <!-- Currency -->
        <t t-if="options.get('company_currency')">
            <div class="row">
                <div class="col-3">Currency:</div>
                <div class="col-9 o_text_muted" t-out="options['company_currency']['currency_name']"/>
            </div>
        </t>

        <!-- Filters -->
        <t t-if="options.get('aml_ir_filters') and any(opt['selected'] for opt in options['aml_ir_filters'])" name="aml_ir_filters">
            <div class="row">
                <t t-set="aml_ir_filters" t-value="opt['name'] for opt in options['aml_ir_filters'] if opt['selected']"/>
                <t t-if="aml_ir_filters">
                    <div class="col-3">Filters:</div>
                    <div class="col-9 o_text_muted" t-out="', '.join(aml_ir_filters)"/>
                </t>
            </div>
        </t>

        <!-- Extra options -->
        <div class="row" name="pdf_options_header">
            <t t-call="{{custom_templates.get('pdf_export_filter_extra_options_template', 'odex30_account_reports.pdf_export_filter_extra_options_template')}}"/>
        </div>
    </template>

    <template id="pdf_export_filter_extra_options_template">
        <t t-set="rounding_unit_display_names" t-value="{k: v[1] for k, v in options['rounding_unit_names'].items() if v[1]}"/>
        <div class="col-3" t-if="(report.filter_show_draft and options['all_entries']) or
                                 (report.filter_unreconciled and options['unreconciled']) or
                                 options.get('include_analytic_without_aml') or
                                 options['rounding_unit'] in rounding_unit_display_names">
            Options:
        </div>
        <div class="col-9 o_text_muted">
            <t t-set="extra_options" t-value="[]"/>

            <!-- All entries -->
            <t t-if="report.filter_show_draft and options['all_entries']" groups="account.group_account_readonly">
                <t t-set="label_draft_entries">With Draft Entries</t>
                <t t-set="extra_options" t-value="extra_options + [label_draft_entries]"/>
            </t>

            <!-- Unreconciled -->
            <t t-if="report.filter_unreconciled and options['unreconciled']">
                <t t-set="label_unreconciled_entries">Unreconciled Entries</t>
                <t t-set="extra_options" t-value="extra_options + [label_unreconciled_entries]"/>
            </t>

            <!-- Analytic -->
            <t t-if="options.get('include_analytic_without_aml')" name="include_analytic">
                <t t-set="label_analytic_simulations">Including Analytic Simulations</t>
                <t t-set="extra_options" t-value="extra_options + [label_analytic_simulations]"/>
            </t>

            <!-- Currency Unit Amount Text -->
            <t t-if="options['rounding_unit'] in rounding_unit_display_names">
                <t t-set="rounding_unit" t-value="options.get('rounding_unit')"/>
                <t t-set="extra_options" t-value="extra_options + [rounding_unit_display_names[rounding_unit]]"/>
            </t>

            <t t-out="', '.join(extra_options)"/>
        </div>
    </template>

    <template id="pdf_export_main_table_header">
        <thead id="table_header">
            <t t-foreach="options['column_headers']" t-as="column_header">
                <tr>
                    <!-- First empty column -->
                    <th/>

                    <!-- Other columns -->
                    <t t-foreach="column_header * column_headers_render_data['level_repetitions'][column_header_index]" t-as="header">
                        <th t-att-colspan="header.get('colspan', column_headers_render_data['level_colspan'][column_header_index]) + (1 if options.get('show_horizontal_group_total') and column_header_first else 0)" class="o_overflow_name">
                            <t t-out="header.get('name')"/>
                        </th>
                    </t>

                    <th t-if="options.get('show_horizontal_group_total') and not column_header_first">
                        <t t-out="[group['name'] for group in options['available_horizontal_groups'] if group['id'] == options['selected_horizontal_group_id']][0]"/>
                    </th>

                    <th t-if="options.get('column_percent_comparison') == 'growth'">%</th>
                </tr>
            </t>
            <!-- Custom subheaders -->
            <t t-if="column_headers_render_data['custom_subheaders']">
                <tr>
                    <!-- First empty column -->
                    <th/>

                    <!-- Other columns -->
                    <t t-foreach="column_headers_render_data['custom_subheaders']" t-as="subheader">
                        <th t-att-colspan="subheader.get('colspan', 1)">
                            <t t-out="subheader.get('name')"/>
                        </th>
                    </t>
                </tr>
            </t>
            <tr>
                <!-- First empty column -->
                <th/>

                <t t-foreach="options['columns']" t-as="subheader">
                    <th>
                        <t t-out="subheader.get('name')"/>
                    </th>
                </t>
                <th t-if="options.get('show_horizontal_group_total')">
                    <t t-out="options['columns'][0].get('name')"/>
                </th>
                <th t-if="options.get('column_percent_comparison') == 'growth'"/>
            </tr>
        </thead>
    </template>

    <template id="pdf_export_main_table_body">
        <t t-foreach="lines" t-as="line">
            <t t-set="o_line_level" t-value="'o_line_level_' + str(line['level'])"/>

            <t t-if="line.get('page_break') and not options.get('horizontal_split')">
                <!-- End current table -->
                <t t-out="table_end"/>

                <!-- Append table header -->
                <t t-call="{{custom_templates.get('pdf_export_main_table_header', 'odex30_account_reports.pdf_export_main_table_header')}}"/>

                <!-- Start new table -->
                <t t-out="table_start"/>
            </t>

            <!-- Adds an empty row above line with level 0 to add some spacing (it is the easiest and cleanest way) -->
            <t t-if="line_index != 0 and line['level'] == 0">
                <tr>
                    <td/>

                    <t t-foreach="line.get('columns')" t-as="cell">
                        <td/>
                    </t>

                    <t t-if="options.get('column_percent_comparison')">
                        <td/>
                    </t>

                    <t t-if="options.get('show_horizontal_group_total')">
                        <td/>
                    </t>
                </tr>
            </t>

            <t t-set="o_bold" t-value="(' o_fw_bold' if line.get('unfolded') or 'total' in line.get('id') else '')"/>
            <t t-set="o_overflow" t-value="(' o_overflow_name' if len(line.get('name') or '') > 42 else '')"/>

            <tr t-att-class="o_line_level + o_bold + o_overflow" name="pdf_export_main_table_body_lines_tr">
                <td t-att-colspan="line.get('colspan', '1')" class="o_line_name_level">
                    <t t-out="line.get('name')"/>
                    <t t-if="line.get('annotations')">
                        <t t-foreach="annotations" t-as="annotation">
                            <t t-if="annotation.get('number') and annotation['number'] in (line.get('annotations') or [])">
                                <sup t-out="annotation['number']"/>
                            </t>
                        </t>
                    </t>
                </td>

                <t t-foreach="line.get('columns')" t-as="cell">
                    <td class="o_cell_td">
                        <t t-if="not env.company.totals_below_sections or options.get('ignore_totals_below_sections') or not line.get('unfolded')">
                            <t t-call="{{custom_templates.get('pdf_export_cell', 'odex30_account_reports.pdf_export_cell')}}"/>
                        </t>
                    </td>
                </t>

                <t t-if="options.get('column_percent_comparison')">
                    <td class="o_column_percent_comparison">
                        <t t-if="line.get('column_percent_comparison_data')">
                            <t t-out="line['column_percent_comparison_data'].get('name')"/>
                        </t>
                    </td>
                </t>

                <t t-if="options.get('show_horizontal_group_total')">
                    <td class="o_cell_td">
                        <t t-if="line.get('horizontal_group_total_data')">
                            <t t-set="o_classes" t-value="'o_line_cell_value_number' + (' o_muted' if line['horizontal_group_total_data'].get('no_format') == 0 else '')"/>
                            <span t-att-class="o_classes" t-out="line['horizontal_group_total_data'].get('name')"/>
                        </t>
                    </td>
                </t>
            </tr>
        </t>
    </template>

    <template id="pdf_export_cell">
        <t t-if="cell.get('figure_type', '') in ['float', 'integer', 'monetary', 'percentage']">
            <t t-set="o_classes" t-value="'o_line_cell_value_number' + (' o_muted' if cell.get('is_zero') else '')"/>
        </t>
        <t t-else="">
            <t t-set="o_classes" t-value="'o_overflow_value'"/>
        </t>

        <span t-att-class="o_classes" t-out="cell.get('name')"/>
    </template>
</odoo>
