<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-set="fg_counter" t-value="0"/>
            <t t-foreach="o.line_ids_fg or []" t-as="fg_item">
            <t t-set="fg_counter" t-value="fg_counter + 1"/>
            <t t-set="all_tables" t-value="all_tables + [(1, fg_item, fg_counter)]"/>
            </t>
            <!-- Add SFG tables -->
            <t t-set="sfg_counter" t-value="0"/>
            <t t-foreach="o.line_ids_sfg or []" t-as="sfg_item">
            <t t-set="sfg_counter" t-value="sfg_counter + 1"/>
            <t t-set="all_tables" t-value="all_tables + [(2, sfg_item, sfg_counter)]"/>
            </t>
            <!-- Add Materials tables -->
            <t t-set="material_counter" t-value="0"/>
            <t t-foreach="o.line_ids_material or []" t-as="material_item">
            <t t-set="material_counter" t-value="material_counter + 1"/>
            <t t-set="all_tables" t-value="all_tables + [(3, material_item, material_counter)]"/>
            </t>
            <!-- Add Installation Kit tables -->
            <t t-set="installation_kit_counter" t-value="0"/>
            <t t-foreach="o.line_ids_installation_kit or []" t-as="installation_kit_item">
            <t t-set="installation_kit_counter" t-value="installation_kit_counter + 1"/>
            <t t-set="all_tables" t-value="all_tables + [(4, installation_kit_item, installation_kit_counter)]"/>
            </t>
            <!-- Add Miscelleneous tables -->
            <t t-set="miscellaneous_counter" t-value="0"/>
            <t t-foreach="o.line_ids_miscellaneous or []" t-as="miscellaneous_item">
            <t t-set="miscellaneous_counter" t-value="miscellaneous_counter + 1"/>
            <t t-set="all_tables" t-value="all_tables + [(5, miscellaneous_item, miscellaneous_counter)]"/>
            </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><t t-esc="o.batter_or_cells"/></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-if="'component_id' in item">
                                            <t t-esc="item.component_id.name or item.component_id.part_no or item.name or 'N/A'"/>
                                        </t>
                                        <t t-else="">
                                            <t t-esc="item.name or 'N/A'"/>
                                        </t>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="column">UOM</td>
                                    <td>
                                        <t t-if="'uom' in item">
                                            <t t-esc="item.uom or 'N/A'"/>
                                        </t>
                                        <t t-else="">
                                            <t t-esc="'N/A'"/>
                                        </t>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="column">Qty</td>
                                    <td>
                                        <t t-if="'singet_set_qty' in item">
                                            <t t-esc="item.singet_set_qty or 'N/A'"/>
                                        </t>
                                        <t t-else="">
                                            <t t-esc="item.quantity or 'N/A'"/>
                                        </t>
                                    </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><t t-esc="o.batter_or_cells"/></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-if="'component_id' in item">
                                                        <t t-esc="item.component_id.name or item.component_id.part_no or item.name or 'N/A'"/>
                                                    </t>
                                                    <t t-else="">
                                                        <t t-esc="item.name or 'N/A'"/>
                                                    </t>
                                                </td>
                                            </tr>
                                        <tr>
                                    <td class="column">UOM</td>
                                    <td>
                                        <t t-if="'uom' in item">
                                            <t t-esc="item.uom or 'N/A'"/>
                                        </t>
                                        <t t-else="">
                                            <t t-esc="'N/A'"/>
                                        </t>
                                    </td>
                                </tr>
                                        <tr>
                                    <td class="column">Qty</td>
                                    <td>
                                        <t t-if="'singet_set_qty' in item">
                                            <t t-esc="item.singet_set_qty or 'N/A'"/>
                                        </t>
                                        <t t-else="">
                                            <t t-esc="item.quantity or 'N/A'"/>
                                        </t>
                                    </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>