import * as WebBrowser from 'expo-web-browser';
import * as React from 'react';
import { Div, Button, Image, Text, Avatar } from 'react-native-magnus';
import { ScrollView } from 'react-native-gesture-handler';

import { useRequest } from '@umijs/hooks';
import useModel from 'flooks';
import User from '../../flooks/User'; // detail模块通用方法

import Header from '../../components/Header';
import MerchantCom from '../Main/Home/MerchantCom';

export default function CollectionScreen() {
  const { id } = useModel(User, ['id']);
  const [Collection, setCollection] = React.useState([]);
  const CollectionRequest = useRequest(
    () => {
      const params = {
        query: {
          userId: id,
          page: 0,
          size: 100,
        },
      };
      const urls = Object.keys(params).map((item) => {
        return `${item}=${encodeURI(JSON.stringify(params[item]))}`;
      });
      return `/myCollection/all?${urls.join('&')}`;
    },
    {
      refreshDeps: [id],
      onSuccess: (result) => {
        setCollection(result.content || []);
      },
    }
  );
  return (
    <>
      <Header title="我的收藏" />
      <ScrollView
        contentContainerStyle={{ backgroundColor: '#fff', flexGrow: 1 }}
      >
        {Collection.length === 0 && (
          <Div p={10}>
            <Text color="gray300" textAlign="center">
              暂无数据
            </Text>
          </Div>
        )}

        {Collection.map((item) => {
          return (
            <MerchantCom
              key={item.id}
              CollectionId={item.id}
              isCollection
              info={item.merchant}
              freash={CollectionRequest.run}
            />
          );
        })}
      </ScrollView>
    </>
  );
}
