import { useRequest } from 'umi';
import CustomTree from '../CustomTree';
import { useEffect } from 'react';
const List = ({
  type,
  appId,
  show,
  disabled = false,
  isLevel = true,
  value,
  onChange,
}: {
  type:
    | '_UacPermissionFeature'
    | '_UacPermissionFlag'
    | '_UacPermissionPolicy'
    | '_UacPermissionRole'
    | '_UacPermissionTemplate';
  appId: number;
  show: boolean;
  disabled?: boolean;
  isLevel?: boolean;
  value?: any;
  onChange?: () => void;
}) => {
  const { data, run } = useRequest(
    {
      url: `/goapi/app/resource`,
      method: 'POST',
      data: {
        condition: {
          type,
          appId,
        },
        pagination: {
          page: 0,
          size: 0,
        },
      },
    },
    { manual: true },
  );
  const formatTreeData = (d: any) => {
    const { child, ...others } = d;
    return {
      ...others,
      children: child?.map(formatTreeData) || [],
    };
  };
  useEffect(() => {
    if (show) {
      run();
    }
  }, [show, run]);
  return (
    <CustomTree
      style={{ width: '100%' }}
      isLevel={isLevel}
      disabled={disabled}
      treeData={data?.list?.map((item) => formatTreeData(item)) || []}
      value={value}
      onChange={onChange}
    />
  );
};

export default List;
