// Copyright (C) 2024 Robert J.A. Wagner; Am Kiefernwald 49b; D-64297 Darmstadt - All Rights Reserved 

import toParent from "./oFormElem_10_base.js";


export default class toFormElem_button extends toParent {
    constructor() {
        super();
    }
    setThisFE() {
        this.setFE({
            Icon: { elem: { FE_Title: "Icon" }, FE_Data: { ynExport: 'Y', ynGenerateView: 'Y' }, ynSkipSave: 'Y', ynReadOnly: 'Y', ynSkipOnSummary: 'Y' },
            GenericButton: { elem: { FE_Title: "Button" }, FE_Data: { ynExport: 'Y', ynGenerateView: 'Y' }, ynSkipSave: 'Y', ynReadOnly: 'Y', ynSkipOnSummary: 'Y' },
        });
    }
    htmlButtonIcon(item, icon) {
        let ptop = 5;
        let _style = item.caption ? "float:left;text-align:left;" : "text-align:center;";
        _style += `padding-top:${ptop}px;`
        return { tag: "SPAN", _class: XRW.Broker.faIcon[icon], _style };
    }
    //------------------------------
    //  render-functions
    //------------------------------
    render_ojsonElemForm(next, form, control, item, cell, arg, FGM_Key) {
        let icon = item.icon;
        var ojson = { _name: item.FGM_Key };
        if (!icon && XRW.is.string(arg[FGM_Key])) {
            icon = arg[FGM_Key];
        }
        if (item.FE_Type == 'Icon') {
            ojson.tag = 'I';
            /*
            if (item.XFonClick || item.XFonClick) {
                // ojson.tag = 'BUTTON';
                ojson._type = "button";
                ojson._tabindex=1;
            }
            */
            if (!icon) return XRW.panic(`toFormElem_button: no icon`);
            ojson._class = XRW.Broker.faIcon[icon];
            ojson._title = item.FE_Title || icon;
        } else {
            let _class = item.class || `w3-button w3-card-4 w3-center w3-hover-black`;
            Object.assign(ojson, {
                tag: "BUTTON",
                _class,
                _style: `min-width: 100%;`,
                _tabindex: 0,
                _title: item.FE_Title,
                hook: "hkOnClick",
                _type: "button",
                next: []
            });
            if (icon) {
                ojson.next.push(this.htmlButtonIcon(item, icon));
            }
            if ('caption' in item) {
                // caption may be omitted by setting "caption":"" or ynClearCaption='Y'
                ojson.next.push({ tag: "DIV", _class: 'w3-rw-buttonText', _style: `margin-left:8px;float:left;text-align:center;`, inner: item.caption });
            }
            if (item.ynTypeMenu == 'Y') {
                ojson._class += " w3-rw-menu-button";
            }
            if (item.numberCircle) {
                let colorClass = item.numberCircleColor || "w3-deep-orange";
                ojson.next.push({ tag: "SPAN", _class: `w3-rw-numberCircle ${colorClass}`, next: [{ tag: "SPAN", _id: item.numberCircle, inner: 0 }] });
            }
        }
        next.push(ojson);
    }
}

new toFormElem_button();
