import 'dart:convert';
import 'dart:io';

import 'package:eitc_erm_app/select_department.dart';
import 'package:eitc_erm_app/select_doctor.dart';
import 'package:eitc_erm_app/utils/Component.dart';
import 'package:eitc_erm_app/utils/Constants.dart';
import 'package:eitc_erm_app/utils/DateUtils.dart';
import 'package:eitc_erm_app/widget/loading.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

import 'bean/chat_list.dart';
import 'bean/doctor_chat_list.dart';
import 'bean/pda_patrol_time_list.dart';
import 'package:http/http.dart' as http;

import 'bean/user_list.dart';
import 'chat/chat_home.dart';
import 'chat/chat_home_doctor.dart';
import 'confirm_registration.dart';

/*void main() => runApp(DoctorTalkList());*/

class DoctorTalkList extends StatelessWidget {
  DateTime now = DateTime.now();
  ValueNotifier<dynamic> result = ValueNotifier(null);
  late Future<DoctorChatList?> _future = fetchData();

  Future<DoctorChatList?> fetchData() async {
    String url = '${Global.BaseUrl}chat/getChatPatientList?dockerId=${Global.user.data?.id}';
    final response = await http.get(
        Uri.parse(url),
        headers: {
          HttpHeaders.contentTypeHeader: "application/json; charset=utf-8",
          'token': '${Global.token}',
        });
    print(url);
    print(Global.token);
    if (response.statusCode == 200) {
      final jsonString = utf8.decode(response.bodyBytes);
      final jsonResponse = jsonDecode(jsonString);
      print(jsonResponse);
      DoctorChatList mDoctorChatList = new DoctorChatList.fromJson(jsonResponse);
      if (mDoctorChatList.code == Global.responseSuccessCode) {
      } else {
        Component.toast(mDoctorChatList.msg.toString(), 0);
      }
      return mDoctorChatList;
    } else {
      Component.toast("出错了，请稍后再试！", 0);
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: new AppBar(
          title: new Text('患者沟通列表',
              style: TextStyle(
                color: Colors.white,
              )),
          centerTitle: true,
          elevation: 0.5,
          backgroundColor: Global.StatusBarColor,
        ),
        body: Column(children: [
          Expanded(
            child: FutureBuilder<DoctorChatList?>(
              future: _future,
              builder: (context, snapshot) {
                if (snapshot.hasData) {
                  DoctorChatList? data = snapshot.data;
                  return ListView.builder(
                    shrinkWrap: true,
                    itemCount: data?.data?.length,
                    itemBuilder: (context, index) {
                      return ListTile(
                        shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.only(
                                topLeft: Radius.circular(5),
                                topRight: Radius.circular(5),
                                bottomRight: Radius.circular(5),
                                bottomLeft: Radius.circular(5))),
                        tileColor: Colors.white,
                        iconColor: Colors.white,
                        leading: Container(
                          height: 55,
                          child: Image.network(
                            "${Global.ImageUrl}${data!.data?[index].avatar}",
                            width: 55,
                            fit: BoxFit.cover,
                            excludeFromSemantics: true,
                            errorBuilder: (context, error, stackTrace) => Icon(
                              Icons.person_pin,
                              size: 55,
                              color: Colors.grey,
                            ),
                          ),
                        ),
                        title: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                '与 ${data!.data?[index].phoneNumber} 的聊天记录',
                                textAlign: TextAlign.center,
                                style: const TextStyle(
                                    fontSize: 15, color: Colors.black),
                              ),
                              /*Text(
                                '${data!.data?[index].updateDate}',
                                textAlign: TextAlign.center,
                                style: const TextStyle(
                                    fontSize: 13, color: Colors.grey),
                              ),*/
                            ]),
                        subtitle: Text(
                          '${data!.data?[index].updateDate}',
                          style: const TextStyle(
                              fontSize: 13, color: Colors.grey),
                        ),
                        onTap: () {
                          Navigator.push(
                            context,
                            MaterialPageRoute(builder: (context) => ChatHomeDoctor(data!.data![index].id.toString(), data!.data![index].phoneNumber.toString())),
                          );
                        },
                      );
                    },
                  );
                } else if (snapshot.hasError) {
                  return Text('Error: ${snapshot.error}');
                }
                return ColorLoader();
              },
            ),
          ),
        ]),
      ),
    );
  }
}
