<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>
        <record id="view_hr_business_trip_form" model="ir.ui.view">
            <field name="name">hr.business.trip.form</field>
            <field name="model">hr.business.trip</field>
            <field name="arch" type="xml">
                <form>
                    <header>
                        <button name="action_submit" type="object" string="Submit"
                                class="btn-primary" invisible="state != 'draft' or not can_submit_request"/>
                        <button name="action_acknowledge" type="object" string="Acknowledge"
                                class="btn-primary" invisible="state != 'confirm' or not can_hr_decide"/>
                        <button name="action_reject" type="object" string="Reject"
                                class="btn-secondary" invisible="state != 'confirm' or not can_hr_decide"
                                confirm="Reject this Business Trip request?"/>
                        <button name="action_reset_to_draft" type="object" string="Reset to Draft"
                                invisible="state != 'rejected'"/>
                        <field name="state" widget="statusbar" statusbar_visible="draft,confirm,acknowledged"/>
                    </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 HR for acknowledgment.
                        </div>
                        <div class="oe_title">
                            <h1><field name="name" readonly="1"/></h1>
                        </div>
                        <group>
                            <group string="Trip">
                                <field name="employee_id" options="{'no_create': True}"/>
                                <field name="date_from"/>
                                <field name="date_to"/>
                                <field name="number_of_days"/>
                                <field name="trip_type"/>
                                <field name="trip_type_other"
                                       invisible="trip_type != 'others'"
                                       required="trip_type == 'others'"/>
                            </group>
                            <group string="Location">
                                <field name="address"/>
                                <field name="city"/>
                                <field name="country_id" options="{'no_create': True}"/>
                            </group>
                        </group>
                        <group string="Reason">
                            <field name="reason" nolabel="1" colspan="2"/>
                        </group>
                        <group string="Requirements">
                            <field name="requirements_description" nolabel="1" colspan="2"/>
                        </group>
                        <group string="Remark">
                            <field name="remark" nolabel="1" colspan="2"/>
                        </group>
                    </sheet>
                    <chatter/>
                </form>
            </field>
        </record>

        <!-- Employee self-service variant: Employee is never shown/selectable
             (an employee can only ever create a request for themselves,
             enforced server-side in hr_business_trip.py's create()/write()),
             same convention as hr.overtime.request's own "_my" form. -->
        <record id="view_hr_business_trip_form_my" model="ir.ui.view">
            <field name="name">hr.business.trip.form.my</field>
            <field name="model">hr.business.trip</field>
            <field name="inherit_id" ref="view_hr_business_trip_form"/>
            <field name="arch" type="xml">
                <field name="employee_id" position="attributes">
                    <attribute name="invisible">1</attribute>
                </field>
            </field>
        </record>

        <record id="view_hr_business_trip_list" model="ir.ui.view">
            <field name="name">hr.business.trip.list</field>
            <field name="model">hr.business.trip</field>
            <field name="arch" type="xml">
                <list decoration-danger="state == 'rejected'" decoration-success="state == 'acknowledged'"
                      decoration-muted="state == 'draft'">
                    <field name="employee_id"/>
                    <field name="date_from"/>
                    <field name="date_to"/>
                    <field name="number_of_days" optional="show"/>
                    <field name="trip_type"/>
                    <field name="city" optional="show"/>
                    <field name="country_id" optional="show"/>
                    <field name="state" widget="badge"
                           decoration-muted="state == 'draft'"
                           decoration-info="state == 'confirm'"
                           decoration-success="state == 'acknowledged'"
                           decoration-danger="state == 'rejected'"/>
                </list>
            </field>
        </record>

        <record id="view_hr_business_trip_search" model="ir.ui.view">
            <field name="name">hr.business.trip.search</field>
            <field name="model">hr.business.trip</field>
            <field name="arch" type="xml">
                <search>
                    <field name="employee_id"/>
                    <field name="trip_type"/>
                    <field name="city"/>
                    <field name="country_id"/>
                    <filter name="draft" string="Draft" domain="[('state', '=', 'draft')]"/>
                    <filter name="pending" string="Pending HR" domain="[('state', '=', 'confirm')]"/>
                    <filter name="acknowledged" string="Acknowledged" domain="[('state', '=', 'acknowledged')]"/>
                    <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'}"/>
                        <filter name="group_by_type" string="Type" context="{'group_by': 'trip_type'}"/>
                    </group>
                </search>
            </field>
        </record>

        <record id="action_hr_business_trip_my" model="ir.actions.act_window">
            <field name="name">Business Trips</field>
            <field name="res_model">hr.business.trip</field>
            <field name="view_mode">list,form</field>
            <field name="domain">[('employee_id.user_id', '=', uid)]</field>
            <field name="context">{}</field>
            <field name="search_view_id" ref="view_hr_business_trip_search"/>
            <field name="help" type="html">
                <p class="o_view_nocontent_smiling_face">No Business Trip requests yet.</p>
                <p>Create a new Business Trip request -- it starts as a Draft you can freely edit.
                   Click Submit when it's ready for HR's acknowledgment. Once Acknowledged, you're
                   exempt from check-in/out for the trip's dates and it shows as "BT" on the
                   Monthly Attendance report.</p>
            </field>
        </record>

        <record id="action_hr_business_trip_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_business_trip_form_my"/>
            <field name="act_window_id" ref="action_hr_business_trip_my"/>
        </record>

        <record id="action_hr_business_trip_approvals" model="ir.actions.act_window">
            <field name="name">Business Trip Approvals</field>
            <field name="res_model">hr.business.trip</field>
            <field name="view_mode">list,form</field>
            <field name="domain">[('state', '!=', 'draft')]</field>
            <field name="search_view_id" ref="view_hr_business_trip_search"/>
            <field name="context">{'search_default_pending': 1}</field>
        </record>
    </data>
    <data noupdate="1">
        <!-- Both live under "My Attendance" (menu_attendance_my), the same
             standalone top-level tab every regular employee already sees
             for self-service attendance: xeno_attendance can't reference
             xeno_leave's "HR" app menu (xeno_overtime's own placement)
             since xeno_leave depends on xeno_attendance, not the other way
             around. -->
        <menuitem id="menu_hr_business_trip_my"
                  name="Business Trips"
                  parent="menu_attendance_my"
                  action="action_hr_business_trip_my"
                  groups="base.group_user"
                  sequence="3"/>

        <menuitem id="menu_hr_business_trip_approvals"
                  name="Business Trip Approvals"
                  parent="menu_attendance_my"
                  action="action_hr_business_trip_approvals"
                  groups="hr.group_hr_user"
                  sequence="4"/>
    </data>
</odoo>
