/*
 * @Author: WuWei
 * @Date: 2020-07-16 09:42:16
 * @LastEditors  : WuWei
 * @LastEditTime : 2023-05-16 17:11:32
 * @Description:
 */
import 'package:app_package/app_package.dart';
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import '../service/impl/repair_service_impl.dart';
import '../service/repair_service.dart';
// import '../view_models/private_detail_model.dart';
import '../models/repair_person_list.dart';

class HandleChoosePerson extends StatefulWidget {
  HandleChoosePerson({
    Key? key,
    this.initPerson,
  }) : super(key: key);

  final RepairPersonListModel? initPerson;

  static Future<RepairPersonListModel?> show(
      context, RepairPersonListModel? initPerson) async {
    return await showModalBottomSheet<RepairPersonListModel>(
      context: context,
      backgroundColor: Colors.transparent,
      builder: (context) {
        return HandleChoosePerson(initPerson: initPerson);
      },
    );
  }

  @override
  _HandleChoosePersonState createState() => _HandleChoosePersonState();
}

class _HandleChoosePersonState extends State<HandleChoosePerson> {
  List<RepairPersonListModel>? personList;
  final RepairService _repairService = RepairServiceImpl();
  final communityGlobalStore = GetIt.I.get<CommunityGlobalStore>();

  ///从储存中取出的当前小区 拿出来可能是空
  CommunityModel? get currentCommunity => communityGlobalStore.data;

  /// 受理人列表
  Future getPerson() async {
    var data = await _repairService
        .getPersonList({'communityId': currentCommunity!.id});
    setState(() {
      personList = data.data;
    });
  }

  String? chosenPerson;

  /// 选择受理人
  choosePerson(value) async {
    chosenPerson = value;
    setState(() {});
    for (RepairPersonListModel item in personList!) {
      if (item.id == chosenPerson) {
        await Future.delayed(Duration(milliseconds: 300));
        Navigator.pop(context, item);
      }
    }
  }

  @override
  void initState() {
    setState(() {
      chosenPerson = widget.initPerson?.id;
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    return WisBottomModal(
      title: Text('维修师傅'),
      body: WisScrollView(
        onInit: () => getPerson(),
        slivers: <Widget>[
          WisSliverList.childBuilder(
              childCount: personList?.length ?? 0,
              builder: (_, i) {
                var item = personList![i];
                return Container(
                    margin: i != 0
                        ? EdgeInsets.only(left: 25.pt, right: 15.pt)
                        : null,
                    padding: i == 0
                        ? EdgeInsets.only(left: 25.pt, right: 15.pt)
                        : null,
                    decoration: BoxDecoration(
                      border: i == 0
                          ? Border(
                              top: BorderSide(
                                width: 0.5,
                                color: colorScheme.divider,
                              ),
                            )
                          : Border(top: Divider.createBorderSide(context)),
                    ),
                    child: ListTile(
                      onTap: () => choosePerson(item.id),
                      contentPadding: EdgeInsets.zero,
                      title: Wisdom.row(
                        padding: EdgeInsets.symmetric(horizontal: 0.pt),
                        // color: Color(0XFFFF495E),
                        children: [
                          Wisdom(
                              width: 80.pt, child: Text('${item.name ?? ''}')),
                          Wisdom(
                            margin: EdgeInsets.only(left: 20.pt),
                            child: WisText(
                              '${item.positionName ?? ''}',
                              color: colorScheme.grey,
                            ),
                          ),
                        ],
                      ),
                      trailing: chosenPerson == item.id
                          ? Icon(
                              Icons.done,
                              color: colorScheme.primary,
                              size: 25.pt,
                            )
                          : null,
                    ));
              })
        ],
      ),
    );
  }
}
