<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
    <record id="view_import_odoo_database_tree" model="ir.ui.view">
        <field name="model">import.odoo.database</field>
        <field name="arch" type="xml">
            <tree>
                <field name="url" />
                <field name="database" />
            </tree>
        </field>
    </record>
    <record id="view_import_odoo_database_form" model="ir.ui.view">
        <field name="model">import.odoo.database</field>
        <field name="arch" type="xml">
            <form>
                <header>
                    <button
                        type="object"
                        name="action_import"
                        string="Run import"
                        class="oe_highlight"
                        attrs="{'invisible': [('cronjob_running', '=', True)]}"
                    />
                    <button
                        type="object"
                        name="exists"
                        string="Refresh"
                        class="oe_highlight"
                        attrs="{'invisible': [('cronjob_running', '=', False)]}"
                    />
                    <field name="cronjob_running" invisible="1" />
                </header>
                <sheet>
                    <field
                        name="status_html"
                        attrs="{'invisible': [('status_html', '=', False)]}"
                    />
                    <group col="4" name="credentials">
                        <field
                            name="url"
                            widget="url"
                            attrs="{'readonly': [('cronjob_running', '=', True)]}"
                        />
                        <field name="ssl" />
                        <field
                            name="ssl_bypass"
                            attrs="{'readonly': [('cronjob_running', '=', True)], 'invisible': [('ssl', '=', False)]}"
                        />
                        <field
                            name="database"
                            attrs="{'readonly': [('cronjob_running', '=', True)]}"
                        />
                        <field
                            name="user"
                            attrs="{'readonly': [('cronjob_running', '=', True)]}"
                        />
                        <field
                            name="password"
                            password="True"
                            attrs="{'readonly': [('cronjob_running', '=', True)], 'required': [('cronjob_running', '=', False)]}"
                        />
                        <field name="null_password" />
                        <field name="duplicates" />
                        <field
                            name="cronjob_id"
                            attrs="{'invisible': [('cronjob_id', '=', False)]}"
                        />
                    </group>
                    <label for="import_line_ids" />
                    <field
                        name="import_line_ids"
                        attrs="{'readonly': [('cronjob_running', '=', True)]}"
                    >
                        <tree editable="top">
                            <field name="sequence" widget="handle" />
                            <field name="model_id" />
                            <field name="domain" />
                            <field name="extra_fields" />
                            <field name="defaults" />
                            <field
                                name="postprocess"
                                placeholder="# `vals` is the dictionary to be passed to create/write, and you have access to `env`, `_id`, `remote`"
                            />
                        </tree>
                    </field>
                    <label for="import_field_mappings" />
                    <field
                        name="import_field_mappings"
                        attrs="{'readonly': [('cronjob_running', '=', True)]}"
                    />
                </sheet>
            </form>
        </field>
    </record>
    <template id="view_import_odoo_database_qweb">
        <script type="text/javascript">
            odoo.define(null, function(require) {
                var rpc = require('web.rpc');
                $("body").on("click", ".base_import_odoo_results a", function() {
                    var model = $(this).data('model');
                    var model_name = $(this).data('model-name');
                    var database_id = $(this).data('database-id');
                    console.log(model, model_name, database_id);
                    rpc.query({
                        model: 'ir.model.data',
                        method: 'search_read',
                        fields: ['res_id'],
                        domain: [
                            ['module', '=', '__base_import_odoo__'],
                            ['model', '=', model],
                            ['import_database_id', '=', database_id],
                        ],
                    })
                    .then(function(data)
                    {
                        return require('web.web_client').action_manager.do_action({
                            'name': model_name,
                            'type': 'ir.actions.act_window',
                            'views': [[false, 'list'], [false, 'form']],
                            'res_model': model,
                            'domain': [['id', 'in', _.map(data, function(x) {return x.res_id})]]
                        });
                    });
                });
            })
        </script>
        <h2 t-if="object.cronjob_running">Import progress</h2>
        <h2 t-if="not object.cronjob_running">Import results</h2>
        <div style="display: flex; flex-flow: row wrap">
            <div
                style="margin-right: .5em"
                t-foreach="object.import_line_ids"
                t-as="import_line"
                class="base_import_odoo_results"
            >
                <t t-set="model_name" t-value="import_line.model_id.model" />
                <t t-set="model_display_name" t-value="import_line.model_id.name" />
                <h3 t-esc="model_display_name" />
                <a
                    href="#"
                    t-att-data-model="model_name"
                    t-att-data-model-name="model_display_name"
                    t-att-data-database-id="object.id"
                >
                    <span
                        t-esc="object.status_data.get('done', {}).get(model_name, 0)"
                    />/<span
                        t-esc="object.status_data.get('counts', {}).get(model_name, 0)"
                    /> done
                </a>
            </div>
        </div>
        <t t-if="object.status_data.get('error')">
            <pre t-esc="object.status_data['error']" />
        </t>
        <div t-if="object.status_data.get('dummies')">
            The following remote ids don't have a mapping but have to be imported anyways due to not null constraints.
            <dl>
                <t t-foreach="object.status_data['dummies']" t-as="model_name">
                    <dt t-esc="model_name" />
                    <dd t-esc="object.status_data['dummies'][model_name]" />
                </t>
            </dl>
            To fix this, create mappings for the remote ids listed, or if this is not feasible, map the whole model. You might also have a too specific domain on your import model definition.
        </div>
    </template>
</odoo>
