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

import 'package:eitc_erm_app/login.dart';
import 'package:eitc_erm_app/utils/Constants.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:http/http.dart' as http;

import 'bean/message_box_s_list_entity.dart';
import 'bean/pda_patrol_time_list.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(SelectHospital());
}

class SelectHospital
    extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => SelectHospitalState();
}

class SelectHospitalState extends State<SelectHospital> {
  ValueNotifier<dynamic> result = ValueNotifier(null);
  late Future<List<DataList>?> _future;

  @override
  void initState() {
    super.initState();
    _future = fetchData();
  }

  Future<List<DataList>?> fetchData() async {
    final response = await http.get(
        Uri.parse(
            '${Global.ServerUrl}/getPdaPatrolTimeList'),
        headers: {
          HttpHeaders.contentTypeHeader: "application/json; charset=utf-8"
        });
    if (response.statusCode == 200) {
      final jsonString = utf8.decode(response.bodyBytes);
      final jsonResponse = jsonDecode(jsonString);
      print(jsonResponse);
      PdaPatrolTimeList mPatrolTime =
      new PdaPatrolTimeList.fromJson(jsonResponse);
      return mPatrolTime.data?[0].dataList;
    } else {
      Fluttertoast.showToast(
          msg: "出错了，请稍后再试！",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER,
          timeInSecForIosWeb: 1,
          backgroundColor: Colors.blue,
          textColor: Colors.white,
          fontSize: 16.0);
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('请选择您需要挂号的医院/诊所'), centerTitle: true),
        body: Center(
          child: FutureBuilder<List<DataList>?>(
            future: _future,
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                List<DataList>? data = snapshot.data;
                return ListView.builder(
                  itemCount: data?.length,
                  itemBuilder: (context, index) {
                    return ListTile(
                      title: Text(
                          textAlign: TextAlign.center,
                          '| 智汇口腔诊所  ' +
                              (data?[index].startTime as String)),
                      // subtitle: Text('   巡检人：' +
                      //     (data?[index].patrolPersons as String) +
                      //     '\n   ${(data?[index].finishNumber.toString())}个点位已处理\n   异常数：${(data?[index].errorNumber.toString())}个'),
                      onTap: () {
                        Navigator.push(
                          context,
                          MaterialPageRoute(builder: (context) => Login()),
                        );
                      },
                    );
                  },
                );
              } else if (snapshot.hasError) {
                return Text('Error: ${snapshot.error}');
              }
              return CircularProgressIndicator();
            },
          ),
        ),
      ),
    );
  }

}
