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

    <!-- ============================================================ -->
    <!-- ASSESSMENT TREE VIEW                                          -->
    <!-- ============================================================ -->
    <record id="view_wc_assessment_tree" model="ir.ui.view">
        <field name="name">fusion.wc.assessment.tree</field>
        <field name="model">fusion.wc.assessment</field>
        <field name="arch" type="xml">
            <list decoration-info="state == 'draft'"
                  decoration-warning="state == 'review'"
                  decoration-success="state == 'quoted'"
                  decoration-muted="state == 'cancelled'">
                <field name="reference"/>
                <field name="client_name"/>
                <field name="equipment_type"/>
                <field name="wheelchair_type"/>
                <field name="client_type"/>
                <field name="build_type"/>
                <field name="sales_rep_id"/>
                <field name="assessment_date"/>
                <field name="total_estimate" sum="Total"/>
                <field name="state" widget="badge"
                    decoration-info="state == 'draft'"
                    decoration-warning="state == 'review'"
                    decoration-success="state == 'quoted'"
                    decoration-danger="state == 'cancelled'"/>
            </list>
        </field>
    </record>

    <!-- ============================================================ -->
    <!-- ASSESSMENT KANBAN VIEW                                        -->
    <!-- ============================================================ -->
    <record id="view_wc_assessment_kanban" model="ir.ui.view">
        <field name="name">fusion.wc.assessment.kanban</field>
        <field name="model">fusion.wc.assessment</field>
        <field name="arch" type="xml">
            <kanban default_group_by="state" class="o_kanban_small_column">
                <field name="state"/>
                <field name="reference"/>
                <field name="client_name"/>
                <field name="equipment_type"/>
                <field name="sales_rep_id"/>
                <field name="total_estimate"/>
                <templates>
                    <t t-name="card">
                        <div class="oe_kanban_content">
                            <strong><field name="reference"/></strong>
                            <div><field name="client_name"/></div>
                            <div class="text-muted">
                                <field name="equipment_type"/>
                            </div>
                            <div>
                                <field name="sales_rep_id" widget="many2one_avatar_user"/>
                                <span class="float-end fw-bold">
                                    $<field name="total_estimate"/>
                                </span>
                            </div>
                        </div>
                    </t>
                </templates>
            </kanban>
        </field>
    </record>

    <!-- ============================================================ -->
    <!-- ASSESSMENT SEARCH VIEW                                        -->
    <!-- ============================================================ -->
    <record id="view_wc_assessment_search" model="ir.ui.view">
        <field name="name">fusion.wc.assessment.search</field>
        <field name="model">fusion.wc.assessment</field>
        <field name="arch" type="xml">
            <search string="Wheelchair Assessments">
                <field name="reference"/>
                <field name="client_name"/>
                <field name="partner_id"/>
                <field name="sales_rep_id"/>
                <field name="authorizer_id"/>
                <separator/>
                <filter name="draft" string="In Progress"
                    domain="[('state', '=', 'draft')]"/>
                <filter name="review" string="Ready for Review"
                    domain="[('state', '=', 'review')]"/>
                <filter name="quoted" string="Quoted"
                    domain="[('state', '=', 'quoted')]"/>
                <separator/>
                <filter name="manual" string="Manual Wheelchair"
                    domain="[('equipment_type', '=', 'manual_wheelchair')]"/>
                <filter name="power" string="Power Wheelchair / Scooter"
                    domain="[('equipment_type', '=', 'power_wheelchair')]"/>
                <filter name="walker" string="Walker / Ambulation Aid"
                    domain="[('equipment_type', '=', 'walker')]"/>
                <separator/>
                <filter name="my_assessments" string="My Assessments"
                    domain="[('sales_rep_id', '=', uid)]"/>
                <separator/>
                <filter name="group_state" string="Status"
                    context="{'group_by': 'state'}"/>
                <filter name="group_equipment" string="Equipment Type"
                    context="{'group_by': 'equipment_type'}"/>
                <filter name="group_rep" string="Sales Rep"
                    context="{'group_by': 'sales_rep_id'}"/>
                <filter name="group_date" string="Date"
                    context="{'group_by': 'assessment_date:month'}"/>
            </search>
        </field>
    </record>

    <!-- ============================================================ -->
    <!-- ASSESSMENT FORM VIEW                                          -->
    <!-- ============================================================ -->
    <record id="view_wc_assessment_form" model="ir.ui.view">
        <field name="name">fusion.wc.assessment.form</field>
        <field name="model">fusion.wc.assessment</field>
        <field name="arch" type="xml">
            <form string="Wheelchair Assessment">
                <header>
                    <button name="action_generate_quotation" string="Generate Quotation"
                        type="object" class="btn-primary"
                        invisible="state not in ('draft', 'review')"
                        confirm="Generate a quotation from this assessment?"/>
                    <button name="action_mark_review" string="Mark Ready"
                        type="object" class="btn-secondary"
                        invisible="state != 'draft'"/>
                    <button name="action_cancel" string="Cancel"
                        type="object" class="btn-secondary"
                        invisible="state in ('quoted', 'cancelled')"/>
                    <button name="action_reset_draft" string="Reset to Draft"
                        type="object" class="btn-secondary"
                        invisible="state != 'cancelled'"/>
                    <field name="state" widget="statusbar"
                        statusbar_visible="draft,review,quoted"/>
                </header>
                <sheet>
                    <div class="oe_button_box" name="button_box">
                        <button name="action_view_quotation" type="object"
                            class="oe_stat_button" icon="fa-file-text-o"
                            invisible="not sale_order_id">
                            <span class="o_stat_text">Quotation</span>
                        </button>
                        <button name="action_open_portal_form" type="object"
                            class="oe_stat_button" icon="fa-external-link">
                            <span class="o_stat_text">Open Form</span>
                        </button>
                        <button name="action_generate_share_link" type="object"
                            class="oe_stat_button" icon="fa-share-alt">
                            <span class="o_stat_text">Share Link</span>
                        </button>
                    </div>
                    <div class="oe_title">
                        <h1>
                            <field name="reference" readonly="1"/>
                        </h1>
                    </div>

                    <notebook>
                        <!-- TAB 1: CLIENT & EQUIPMENT -->
                        <page string="Client &amp; Equipment" name="client">
                            <group>
                                <group string="Client">
                                    <field name="partner_id"
                                        invisible="create_new_partner"/>
                                    <field name="create_new_partner"/>
                                    <field name="client_first_name"
                                        invisible="not create_new_partner"/>
                                    <field name="client_last_name"
                                        invisible="not create_new_partner"/>
                                    <field name="client_phone"
                                        invisible="not create_new_partner"/>
                                    <field name="client_email"
                                        invisible="not create_new_partner"/>
                                    <field name="client_health_card"/>
                                    <field name="client_dob"/>
                                </group>
                                <group string="Equipment">
                                    <field name="equipment_type"/>
                                    <field name="wheelchair_type"
                                        invisible="equipment_type != 'manual_wheelchair'"/>
                                    <field name="powerchair_type"
                                        invisible="equipment_type != 'power_wheelchair'"/>
                                    <field name="walker_type"
                                        invisible="equipment_type != 'walker'"/>
                                    <field name="client_type"/>
                                    <field name="build_type"
                                        invisible="equipment_type == 'walker'"/>
                                    <field name="reason_for_application"/>
                                </group>
                            </group>
                            <group>
                                <group string="Assessment">
                                    <field name="sales_rep_id"/>
                                    <field name="authorizer_id"/>
                                    <field name="assessment_date"/>
                                </group>
                            </group>
                        </page>

                        <!-- TAB 2: ADP MEASUREMENTS -->
                        <page string="Measurements" name="measurements">

                            <!-- MANUAL WHEELCHAIR MEASUREMENTS (Section 2b) -->
                            <group string="Manual Wheelchair Prescription Details"
                                invisible="equipment_type != 'manual_wheelchair'">
                                <group>
                                    <label for="seat_width"/>
                                    <div class="d-flex gap-2">
                                        <field name="seat_width" class="oe_inline"/>
                                        <field name="seat_width_unit" class="oe_inline"/>
                                    </div>
                                    <label for="seat_depth"/>
                                    <div class="d-flex gap-2">
                                        <field name="seat_depth" class="oe_inline"/>
                                        <field name="seat_depth_unit" class="oe_inline"/>
                                    </div>
                                    <label for="finished_seat_to_floor_height"/>
                                    <div class="d-flex gap-2">
                                        <field name="finished_seat_to_floor_height" class="oe_inline"/>
                                        <field name="seat_to_floor_unit" class="oe_inline"/>
                                    </div>
                                    <label for="back_cane_height"/>
                                    <div class="d-flex gap-2">
                                        <field name="back_cane_height" class="oe_inline"/>
                                        <field name="cane_height_unit" class="oe_inline"/>
                                    </div>
                                </group>
                                <group>
                                    <label for="finished_back_height"/>
                                    <div class="d-flex gap-2">
                                        <field name="finished_back_height" class="oe_inline"/>
                                        <field name="back_height_unit" class="oe_inline"/>
                                    </div>
                                    <label for="finished_leg_rest_length"/>
                                    <div class="d-flex gap-2">
                                        <field name="finished_leg_rest_length" class="oe_inline"/>
                                        <field name="leg_rest_unit" class="oe_inline"/>
                                    </div>
                                    <label for="client_weight"/>
                                    <div class="d-flex gap-2">
                                        <field name="client_weight" class="oe_inline"/>
                                        <field name="client_weight_unit" class="oe_inline"/>
                                    </div>
                                </group>
                            </group>

                            <!-- WALKER / ROLLATOR MEASUREMENTS (Section 2a) -->
                            <group string="Wheeled Walker Prescription Details"
                                invisible="equipment_type != 'walker'">
                                <group>
                                    <label for="walker_seat_height"/>
                                    <div class="d-flex gap-2">
                                        <field name="walker_seat_height" class="oe_inline"/>
                                        <field name="walker_seat_height_unit" class="oe_inline"/>
                                    </div>
                                    <label for="push_handle_height"/>
                                    <div class="d-flex gap-2">
                                        <field name="push_handle_height" class="oe_inline"/>
                                        <field name="push_handle_height_unit" class="oe_inline"/>
                                    </div>
                                    <field name="hand_grips"/>
                                    <field name="forearm_attachments"/>
                                    <label for="width_between_push_handles"/>
                                    <div class="d-flex gap-2">
                                        <field name="width_between_push_handles" class="oe_inline"/>
                                        <field name="push_handle_width_unit" class="oe_inline"/>
                                    </div>
                                </group>
                                <group>
                                    <label for="client_weight"/>
                                    <div class="d-flex gap-2">
                                        <field name="client_weight" class="oe_inline"/>
                                        <field name="client_weight_unit" class="oe_inline"/>
                                    </div>
                                    <field name="walker_brakes"/>
                                    <field name="walker_brake_type"/>
                                    <field name="walker_num_wheels"/>
                                    <field name="walker_wheel_size"/>
                                    <field name="walker_back_support"/>
                                </group>
                            </group>
                            <group string="Walker ADP Funded Options"
                                invisible="equipment_type != 'walker'">
                                <group>
                                    <field name="walker_adolescent_wheeled_walker"/>
                                    <field name="walker_adolescent_walking_frame"/>
                                    <field name="walker_adolescent_standing_frame"/>
                                </group>
                            </group>

                            <!-- POWER BASE / SCOOTER MEASUREMENTS (Section 2c) -->
                            <group string="Power Device Prescription Details"
                                invisible="equipment_type != 'power_wheelchair'">
                                <group>
                                    <label for="seat_width"/>
                                    <div class="d-flex gap-2">
                                        <field name="seat_width" class="oe_inline"/>
                                        <field name="seat_width_unit" class="oe_inline"/>
                                    </div>
                                    <label for="finished_back_height"/>
                                    <div class="d-flex gap-2">
                                        <field name="finished_back_height" class="oe_inline"/>
                                        <field name="back_height_unit" class="oe_inline"/>
                                    </div>
                                    <label for="finished_seat_to_floor_height"/>
                                    <div class="d-flex gap-2">
                                        <field name="finished_seat_to_floor_height" class="oe_inline"/>
                                        <field name="seat_to_floor_unit" class="oe_inline"/>
                                    </div>
                                </group>
                                <group>
                                    <label for="finished_leg_rest_length"/>
                                    <div class="d-flex gap-2">
                                        <field name="finished_leg_rest_length" class="oe_inline"/>
                                        <field name="leg_rest_unit" class="oe_inline"/>
                                    </div>
                                    <label for="seat_depth"/>
                                    <div class="d-flex gap-2">
                                        <field name="seat_depth" class="oe_inline"/>
                                        <field name="seat_depth_unit" class="oe_inline"/>
                                    </div>
                                    <label for="client_weight"/>
                                    <div class="d-flex gap-2">
                                        <field name="client_weight" class="oe_inline"/>
                                        <field name="client_weight_unit" class="oe_inline"/>
                                    </div>
                                </group>
                            </group>
                            <group string="Power Base ADP Funded Options"
                                invisible="equipment_type != 'power_wheelchair'">
                                <group>
                                    <field name="pw_adjustable_tension_back"/>
                                    <field name="pw_midline_control"/>
                                    <field name="pw_manual_recline"/>
                                    <field name="pw_angle_adjustable_footplates"/>
                                    <field name="pw_manual_elevating_legrests"/>
                                    <field name="pw_swingaway_bracket"/>
                                </group>
                                <group>
                                    <field name="pw_front_riggings"/>
                                    <field name="pw_seat_package_1"/>
                                    <field name="pw_seat_package_2"/>
                                    <field name="pw_oxygen_tank"/>
                                    <field name="pw_ventilator_tray"/>
                                </group>
                            </group>
                            <group string="Specialty Components (* Provide Clinical Rationale)"
                                invisible="equipment_type != 'power_wheelchair'">
                                <group>
                                    <field name="pw_specialty_1_joystick"/>
                                    <field name="pw_specialty_2_chin"/>
                                    <field name="pw_specialty_3_touch"/>
                                    <field name="pw_specialty_4_proximity"/>
                                </group>
                                <group>
                                    <field name="pw_specialty_5_breath"/>
                                    <field name="pw_specialty_6_scanners"/>
                                    <field name="pw_auto_correction"/>
                                </group>
                                <group colspan="2">
                                    <field name="pw_specialty_rationale"
                                        placeholder="Clinical rationale for specialty components..."
                                        invisible="not pw_specialty_1_joystick and not pw_specialty_2_chin and not pw_specialty_3_touch and not pw_specialty_4_proximity and not pw_specialty_5_breath and not pw_specialty_6_scanners and not pw_auto_correction"/>
                                </group>
                            </group>
                            <group string="Power Positioning Devices"
                                invisible="equipment_type != 'power_wheelchair'">
                                <group>
                                    <field name="pw_power_tilt_only"/>
                                    <field name="pw_power_recline_only"/>
                                    <field name="pw_power_tilt_recline"/>
                                </group>
                                <group>
                                    <field name="pw_power_elevating_footrests"/>
                                    <field name="pw_multi_function_control"/>
                                    <field name="pw_power_add_on"/>
                                </group>
                            </group>

                        </page>

                        <!-- TAB 3: FRAME -->
                        <page string="Frame" name="frame">
                            <group>
                                <group>
                                    <field name="frame_product_id"/>
                                </group>
                            </group>
                            <field name="frame_notes" placeholder="Frame notes..."/>
                        </page>

                        <!-- TAB 4: SELECTED ITEMS -->
                        <page string="Selected Items" name="lines">
                            <field name="line_ids">
                                <list editable="bottom">
                                    <field name="section_id"/>
                                    <field name="product_id"/>
                                    <field name="product_name"/>
                                    <field name="build_type"/>
                                    <field name="quantity"/>
                                    <field name="adp_device_code"/>
                                    <field name="adp_price"/>
                                    <field name="unit_price"/>
                                    <field name="subtotal" sum="Total"/>
                                    <field name="adp_portion" sum="ADP Total"/>
                                    <field name="client_portion" sum="Client Total"/>
                                    <field name="is_upcharge"/>
                                    <field name="upcharge_reason"
                                        invisible="not is_upcharge"/>
                                </list>
                            </field>
                        </page>

                        <!-- TAB 5: UPCHARGES -->
                        <page string="Upcharges" name="upcharges">
                            <field name="upcharge_line_ids" readonly="1">
                                <list>
                                    <field name="adp_device_code"/>
                                    <field name="product_name"/>
                                    <field name="upcharge_reason"/>
                                    <field name="unit_price"/>
                                    <field name="adp_portion"/>
                                </list>
                            </field>
                        </page>

                        <!-- TAB 6: QUOTATION -->
                        <page string="Quotation" name="quotation">
                            <group>
                                <group>
                                    <field name="sale_order_id"/>
                                    <field name="total_estimate"/>
                                    <field name="total_adp_estimate"/>
                                    <field name="total_client_estimate"/>
                                </group>
                            </group>
                            <field name="notes" placeholder="Additional notes..."/>
                        </page>

                        <!-- TAB 7: SHARING -->
                        <page string="Sharing" name="sharing">
                            <div class="alert alert-info" role="alert"
                                 invisible="access_token">
                                <i class="fa fa-info-circle me-1"/>
                                Click <b>Share Link</b> above to generate a public URL
                                that can be opened without logging in, or embedded in
                                an external website via an iframe.
                            </div>
                            <group invisible="not access_token">
                                <group string="Public Access">
                                    <field name="public_url" widget="CopyClipboardChar"
                                           readonly="1" string="Public URL"/>
                                    <field name="portal_url" widget="CopyClipboardChar"
                                           readonly="1" string="Portal URL (login required)"/>
                                    <field name="access_token" readonly="1"/>
                                </group>
                                <group string="Embed Code">
                                    <div colspan="2">
                                        <p class="text-muted mb-2">
                                            Copy this snippet to embed the form in an external page:
                                        </p>
                                        <code style="display:block;white-space:pre-wrap;background:#f8f9fa;padding:12px;border-radius:6px;font-size:13px;">&lt;iframe src="<field name="public_url" readonly="1"/>/embed"
        width="100%" height="900"
        frameborder="0"
        allow="clipboard-write"&gt;&lt;/iframe&gt;</code>
                                    </div>
                                </group>
                            </group>
                        </page>
                    </notebook>
                </sheet>
                <chatter/>
            </form>
        </field>
    </record>

    <!-- ============================================================ -->
    <!-- ASSESSMENT ACTION                                             -->
    <!-- ============================================================ -->
    <record id="action_wc_assessment" model="ir.actions.act_window">
        <field name="name">Wheelchair Assessments</field>
        <field name="res_model">fusion.wc.assessment</field>
        <field name="view_mode">list,kanban,form</field>
        <field name="context">{'search_default_my_assessments': 1}</field>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                Create a wheelchair assessment
            </p>
            <p>
                Start a new wheelchair assessment to configure a chair
                and automatically generate a quotation.
            </p>
        </field>
    </record>

</odoo>
