import React, {useState, useEffect} from 'react';
import {Alert} from 'react-native';
import HistoryView from './HistoryView';
import {getListTransaction} from '../../../apis/Functions/Widthdraw';

const History = (props) => {
  const [selected, setSelected] = useState('');
  const [page, setPage] = useState(1);
  const [data, setData] = useState([]);
  const [tottalPage, setTotalPage] = useState(1);
  const [isRefresh, setisRefresh] = useState(false);
  const [fillters, setFillters] = useState([]);

  useEffect(() => {
    getData();
  }, []);

  // useEffect(() => {
  //   getData();
  // }, [selected]);

  const getData = async () => {
    setisRefresh(true);
    setPage(1);
    const res = await getListTransaction({
      keyword: '',
      type: 'ALL',
      platform: Platform.OS,
      page_size: 10,
      page_index: 1,
      start_date: '',
      end_date: '',
    });
    console.log(res);
    setisRefresh(false);
    if ((res.data.code = 200 && res.data.data)) {
      setData(res.data.data);
      setTotalPage(res.data.meta.pages);
    } else {
      Alert.alert('Thông báo!', res.data.message);
    }
  };

  const onRefresh = () => {
    getData();
  };

  const onLoadMore = () => {
    console.log(tottalPage);
    // if (page < tottalPage) getDataLoadMore();
  };

  const getDataLoadMore = async () => {
    setisRefresh(true);
    const res = await getListNew({
      keyword: '',
      category_id: selected,
      platform: Platform.OS,
      page_size: 10,
      page_index: page + 1,
    });
    setPage(page + 1);

    if (res.data.code == 200) {
      setData(data.concat(res.data.data));
    }
    setisRefresh(false);
  };

  return (
    <HistoryView
      onLoadMore={onLoadMore}
      isRefresh={isRefresh}
      onRefresh={onRefresh}
      data={data}
    />
  );
};

export default History;
