import { Apis } from '@/gen/Apis';
import { Transfer } from 'antd';
import { useEffect, useState } from 'react';
const MyTransferProject = (props: any) => {
  const [getLoading, setLoading] = useState(false);
  const [dataSource, setTransferData] = useState<any[]>([]);
  const [targetKeys, setTargetKeys] = useState<string[]>([]);
  useEffect(() => {
    Apis.Asset.AssetProjects.Select({})
      .then((res) => {
        setLoading(true);
        const data =
          res.data?.map((item: any) => ({
            key: item.value?.toString(),
            title: item.label,
          })) || [];
        setTransferData(data);
      })
      .catch(() => {
        setTransferData([]);
      });
  }, []);

  useEffect(() => {
    console.log(props.value, 'props.value');
    if (props.value?.length) {
      setTargetKeys(props.value);
    }
  }, [props.value]);

  return (
    getLoading && (
      <Transfer
        dataSource={dataSource}
        targetKeys={targetKeys}
        onChange={(targetKeys) => {
          let dataIds: any = [];
          console.log(targetKeys, 'targetKeys', props.value);
          targetKeys?.forEach((res: any) => {
            dataSource?.forEach((k: any) => {
              if (res === k.key) {
                dataIds?.push(k?.key);
              }
            });
          });
          setTargetKeys(targetKeys as string[]);
          props?.onChange?.(dataIds);
        }}
        render={(item) => item.title}
        titles={['可选项目', '已选项目']}
        showSearch
        listStyle={{
          width: 250,
          height: 300,
        }}
        operations={['选择', '移除']}
        operationStyle={{ marginTop: 20 }}
        locale={{
          itemUnit: '项',
          itemsUnit: '项',
          searchPlaceholder: '请输入搜索内容',
          notFoundContent: '列表为空',
        }}
        onSelectChange={(sourceSelectedKeys, targetSelectedKeys) => {
          console.log(
            sourceSelectedKeys,
            targetSelectedKeys,
            'sourceSelectedKeys',
          );
          // 处理选择变化，但不触发表单提交
        }}
      />
    )
  );
};

export default MyTransferProject;
