<?xml version="1.0" encoding="utf-8" ?>
<odoo>
    <record id="sql_export_view_form" model="ir.ui.view">
        <field name="name">Sql_export_form_view</field>
        <field name="model">sql.export</field>
        <field name="arch" type="xml">
            <form string="SQL export">
                <header>
                    <button
                        name="button_validate_sql_expression"
                        type="object"
                        states="draft"
                        string="Validate SQL Expression"
                        class="oe_highlight"
                        groups="sql_request_abstract.group_sql_request_manager"
                    />
                    <button
                        name="button_preview_sql_expression"
                        type="object"
                        states="draft"
                        string="Preview Results"
                        attrs="{'invisible': [('field_ids', '!=', False)]}"
                    />
                    <button
                        name="button_set_draft"
                        type="object"
                        states="sql_valid"
                        string="Set to Draft"
                        groups="sql_request_abstract.group_sql_request_manager"
                    />
                    <button
                        name="export_sql_query"
                        string="Execute Query"
                        states="sql_valid"
                        type="object"
                        class="oe_highlight"
                        icon="fa-arrow-right text-success"
                    />
                    <field name="state" widget="statusbar" />
                </header>
                <sheet>
                    <group>
                        <h1>
                            <field
                                name="name"
                                nolabel="1"
                                placeholder="Request Name"
                                attrs="{'readonly': [('state', '!=', 'draft')]}"
                            />
                        </h1>
                    </group>
                    <group
                        name="option"
                        groups="sql_request_abstract.group_sql_request_user"
                    >
                        <field
                            name="file_format"
                            attrs="{'readonly': [('state', '!=', 'draft')]}"
                        />
                        <field
                            name="copy_options"
                            attrs="{'invisible': [('file_format', '!=', 'csv')], 'required': [('file_format', '=', 'csv')], 'readonly': [('state', '!=', 'draft')]}"
                        />
                        <field
                            name="encoding"
                            attrs="{'readonly': [('state', '!=', 'draft')]}"
                        />
                    </group>
                    <group
                        name="request"
                        string="SQL Request"
                        groups="sql_request_abstract.group_sql_request_user"
                    >
                        <field
                            name="query"
                            nolabel="1"
                            placeholder="select * from res_partner"
                            attrs="{'readonly': [('state', '!=', 'draft')]}"
                        />
                    </group>
                    <group
                        name="parameters"
                        string="Parameters"
                        groups="sql_request_abstract.group_sql_request_user"
                    >
                    <field
                            name="field_ids"
                            nolabel="1"
                            options="{'no_create': True}"
                            context="{'tree_view_ref': 'sql_export.sql_parameter_view_tree', 'form_view_ref': 'sql_export.sql_parameter_view_form'}"
                            attrs="{'readonly': [('state', '!=', 'draft')]}"
                        />
                    </group>
                    <group groups="sql_request_abstract.group_sql_request_manager">
                        <group string="Allowed Users" name="users_allowed">
                            <field name="user_ids" nolabel="1" />
                        </group>
                        <group string="Allowed Groups" name="groups_allowed">
                            <field name="group_ids" nolabel="1" />
                        </group>

                    </group>
                </sheet>
            </form>
        </field>
    </record>

    <record id="sql_export_view_tree" model="ir.ui.view">
        <field name="name">Sql_export_tree_view</field>
        <field name="model">sql.export</field>
        <field name="arch" type="xml">
            <tree decoration-info="state=='draft'">
                <field name="name" />
                <field name="state" />
                <button
                    name="export_sql_query"
                    string="Execute Query"
                    states="sql_valid"
                    type="object"
                    icon="fa-arrow-right text-success"
                />
            </tree>
        </field>
    </record>

    <record id="sql_export_tree_action" model="ir.actions.act_window">
        <field name="name">SQL Export</field>
        <field name="res_model">sql.export</field>
        <field name="view_mode">tree,form</field>
    </record>


    <menuitem
        id="sql_export_menu"
        name="Sql Export"
        parent="base.menu_reporting_dashboard"
        sequence="80"
    />

    <menuitem
        id="sql_export_menu_view"
        name="Sql Export"
        parent="sql_export_menu"
        action="sql_export_tree_action"
        sequence="1"
    />

    <record id="sql_parameter_view_form" model="ir.ui.view">
        <field name="name">Sql_parameter_form_view</field>
        <field name="model">ir.model.fields</field>
    <field name="priority">150</field>
        <field name="arch" type="xml">
            <form string="SQL export">
                <group>
                    <field name="name" />
                    <field name="field_description" />
                    <field name="ttype" />
                    <field
                        name="relation"
                        attrs="{'invisible': [('ttype', 'not in', ('many2one', 'many2many', 'one2many'))], 'required': [('ttype', 'in', ('many2one', 'many2many', 'one2many'))]}"
                    />
                    <field name="model_id" readonly="1" />
                    <field name="model" invisible="1" />
                    <field name="required" />
                </group>
            </form>
        </field>
    </record>

    <record id="sql_parameter_view_tree" model="ir.ui.view">
        <field name="name">Sql_parameter_tree_view</field>
        <field name="model">ir.model.fields</field>
        <field name="priority">150</field>
        <field name="arch" type="xml">
            <tree>
                <field name="name" />
                <field name="field_description" />
                <field name="ttype" />
                <field name="required" />
            </tree>
        </field>
    </record>

    <record id="sql_parameter_action" model="ir.actions.act_window">
        <field name="name">SQL Parameter</field>
        <field name="res_model">ir.model.fields</field>
        <field name="view_mode">tree,form</field>
        <field
            name="context"
            eval="{'default_model_id': ref('sql_export.model_sql_file_wizard'), 'default_size': 64, 'search_default_state': 'manual', 'default_model': 'sql.file.wizard'}"
        />
        <field
            name="domain"
        >[('model','=','sql.file.wizard'), ('state', '=', 'manual')]</field>
    </record>

        <record id="sql_parameter_action_view_tree" model="ir.actions.act_window.view">
            <field name="sequence" eval="1" />
            <field name="view_mode">tree</field>
            <field name="view_id" ref="sql_parameter_view_tree" />
            <field name="act_window_id" ref="sql_parameter_action" />
        </record>

        <record id="sql_parameter_action_view_form" model="ir.actions.act_window.view">
            <field name="sequence" eval="2" />
            <field name="view_mode">form</field>
            <field name="view_id" ref="sql_parameter_view_form" />
            <field name="act_window_id" ref="sql_parameter_action" />
        </record>

    <menuitem
        id="sql_parameter_menu_view"
        name="Sql Export Variables"
        parent="sql_export_menu"
        action="sql_parameter_action"
        sequence="5"
        groups="sql_request_abstract.group_sql_request_manager"
    />

</odoo>
