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

        <record id="view_mail_sync_message_tree" model="ir.ui.view">
            <field name="name">mail.sync.message.tree</field>
            <field name="model">mail.sync.message</field>
            <field name="arch" type="xml">
                <tree string="Inbox" decoration-muted="state == 'archived'"
                      decoration-info="state == 'new'">
                    <field name="date_received"/>
                    <field name="email_from"/>
                    <field name="subject"/>
                    <field name="is_routed" optional="show"/>
                    <field name="is_routed_lead" optional="show"/>
                    <field name="partner_ids" widget="many2many_tags" optional="show"/>
                    <field name="lead_ids" widget="many2many_tags" optional="show"/>
                    <field name="server_id" optional="hide"/>
                    <field name="state" widget="badge"
                           decoration-info="state == 'new'"
                           decoration-success="state == 'read'"
                           decoration-muted="state == 'archived'"/>
                </tree>
            </field>
        </record>

        <record id="view_mail_sync_message_form" model="ir.ui.view">
            <field name="name">mail.sync.message.form</field>
            <field name="model">mail.sync.message</field>
            <field name="arch" type="xml">
                <form string="Message">
                    <header>
                        <button name="action_route_contacts" type="object"
                                string="Route to Contacts / CRM"
                                groups="jb_mail_inbox.group_mail_inbox_manager"/>
                        <button name="action_mark_read" type="object"
                                string="Mark Read" invisible="state != 'new'"/>
                        <button name="action_mark_new" type="object"
                                string="Mark New" invisible="state == 'new'"/>
                        <button name="action_archive" type="object"
                                string="Archive" invisible="state == 'archived'"/>
                        <field name="state" widget="statusbar"
                               statusbar_visible="new,read,archived"/>
                    </header>
                    <sheet>
                        <group>
                            <group>
                                <field name="email_from"/>
                                <field name="email_to"/>
                                <field name="email_cc"/>
                                <field name="email_bcc" groups="jb_mail_inbox.group_mail_inbox_manager"/>
                                <field name="date_received"/>
                            </group>
                            <group>
                                <field name="server_id" readonly="1"/>
                                <field name="message_id" readonly="1"/>
                                <field name="uid" readonly="1"/>
                                <field name="is_routed" readonly="1"/>
                                <field name="routed_date" readonly="1"/>
                                <field name="is_routed_lead" readonly="1"/>
                                <field name="lead_routed_date" readonly="1"/>
                            </group>
                        </group>
                        <group>
                            <field name="subject" colspan="2"/>
                            <field name="partner_ids" widget="many2many_tags" readonly="1"/>
                            <field name="lead_ids" widget="many2many_tags" readonly="1"/>
                        </group>
                        <notebook>
                            <page string="Message" name="body">
                                <field name="body_html" widget="html" nolabel="1"
                                       invisible="not body_html"/>
                                <field name="body_text" nolabel="1"
                                       invisible="body_html"/>
                            </page>
                            <page string="Attachments" name="attachments"
                                  invisible="not attachment_ids">
                                <field name="attachment_ids" widget="many2many_binary"
                                       nolabel="1" readonly="1"/>
                            </page>
                            <page string="Log Notes" name="log_notes"
                                  invisible="not mail_message_ids"
                                  groups="jb_mail_inbox.group_mail_inbox_manager">
                                <field name="mail_message_ids" nolabel="1" readonly="1"/>
                            </page>
                            <page string="Headers" name="headers"
                                  groups="jb_mail_inbox.group_mail_inbox_manager">
                                <field name="raw_headers" nolabel="1" readonly="1"/>
                            </page>
                        </notebook>
                    </sheet>
                </form>
            </field>
        </record>

        <record id="view_mail_sync_message_search" model="ir.ui.view">
            <field name="name">mail.sync.message.search</field>
            <field name="model">mail.sync.message</field>
            <field name="arch" type="xml">
                <search string="Inbox">
                    <field name="subject"/>
                    <field name="email_from"/>
                    <field name="body_text"/>
                    <field name="partner_ids"/>
                    <field name="lead_ids"/>
                    <field name="server_id"/>
                    <filter string="New" name="filter_new"
                            domain="[('state', '=', 'new')]"/>
                    <filter string="Read" name="filter_read"
                            domain="[('state', '=', 'read')]"/>
                    <filter string="Archived" name="filter_archived"
                            domain="[('state', '=', 'archived')]"/>
                    <separator/>
                    <filter string="Routed to Contact" name="filter_routed"
                            domain="[('is_routed', '=', True)]"/>
                    <filter string="Routed to CRM Lead" name="filter_routed_lead"
                            domain="[('is_routed_lead', '=', True)]"/>
                    <filter string="Not Routed" name="filter_not_routed"
                            domain="[('is_routed', '=', False), ('is_routed_lead', '=', False)]"/>
                    <separator/>
                    <filter string="Today" name="filter_today" date="date_received"/>
                    <group expand="0" string="Group By">
                        <filter string="Status" name="group_state"
                                context="{'group_by': 'state'}"/>
                        <filter string="From" name="group_from"
                                context="{'group_by': 'email_from'}"/>
                        <filter string="Contact" name="group_partner"
                                context="{'group_by': 'partner_ids'}"/>
                        <filter string="CRM Lead" name="group_lead"
                                context="{'group_by': 'lead_ids'}"/>
                        <filter string="Server" name="group_server"
                                context="{'group_by': 'server_id'}"/>
                        <filter string="Date" name="group_date"
                                context="{'group_by': 'date_received:day'}"/>
                    </group>
                </search>
            </field>
        </record>

        <record id="action_mail_sync_message_route_unrouted" model="ir.actions.server">
            <field name="name">Route All Unrouted</field>
            <field name="model_id" ref="model_mail_sync_message"/>
            <field name="binding_model_id" ref="model_mail_sync_message"/>
            <field name="binding_view_types">list</field>
            <field name="state">code</field>
            <field name="code">action = model.action_route_all_unrouted()</field>
            <field name="groups_id" eval="[(4, ref('jb_mail_inbox.group_mail_inbox_manager'))]"/>
        </record>

        <record id="action_mail_sync_message" model="ir.actions.act_window">
            <field name="name">Inbox</field>
            <field name="res_model">mail.sync.message</field>
            <field name="view_mode">tree,form</field>
            <field name="context">{'search_default_filter_new': 1}</field>
            <field name="help" type="html">
                <p class="o_view_nocontent_smiling_face">
                    No messages yet
                </p>
                <p>
                    Configure an IMAP mail server and fetch mail to populate this inbox.
                </p>
            </field>
        </record>

    </data>
</odoo>
