<?xml version="1.0"?>
<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="page_approval_to_approve" type="object" string="Send to Review" state="draft" class="oe_highlight"
                            attrs="{'invisible':['|','|',('is_approval_required','=',False),('am_i_owner','=',False),('state', 'not in', ['draft'])]}"/>
                    <!-- approve if i am approver -->
                    <button name="page_approval_approved" type="object" string="Approve" state="to aprrove" 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="page_approval_approved" 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="page_approval_cancelled" 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="page_approval_draft" type="object" string="Back to draft" state="cancelled"
                            attrs="{'invisible':['|','&amp;',('am_i_owner','=',False),('am_i_approver','=',False),('state','not in',['cancelled'])]}"/>
                    <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>
            <field name="content" position="attributes">
                <attribute name="attrs">{'readonly': [('state', 'not in', ['draft'])]}</attribute>
            </field>
            <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>

    <!-- 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>
                <field name="is_approval_required" invisible="1"/>
                <field name="has_changes_pending_approval" invisible="1"/>
            </sheet>

            <field name="type" position="before">
                <div class="oe_button_box" name="button_box">
                    <button class="oe_stat_button" name="action_changes_pending_approval"
                            string="Change Requests" type="object"
                            attrs="{'invisible':[('has_changes_pending_approval','=',False)]}" icon="fa-edit"/>
                </div>
            </field>

            <field name="content_uid" position="after">
                <field name="approved_uid"/>
            </field>

            <field name="content_date" position="replace">
                <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_author" position="before">
                <filter name="group_state" string="State" context="{'group_by':'state'}" />
            </filter>
            <filter name="group_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_type">form</field>
        <field name="view_mode">tree,form</field>
        <field name="context">{'search_default_state':'to approve'}</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>
