<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>
        <record id="view_hr_overtime_request_form" model="ir.ui.view">
            <field name="name">hr.overtime.request.form</field>
            <field name="model">hr.overtime.request</field>
            <field name="arch" type="xml">
                <form js_class="xeno_overtime_request_form">
                    <header>
                        <button name="action_submit" type="object" string="Submit"
                                class="btn-primary" invisible="state != 'draft' or not can_submit_request"/>
                        <button name="action_manager_approve" type="object" string="Approve (Manager)"
                                class="btn-primary" invisible="state != 'confirm' or not can_manager_decide"/>
                        <button name="action_manager_reject" type="object" string="Reject (Manager)"
                                class="btn-secondary" invisible="state != 'confirm' or not can_manager_decide"
                                confirm="Reject this OT request?"/>
                        <button name="action_hod_approve" type="object" string="Approve (HOD)"
                                class="btn-primary" invisible="state != 'manager_approved' or not can_hod_decide"/>
                        <button name="action_hod_reject" type="object" string="Reject (HOD)"
                                class="btn-secondary" invisible="state != 'manager_approved' or not can_hod_decide"
                                confirm="Reject this OT request?"/>
                        <field name="state" widget="statusbar"
                               statusbar_visible="draft,confirm,manager_approved,hod_approved"/>
                    </header>
                    <sheet>
                        <div class="alert alert-warning" role="alert" invisible="state != 'rejected'">
                            <span invisible="not rejection_reason">Reason: <field name="rejection_reason" readonly="1" nolabel="1"/></span>
                            <span invisible="rejection_reason">This request was rejected.</span>
                        </div>
                        <div class="alert alert-info" role="alert" invisible="state != 'draft'">
                            This request is still a Draft -- freely editable. Click Submit when it's
                            ready to go to your Manager for approval.
                        </div>
                        <div class="oe_title">
                            <h1><field name="name" readonly="1"/></h1>
                        </div>
                        <group>
                            <group string="Request">
                                <field name="employee_id" options="{'no_create': True}"/>
                                <field name="date"/>
                                <field name="time_from" widget="float_time" options="{'type': 'time'}"/>
                                <field name="time_to" widget="float_time" options="{'type': 'time'}"/>
                                <field name="working_place"/>
                            </group>
                            <group string="Approvers">
                                <field name="supervisor_id" options="{'no_create': True}"/>
                                <field name="head_of_department_id" options="{'no_create': True}"/>
                                <field name="is_weekend_or_holiday" invisible="1"/>
                            </group>
                        </group>
                        <group string="Description">
                            <field name="description" nolabel="1" colspan="2"/>
                        </group>
                        <group string="Work File">
                            <field name="attachment" filename="attachment_filename"/>
                            <field name="attachment_filename" invisible="1"/>
                        </group>
                        <group string="Timesheet" invisible="state != 'hod_approved'">
                            <group>
                                <field name="time_in" widget="float_time" options="{'type': 'time'}" readonly="not can_edit_timesheet_line"/>
                                <field name="time_out" widget="float_time" options="{'type': 'time'}" readonly="not can_edit_timesheet_line"/>
                                <field name="break_hours" widget="float_time" readonly="not can_edit_timesheet_line"/>
                            </group>
                            <group>
                                <field name="hrs_worked" widget="float_time"/>
                                <field name="computed_ot_hours" widget="float_time"/>
                                <field name="timesheet_id"/>
                                <field name="timesheet_state"/>
                            </group>
                        </group>
                    </sheet>
                    <chatter/>
                </form>
            </field>
        </record>

        <!-- Employee self-service variant of the same form: Employee is never
             shown/selectable (an employee can only ever create a request for
             themselves, enforced server-side in hr_overtime_request.py's
             create()/write(); this just matches the UI to that reality,
             mirroring how the native My Leaves form has no Employee field
             either). The field tag stays in the arch (just marked invisible),
             which keeps the _onchange_employee_id_xeno_fill_approvers onchange
             firing against the record's own default value, so Supervisor/HOD
             still auto-fill with no interaction needed, verified live via
             Odoo's Form() test helper. -->
        <record id="view_hr_overtime_request_form_my" model="ir.ui.view">
            <field name="name">hr.overtime.request.form.my</field>
            <field name="model">hr.overtime.request</field>
            <field name="inherit_id" ref="view_hr_overtime_request_form"/>
            <field name="arch" type="xml">
                <field name="employee_id" position="attributes">
                    <attribute name="invisible">1</attribute>
                </field>
            </field>
        </record>

        <record id="view_hr_overtime_request_list" model="ir.ui.view">
            <field name="name">hr.overtime.request.list</field>
            <field name="model">hr.overtime.request</field>
            <field name="arch" type="xml">
                <list decoration-danger="state == 'rejected'" decoration-success="state == 'hod_approved'"
                      decoration-muted="state == 'draft'">
                    <field name="date"/>
                    <field name="employee_id"/>
                    <field name="time_from" widget="float_time"/>
                    <field name="time_to" widget="float_time"/>
                    <field name="supervisor_id" optional="show"/>
                    <field name="head_of_department_id" optional="show"/>
                    <field name="computed_ot_hours" widget="float_time" optional="show"/>
                    <field name="timesheet_state" optional="show"/>
                    <field name="state" widget="badge"
                           decoration-muted="state == 'draft'"
                           decoration-info="state in ('confirm', 'manager_approved')"
                           decoration-success="state == 'hod_approved'"
                           decoration-danger="state == 'rejected'"/>
                </list>
            </field>
        </record>

        <record id="view_hr_overtime_request_search" model="ir.ui.view">
            <field name="name">hr.overtime.request.search</field>
            <field name="model">hr.overtime.request</field>
            <field name="arch" type="xml">
                <search>
                    <field name="employee_id"/>
                    <field name="supervisor_id"/>
                    <field name="head_of_department_id"/>
                    <filter name="draft" string="Draft" domain="[('state', '=', 'draft')]"/>
                    <filter name="pending_manager" string="Pending Manager" domain="[('state', '=', 'confirm')]"/>
                    <filter name="pending_hod" string="Pending HOD" domain="[('state', '=', 'manager_approved')]"/>
                    <filter name="approved" string="Approved" domain="[('state', '=', 'hod_approved')]"/>
                    <filter name="not_on_timesheet" string="Not Yet on a Timesheet"
                            domain="[('state', '=', 'hod_approved'), ('timesheet_id', '=', False)]"/>
                    <filter name="rejected" string="Rejected" domain="[('state', '=', 'rejected')]"/>
                    <group>
                        <filter name="group_by_employee" string="Employee" context="{'group_by': 'employee_id'}"/>
                        <filter name="group_by_state" string="Status" context="{'group_by': 'state'}"/>
                    </group>
                </search>
            </field>
        </record>

        <record id="action_hr_overtime_my" model="ir.actions.act_window">
            <field name="name">My Overtime</field>
            <field name="res_model">hr.overtime.request</field>
            <field name="view_mode">list,form</field>
            <field name="domain">[('employee_id.user_id', '=', uid)]</field>
            <!-- Explicitly {} rather than just omitting the field: an earlier
                 version of this action set {'default_employee_id': False} and
                 XML data loading only updates fields present in the record,
                 so removing the <field> tag alone does NOT reset a previously
                 applied value, confirmed live (the stale context persisted
                 through an upgrade and silently defeated default_get()). -->
            <field name="context">{}</field>
            <field name="search_view_id" ref="view_hr_overtime_request_search"/>
            <field name="help" type="html">
                <p class="o_view_nocontent_smiling_face">No OT requests yet.</p>
                <p>Create a new Overtime Request -- it starts as a Draft you can freely edit. Click
                   Submit when it's ready; your Manager and then your Head of Department approve it in
                   order. Once Approved (HOD), it's ready to appear on your weekly Overtime Timesheet
                   for that week.</p>
            </field>
        </record>

        <record id="action_hr_overtime_my_view_form" model="ir.actions.act_window.view">
            <field name="sequence">1</field>
            <field name="view_mode">form</field>
            <field name="view_id" ref="view_hr_overtime_request_form_my"/>
            <field name="act_window_id" ref="action_hr_overtime_my"/>
        </record>

        <record id="action_hr_overtime_approvals" model="ir.actions.act_window">
            <field name="name">OT Approvals</field>
            <field name="res_model">hr.overtime.request</field>
            <field name="view_mode">list,form</field>
            <field name="domain">['&amp;', ('state', '!=', 'draft'), '|', ('supervisor_id.user_id', '=', uid), ('head_of_department_id.user_id', '=', uid)]</field>
            <field name="search_view_id" ref="view_hr_overtime_request_search"/>
            <field name="context">{'search_default_pending_manager': 1}</field>
        </record>

        <!-- HR-facing only: pulls every hod_approved request company-wide
             with no per-approver domain (unlike OT Approvals above), so a
             regular employee landing here via the menu would see a
             misleadingly partial "summary" limited to just their own
             record-rule-visible rows. Gated on the menuitem below. -->
        <record id="action_hr_overtime_summary_report" model="ir.actions.client">
            <field name="name">OT Summary Report</field>
            <field name="tag">xeno_overtime_summary_report</field>
        </record>
    </data>
    <data noupdate="1">
        <menuitem id="menu_xeno_overtime_group"
                  name="Overtime"
                  parent="xeno_leave.menu_xeno_hr_attendance"
                  sequence="3"/>

        <menuitem id="menu_xeno_overtime_my"
                  name="OT Request"
                  parent="menu_xeno_overtime_group"
                  action="action_hr_overtime_my"
                  sequence="1"/>

        <menuitem id="menu_xeno_overtime_approvals"
                  name="OT Approvals"
                  parent="menu_xeno_overtime_group"
                  action="action_hr_overtime_approvals"
                  sequence="2"/>

        <menuitem id="menu_xeno_overtime_summary_report"
                  name="OT Summary Report"
                  parent="menu_xeno_overtime_group"
                  action="action_hr_overtime_summary_report"
                  groups="hr_holidays.group_hr_holidays_user"
                  sequence="3"/>
    </data>
</odoo>
