import 'package:azlistview/azlistview.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:liftmanager/internal/team/model/team_user_entity.dart';
import 'package:liftmanager/internal/team/team_router.dart';
import 'package:liftmanager/net/api_service.dart';
import 'package:liftmanager/res/resources.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/utils/image_utils.dart';
import 'package:liftmanager/utils/theme_utils.dart';
import 'package:liftmanager/utils/toast.dart';
import 'package:liftmanager/widgets/app_bar.dart';

class TeamUserPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return TeamUserPageState();
  }
}

class TeamUserPageState extends State<TeamUserPage> {
  int pageNum = 1;
  int pageSize = 1000;

  List<UserItem> _userList = [];

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

  ///获取团队用户列表
  _loadData() {
    ApiService(context: context).teamUserList(pageNum, pageSize,
        onSuccess: (res) {
      _userList = res.rows;
      _userList.sort(
          (a, b) => (a.getSuspensionTag()).compareTo(b.getSuspensionTag()));
      setState(() {});
    }, onError: (code, msg) {
      toasts(msg);
    });
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        onWillPop: () {
          NavigatorUtils.goBackWithParams(context, true);
          return Future.value(false);
        },
        child: Scaffold(
          appBar: MyAppBar(
            centerTitle: "团队成员",
          ),
          body: SafeArea(
            child: AzListView(
              data: _userList,
              itemBuilder: (context, model) => _buildListItem(model),
              isUseRealIndex: true,
              itemHeight: 40,
              suspensionWidget: null,
              suspensionHeight: 0,
              indexBarBuilder: (context, list, onTouch) {
                return IndexBar(
                    onTouch: onTouch,
                    data: list,
                    itemHeight: 18,
                    touchDownColor: Colors.transparent,
                    textStyle: Theme.of(context).textTheme.subtitle);
              },
            ),
          ),
        ));
  }

  Widget _buildListItem(UserItem model) {
    bool isDark = ThemeUtils.isDark(context);
    return InkWell(
      onTap: () {
        NavigatorUtils.push(context,
            "${TeamRouter.teamUserDetailPage}?name=${Uri.encodeComponent(model.userName)}&remark=${Uri.encodeComponent(model.userRemarks)}&role=${Uri.encodeComponent(model.userRoleName)}&logoImg=${Uri.encodeComponent(model.userAvatarUrl)}&isCertificated=${model.liftCertificate.status}&certificateType=${Uri.encodeComponent(model.liftCertificate.certificateType)}&expirationDate=${Uri.encodeComponent(model.liftCertificate.expirationDate)}&issuanceAgency=${Uri.encodeComponent(model.liftCertificate.issuanceAgency)}");
      },
      child: Container(
        color: ThemeUtils.getTabsBg(context),
        padding: const EdgeInsets.only(left: 16.0, right: 34.0),
        height: 67.0,
        child: Container(
          decoration: BoxDecoration(
              border: (model.isShowSuspension)
                  ? Border(
                      top: Divider.createBorderSide(context, width: 0.6),
                    )
                  : null),
          child: Row(
            children: <Widget>[
              CircleAvatar(
                  radius: 21.5,
                  backgroundColor: Colors.transparent,
                  backgroundImage:
                      ImageUtils.getImageProvider(model.userAvatarUrl)),
              Gaps.hGap10,
              Expanded(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Row(
                      children: <Widget>[
                        Text(
                          model.userName,
                          style: TextStyle(
                              fontSize: 15,
                              color: isDark ? Colours.dark_text : Colours.text),
                        ),
                        Gaps.hGap10,
                        Container(
                          decoration: BoxDecoration(
                            color: Colours.app_main,
                            borderRadius: BorderRadius.circular(6.0),
                          ),
                          padding: EdgeInsets.fromLTRB(5, 0, 5, 0),
                          child: Text(
                            model.userRoleName,
                            style: TextStyle(fontSize: 12, color: Colors.white),
                          ),
                        )
                      ],
                    ),
                    Text(
                      model.userMobile,
                      style: TextStyles.textGray13,
                    )
                  ],
                ),
              ),
              Text(
                model.liftCertificate?.status == 1
                    ? "待审核"
                    : model.liftCertificate?.status == 2
                        ? "审核未通过"
                        : model.liftCertificate?.status == 3
                            ? "审核通过"
                            : model.liftCertificate?.status == 3 ? "超期" : "无证",
                style: TextStyle(
                    fontSize: 15,
                    color: model.liftCertificate?.status == 1
                        ? Color(0xFF01AFFF)
                        : model.liftCertificate?.status == 2
                            ? Color(0xFF00A863)
                            : model.liftCertificate?.status == 3
                                ? Color(0xFF01AFFF)
                                : model.liftCertificate?.status == 3
                                    ? Color(0xFFFF3B301)
                                    : Color(0xFF999999)),
              ),
            ],
          ),
        ),
      ),
    );
  }
}