import { FormDatas } from "./interface";
import { useEffect, useState } from "react";
import { base } from "@/service/base";
import { Form, TreeSelect } from 'antd';

export const FormTreeSelect = (v: FormDatas) => {
    const [list, setList] = useState<any>([]);
    useEffect(() => {
        if (v.selectList && v.selectList.length > 0) {
            setList(v.selectList);
        } else {
            base.get(`${v.selectUrl}/?size=50&offset=1`, "").then((res) => {
                setList(res.data.record ?? []);
            });
        }
    }, [v.selectUrl, v.selectList]);
    return (
        <Form.Item
            key={v.label}
            label={v.label}
            name={v.name}
            rules={v.rules}
        >
            <TreeSelect
                key={v.name}
                fieldNames={{
                    label: "name",
                    value: "identity",
                    children: "children"
                }}
                multiple={v.treeMultiple}
                showSearch
                style={{ width: '100%' }}
                placeholder="请选择"
                allowClear
                treeDefaultExpandAll
                treeData={list}
            />
        </Form.Item>
    );
};
