/*
 * @Author: WuWei
 * @Date: 2020-07-16 09:42:16
 * @LastEditors  : WuWei
 * @LastEditTime : 2023-05-25 15:53:12
 * @Description:
 */

import 'package:app_package/app_package.dart';
import 'package:flutter/material.dart';
import 'package:report_package/src/models/report_person_list.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
// import '../models/report_person_list.dart';
import '../service/impl/report_service_impl.dart';
import '../service/report_service.dart';

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

  final ReportPersonListModel? initPerson;

  static Future<ReportPersonListModel?> show(
      context, ReportPersonListModel? initPerson) async {
    return await showModalBottomSheet<ReportPersonListModel>(
      context: context,
      backgroundColor: Colors.transparent,
      builder: (context) {
        // final style = WTheme.of(context);final colorScheme = style.colorScheme;
        return HandleChoosePerson(initPerson: initPerson);
      },
    );
  }

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

class _HandleChoosePersonState extends State<HandleChoosePerson> {
  List<ReportPersonListModel>? personList;
  final ReportService _repairService = ReportServiceImpl();
  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 (ReportPersonListModel 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) {
    return WisBottomModal(
      title: Text('维修师傅'),
      body: WisScrollView(
        onInit: () => getPerson(),
        slivers: <Widget>[
          WisSliverList.childBuilder(
              childCount: personList?.length ?? 0,
              builder: (_, i) {
                var item = personList![i];
                return Wisdom(
                  onTap: () => choosePerson(item.id),
                  child: ListTile(
                    title:
                        Text('${item.name ?? ''}-${item.positionName ?? ''}'),
                    leading: Radio(
                      value: item.id,
                      groupValue: chosenPerson,
                      onChanged: (dynamic value) => choosePerson(value),
                    ),
                  ),
                );
              })
        ],
      ),
    );
  }
}
