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

    <!-- ================================================================== -->
    <!-- FORM SUBMISSION - LIST VIEW                                         -->
    <!-- ================================================================== -->
    <record id="view_ltc_form_submission_list" model="ir.ui.view">
        <field name="name">fusion.ltc.form.submission.list</field>
        <field name="model">fusion.ltc.form.submission</field>
        <field name="arch" type="xml">
            <list string="Form Submissions" default_order="submitted_date desc"
                  decoration-danger="is_emergency"
                  decoration-info="status == 'submitted'"
                  decoration-success="status == 'processed'">
                <field name="name"/>
                <field name="form_type"/>
                <field name="facility_id"/>
                <field name="client_name"/>
                <field name="room_number"/>
                <field name="product_serial" optional="show"/>
                <field name="is_emergency"/>
                <field name="submitted_date"/>
                <field name="ip_address" optional="hide"/>
                <field name="repair_id" optional="show"/>
                <field name="status" widget="badge"
                       decoration-info="status == 'submitted'"
                       decoration-success="status == 'processed'"
                       decoration-danger="status == 'rejected'"/>
            </list>
        </field>
    </record>

    <!-- ================================================================== -->
    <!-- FORM SUBMISSION - FORM VIEW                                         -->
    <!-- ================================================================== -->
    <record id="view_ltc_form_submission_form" model="ir.ui.view">
        <field name="name">fusion.ltc.form.submission.form</field>
        <field name="model">fusion.ltc.form.submission</field>
        <field name="arch" type="xml">
            <form string="Form Submission">
                <sheet>
                    <div class="oe_button_box" name="button_box">
                        <button name="action_view_repair" type="object"
                                class="oe_stat_button" icon="fa-wrench"
                                invisible="not repair_id">
                            <div class="o_field_widget o_stat_info">
                                <span class="o_stat_text">Repair</span>
                            </div>
                        </button>
                    </div>
                    <div class="oe_title">
                        <label for="name"/>
                        <h1>
                            <field name="name" readonly="1"/>
                        </h1>
                    </div>
                    <group>
                        <group>
                            <field name="form_type"/>
                            <field name="facility_id"/>
                            <field name="client_name"/>
                            <field name="room_number"/>
                            <field name="product_serial"/>
                            <field name="is_emergency"/>
                        </group>
                        <group>
                            <field name="status"/>
                            <field name="submitted_date"/>
                            <field name="ip_address"/>
                            <field name="repair_id"/>
                        </group>
                    </group>
                </sheet>
            </form>
        </field>
    </record>

    <!-- ================================================================== -->
    <!-- FORM SUBMISSION - SEARCH VIEW                                       -->
    <!-- ================================================================== -->
    <record id="view_ltc_form_submission_search" model="ir.ui.view">
        <field name="name">fusion.ltc.form.submission.search</field>
        <field name="model">fusion.ltc.form.submission</field>
        <field name="arch" type="xml">
            <search string="Search Submissions">
                <field name="name"/>
                <field name="client_name"/>
                <field name="facility_id"/>
                <field name="room_number"/>
                <separator/>
                <filter string="Emergency" name="filter_emergency"
                        domain="[('is_emergency', '=', True)]"/>
                <separator/>
                <filter string="Submitted" name="filter_submitted"
                        domain="[('status', '=', 'submitted')]"/>
                <filter string="Processed" name="filter_processed"
                        domain="[('status', '=', 'processed')]"/>
                <filter string="Rejected" name="filter_rejected"
                        domain="[('status', '=', 'rejected')]"/>
                <separator/>
                <filter string="Today" name="filter_today"
                        domain="[('submitted_date', '>=', (context_today()).strftime('%Y-%m-%d'))]"/>
                <filter string="This Week" name="filter_week"
                        domain="[('submitted_date', '>=', (context_today() - datetime.timedelta(days=7)).strftime('%Y-%m-%d'))]"/>
                <filter string="This Month" name="filter_month"
                        domain="[('submitted_date', '>=', (context_today()).strftime('%Y-%m-01'))]"/>
                <separator/>
                <filter string="Facility" name="group_facility"
                        context="{'group_by': 'facility_id'}"/>
                <filter string="Form Type" name="group_type"
                        context="{'group_by': 'form_type'}"/>
                <filter string="Status" name="group_status"
                        context="{'group_by': 'status'}"/>
                <filter string="Submitted Date" name="group_date"
                        context="{'group_by': 'submitted_date:day'}"/>
            </search>
        </field>
    </record>

    <!-- ================================================================== -->
    <!-- ACTIONS                                                             -->
    <!-- ================================================================== -->
    <record id="action_ltc_form_submissions" model="ir.actions.act_window">
        <field name="name">Form Submissions</field>
        <field name="res_model">fusion.ltc.form.submission</field>
        <field name="view_mode">list,form</field>
        <field name="search_view_id" ref="view_ltc_form_submission_search"/>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                No form submissions yet
            </p>
            <p>Submissions from portal repair forms will appear here.</p>
        </field>
    </record>

</odoo>
