import 'package:flutter/material.dart';
import 'package:getx_scaffold/getx_scaffold.dart';
import 'package:ln_jq_app/common/login_util.dart';
import 'package:ln_jq_app/pages/c_page/base_widgets/NativePageIOS.dart';
import 'package:ln_jq_app/pages/c_page/car_info/view.dart';

import 'package:ln_jq_app/pages/c_page/mine/view.dart';
import 'package:ln_jq_app/pages/c_page/reservation/view.dart';

import '../mall/mall_view.dart';
import 'index.dart';

class BaseWidgetsPage extends GetView<BaseWidgetsController> {
  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();
    _pageController.jumpToPage(controller.pageIndex);
  }

  List<Widget> _buildPages() {
    return [ReservationPage(), NativePageIOS(), MallPage(), CarInfoPage(), MinePage()];
  }

  // 自定义导航栏 (悬浮胶囊样式)
  Widget _buildNavigationBar() {
    return SafeArea(
      child: Container(
        height: Get.height * 0.05,
        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_map_select@2x", "ic_map@2x"),
            _buildNavItem(2, "ic_mall_select@2x", "ic_mall@2x"),
            _buildNavItem(3, "ic_car_select@2x", "ic_car@2x"),
            _buildNavItem(4, "ic_user_select@2x", "ic_user@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: const EdgeInsets.symmetric(horizontal: 20, 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<BaseWidgetsController>(
      init: BaseWidgetsController(),
      id: 'baseWidgets',
      builder: (_) {
        return Scaffold(
          extendBody: true, // 重要：让 body 延伸到导航栏后面
          resizeToAvoidBottomInset: false,
          bottomNavigationBar: _buildNavigationBar(),
          body: _buildView(), // 移除 SafeArea 以获得更好的全屏沉浸感
        );
      },
    );
  }
}
