/** @odoo-module **/

import { registry } from "@web/core/registry";
import { _lt } from "@web/core/l10n/translation";
import { standardFieldProps } from "@web/views/fields/standard_field_props";
import { useInputField } from "@web/views/fields/input_field_hook";
import { session } from "@web/session";
import core from 'web.core';
import rpc from 'web.rpc';

var QWeb = core.qweb;

import { Component,useRef,useEffect } from "@odoo/owl";

export class CTemplateField extends Component {
    setup() {
        this.show_template = useRef('showTemplate')
        useInputField({
             getValue: () => this.props.value || ""
        });

        useEffect(
            (el) => {
                if (!el) {
                    return;
                }
                return this.formatTemplate($(el));
            },
            () => [this.show_template.el],
        )
    }

    formatTemplate($el) {
        console.log($el)
        var self=this;
        if (this.props.readonly && this.props.value) {
            //value =  this.props.value + ' | ' + this.props.template_xml_id;
            if (this.props.type == 'one2many' || this.props.type == 'many2many'){
                // 如果是one2many或many2many，就重新获取对象列表
                rpc.query({
                    model: this.props.value.resModel,
                    method: 'read',
                    args: [this.props.value.currentIds,[]],
                }).then((result) => {
                   console.log(result);
                   return $el.html(QWeb.render(this.props.template_xml_id,{'record':this.props.record,'current_field':result}))
                });
            }
            else if (this.props.type == 'many2one'){
                // 如果是one2many或many2many，就重新获取对象列表
                rpc.query({
                    model: this.props.record.fields[this.props.name].relation,
                    method: 'read',
                    args: [this.props.value[0],[]],
                }).then((result) => {
                   console.log(result);
                   return $el.html(QWeb.render(this.props.template_xml_id,{'record':this.props.record,'current_field':result[0]}))
                });
            }
            else{
                if (this.props.template_xml_id === undefined)
                {
                    return $el.html(this.props.value);
                }else{
                    return $el.html(QWeb.render(this.props.template_xml_id,{'record':this.props.record,'current_field':this.props.value}));
                }

            }
        }

       //
    }

}

CTemplateField.template = "owl_demo.CTemplateField";
CTemplateField.props = {
    ...standardFieldProps,
    template_xml_id: { type: String, optional: true },
    placeholder: { type: String, optional: true },
};
CTemplateField.defaultProps = {
//    hideSymbol: false,
//    inputType: "text",
};

CTemplateField.supportedTypes = ["many2one","char"];
CTemplateField.displayName = _lt("CTemplate");

CTemplateField.extractProps = ({ attrs }) => {
    return {
        template_xml_id: attrs.options.template,
        placeholder: attrs.placeholder,
    };
};

registry.category("fields").add("CTemplate", CTemplateField);
