import React, { useEffect, useState, forwardRef } from 'react';
import { VtxSelect } from '@vtx/components';
import { request } from '@vtx/utils';
import PropTypes from 'prop-types';
import useSettings from '../vm-hooks/useSettings';
const { Option } = VtxSelect;

// 获取参数配置
const getTypeCode = params => {
    return request.get('/cloud/management/rest/param/getByParamTypeCode.read', {
        body: {
            parameters: JSON.stringify(params),
        },
    });
};

const ParameterSelect = ({ code, ...restProps }, ref) => {
    const [dataList, setDataList] = useState([]);
    const { theme } = useSettings();
    useEffect(() => {
        init().then(data => {
            setDataList(data);
        });
    }, []);

    async function init() {
        if (code) {
            const res = await getTypeCode({
                paramTypeCode: code,
            });
            if (res?.result === 0 && Array.isArray(res.data)) {
                return res.data;
            }
            return [];
        }
    }

    return (
        <VtxSelect ref={ref} {...restProps} dropdownClassName={theme}>
            {dataList.map(item => (
                <Option key={item.parmCode}>{item.parmName}</Option>
            ))}
        </VtxSelect>
    );
};

ParameterSelect.propTypes = {
    code: PropTypes.string,
};

export default forwardRef(ParameterSelect);
