import 'package:azlistview/azlistview.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:liftmanager/internal/project/model/worker_entity.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/toast.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:liftmanager/widgets/select_worker.dart';

/// 可查看/单选/多选
enum SelectType { NONE, SINGLE, MANY }

class ProjectUsersPage extends StatefulWidget {
  ProjectUsersPage(this.projectId, this.type);

  final String projectId;
  final SelectType type;

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

class ProjectUsersPageState extends State<ProjectUsersPage> {
  List<WorkerItem> _list = [];

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

  ///获取项目用户
  getProjectUser() {
    ApiService(context: context).projectUser(widget.projectId,
        onSuccess: (data) {
      if (data != null) {
        _list.addAll(data);
      } else {}
      setState(() {});
    }, onError: (code, msg) {
      toasts(msg);
    });
  }

  ///点击item
  _clickItem(WorkerItem model) {
    switch (widget.type) {
      case SelectType.NONE:
        break;
      case SelectType.SINGLE:
        NavigatorUtils.goBackWithParams(
            context, {"id": model.userId, "name": model.name});
        break;
      case SelectType.MANY:
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: MyAppBar(
          centerTitle: "维保人员",
          actions: <Widget>[
            widget.type == SelectType.MANY
                ? FlatButton(
                    onPressed: () {},
                    child: Text(
                      "确定",
                      key: const Key('actionName'),
                      style: TextStyle(color: Colors.white),
                    ),
                    textColor: Colours.text,
                    highlightColor: Colors.transparent)
                : Container(),
          ],
        ),
        body: SafeArea(
          child: AzListView(
            data: _list,
            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);
            },
          ),
        ));
  }

  bool test = false;

  Widget _buildListItem(WorkerItem model) {
    return InkWell(
      onTap: () {
        _clickItem(model);
      },
      child: Container(
        child: Container(
          decoration: BoxDecoration(
              border: (model.isShowSuspension)
                  ? Border(
                      top: Divider.createBorderSide(context, width: 0.6),
                    )
                  : null),
          child: SelectWorker(
            avatar: model.avatarUrl,
            title: model.name,
            isCheckbox: true,
            isChecked: test,
//            onTap: () {
//              setState(() {
//                test = !test;
//              });
//            },
//            onClick: (bool bol) {
//              setState(() {
//                test = bol;
//              });
//            },
          ),
        ),
      ),
    );
  }
}
