<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
    <!-- History Form View -->
    <record id="wiki_history_form_inherit" model="ir.ui.view">
        <field name="name">document.page.history.form</field>
        <field name="model">document.page.history</field>
        <field name="inherit_id" ref="document_page.wiki_history_form" />
        <field name="arch" type="xml">
            <sheet position="before">
                <header>
                    <!-- draft -> to approve -->
                    <button
                        name="action_to_approve"
                        type="object"
                        string="Send to Review"
                        class="oe_highlight"
                        attrs="{'invisible':['|','|',('is_approval_required','=',False),('am_i_owner','=',False),('state', 'not in', ['draft'])]}"
                    />
                    <!-- approve if i am approver -->
                    <button
                        name="action_approve"
                        type="object"
                        string="Approve"
                        class="oe_highlight"
                        attrs="{'invisible':['|','|',('is_approval_required','=',False),('am_i_approver','=',False),('state','not in',['draft','to approve'])]}"
                    />
                    <!-- approve if it's not required and i am owner -->
                    <button
                        name="action_approve"
                        type="object"
                        string="Approve"
                        class="oe_highlight"
                        attrs="{'invisible':['|','|',('is_approval_required','=',True),('am_i_owner','=',False),('state','not in',['draft', 'to approve'])]}"
                    />
                    <!-- cancel if i am owner or approver -->
                    <button
                        name="action_cancel"
                        type="object"
                        string="Cancel"
                        attrs="{'invisible':['|','&amp;',('am_i_owner','=',False),('am_i_approver','=',False),('state','not in',['draft','to approve'])]}"
                    />
                    <!-- reopen if i am owner or approver -->
                    <button
                        name="action_draft"
                        type="object"
                        string="Back to draft"
                        attrs="{'invisible':['|','&amp;',('am_i_owner','=',False),('am_i_approver','=',False),('state','not in',['cancelled'])]}"
                    />
                    <!-- cancel & reopen, if i am owner or approver -->
                    <button
                        name="action_draft"
                        type="object"
                        string="Back to draft"
                        attrs="{'invisible':['|','&amp;',('am_i_owner','=',False),('am_i_approver','=',False),('state','not in',['to approve'])]}"
                    />
                    <field name="am_i_owner" invisible="1" />
                    <field name="am_i_approver" invisible="1" />
                    <field name="is_approval_required" invisible="1" />
                    <field
                        name="state"
                        widget="statusbar"
                        statusbar_visible="draft,approved"
                    />
                </header>
            </sheet>
            <xpath expr="//field[@name='create_uid']/parent::group" position="after">
                <group>
                    <field
                        name="approved_uid"
                        readonly="1"
                        attrs="{'invisible':[('state','not in',['approved'])]}"
                    />
                    <field
                        name="approved_date"
                        readonly="1"
                        attrs="{'invisible':[('state','not in',['approved'])]}"
                    />
                </group>
            </xpath>
            <!-- Readonly fields -->
            <field name="content" position="attributes">
                <attribute
                    name="attrs"
                >{'readonly': [('state', 'not in', ['draft'])]}</attribute>
            </field>
            <field name="page_id" position="attributes">
                <attribute
                    name="attrs"
                >{'readonly': [('state', 'not in', ['draft'])]}</attribute>
            </field>
            <field name="name" position="attributes">
                <attribute
                    name="attrs"
                >{'readonly': [('state', 'not in', ['draft'])]}</attribute>
            </field>
            <field name="summary" position="attributes">
                <attribute
                    name="attrs"
                >{'readonly': [('state', 'not in', ['draft'])]}</attribute>
            </field>
            <!-- Chatter -->
            <sheet position="after">
                <div class="oe_chatter">
                    <field name="message_follower_ids" widget="mail_followers" />
                    <field name="message_ids" widget="mail_thread" />
                </div>
            </sheet>
        </field>
    </record>
    <!-- History Form View Manager Rights -->
    <record id="wiki_history_form_inherit_manager" model="ir.ui.view">
        <field name="name">document.page.history.form</field>
        <field name="model">document.page.history</field>
        <field name="inherit_id" ref="wiki_history_form_inherit" />
        <field name="arch" type="xml">
            <!-- Readonly fields -->
            <field
                name="name"
                position="attributes"
                groups="document_page.group_document_manager"
            >
                <attribute name="attrs">{'readonly': False}</attribute>
            </field>
            <field
                name="summary"
                position="attributes"
                groups="document_page.group_document_manager"
            >
                <attribute name="attrs">{'readonly': False}</attribute>
            </field>
        </field>
    </record>
    <!-- Page Form View -->
    <record id="wiki_form_inherit" model="ir.ui.view">
        <field name="name">document.page.form</field>
        <field name="model">document.page</field>
        <field name="inherit_id" ref="document_page.view_wiki_form" />
        <field name="arch" type="xml">
            <sheet position="before">
                <div
                    class="alert alert-info"
                    role="alert"
                    style="margin-bottom:0px;"
                    attrs="{'invisible': [('has_changes_pending_approval','=',False)]}"
                >
                    This document has <b
                    >Changes Pending Approval</b>. You are viewing the last approved content.
                </div>
                <div
                    class="alert alert-warning oe_edit_only"
                    role="alert"
                    style="margin-bottom:0px;"
                    attrs="{'invisible': [('is_approval_required','=',False)]}"
                >
                    This document requires approval. If edited, you will create a new <b
                    >Change Request</b>.
                </div>
                <div
                    class="alert alert-warning oe_edit_only"
                    role="alert"
                    style="margin-bottom:0px;"
                    attrs="{'invisible': [('user_has_drafts','=',False)]}"
                >
                    <b>You already have a Draft Change Request for this page.</b>
                    It is highly recommended that you edit that one instead of creating a new one.
                </div>
                <field name="is_approval_required" invisible="1" />
                <field name="has_changes_pending_approval" invisible="1" />
                <field name="user_has_drafts" invisible="1" />
            </sheet>
            <xpath expr="//div[@name='button_box']" position="inside">
                <button
                    class="oe_stat_button"
                    name="action_changes_pending_approval"
                    string="Change Requests"
                    type="object"
                    attrs="{'invisible':[('has_changes_pending_approval','=',False),('user_has_drafts','=',False)]}"
                    icon="fa-edit"
                />
            </xpath>
            <field name="content_uid" position="after">
                <field name="approved_uid" />
            </field>
            <field name="content_date" position="attributes">
                <attribute name="invisible">1</attribute>
            </field>
            <field name="content_date" position="after">
                <field name="approved_date" />
            </field>
            <field name="history_ids" position="inside">
                <tree>
                    <field name="id" />
                    <field name="approved_date" />
                    <field name="summary" />
                    <field name="create_uid" />
                    <field name="approved_uid" />
                </tree>
            </field>
        </field>
    </record>
    <!-- Page Menu Form View -->
    <record id="view_wiki_menu_form_inherit" model="ir.ui.view">
        <field name="name">document.page.form</field>
        <field name="model">document.page</field>
        <field name="inherit_id" ref="document_page.view_wiki_menu_form" />
        <field name="arch" type="xml">
            <field name="content" position="before">
                <group
                    class="oe_read_only"
                    attrs="{'invisible':[('type','!=','content')]}"
                >
                    <field name="approved_date" />
                    <field name="approved_uid" />
                </group>
            </field>
        </field>
    </record>
    <!-- Catgory Form View -->
    <record id="view_category_form_inherit" model="ir.ui.view">
        <field name="name">document.page.category.form</field>
        <field name="model">document.page</field>
        <field name="inherit_id" ref="document_page.view_category_form" />
        <field name="arch" type="xml">
            <field name="parent_id" position="after">
                <field name="approval_required" />
                <field
                    name="approver_gid"
                    attrs="{'invisible':[('approval_required','!=', True)], 'required':[('approval_required','=', True)]}"
                />
            </field>
        </field>
    </record>
    <!-- History Tree view -->
    <record id="view_wiki_history_tree_inherit" model="ir.ui.view">
        <field name="name">document.page.history.tree</field>
        <field name="model">document.page.history</field>
        <field name="inherit_id" ref="document_page.view_wiki_history_tree" />
        <field name="arch" type="xml">
            <tree position="attributes">
                <attribute name="decoration-info">state=='draft'</attribute>
                <attribute name="decoration-primary">state=='to approve'</attribute>
                <attribute name="decoration-muted">state=='cancelled'</attribute>
            </tree>
            <tree position="inside">
                <field name="state" />
                <field name="approved_uid" />
                <field name="approved_date" />
            </tree>
        </field>
    </record>
    <!-- History Search view  -->
    <record id="view_wiki_history_filter" model="ir.ui.view">
        <field name="name">document.page.history.search</field>
        <field name="model">document.page.history</field>
        <field name="inherit_id" ref="document_page.view_wiki_history_filter" />
        <field name="arch" type="xml">
            <field name="page_id" position="before">
                <field name="state" />
            </field>
            <field name="create_uid" position="after">
                <filter name="draft" string="Draft" domain="[('state','=','draft')]" />
                <filter
                    name="pending"
                    string="Pending Approval"
                    domain="[('state','=','to approve')]"
                />
                <filter
                    name="approved"
                    string="Approved"
                    domain="[('state','=','approved')]"
                />
                <filter
                    name="cancelled"
                    string="Cancelled"
                    domain="[('state','=','cancelled')]"
                />
            </field>
            <filter name="group_by_author" position="before">
                <filter
                    name="group_state"
                    string="State"
                    context="{'group_by':'state'}"
                />
            </filter>
            <filter name="group_by_author" position="after">
                <filter
                    name="group_approver"
                    string="Approver"
                    context="{'group_by':'approved_uid'}"
                />
            </filter>
        </field>
    </record>
    <!-- Change Requests Action  -->
    <record model="ir.actions.act_window" id="action_change_requests">
        <field name="name">Change Requests</field>
        <field name="res_model">document.page.history</field>
        <field name="view_mode">tree,form</field>
        <field
            name="context"
        >{'search_default_draft': 1, 'search_default_pending': 1}</field>
    </record>
    <menuitem
        id="menu_page_change_requests"
        name="Change Requests"
        parent="document_page.menu_wiki"
        action="action_change_requests"
        sequence="25"
        groups="document_page.group_document_editor"
    />
</odoo>
