<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2020 Creu Blanca
     License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->

<odoo>

    <record model="ir.actions.act_window" id="kpi_kpi_history_act_window">
        <field name="name">Kpi History</field>
        <field name="res_model">kpi.kpi.history</field>
        <field name="view_mode">tree</field>
        <field name="domain">[('kpi_id', '=', active_id)]</field>
        <field name="context">{}</field>
    </record>

    <record model="ir.ui.view" id="kpi_kpi_history_widget_form_view">
        <field name="name">kpi.kpi.history.raw.form (in kpi_dashboard)</field>
        <field name="model">kpi.kpi.history</field>
        <field name="arch" type="xml">
            <form>
                <field name="create_date" invisible="1"/>
                <field name="widget" invisible="1"/>
                <field name="name" invisible="1"/>
                <field name="value"
                       widget="kpi"
                       options="{'date': 'create_date', 'widget': 'widget', 'name': 'name'}"/>
            </form>
        </field>
    </record>

    <record model="ir.ui.view" id="kpi_kpi_history_raw_form_view">
        <field name="name">kpi.kpi.history.raw.form (in kpi_dashboard)</field>
        <field name="model">kpi.kpi.history</field>
        <field name="arch" type="xml">
            <form>
                <field name="raw_value"/>
            </form>
        </field>
    </record>

    <record model="ir.ui.view" id="kpi_kpi_history_tree_view">
        <field name="name">kpi.kpi.history.tree (in kpi_dashboard)</field>
        <field name="model">kpi.kpi.history</field>
        <field name="arch" type="xml">
            <tree create="0" delete="0">
                <field name="create_date"/>
                <button name="show_form" string="Show value" type="object"
                        context="{'form_id': %(kpi_dashboard.kpi_kpi_history_widget_form_view)d}"
                />
                <button name="show_form" string="Raw value" type="object"
                        context="{'form_id': %(kpi_dashboard.kpi_kpi_history_raw_form_view)d}"
                />
            </tree>
        </field>
    </record>

    <record model="ir.ui.view" id="kpi_kpi_form_view">
        <field name="name">kpi.kpi.form (in kpi_dashboard)</field>
        <field name="model">kpi.kpi</field>
        <field name="arch" type="xml">
            <form>
                <header>
                    <button name="generate_cron" string="Generate cron" type="object"
                            attrs="{'invisible': ['|', ('cron_id', '!=',False), ('compute_on_fly', '=', True)]}"/>
                    <button name="compute" string="Compute now" type="object" attrs="{'invisible': [('compute_on_fly', '=', True)]}"/>
                </header>
                <sheet>
                    <div class="oe_button_box" name="button_box">
                        <button name="%(kpi_dashboard.kpi_kpi_history_act_window)d"
                                string="Show history"
                                type="action"
                                attrs="{'invisible': ['|', ('store_history', '=', False), ('compute_on_fly', '=', True)]}"
                                icon="fa-history"/>
                        <button string="Show value" type="object" name="show_value"
                                icon="fa-paint-brush"
                        />
                    </div>
                    <h2>
                        <field name="name"/>
                    </h2>
                    <group>
                        <group>
                            <field name="computation_method"/>
                            <field name="widget"/>
                            <field name="store_history" attrs="{'invisible': [('compute_on_fly', '=', True)]}"/>
                            <field name="store_history_interval" attrs="{'invisible': [('store_history', '=', False)]}"/>
                            <field name="store_history_interval_number" attrs="{'invisible': [('store_history', '=', False)]}"/>
                            <field name="compute_on_fly" attrs="{'invisible': [('store_history', '=', True)]}"/>
                            <field name="model_id" attrs="{'invisible': [('computation_method', '!=', 'function')]}"/>
                            <field name="function" attrs="{'required': [('computation_method', '=', 'function')], 'invisible': [('computation_method', '!=', 'function')]}"/>
                            <field name="args" attrs="{'invisible': [('computation_method', '!=', 'function')]}"/>
                            <field name="kwargs" attrs="{'invisible': [('computation_method', '!=', 'function')]}"/>
                        </group>
                        <group>
                            <field name="cron_id" attrs="{'invisible': [('cron_id', '=',False)]}" readonly="True"/>
                        </group>
                        <group>
                            <field name="suffix"/>
                            <field name="prefix"/>
                        </group>
                    </group>

                    <notebook>
                        <page name="action" string="Actions">
                            <field name="action_ids">
                                <tree editable="bottom">
                                    <field name="action"/>
                                    <field name="context"/>
                                </tree>
                            </field>
                        </page>
                        <page name="code" string="Code" attrs="{'invisible': [('computation_method', '!=', 'code')]}">
                            <field name="code" widget="ace"
                                   options="{'mode': 'python'}"
                                   placeholder="Enter Python code here."/>
                        </page>
                    </notebook>
                </sheet>
            </form>
        </field>
    </record>

    <record model="ir.ui.view" id="kpi_kpi_widget_form_view">
        <field name="name">kpi.kpi.raw.form (in kpi_dashboard)</field>
        <field name="model">kpi.kpi</field>
        <field name="arch" type="xml">
            <form>
                <field name="computed_date" invisible="1"/>
                <field name="widget" invisible="1"/>
                <field name="name" invisible="1"/>
                <field name="computed_value"
                       widget="kpi"
                       options="{'date': 'computed_date', 'widget': 'widget', 'name': 'name'}"/>
            </form>
        </field>
    </record>

    <record model="ir.ui.view" id="kpi_kpi_search_view">
        <field name="name">kpi.kpi.search (in kpi_dashboard)</field>
        <field name="model">kpi.kpi</field>
        <field name="arch" type="xml">
            <search>
                <field name="name"/>
            </search>
        </field>
    </record>

    <record model="ir.ui.view" id="kpi_kpi_tree_view">
        <field name="name">kpi.kpi.tree (in kpi_dashboard)</field>
        <field name="model">kpi.kpi</field>
        <field name="arch" type="xml">
            <tree>
                <field name="name"/>
            </tree>
        </field>
    </record>

    <record model="ir.actions.act_window" id="kpi_kpi_act_window">
        <field name="name">Kpi</field>
        <field name="res_model">kpi.kpi</field>
        <field name="view_mode">tree,form</field>
        <field name="domain">[]</field>
        <field name="context">{}</field>
    </record>

    <record model="ir.ui.menu" id="kpi_kpi_menu">
        <field name="name">Configure Kpi</field>
        <field name="parent_id" ref="menu_configuration_kpi_dashboards"/>
        <field name="action" ref="kpi_kpi_act_window"/>
        <field name="sequence" eval="20"/>
    </record>

</odoo>
