/*
 * @Author: WuWei
 * @Date: 2020-07-24 14:27:21
 * @LastEditors  : WuWei
 * @LastEditTime : 2023-05-16 17:12:11
 * @Description:
 */

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

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

  final ReportClassifyModel? initClassify;

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

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

class _HandleChoosePersonState extends State<HandleChooseClassify> {
  List<ReportClassifyModel>? classList;
  final ReportService _repairService = ReportServiceImpl();
  final communityGlobalStore = GetIt.I.get<CommunityGlobalStore>();

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

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

  String? chooseClass;

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

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

  @override
  Widget build(BuildContext context) {
    return WisBottomModal(
      title: Text('反馈分类'),
      body: WisScrollView(
        onInit: () => getClassify(),
        slivers: <Widget>[
          WisSliverList.childBuilder(
              childCount: classList?.length ?? 0,
              builder: (_, i) {
                var item = classList![i];
                return Wisdom(
                  onTap: () => chooseClassify(item.id),
                  child: ListTile(
                    title: Text('${item.sortName ?? ''}'),
                    leading: Radio(
                      value: item.id,
                      groupValue: chooseClass,
                      onChanged: (dynamic value) => chooseClassify(value),
                    ),
                  ),
                );
              })
        ],
      ),
    );
  }
}
