/** @odoo-module **/

const { Component, useState, mount, xml } = owl;

import { registry } from "@web/core/registry";
import { formView } from "@web/views/form/form_view";
import { FormCompiler } from "@web/views/form/form_compiler";
import { FormRenderer } from '@web/views/form/form_renderer';

class PartnerOrderSummary extends Component {
    partner = useState({});
    constructor(self, partner) {
        super();
        this.partner = partner;
    }
};

PartnerOrderSummary.template = "owl_demo.PartnerOrderSummary";


function compilePartner(el, params) {
//    this._rpc({
//                model: "res.partner",
//                method: "read",
//                args: [[this.state.data.partner_id.res_id]]
//            }).then(data => {
//                (new ComponentWrapper(this,
//                    PartnerOrderSummary,
//                    useState(data[0])
//                )).mount(element);
//            });

    return "<h1>hello world</h1>";
}

function compileForm() {
    const res = this.compileForm(...arguments);
    res.classList.append("test111");
    return res;
}

export class SaleOrderFormCompiler extends FormCompiler {
    setup() {
        super.setup();
        this.compilers.unshift(
            { selector: "form", fn: compileForm },
            { selector: "div .o_partner_order_summary", fn: compilePartner }
        );
    }
}

export class SaleOrderFormRenderer extends FormRenderer { }

SaleOrderFormRenderer.components = {
    ...FormRenderer.components,
    PartnerOrderSummary,
};

registry.category('views').add('sale.view_order_form', {
    ...formView,
    Compiler: SaleOrderFormCompiler,
    Renderer: SaleOrderFormRenderer,
});


/*** 重载表单渲染器，对任何包含o_partner_order_summary这一class 的div挂载当前组件 */
//FormRenderer.include({
//    _render: async function() {
//        await this._super(...arguments);
//        for (const element of this.el.querySelectorAll(".o_partner_order_summary")) {
//            this._rpc({
//                model: "res.partner",
//                method: "read",
//                args: [[this.state.data.partner_id.res_id]]
//            }).then(data => {
//                (new ComponentWrapper(this,
//                    PartnerOrderSummary,
//                    useState(data[0])
//                )).mount(element);
//            });
//        }
//    }
//});

