<odoo>
    <record id="action_report_labels" model="ir.actions.report">
        <field name="name">Labels</field>
        <field name="model">sos_deliverables_boq</field>
        <field name="report_type">qweb-pdf</field>
        <field name="report_name">sos_inventory.labels_print</field>
        <field name="report_file">sos_inventory.labels_print</field>
        <field name="binding_model_id" eval="False"/>
        <field name="binding_type">report</field>
    </record>

    <template id="labels_print">
        <t t-call="web.html_container">
            <t t-foreach="docs" t-as="o">
                <t t-call="web.basic_layout">
                    <!-- Include Bootstrap CSS -->
                    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous"/>

                    <style>
                        /* Reset default margins and ensure full-page layout */
                        .page {
                            margin: 3mm; /* Reduced margins */
                            padding: 0;
                            width: 100%;
                            box-sizing: border-box;
                            overflow: hidden; /* Prevent content from overflowing */
                        }

                        /* Container for each set of tables (FG, SFG, Materials) */
                        .label-set {
                            page-break-inside: avoid; /* Try to keep the entire set on one page */
                            page-break-after: always; /* Force a page break after each set */
                        }

                        /* Ensure tables stay within their column */
                        .table-container {
                            width: 100%; /* Full width of the column */
                            page-break-inside: avoid; /* Prevent table from splitting across pages */
                            box-sizing: border-box;
                        }

                        /* Clearfix for the last table if odd number of tables */
                        .label-set::after {
                            content: "";
                            display: block;
                            clear: both;
                        }

                        /* Compact table styling */
                        .label-table {
                            width: 100%;
                            border-collapse: collapse;
                        }

                        .label-table td {
                            font-size: 15px; /* Smaller font size for compactness */
                            padding: 2mm; /* Reduced padding */
                            border: 1px solid #000;
                            box-sizing: border-box;
                        }

                        .label-table .column {
                            font-weight: bold;
                            font-size: 14px;
                            width:20%
                        }

                        /* Adjust Bootstrap row margins for PDF */
                        .row {
                            margin-left: 0;
                            margin-right: 0;
                            margin-bottom:40px;
                        }

                        /* Adjust Bootstrap column padding for PDF */
                        [class*="col-"] {
                            padding-left: 5px;
                            padding-right: 5px;
                        }

                        /* Style for empty column placeholder */
                        .empty-col {
                            font-size: 10px;
                            text-align: center;
                            padding: 2mm;
                        }
                    </style>

                    <div class="page">
                        

                        <t t-set="counter" t-value="0"/>
                        <t t-foreach="range(o.master_quantity)" t-as="line_items">
                            <t t-set="counter" t-value="counter + 1"/>
                            <div class="label-set">
                                <!-- Combine all tables into a single list -->
                                <t t-set="all_tables" t-value="[]"/>
                                <!-- Add FG tables -->
                                <t t-foreach="o.line_ids_fg or []" t-as="fg_item">
                                    <t t-set="fg_index_safe" t-value="fg_index if fg_index is not None else 0"/>
                                    <t t-set="all_tables" t-value="all_tables + [(1, fg_item, fg_index_safe + 1)]"/>
                                </t>
                                <!-- Add SFG tables -->
                                <t t-foreach="o.line_ids_sfg or []" t-as="sfg_item">
                                    <t t-set="sfg_index_safe" t-value="sfg_index if sfg_index is not None else 0"/>
                                    <t t-set="all_tables" t-value="all_tables + [(2, sfg_item, sfg_index_safe + 1)]"/>
                                </t>
                                <!-- Add Materials tables -->
                                <t t-foreach="o.line_ids_material or []" t-as="material_item">
                                    <t t-set="material_index_safe" t-value="material_index if material_index is not None else 0"/>
                                    <t t-set="all_tables" t-value="all_tables + [(3, material_item, material_index_safe + 1)]"/>
                                </t>

                           

                                <!-- Process all tables in pairs -->
                                <t t-foreach="range(0, len(all_tables), 2)" t-as="pair_index">
                                    <div class="row">
                                        <!-- First table in the pair -->
                                        <div class="col-6">
                                            <t t-set="table_info" t-value="all_tables[pair_index]"/>
                                            <t t-set="category" t-value="table_info[0]"/>
                                            <t t-set="item" t-value="table_info[1]"/>
                                            <t t-set="index" t-value="table_info[2]"/>
                                            <div class="table-container">
                                                <table class="label-table">
                                                    <tbody>
                                                        <tr>
                                                            <td class="column">Set No</td>
                                                            <td>#<t t-esc="counter"/>/<t t-esc="o.master_quantity"/></td>
                                                        </tr>
                                                        <tr>
                                                            <td class="column">Battery</td>
                                                            <td></td>
                                                        </tr>
                                                        <tr>
                                                            <td class="column">No</td>
                                                            <td><t t-esc="category"/>.<t t-esc="index"/></td>
                                                        </tr>
                                                        <tr>
                                                            <td class="column">Name</td>
                                                            <td><t t-esc="item.component_id.name or 'N/A'"/></td>
                                                        </tr>
                                                        <tr>
                                                            <td class="column">UOM</td>
                                                            <td><t t-esc="item.uom or 'N/A'"/></td>
                                                        </tr>
                                                        <tr>
                                                            <td class="column">Qty</td>
                                                            <td><t t-esc="item.singet_set_qty or 'N/A'"/></td>
                                                        </tr>
                                                        <tr>
                                                            <td class="column">S.No</td>
                                                            <td></td>
                                                        </tr>
                                                    </tbody>
                                                </table>
                                            </div>
                                        </div>

                                        <!-- Second table in the pair (if exists) -->
                                        <div class="col-6">
                                            <t t-if="pair_index + 1 &lt; len(all_tables)">
                                                <t t-set="table_info" t-value="all_tables[pair_index + 1]"/>
                                                <t t-set="category" t-value="table_info[0]"/>
                                                <t t-set="item" t-value="table_info[1]"/>
                                                <t t-set="index" t-value="table_info[2]"/>
                                                <div class="table-container">
                                                    <table class="label-table">
                                                        <tbody>
                                                            <tr>
                                                                <td class="column">Set No</td>
                                                                <td>#<t t-esc="counter"/>/<t t-esc="o.master_quantity"/></td>
                                                            </tr>
                                                            <tr>
                                                                <td class="column">Battery</td>
                                                                <td></td>
                                                            </tr>
                                                            <tr>
                                                                <td class="column">No</td>
                                                                <td><t t-esc="category"/>.<t t-esc="index"/></td>
                                                            </tr>
                                                            <tr>
                                                                <td class="column">Name</td>
                                                                <td><t t-esc="item.component_id.name or 'N/A'"/></td>
                                                            </tr>
                                                            <tr>
                                                                <td class="column">UOM</td>
                                                                <td><t t-esc="item.uom or 'N/A'"/></td>
                                                            </tr>
                                                            <tr>
                                                                <td class="column">Qty</td>
                                                                <td><t t-esc="item.singet_set_qty or 'N/A'"/></td>
                                                            </tr>
                                                            <tr>
                                                                <td class="column">S.No</td>
                                                                <td></td>
                                                            </tr>
                                                        </tbody>
                                                    </table>
                                                </div>
                                            </t>
                                            <t t-else="">
                                                <div class="empty-col"></div>
                                            </t>
                                        </div>
                                    </div> <!-- Close row -->
                                </t>
                            </div>
                        </t>
                    </div>
                </t>
            </t>
        </t>
    </template>
</odoo>