import Header from './Header';
import Taro from '@tarojs/taro';
import {useShareAppMessage, useShareTimeline} from "@tarojs/taro"
import {useEffect, useState} from "react";
import {getShopInfo} from "@/api/layout";
import {View, Text} from '@tarojs/components'
import Grid from "@/pages/index/Grid";
import Banner from "./Banner";
import PopUpAd from "@/pages/index/PopUpAd";
import TrustSection from "./TrustSection";
import ContactSection from "./ContactSection";
import CatalogShowcase from "./CatalogShowcase";
import {configWebsiteField} from "@/api/cms/cmsWebsiteField";
import type {Config} from "@/api/cms/cmsWebsiteField/model";
import './index.scss'

function Home() {
  const [config, setConfig] = useState<Config>()

  useShareTimeline(() => {
    return {
      title: '南南佐顿门窗 - 网宿软件',
      path: `/pages/index/index`
    };
  });

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

  const showAuthModal = () => {
    Taro.showModal({
      title: '授权提示',
      content: '需要获取您的用户信息',
      confirmText: '去授权',
      cancelText: '取消',
      success: (res) => {
        if (res.confirm) {
          openSetting();
        }
      }
    });
  };

  const openSetting = () => {
    Taro.openSetting({
      success: (res) => {
        if (res.authSetting['scope.userInfo']) {
          reload();
        } else {
          Taro.showToast({
            title: '授权失败',
            icon: 'none'
          });
        }
      }
    });
  };

  const reload = () => {

  };

  useEffect(() => {
    getShopInfo().then(() => {})
    configWebsiteField({}).then(data => {
      setConfig(data)
    })
    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 (
    <>
      <Header/>
      <View className={'home-page'}>

        {/* ═══ Hero 区域：Banner 融入头部渐变背景 ═══ */}
        <View className={'hero-zone my-2'}>
          <View className={'hero-banner-wrap'}>
            <Banner />
          </View>

          {/* 功能菜单 — 直接铺在Banner下方，无卡片包裹 */}
          <View className={'hero-grid my-4'}>
            <Grid />
          </View>
        </View>

        {/* ═══ 滚动公告条 — 轻量嵌入 ═══ */}
        {/*<View className={'notice-strip my-4'}>*/}
        {/*  <View className={'notice-strip__dot'} />*/}
        {/*  <Text className="text-15">*/}
        {/*    {config?.NoticeBar || '南南佐顿门窗，专业门窗定制安装服务，10年质保，德国进口五金，5000+家庭选择'}*/}
        {/*  </Text>*/}
        {/*</View>*/}

        {/* ═══ 品牌画册 — 沉浸式视觉卡片 ═══ */}
        <CatalogShowcase />

        {/* ═══ 品质信任区 — 横向滑动卡片 ═══ */}
        <TrustSection />

        {/* ═══ 联系我们 — 全宽底部 ═══ */}
        <ContactSection />

      </View>
      <PopUpAd />
    </>
  )
}

export default Home
