import 'package:flutter/material.dart';
import 'package:liftmanager/net/api_service.dart';
import 'package:liftmanager/utils/toast.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:provider/provider.dart';
import 'package:liftmanager/internal/account/provider/user_provider.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:liftmanager/widgets/load_image.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/common/common.dart';
import 'package:liftmanager/internal/account/account_router.dart';
import 'package:liftmanager/utils/time_format.dart';
import 'package:liftmanager/res/resources.dart';
import 'package:liftmanager/internal/wode/model/privilege_model.dart';

class Privilege extends StatefulWidget {
  Privilege(this.id);

  final String id;

  @override
  State<StatefulWidget> createState() {
    return PrivilegeState();
  }
}

class PrivilegeState extends State<Privilege>
    with AutomaticKeepAliveClientMixin {
  UserProvider provider = UserProvider();

  List<MenuList> privilegeList = [];

  @override
  void initState() {
    getAllMenu();
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  void getAllMenu() {
    NewApiService().getAllMenu(widget.id, onSuccess: (res) {
      privilegeList = res;
      setState(() {});
    }, onError: (code, msg) {
      privilegeList = [];
      toasts(msg);
    });
  }

  List<Widget> listWidget(context) => privilegeList.map((item) {
        double width = MediaQuery.of(context).size.width;
        return GestureDetector(
          child: Container(
            padding: EdgeInsets.only(
              left: ScreenUtil().setWidth(15),
              right: ScreenUtil().setWidth(15),
              top: ScreenUtil().setHeight(10),
              bottom: ScreenUtil().setHeight(10),
            ),
            decoration: BoxDecoration(
              border: Border(
                bottom: BorderSide(width: 0.5, color: Colours.line),
              ),
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
                  item.name ?? '',
                  textAlign: TextAlign.left,
                  style: TextStyle(fontSize: ScreenUtil().setSp(15)),
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                ),
                Text(
                  item.descr ?? '',
                  textAlign: TextAlign.right,
                  style: TextStyle(
                    color: Color(0xff999999),
                    fontSize: ScreenUtil().setSp(14),
                  ),
                )
              ],
            ),
          ),
          onTap: () {
            // print(item);
          },
        );
      }).toList();

  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;
    double height = MediaQuery.of(context).size.height;

    return ChangeNotifierProvider<UserProvider>(
      create: (_) => provider,
      child: Container(
        child: Scaffold(
          resizeToAvoidBottomPadding: false,
          appBar: MyAppBar(
            centerTitle: "会员特权",
          ),
          body: Consumer<UserProvider>(
            builder: (_, provider, __) {
              return ListView(
                padding: EdgeInsets.all(0.0),
                children: listWidget(context),
              );
            },
          ),
        ),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
