import 'package:app/providers/user_store.dart';
import 'package:app/router/route_paths.dart';
import 'package:app/utils/time.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';
import 'package:remixicon/remixicon.dart';

class UserHeader extends StatelessWidget implements PreferredSizeWidget {
  const UserHeader({super.key});

  @override
  Widget build(BuildContext context) {
    final userStore = context.read<UserStore>();
    return AppBar(
      title: const Text('学光自习室'),
      actions: [
        Container(
          padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 8),
          decoration: BoxDecoration(
            gradient: const LinearGradient(
              colors: [Color(0xffffb900), Color(0xffff8904)],
              begin: Alignment.centerLeft,
              end: Alignment.centerRight,
            ),
            borderRadius: BorderRadius.circular(20),
          ),
          child: Row(
            spacing: 5,
            children: [
              const Icon(
                RemixIcons.vip_crown_line,
                color: Colors.white,
                size: 18,
              ),
              Text(
                "会员至 ${formatDate(userStore.userInfo?.extraInfo.vipEndTime,'YYYY-MM-DD')}",
                style: TextStyle(color: Colors.white, fontSize: 14),
              ),
            ],
          ),
        ),
        PopupMenuButton(
          color: Colors.white,
          padding: EdgeInsets.zero,
          position: PopupMenuPosition.under,
          onSelected: (value) {
            if (value == 1) {
              UserStore userStore = context.read<UserStore>();
              userStore.logout();
              context.go(RoutePaths.login);
            }
          },
          itemBuilder: (context) => [
            PopupMenuItem(
              value: 1,
              child: Text("退出登录", textAlign: TextAlign.center),
            ),
          ],
          child: IconButton(
            onPressed: null,
            icon: Icon(RemixIcons.user_line),
          ),
        ),
      ],
    );
  }

  @override
  Size get preferredSize => const Size.fromHeight(kToolbarHeight);
}
