import * as React from "react";
import { Select, Input } from "@alifd/next";
import { createElement } from 'react';
import { IPublicEnumTransformStage, IPublicModelNode } from '@alilc/lowcode-types';

import { project } from '@alilc/lowcode-engine';
import { string } from "prop-types";
export default class AltSelbcSetter extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            bcds: new Array(),
            bcHashtable: new Map()
        }
    }
    componentDidMount() {
        const schema = project.exportSchema(IPublicEnumTransformStage.Save);
        let bcHashtable: 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,bcHashtable)
                bcarray.push(Aobj[0]);
                bcHashtable=new Map(Array.from(bcHashtable).concat(Array.from(Aobj[1])));  
            }
            
            this.setState({
                bcds: bcarray.filter(item => !Array.isArray(item)),//给递归方法打补丁，删除不需要的对象
                bcHashtable: bcHashtable
            });
        }
    }

     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 = 'AltSelbcSetter';
    render() {
        const { onChange, value, t } = this.props;
        const bconChange = (v, acttype, item) => {
            this.setState({
                bcField: v
            });
            if (t.parent.getNode().componentName == "Table") {
                let colarray = this.state.bcHashtable.get(v);
                let lowtablecolarray = new Array();
                for (let col of colarray) {
                    let colobj = {};
                    colobj["title"] = col.caption;
                    colobj["dataIndex"] = col.fieldName;
                    colobj["key"] = col.fieldName;
                    lowtablecolarray.push(colobj)
                    if(col.caption=="主键"&&col.fieldKind==0)
                    {
                        t.parent.setPropValue("rowKey", col.fieldName);
                    }
                }
                t.parent.setPropValue("columns", lowtablecolarray);
            } else if (t.parent.getNode().componentName == "Form") {
                let colarray = this.state.bcHashtable.get(v);
                let lowformcolarray = new Array();
                for (let col of colarray) {
                    if (col.fieldKind == 3)
                        continue;
                    if (col.fieldType == 46)
                        continue;
                    let formcoldisabled = (col.fieldKind == 2) ? true : false;
                    let formnode = {
                        "componentName": "FormItem",
                        "props": {
                            "label": col.caption,
                            "labelAlign": "right",
                            "colon": true,
                            "required": false,
                            "noStyle": false,
                            "valuePropName": "value",
                            "name": col.fieldName,
                            "bcFieldInfo": {
                                "bcName": v,
                                "bcField": col.fieldName
                            },
                            "bcName": v,
                            "bcField": col.fieldName,
                            "requiredobj": {
                                "required": false,
                                "message": "必填"
                            },
                            "typeobj": {
                                "type": null,
                                "message": null
                            },
                            "lenobj": {
                                "max": null,
                                "min": null,
                                "message": null
                            },
                            "patternobj": {
                                "pattern": null,
                                "message": null
                            }
                        },
                        "children": [

                        ]
                    }
                    if (col.fieldType == 8) {
                        formnode.children.push(
                            {
                                "componentName": "DatePicker",
                                "id": "node_oclqdo7fude",
                                "props": {
                                    "format": "YYYY-MM-DD",
                                    "allowClear": true,
                                    "bordered": true,
                                    "showToday": true,
                                    "autoFocus": false,
                                    "disabled": formcoldisabled,
                                    "inputReadOnly": false,
                                    "showTime": false,
                                    "bcFieldInfo": {
                                        "bcName": v,
                                        "bcField": col.fieldName
                                    },
                                    "bcName": v,
                                    "bcField": col.fieldName,
                                },
                                "hidden": false,
                                "title": "",
                                "isLocked": false,
                                "condition": true,
                                "conditionGroup": ""
                            }
                        );
                    } else if (col.fieldType == 33) {
                        formnode.children.push(
                            {
                                "componentName": "DatePicker",
                                "id": "node_oclqdo7fude",
                                "props": {
                                    "format": "YYYY-MM-DD HH:mm",
                                    "allowClear": true,
                                    "bordered": true,
                                    "showToday": true,
                                    "autoFocus": false,
                                    "disabled": formcoldisabled,
                                    "inputReadOnly": false,
                                    "showTime": true,
                                    "bcFieldInfo": {
                                        "bcName": v,
                                        "bcField": col.fieldName
                                    },
                                    "bcName": v,
                                    "bcField": col.fieldName,
                                },
                                "hidden": false,
                                "title": "",
                                "isLocked": false,
                                "condition": true,
                                "conditionGroup": ""
                            }
                        );
                    } else if (col.isCodeTable) {
                        formnode.children.push({
                            "componentName": "Select",
                            "props": {
                                "style": {
                                    "width": 200
                                },
                                "options": [
                                    {
                                        "label": "A",
                                        "value": "A"
                                    },
                                    {
                                        "label": "B",
                                        "value": "B"
                                    },
                                    {
                                        "label": "C",
                                        "value": "C"
                                    }
                                ],
                                "allowClear": false,
                                "autoFocus": false,
                                "defaultActiveFirstOption": true,
                                "disabled": formcoldisabled,
                                "labelInValue": false,
                                "showSearch": false,
                                "size": "middle",
                                "loading": false,
                                "bordered": true,
                                "filterOption": true,
                                "optionFilterProp": "value",
                                "tokenSeparators": [],
                                "isCodeTable":col.isCodeTable,
                                "codeTable":col.codeTable,
                                "bcFieldInfo": {
                                    "bcName": v,
                                    "bcField": col.fieldName
                                },
                                "bcName": v,
                                "bcField": col.fieldName,
                            }
                        });
                    } else if ((col.fieldType == 2 && col.length >= 2000) || col.fieldType == 6) {
                        formnode.children.push({
                            "componentName": "TextArea",
                            "props": {
                                "autoSize": {
                                    "minRows": 3,
                                    "maxRows": 3
                                },
                                "placeholder": "请输入",
                                "bordered": true,
                                "disabled": formcoldisabled,
                                "showCount": false,
                                "size": "middle",
                                "bcFieldInfo": {
                                    "bcName": v,
                                    "bcField": col.fieldName
                                },
                                "bcName": v,
                                "bcField": col.fieldName,
                            }
                        });
                    } else if (col.fieldType == 20) {
                        formnode.children.push({
                            "componentName": "Switch",
                            "props": {
                                "defaultChecked": false,
                                "autoFocus": false,
                                "disabled": false,
                                "loading": false,
                                "size": "default",
                                "bcFieldInfo": {
                                    "bcName": v,
                                    "bcField": col.fieldName
                                },
                                "bcName": v,
                                "bcField": col.fieldName,
                            }
                        });
                    } else if ([4, 10, 13, 14].includes(col.fieldType)) {
                        formnode.children.push({
                            "componentName": "InputNumber",
                            "props": {
                                "placeholder": "请输入",
                                "autoFocus": false,
                                "disabled": false,
                                "controls": true,
                                "bordered": true,
                                "size": "middle",
                                "bcFieldInfo": {
                                    "bcName": v,
                                    "bcField": col.fieldName
                                },
                                "bcName": v,
                                "bcField": col.fieldName,
                            }
                        });
                    }
                    else {
                        formnode.children.push({
                            "componentName": "Input",
                            "props": {
                                "placeholder": "请输入",
                                "bordered": true,
                                "disabled": formcoldisabled,
                                "size": "middle",
                                "bcFieldInfo": {
                                    "bcName": v,
                                    "bcField": col.fieldName
                                },
                                "bcName": v,
                                "bcField": col.fieldName,
                            }
                        });
                    }
                    lowformcolarray.push(formnode)
                }

                t.parent.getNode().children.importSchema(lowformcolarray);

            }
            onChange(v)
        };

        return (
            <Select style={{ width: '100%' }}
                value={value}
                onChange={bconChange}
                dataSource={this.state.bcds}
            ></Select>
        );
    }
}