<?xml version="1.0" encoding="utf-8"?>
<odoo>

    <template id="report_ltc_nursing_station_document">
        <t t-call="web.html_container">
            <t t-foreach="docs" t-as="facility">
                <t t-call="web.external_layout">
                    <div class="page" style="font-size: 11px;">
                        <h3 class="text-center mb-3">
                            Equipment Repair Log - <t t-esc="facility.name"/>
                        </h3>
                        <p class="text-center text-muted mb-4">
                            Generated: <t t-esc="context_timestamp(datetime.datetime.now()).strftime('%B %d, %Y')"/>
                        </p>

                        <table class="table table-bordered" style="border: 1px solid #000;">
                            <thead>
                                <tr style="background-color: #f0f0f0; height: 20px;">
                                    <th style="border: 1px solid #000; width: 5%; text-align: center;">S/N</th>
                                    <th style="border: 1px solid #000; width: 18%;">Client Name</th>
                                    <th style="border: 1px solid #000; width: 8%; text-align: center;">Room #</th>
                                    <th style="border: 1px solid #000; width: 27%;">Issue Description</th>
                                    <th style="border: 1px solid #000; width: 12%; text-align: center;">Reported Date</th>
                                    <th style="border: 1px solid #000; width: 20%;">Resolution</th>
                                    <th style="border: 1px solid #000; width: 10%; text-align: center;">Fixed Date</th>
                                </tr>
                            </thead>
                            <tbody>
                                <t t-set="counter" t-value="0"/>
                                <t t-foreach="facility.repair_ids.sorted(key=lambda r: r.issue_reported_date or '', reverse=True)"
                                   t-as="repair">
                                    <t t-set="counter" t-value="counter + 1"/>
                                    <tr style="height: 20px;">
                                        <td style="border: 1px solid #000; text-align: center;">
                                            <t t-esc="counter"/>
                                        </td>
                                        <td style="border: 1px solid #000;">
                                            <t t-esc="repair.display_client_name"/>
                                        </td>
                                        <td style="border: 1px solid #000; text-align: center;">
                                            <t t-esc="repair.room_number"/>
                                        </td>
                                        <td style="border: 1px solid #000; font-size: 10px;">
                                            <t t-esc="repair.issue_description"/>
                                        </td>
                                        <td style="border: 1px solid #000; text-align: center;">
                                            <t t-if="repair.issue_reported_date">
                                                <t t-esc="repair.issue_reported_date" t-options='{"widget": "date"}'/>
                                            </t>
                                        </td>
                                        <td style="border: 1px solid #000; font-size: 10px;">
                                            <t t-esc="repair.resolution_description or ''"/>
                                        </td>
                                        <td style="border: 1px solid #000; text-align: center;">
                                            <t t-if="repair.issue_fixed_date">
                                                <t t-esc="repair.issue_fixed_date" t-options='{"widget": "date"}'/>
                                            </t>
                                        </td>
                                    </tr>
                                </t>
                            </tbody>
                        </table>
                    </div>
                </t>
            </t>
        </t>
    </template>

    <template id="report_ltc_repairs_summary_document">
        <t t-call="web.html_container">
            <t t-foreach="docs" t-as="facility">
                <t t-call="web.external_layout">
                    <div class="page">
                        <h3 class="text-center mb-3">
                            Repair Summary - <t t-esc="facility.name"/>
                        </h3>
                        <p class="text-center text-muted mb-4">
                            Total Repairs: <t t-esc="len(facility.repair_ids)"/>
                        </p>

                        <h5>Repairs by Stage</h5>
                        <table class="table table-bordered">
                            <thead>
                                <tr>
                                    <th>Stage</th>
                                    <th class="text-end">Count</th>
                                </tr>
                            </thead>
                            <tbody>
                                <t t-set="stages" t-value="{}"/>
                                <t t-foreach="facility.repair_ids" t-as="r">
                                    <t t-set="sname" t-value="r.stage_id.name or 'No Stage'"/>
                                    <t t-if="sname not in stages">
                                        <t t-set="_" t-value="stages.__setitem__(sname, 0)"/>
                                    </t>
                                    <t t-set="_" t-value="stages.__setitem__(sname, stages[sname] + 1)"/>
                                </t>
                                <t t-foreach="stages" t-as="stage_name">
                                    <tr>
                                        <td><t t-esc="stage_name"/></td>
                                        <td class="text-end"><t t-esc="stages[stage_name]"/></td>
                                    </tr>
                                </t>
                            </tbody>
                        </table>

                        <h5 class="mt-4">Recent Repairs</h5>
                        <table class="table table-bordered">
                            <thead>
                                <tr>
                                    <th>Reference</th>
                                    <th>Client</th>
                                    <th>Room</th>
                                    <th>Reported</th>
                                    <th>Fixed</th>
                                    <th>Stage</th>
                                </tr>
                            </thead>
                            <tbody>
                                <t t-foreach="facility.repair_ids.sorted(key=lambda r: r.issue_reported_date or '', reverse=True)[:50]"
                                   t-as="repair">
                                    <tr>
                                        <td><t t-esc="repair.name"/></td>
                                        <td><t t-esc="repair.display_client_name"/></td>
                                        <td><t t-esc="repair.room_number"/></td>
                                        <td><t t-esc="repair.issue_reported_date" t-options='{"widget": "date"}'/></td>
                                        <td><t t-esc="repair.issue_fixed_date" t-options='{"widget": "date"}'/></td>
                                        <td><t t-esc="repair.stage_id.name"/></td>
                                    </tr>
                                </t>
                            </tbody>
                        </table>
                    </div>
                </t>
            </t>
        </t>
    </template>

</odoo>
