import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:ustest/Services/UserService.dart';
import 'package:ustest/tool.dart';
import 'Services/BuildService.dart';

class BuildHistoryList extends StatefulWidget {
  BuildHistoryList({Key? key, required this.token, required this.histories})
      : super();
  final String token;
  final List<BuildHistory> histories;

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

class _ConsultationListState extends State<BuildHistoryList> {
  @override
  void initState() {
    var service = GetIt.instance.get<UserService>();
    service.NotificationReceived.subscribe((args) {
      if (args == null) {
        return;
      }
    });
    super.initState();
  }

  @override
  void dispose() {
    var service = GetIt.instance.get<UserService>();
    service.NotificationReceived.unsubscribe((args) {});
    super.dispose();
  }

  void showConfirmDialog(BuildContext context, String content,
      Function confirmCallback, Function rejectCallback) {
    showDialog(
        context: context,
        builder: (context) {
          return new AlertDialog(
            title: new Text("提示"),
            content: new Text(content),
            actions: <Widget>[
              new TextButton(
                onPressed: () {
                  confirmCallback();
                  Navigator.of(context).pop();
                },
                child: new Text("接受"),
              ),
              new TextButton(
                onPressed: () {
                  Navigator.of(context).pop();
                },
                child: new Text("拒絕"),
              ),
            ],
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Container(
      height: 600,
      child: Row(
        children: [
          Container(
            height: 500,
            width: 400,
            child: ListView(
              restorationId: 'list_demo_list_view',
              padding: const EdgeInsets.symmetric(vertical: 0),
              children: [
                for (int index = 0; index < widget.histories.length; index++)
                  Row(
                    children: [
                      Expanded(
                          child: Container(
                        child: ListTile(
                          leading: ExcludeSemantics(
                            child: Icon(Icons.person),
                          ),
                          title: Text(widget.histories[index].id),
                          onTap: (() =>
                              onTabPatient(widget.histories[index].id)),
                        ),
                      )),
                      Row(
                        children: [Offstage()],
                      ),
                      Container(
                          alignment: Alignment.topRight,
                          child: Container(
                            child: Column(
                              children: [
                                ClipRRect(
                                    borderRadius: BorderRadius.circular(2),
                                    child: Container(
                                        color: Colors.amber[50],
                                        child: Row(children: [
                                          Icon(
                                            Icons.star,
                                            color: Colors.amber[500],
                                            size: 12,
                                          ),
                                          Text(
                                            "5.0",
                                            style: TextStyle(
                                                color: Colors.amber[900],
                                                fontSize: 9),
                                          ),
                                        ]))),
                                Container(
                                    child: Row(
                                  children: [
                                    TextButton(
                                        child: Text("Start"),
                                        onPressed: (() =>
                                            OnStartLiveConsultation(
                                                widget.histories[index].id))),
                                  ],
                                ))
                              ],
                            ),
                          )),
                    ],
                  ),
              ],
            ),
          ),
        ],
      ),
    ));
  }

  void onTabPatient(String id) async {}

  void OnStartLiveConsultation(id) async {
    try {
      //var service = GetIt.instance.get<ConsultationService>();

      Navigator.of(context).pushNamed('/meeting');
    } catch (ex) {
      print('OnStartLiveConsultation.to ex:$ex');
      MeetingTool.toast(ex.toString(), context);
    }
  }
}