/**
 * 行政区划
 */
import React, { forwardRef, useEffect, useState } from 'react';
import { VtxZtreeSelect } from '@vtx/components';
import { request } from '@vtx/utils';
import '@vtx/components/lib/vtx-ztree-select/style/index';

//行政区划树
const getDivisionTree = params => {
    return request.get('/cloud/management/api/v101/tree/loadDivisionTree', {
        body: { parameters: JSON.stringify(params) },
    });
};

const init = async () => {
    const res = await getDivisionTree({
        isControlPermission: true,
    });
    let tree = [];
    if (res?.result === 0 && res?.data) {
        tree = [res.data] || [];
    }
    return tree;
};

function Division(props, ref) {
    const [divisions, setDivisions] = useState([]);

    useEffect(() => {
        init().then(res => {
            setDivisions(res);
        });
    }, []);

    return (
        <VtxZtreeSelect
            ref={ref}
            data={divisions}
            treeDefaultExpandAll
            showSearch
            placeholder="请选择"
            {...props}
        />
    );
}

export default forwardRef(Division);
