import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:liftmanager/internal/heavy/model/heavy_user_entity.dart';
import 'package:liftmanager/net/api_service.dart';
import 'package:liftmanager/utils/theme_utils.dart';
import 'package:liftmanager/utils/toast.dart';
import 'package:liftmanager/widgets/app_bar.dart';

class HeavyUserPage extends StatefulWidget {
  HeavyUserPage(this.projectId);

  String projectId;

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

class HeavyUserPageState extends State<HeavyUserPage> {
  int pageNum = 1;
  int pageSize = 1000;

  List<HeavyUserEntity> _userList = [];

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

  void showDetail(int i) {
    showDialog(
      barrierDismissible: false,
      context: context,
      builder: (context) => new AlertDialog(
        backgroundColor: ThemeUtils.getTabsBg(context),
        content: Container(
          width: 295,
          height: 350,
          child: ListView.separated(
            shrinkWrap: true,
            itemCount: _userList[i].attendanceRecord.length,
            separatorBuilder: (BuildContext context, int index) {
              return Container(
                height: 0.5,
                color: Color(0xFFF5F5F5),
              );
            },
            itemBuilder: (_, index) {
              return InkWell(
                  child: Container(
                color: Colors.white,
                padding: const EdgeInsets.symmetric(
                    horizontal: 15.0, vertical: 12.0),
                alignment: Alignment.centerLeft,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: <Widget>[
                    Text("${_userList[i].attendanceRecord[index].signDate}",style: TextStyle(fontSize: 15,fontWeight: FontWeight.w800),),
                    Text(
                        "入场时间：${_userList[i].attendanceRecord[index].approachTime}"),
                    Text(
                        "离场时间：${_userList[i].attendanceRecord[index].leavingTime}"),
                  ],
                ),
              ));
            },
          ),
        ),
        actions: <Widget>[
          new FlatButton(
            onPressed: () {
              setState(() {});
              Navigator.pop(context);
            },
            child: new Text("关闭"),
          ),
        ],
      ),
    );
  }

  ///获取团队用户列表
  _loadData() {
    ApiService(context: context).heavyUserList(widget.projectId, false,"",
        onSuccess: (res) {
      _userList = res;
      setState(() {});
    }, onError: (code, msg) {
      toasts(msg);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        centerTitle: "考勤列表",
      ),
      body: SafeArea(
        child: ListView.separated(
          shrinkWrap: true,
          itemCount: _userList.length,
          separatorBuilder: (BuildContext context, int index) {
            return Container(
              height: 0.5,
              color: Color(0xFFF5F5F5),
            );
          },
          itemBuilder: (_, index) {
            return InkWell(
                onTap: () {
                  showDetail(index);
                },
                child: Container(
                  color: Colors.white,
                  padding: const EdgeInsets.symmetric(
                      horizontal: 15.0, vertical: 12.0),
                  alignment: Alignment.centerLeft,
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Text("${_userList[index].userName}"),
                      Text("查看")
                    ],
                  ),
                ));
          },
        ),
      ),
    );
  }

//  _listWidget(_userList[index])
  List<Widget> _listWidget(HeavyUserEntity item) {
    List<Widget> list = [];
    list.add(Text("${item.userName}"));

    for (int i = 0; i < item.attendanceRecord.length; i++) {
      list.add(Container(
        color: ThemeUtils.getTabsBg(context),
        padding: const EdgeInsets.symmetric(horizontal: 15.0, vertical: 12.0),
        alignment: Alignment.centerLeft,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            Text("入场时间：${item.attendanceRecord[i].approachTime}"),
            Text("离场时间：${item.attendanceRecord[i].leavingTime}")
          ],
        ),
      ));
    }
    return list;
  }
}
