import React, { useState, useEffect, useCallback, useRef } from 'react';
import { Spin, Button } from 'antd';
import ListCardItem from './listCardItem';
import { get, post } from '../../services';
import { orgTest } from '@/services/orgnazation/api';

const tip = 'loading...';
const ListCard = props => {
  const { ouid, searchWord, valueCallback, onCommit = () => {} } = props;
  const [valueList, setValueList] = useState({});
  const [dataList, setdataList] = useState([]);
  const [loading, setLoading] = useState(true);

  const getValueCallback = useCallback((value, index) => {
    valueList[index] = value;
    setValueList({ ...valueList });
    valueCallback({ ...valueList });
  }, []);

  useEffect(() => {
    setLoading(true);
    const defaultConfig = {
      optionsList: [],
      title: '默认组',
      id: '',
    };
    //  /Cityinterface/rest/services/OMS.svc/U_GetUserListForBatchOper
    get('/Cityinterface/rest/services/OMS.svc/P_GetUserByStation', {
      // OUID:ouid||'',
      stationID: ouid || '',
      _version: 9999,
      _dc: new Date().getTime(),
    })
      .then(res => {
        setLoading(false);
        const list = [];
        // eslint-disable-next-line no-unused-expressions
        res &&
          res.forEach(item => {
            list.push({ ...defaultConfig, ...item });
          });
        setdataList(list);
      })
      .catch(err => {
        console.error(err);
        setLoading(false);
      });
    // 多级嵌套测试
    // orgTest().then(res =>{
    //   console.log(res,'res')
    // })
  }, [ouid]);
  return (
    <div>
      {loading ? (
        <Spin
          size="large"
          spinning={loading}
          tip={tip}
          style={{
            position: 'absolute',
            top: '30%',
            left: '0',
            right: '0',
            bottom: '0',
          }}
        />
      ) : (
        dataList &&
        dataList.length > 0 &&
        dataList.map((item, index) => (
          <ListCardItem
            {...item}
            itemid={index}
            key={`item${index}key`}
            getValueCallback={getValueCallback}
            searchWord={searchWord}
            {...props}
          />
        ))
      )}
      {true && !loading && dataList && (
        <Button
          type="primary"
          onClick={() => onCommit()}
          style={{ marginTop: '20px' }}
        >
          提交
        </Button>
      )}
    </div>
  );
};

export default ListCard;