import * as React from "react";
import { Select, Input } from "@alifd/next";
import { createElement } from 'react';
import { IPublicEnumTransformStage } from '@alilc/lowcode-types';
import { project } from '@alilc/lowcode-engine';
import { string } from "prop-types";
export default class AltSelbcfield2Setter extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            bcName: '',
            bcTextField: '',
            bcValueField: '',
            bcds: new Array(),
            bcfieldds: new Array(),
            bcHashtable: new Map()
        }
    }
    componentDidMount() {
        const schema = project.exportSchema(IPublicEnumTransformStage.Save);
        let bcHashtableEx: Map<string, Array<any>> = new Map();
        if (schema.componentsTree[0].hasOwnProperty('ucmlBCList')){
            const bclistinfo = schema.componentsTree[0].ucmlBCList;
            let bcarray = new Array();
            
            for (let i = 0; i < bclistinfo.length; i++) {
                let bcobj = bclistinfo[i];
                let Aobj= this.setBCSelectList(bcobj,bcarray,bcHashtableEx)
                bcarray.push(Aobj[0]);
                bcHashtableEx=new Map(Array.from(bcHashtableEx).concat(Array.from(Aobj[1])));  

            }
            this.setState({
                bcds: bcarray.filter(item => !Array.isArray(item)),//给递归方法打补丁，删除不需要的对象
                bcHashtable: bcHashtableEx
            });
        } 


        const { onChange, value } = this.props;
        if (value != undefined) {
            this.setState(value);
            
            let bcfieldcols = bcHashtableEx.get(value.bcName);
            let bcfarray=new Array();
            for (let fieldobj of bcfieldcols) {
                let optex = { value: fieldobj.fieldName, label: fieldobj.caption }
                bcfarray.push(optex);
            }
            this.setState({
                bcfieldds: bcfarray
            });
            onChange(value);
        }

    }
    setBCSelectList=(source, bcarray,bcHashtableex)=> {

        // 遍历源对象的属性  
        for (let key in source) {
            if (source.hasOwnProperty(key)) {
                // 如果当前属性是children，则递归处理子节点  
                if (key === 'children') {
                    for (let i = 0; i < source[key].length; i++) {
                        let childSource = source[key][i];
                        this.setBCSelectList(childSource, bcarray,bcHashtableex); // 递归处理子节点   
                    }
                } else {
                    // 否则，将属性值复制到目标对象中  
                    if(key == 'bcName'){
                        let opt = { value: source.bcName, label: source.bcName }
                        bcarray.push(opt);
                        bcHashtableex.set(source.bcName, source.columns)
                    }
                }
            }
        }

        return  [bcarray,bcHashtableex];
    }

    // 声明 Setter 的 title
    static displayName = 'AltSelbcfield2Setter';
    render() {
        const { onChange, value, t } = this.props;
        const bconChange = v => {
            let bcfieldcols = this.state.bcHashtable.get(v);
            let bcfieldarray=new Array();
            for (let fieldobj of bcfieldcols) {
                let optex = { value: fieldobj.fieldName, label: fieldobj.caption }
                bcfieldarray.push(optex);
            }
            this.setState({
                bcName: v,
                bcfieldds: bcfieldarray
            });

            onChange({ bcName: v, bcTextField: this.state.bcTextField,bcValueField: this.state.bcValueField })
            t.parent.setPropValue("bcName", v);
        };
        const bctextfieldonChange = (v,acttype,item) => {
            this.setState({
                bcTextField: v
            });
            onChange({ bcName: this.state.bcName, bcTextField: v,bcValueField: this.state.bcValueField })
            t.parent.setPropValue("bcName", this.state.bcName);
            if(t.parent.getNode().componentName=="UcmlSelectManBox")
            {
                t.parent.setPropValue("DataTextField", v);
                t.parent.setPropValue("DataValueField", this.state.bcValueField);
            }
        };
        const bcvaluefieldonChange = (v,acttype,item) => {
            this.setState({
                bcValueField: v
            });
            onChange({ bcName: this.state.bcName, bcTextField: this.state.bcTextField,bcValueField: v })
            t.parent.setPropValue("bcName", this.state.bcName);
            if(t.parent.getNode().componentName=="UcmlSelectManBox")
            {
                t.parent.setPropValue("DataTextField", this.state.bcTextField);
                t.parent.setPropValue("DataValueField", v);
            }
        };

        return (
            <div>
                <Select label="BC:" style={{ width: '100%' }}
                    value={this.state.bcName}
                    onChange={bconChange}
                    dataSource={this.state.bcds}
                ></Select>
                <br />
                <br />
                <Select
                    showSearch
                    label="名称字段:"
                    value={this.state.bcTextField}
                    style={{ width: '100%' }}
                    onChange={bctextfieldonChange}
                    dataSource={this.state.bcfieldds}
                ></Select>
                <br />
                <br />
                <Select
                    showSearch
                    label="值字段:"
                    value={this.state.bcValueField}
                    style={{ width: '100%' }}
                    onChange={bcvaluefieldonChange}
                    dataSource={this.state.bcfieldds}
                ></Select>
                <Input style={{ display: 'none' }} value={value} onChange={onChange}></Input>
            </div>
        );
    }
}