import Header from './Header';
import BestSellers from './BestSellers';
import Taro from '@tarojs/taro';
import {useShareAppMessage, useShareTimeline} from "@tarojs/taro"
import {useEffect, useState} from "react";
import {getShopInfo} from "@/api/layout";
import {Sticky, Button} from '@nutui/nutui-react-taro'
import {View} from '@tarojs/components'
import Menu from "./Menu";
import Banner from "./Banner";
import './index.scss'
import Grid from "@/pages/index/Grid";

// import GoodsList from "./GoodsList";

function Home() {
  // 吸顶状态
  const [stickyStatus, setStickyStatus] = useState<boolean>(false)

  useShareTimeline(() => {
    return {
      title: '网宿小店 - 网宿软件',
      path: `/pages/index/index`
    };
  });

  useShareAppMessage(() => {
    return {
      title: '网宿小店 - 网宿软件',
      path: `/pages/index/index`,
      success: function () {
        console.log('分享成功');
      },
      fail: function () {
        console.log('分享失败');
      }
    };
  });

  // const reloadMore = async () => {
  //   setPage(page + 1)
  // }

  const showAuthModal = () => {
    Taro.showModal({
      title: '授权提示',
      content: '需要获取您的用户信息',
      confirmText: '去授权',
      cancelText: '取消',
      success: (res) => {
        if (res.confirm) {
          // 用户点击确认，打开授权设置页面
          openSetting();
        }
      }
    });
  };

  const openSetting = () => {
    // Taro.openSetting：调起客户端小程序设置界面，返回用户设置的操作结果。设置界面只会出现小程序已经向用户请求过的权限。
    Taro.openSetting({
      success: (res) => {
        if (res.authSetting['scope.userInfo']) {
          // 用户授权成功，可以获取用户信息
          reload();
        } else {
          // 用户拒绝授权，提示授权失败
          Taro.showToast({
            title: '授权失败',
            icon: 'none'
          });
        }
      }
    });
  };

  const onSticky = (item: IArguments) => {
    if(item){
      setStickyStatus(!stickyStatus)
    }
  }

  const reload = () => {

  };

  useEffect(() => {
    // 获取站点信息
    getShopInfo().then(() => {

    })
    // Taro.getSetting：获取用户的当前设置。返回值中只会出现小程序已经向用户请求过的权限。
    Taro.getSetting({
      success: (res) => {
        if (res.authSetting['scope.userInfo']) {
          // 用户已经授权过，可以直接获取用户信息
          console.log('用户已经授权过，可以直接获取用户信息')
          reload();
        } else {
          // 用户未授权，需要弹出授权窗口
          console.log('用户未授权，需要弹出授权窗口')
          showAuthModal();
        }
      }
    });
    // 获取用户信息
    Taro.getUserInfo({
      success: (res) => {
        const avatar = res.userInfo.avatarUrl;
        console.log(avatar, 'avatarUrl')
      }
    });
  }, []);

  return (
    <>
      <Sticky threshold={0} onChange={() => onSticky(arguments)}>
        <Header stickyStatus={stickyStatus}/>
      </Sticky>
      <div className={'flex flex-col mt-1'}>
        <Menu/>
        <Banner/>
        <BestSellers/>
        <Grid />
        {/*<GoodsList/>*/}
      </div>
    </>
  )
}

export default Home
