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

    <!-- ================================================================== -->
    <!-- REPAIR - FORM VIEW                                                  -->
    <!-- ================================================================== -->
    <record id="view_ltc_repair_form" model="ir.ui.view">
        <field name="name">fusion.ltc.repair.form</field>
        <field name="model">fusion.ltc.repair</field>
        <field name="arch" type="xml">
            <form string="Repair Request">
                <header>
                    <button name="action_create_sale_order" type="object"
                            string="Create Sale Order" class="btn-primary"
                            invisible="sale_order_id"/>
                    <field name="stage_id" widget="statusbar"
                           options="{'clickable': '1'}"/>
                </header>
                <sheet>
                    <div class="oe_button_box" name="button_box">
                        <button name="action_view_sale_order" type="object"
                                class="oe_stat_button" icon="fa-shopping-cart"
                                invisible="not sale_order_id">
                            <div class="o_field_widget o_stat_info">
                                <span class="o_stat_value">
                                    <field name="sale_order_name"/>
                                </span>
                                <span class="o_stat_text">Sale Order</span>
                            </div>
                        </button>
                        <button name="action_view_task" type="object"
                                class="oe_stat_button" icon="fa-tasks"
                                invisible="not task_id">
                            <div class="o_field_widget o_stat_info">
                                <span class="o_stat_text">Task</span>
                            </div>
                        </button>
                    </div>
                    <widget name="web_ribbon" text="Emergency" bg_color="text-bg-danger"
                            invisible="not is_emergency"/>
                    <div class="oe_title">
                        <label for="name"/>
                        <h1>
                            <field name="name" readonly="1"/>
                        </h1>
                    </div>

                    <group>
                        <group string="Repair Details">
                            <field name="facility_id"/>
                            <field name="client_id"/>
                            <field name="client_name" invisible="client_id"/>
                            <field name="room_number"/>
                            <field name="is_emergency"/>
                            <field name="priority" widget="priority"/>
                        </group>
                        <group string="Product &amp; Assignment">
                            <field name="product_serial"/>
                            <field name="product_id"/>
                            <field name="assigned_technician_id"/>
                            <field name="source"/>
                        </group>
                    </group>

                    <notebook>
                        <page string="Issue" name="issue">
                            <group>
                                <group>
                                    <field name="issue_reported_date"/>
                                </group>
                                <group>
                                    <field name="issue_fixed_date"/>
                                </group>
                            </group>
                            <label for="issue_description"/>
                            <field name="issue_description"
                                   placeholder="Describe the issue in detail..."/>
                            <label for="resolution_description"/>
                            <field name="resolution_description"
                                   placeholder="How was the issue resolved?"/>
                        </page>

                        <page string="Family/POA" name="poa">
                            <group>
                                <group>
                                    <field name="poa_name"/>
                                    <field name="poa_phone" widget="phone"/>
                                </group>
                            </group>
                        </page>

                        <page string="Financial" name="financial">
                            <group>
                                <group>
                                    <field name="sale_order_id"/>
                                    <field name="task_id"/>
                                </group>
                                <group>
                                    <field name="currency_id" invisible="1"/>
                                    <field name="company_id" invisible="1"/>
                                    <field name="repair_value"/>
                                </group>
                            </group>
                        </page>

                        <page string="Photos &amp; Notes" name="photos">
                            <div class="fc-gallery-content">
                                <separator string="Before Photos (Reported Condition)"/>
                                <field name="before_photo_ids" widget="many2many_binary"/>
                                <separator string="After Photos (Completed Repair)"/>
                                <field name="after_photo_ids" widget="many2many_binary"/>
                            </div>
                            <field name="photo_ids" invisible="1"/>
                            <separator string="Internal Notes"/>
                            <field name="notes" placeholder="Internal notes..."/>
                        </page>
                    </notebook>
                </sheet>
                <chatter/>
            </form>
        </field>
    </record>

    <!-- ================================================================== -->
    <!-- REPAIR - KANBAN VIEW                                                -->
    <!-- ================================================================== -->
    <record id="view_ltc_repair_kanban" model="ir.ui.view">
        <field name="name">fusion.ltc.repair.kanban</field>
        <field name="model">fusion.ltc.repair</field>
        <field name="arch" type="xml">
            <kanban default_group_by="stage_id"
                    default_order="is_emergency desc, issue_reported_date desc"
                    highlight_color="color"
                    class="o_kanban_small_column">
                <field name="color"/>
                <field name="is_emergency"/>
                <field name="stage_id"/>
                <field name="kanban_state"/>
                <field name="stage_color"/>
                <progressbar field="kanban_state"
                             colors='{"normal": "200", "done": "success", "blocked": "danger"}'/>
                <templates>
                    <t t-name="menu">
                        <a t-if="widget.editable" role="menuitem" type="open"
                           class="dropdown-item">Edit</a>
                        <a t-if="widget.deletable" role="menuitem" type="delete"
                           class="dropdown-item">Delete</a>
                        <field widget="kanban_color_picker" name="color"/>
                    </t>
                    <t t-name="card" class="flex-row">
                        <main t-att-data-stage="record.stage_color.raw_value || 'secondary'"
                              t-att-data-emergency="record.is_emergency.raw_value ? '1' : '0'"
                              t-att-data-priority="record.priority.raw_value">
                            <div class="d-flex justify-content-between align-items-center">
                                <field name="name" class="fw-bold text-primary"/>
                                <span t-attf-style="#{record.priority.raw_value == '1' ? 'background: rgba(255,152,0,0.1); border-radius: 4px; padding: 1px 5px;' : ''}">
                                    <field name="priority" widget="priority"/>
                                </span>
                            </div>
                            <div class="mt-1">
                                <span class="fw-bold">
                                    <field name="display_client_name"/>
                                </span>
                                <span t-if="record.room_number.raw_value"
                                      class="ms-1 badge text-bg-light">
                                    Rm <field name="room_number"/>
                                </span>
                            </div>
                            <div class="text-muted small">
                                <field name="facility_id"/>
                            </div>
                            <div t-if="record.is_emergency.raw_value"
                                 class="mt-1">
                                <span class="badge text-bg-danger">
                                    <i class="fa fa-exclamation-triangle me-1"/>Emergency
                                </span>
                            </div>
                            <div t-if="record.product_serial.raw_value"
                                 class="text-muted small mt-1">
                                <i class="fa fa-barcode me-1"/>S/N: <field name="product_serial"/>
                            </div>
                            <footer class="mt-2 pt-1 border-top">
                                <span class="text-muted small">
                                    <i class="fa fa-calendar me-1"/><field name="issue_reported_date"/>
                                </span>
                                <div class="ms-auto d-flex align-items-center">
                                    <field name="kanban_state" widget="state_selection"/>
                                    <field name="assigned_technician_id"
                                           widget="many2one_avatar_user"
                                           class="ms-1"/>
                                </div>
                            </footer>
                        </main>
                    </t>
                </templates>
            </kanban>
        </field>
    </record>

    <!-- ================================================================== -->
    <!-- REPAIR - LIST VIEW                                                  -->
    <!-- ================================================================== -->
    <record id="view_ltc_repair_list" model="ir.ui.view">
        <field name="name">fusion.ltc.repair.list</field>
        <field name="model">fusion.ltc.repair</field>
        <field name="arch" type="xml">
            <list string="Repair Requests" default_order="issue_reported_date desc"
                  decoration-danger="is_emergency">
                <field name="stage_color" column_invisible="1"/>
                <field name="name"/>
                <field name="facility_id"/>
                <field name="display_client_name" string="Client"/>
                <field name="room_number"/>
                <field name="product_serial" optional="show"/>
                <field name="issue_reported_date"/>
                <field name="issue_fixed_date" optional="show"/>
                <field name="stage_id" widget="badge"
                       decoration-info="stage_color == 'info'"
                       decoration-warning="stage_color == 'warning'"
                       decoration-success="stage_color == 'success'"
                       decoration-danger="stage_color == 'danger'"
                       optional="show"/>
                <field name="is_emergency" string="Emergency" optional="show"
                       widget="boolean_toggle"/>
                <field name="assigned_technician_id" widget="many2one_avatar_user"
                       optional="show"/>
                <field name="sale_order_id" optional="hide"/>
                <field name="repair_value" optional="hide"/>
                <field name="source" optional="hide"/>
            </list>
        </field>
    </record>

    <!-- ================================================================== -->
    <!-- REPAIR - SEARCH VIEW                                                -->
    <!-- ================================================================== -->
    <record id="view_ltc_repair_search" model="ir.ui.view">
        <field name="name">fusion.ltc.repair.search</field>
        <field name="model">fusion.ltc.repair</field>
        <field name="arch" type="xml">
            <search string="Search Repairs">
                <field name="name"/>
                <field name="display_client_name" string="Client"/>
                <field name="room_number"/>
                <field name="product_serial"/>
                <field name="facility_id"/>
                <field name="assigned_technician_id"/>
                <separator/>
                <filter string="Emergency" name="filter_emergency"
                        domain="[('is_emergency', '=', True)]"/>
                <separator/>
                <filter string="New" name="filter_new"
                        domain="[('stage_id.sequence', '=', 1)]"/>
                <filter string="In Progress" name="filter_in_progress"
                        domain="[('stage_id.fold', '=', False), ('stage_id.sequence', '>', 1)]"/>
                <filter string="Completed" name="filter_completed"
                        domain="[('stage_id.fold', '=', True)]"/>
                <separator/>
                <filter string="Has Sale Order" name="filter_has_so"
                        domain="[('sale_order_id', '!=', False)]"/>
                <filter string="No Sale Order" name="filter_no_so"
                        domain="[('sale_order_id', '=', False)]"/>
                <separator/>
                <filter string="This Month" name="filter_this_month"
                        domain="[('issue_reported_date', '>=', (context_today()).strftime('%Y-%m-01'))]"/>
                <filter string="Last 30 Days" name="filter_30d"
                        domain="[('issue_reported_date', '>=', (context_today() - datetime.timedelta(days=30)).strftime('%Y-%m-%d'))]"/>
                <filter string="This Year" name="filter_this_year"
                        domain="[('issue_reported_date', '>=', (context_today()).strftime('%Y-01-01'))]"/>
                <separator/>
                <filter string="Facility" name="group_facility"
                        context="{'group_by': 'facility_id'}"/>
                <filter string="Stage" name="group_stage"
                        context="{'group_by': 'stage_id'}"/>
                <filter string="Technician" name="group_technician"
                        context="{'group_by': 'assigned_technician_id'}"/>
                <filter string="Source" name="group_source"
                        context="{'group_by': 'source'}"/>
                <filter string="Reported Month" name="group_month"
                        context="{'group_by': 'issue_reported_date:month'}"/>
                <filter string="Fixed Month" name="group_fixed_month"
                        context="{'group_by': 'issue_fixed_date:month'}"/>
            </search>
        </field>
    </record>

    <!-- ================================================================== -->
    <!-- REPAIR STAGE - LIST + FORM (Configuration)                          -->
    <!-- ================================================================== -->
    <record id="view_ltc_repair_stage_list" model="ir.ui.view">
        <field name="name">fusion.ltc.repair.stage.list</field>
        <field name="model">fusion.ltc.repair.stage</field>
        <field name="arch" type="xml">
            <list string="Repair Stages" editable="bottom">
                <field name="sequence" widget="handle"/>
                <field name="name"/>
                <field name="color"/>
                <field name="fold"/>
                <field name="description" optional="hide"/>
            </list>
        </field>
    </record>

    <record id="view_ltc_repair_stage_form" model="ir.ui.view">
        <field name="name">fusion.ltc.repair.stage.form</field>
        <field name="model">fusion.ltc.repair.stage</field>
        <field name="arch" type="xml">
            <form string="Repair Stage">
                <sheet>
                    <group>
                        <field name="name"/>
                        <field name="sequence"/>
                        <field name="color"/>
                        <field name="fold"/>
                        <field name="description"/>
                    </group>
                </sheet>
            </form>
        </field>
    </record>

    <!-- ================================================================== -->
    <!-- ACTIONS                                                             -->
    <!-- ================================================================== -->
    <record id="action_ltc_repairs_kanban" model="ir.actions.act_window">
        <field name="name">Repair Overview</field>
        <field name="res_model">fusion.ltc.repair</field>
        <field name="view_mode">kanban,list,form</field>
        <field name="search_view_id" ref="view_ltc_repair_search"/>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                No repair requests yet
            </p>
            <p>Repair requests submitted via the portal form or manually will appear here.</p>
        </field>
    </record>

    <record id="action_ltc_repairs_all" model="ir.actions.act_window">
        <field name="name">All Repairs</field>
        <field name="res_model">fusion.ltc.repair</field>
        <field name="view_mode">kanban,list,form</field>
        <field name="search_view_id" ref="view_ltc_repair_search"/>
    </record>

    <record id="action_ltc_repairs_new" model="ir.actions.act_window">
        <field name="name">New / Pending</field>
        <field name="res_model">fusion.ltc.repair</field>
        <field name="view_mode">kanban,list,form</field>
        <field name="search_view_id" ref="view_ltc_repair_search"/>
        <field name="domain">[('stage_id.sequence', '&lt;=', 2)]</field>
    </record>

    <record id="action_ltc_repairs_in_progress" model="ir.actions.act_window">
        <field name="name">In Progress</field>
        <field name="res_model">fusion.ltc.repair</field>
        <field name="view_mode">kanban,list,form</field>
        <field name="search_view_id" ref="view_ltc_repair_search"/>
        <field name="domain">[('stage_id.fold', '=', False), ('stage_id.sequence', '>', 2)]</field>
    </record>

    <record id="action_ltc_repairs_completed" model="ir.actions.act_window">
        <field name="name">Completed</field>
        <field name="res_model">fusion.ltc.repair</field>
        <field name="view_mode">kanban,list,form</field>
        <field name="search_view_id" ref="view_ltc_repair_search"/>
        <field name="domain">[('stage_id.fold', '=', True)]</field>
    </record>

    <record id="action_ltc_repair_stages" model="ir.actions.act_window">
        <field name="name">Repair Stages</field>
        <field name="res_model">fusion.ltc.repair.stage</field>
        <field name="view_mode">list,form</field>
    </record>

</odoo>
