import 'package:flutter/material.dart';
import 'package:getx_scaffold/common/index.dart';
import 'package:getx_scaffold/getx_scaffold.dart';
import 'package:ln_jq_app/common/login_util.dart';
import 'package:ln_jq_app/pages/b_page/base_widgets/controller.dart';
import 'package:ln_jq_app/pages/b_page/reservation/view.dart';
import 'package:ln_jq_app/pages/b_page/site/view.dart';

class B_BaseWidgetsPage extends GetView<B_BaseWidgetsController> {
  B_BaseWidgetsPage({super.key});

  final PageController _pageController = PageController();

  // 主视图
  Widget _buildView() {
    return PageView(
      controller: _pageController,
      physics: const NeverScrollableScrollPhysics(), // 禁止滑动
      onPageChanged: (index) {
        jumpTabAndPage(index);
      },
      children: _buildPages(),
    );
  }

  void jumpTabAndPage(int index) {
    controller.pageIndex = index; // 更新页面索引
    controller.updateUi(); // 更新 UI
    _pageController.jumpToPage(controller.pageIndex);
  }

  // 对应的页面
  List<Widget> _buildPages() {
    return [SitePage(), ReservationPage()];
  }

  // 自定义导航栏 (悬浮胶囊样式)
  Widget _buildNavigationBar() {
    return SafeArea(
      child: Container(
        height: 50.h,
        margin: const EdgeInsets.fromLTRB(24, 0, 24, 10), // 悬浮边距
        decoration: BoxDecoration(
          color: Color.fromRGBO(240, 244, 247, 1), // 浅灰色背景
          borderRadius: BorderRadius.circular(30),
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.05),
              blurRadius: 10,
              offset: const Offset(0, 5),
            ),
          ],
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            _buildNavItem(0, "ic_h2_select@2x", "ic_h2@2x"),
            _buildNavItem(1, "ic_h2_my@2x", "ic_h2_my_select@2x"),
          ],
        ),
      ),
    );
  }

  // 构建单个导航项
  Widget _buildNavItem(int index, String icon, String selectedIcon) {
    bool isSelected = controller.pageIndex == index;
    return GestureDetector(
      onTap: () => jumpTabAndPage(index),
      behavior: HitTestBehavior.opaque,
      child: AnimatedContainer(
        duration: const Duration(milliseconds: 200),
        padding:  EdgeInsets.symmetric(horizontal: 50.w, vertical: 8),
        decoration: BoxDecoration(
          color: isSelected ? const Color(0xFF006633) : Colors.transparent, // 选中时的深绿色背景
          borderRadius: BorderRadius.circular(20),
        ),
        child: SizedBox(
          height: 24,
          width: 24,
          child: LoginUtil.getAssImg(isSelected ? selectedIcon : icon),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<B_BaseWidgetsController>(
      init: B_BaseWidgetsController(),
      id: 'b_baseWidgets',
      builder: (_) {
        return Scaffold(
          extendBody: true,
          resizeToAvoidBottomInset: false,
          bottomNavigationBar: _buildNavigationBar(),
          body: _buildView(),
        );
      },
    );
  }
}
