import 'dart:convert';

import 'package:eitc_erm_app/utils/Component.dart';
import 'package:eitc_erm_app/utils/Constants.dart';
import 'package:eitc_erm_app/widget/loading.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

import 'bean/normal_response.dart';
import 'package:html/parser.dart' as parser;
import 'package:html/dom.dart' as dom;

import 'bean/normal_response3.dart';

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

class Agreement extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => AgreementState();
}

class AgreementState extends State<Agreement> {
  ValueNotifier<dynamic> result = ValueNotifier(null);
  late Future<NormalResponse3?> _future;
  String strHtml = "";
  String strHtml1 = "";
  String strHtml2 = "";
  String strHtml3 = "";
  String strHtml4 = "";
  String strHtml5 = "";

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

  Future<NormalResponse3?> fetchData() async {
    Map<String, String> headers = {
      'token':'${Global.token}',
    };

    final response = await http.get(Uri.parse(Global.BaseUrl + 'api/user-notice'));

    if (response.statusCode == 200) {
      final jsonString = utf8.decode(response.bodyBytes);
      final jsonResponse = jsonDecode(jsonString);
      print(jsonString);
      NormalResponse3 mNormalResponse3 =
          new NormalResponse3.fromJson(jsonResponse);
      if (mNormalResponse3.code == Global.responseSuccessCode) {
        int i = 0;
        parser.parse(mNormalResponse3?.data).body?.children.forEach((element) {
          if (i == 0) {
            strHtml = element.text;
            i++;
          } else if (i == 1) {
            strHtml1 = element.text;
            i++;
          } else if (i == 2) {
            strHtml2 = element.text;
            i++;
          } else if (i == 3) {
            strHtml3 = element.text;
            i++;
          } else if (i == 4) {
            strHtml4 = element.text;
            i++;
          } else if (i == 5) {
            strHtml5 = element.text;
            i++;
          }
        });
        setState(() {});
        return mNormalResponse3;
      } else {
        Component.toast(mNormalResponse3.msg.toString(), 0);
      }
      return null;
    } else {
      Component.toast("出错了，请稍后再试！", 0);
      return null;
    }
  }

  @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,
          leading: new IconButton(
            tooltip: '返回上一页',
            icon: const Icon(
              Icons.arrow_back_ios,
              color: Colors.white,
            ),
            onPressed: () {
              Navigator.of(context).pop();
              //_nextPage(-1);
            },
          ),
        ),
        body: SingleChildScrollView(
    child: Center(
            child: Padding(
                padding: const EdgeInsets.all(20),
                child: Column(
                    children: [
                      Center(
                        child: Text(
                          strHtml,
                          style:
                          const TextStyle(fontSize: 18, color: Colors.black,),
                        ),
                      ),
                      Text(
                        strHtml1 + strHtml2 + strHtml3 + strHtml4 + strHtml5,
                        style:
                        const TextStyle(fontSize: 13, color: Colors.grey,),
                      ),
                    ]))),),
      ),
    );
  }
}
