import 'package:flutter/material.dart';
import 'package:liftmanager/res/gaps.dart';
import 'package:liftmanager/net/api_service.dart';
import 'package:liftmanager/utils/toast.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:liftmanager/internal/search/search_router.dart';
import 'package:liftmanager/widgets/app_search_bar.dart';
import 'package:liftmanager/res/resources.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/widgets/load_image.dart';
import 'package:liftmanager/widgets/selected_image.dart';
import 'package:image_picker/image_picker.dart';
import 'package:liftmanager/internal/wode/wode_router.dart';
import 'dart:io';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:liftmanager/widgets/my_card.dart';
// import 'package:liftmanager/internal/bbs/model/question_detail.dart';
import 'package:liftmanager/internal/bbs/model/position_model.dart';
import 'package:liftmanager/utils/time_format.dart';
import 'package:liftmanager/utils/url.dart';
import 'dart:convert';
import 'package:liftmanager/utils/fast_notification.dart';
import 'package:flutter_full_pdf_viewer/flutter_full_pdf_viewer.dart';
import 'package:liftmanager/internal/wode/page/xieyi_pdf.dart';
import 'package:flutter_full_pdf_viewer/full_pdf_viewer_scaffold.dart';
import 'package:flutter/foundation.dart';
import 'package:path_provider/path_provider.dart';
import 'package:liftmanager/utils/utils.dart';

class XieyiList extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    return XieyiListState();
  }
}

class XieyiListState extends State<XieyiList> {
  @override
  void initState() {
    super.initState();
    getXieyiList();
  }

  bool _hasData = false;

  List<dynamic> xieList;

  Future getXieyiList() async {
    await NewApiService().getXieyiListNoPage(
        onSuccess: (res) {
      if (res != null) {
        print(123456);
        print(res);
        xieList = res;
        _hasData = true;
        setState(() {});
      }
    }, onError: (code, msg) {
      toasts(msg);
    });
  }

  Future<File> createFileOfPdfUrl(url) async {
    final filename = url.substring(url.lastIndexOf("/") + 1);
    var request = await HttpClient().getUrl(Uri.parse(url));
    var response = await request.close();
    var bytes = await consolidateHttpClientResponseBytes(response);
    String dir = (await getApplicationDocumentsDirectory()).path;
    File file = new File('$dir/$filename');
    await file.writeAsBytes(bytes);
    return file;
  }

  @override
  Widget build(BuildContext context) {

    double width = MediaQuery.of(context).size.width;

    return Scaffold(
        resizeToAvoidBottomPadding: false, //不让键盘弹上去
        appBar: MyAppBar(
          centerTitle: "专家协议列表",
        ),
        body: _hasData
            ? Container(
              child: ListView(
                children:xieList.map((i) {
        return InkWell(
          child: Container(
            padding: EdgeInsets.only(
                left: ScreenUtil().setWidth(15),
                right: ScreenUtil().setWidth(15),
                top: ScreenUtil().setHeight(10),
                bottom: ScreenUtil().setHeight(10)),
            decoration: BoxDecoration(
              border: Border(
                bottom: BorderSide(width: 0.5, color: Colours.line),
              ),
            ),
            child: Text(
                        i.name ?? "",
                        textAlign: TextAlign.left,
                        style: TextStyle(
                          fontSize: ScreenUtil().setSp(15),
                          color: Color(0xff333333),
                        ),
                      ),
          ),
          onTap: () {
            print(i.url);
            String fileUrl = Utils.getImagePath(i.url);
            createFileOfPdfUrl(fileUrl).then(
                                  (f) {
                                    setState(
                                      () {
                                        Navigator.push(
                                          context,
                                          MaterialPageRoute(
                                            builder: (context) =>
                                                XieyiPdf(f.path),
                                          ),
                                        );
                                      },
                                    );
                                  },
                                );

            Navigator.push(
                                          context,
                                          MaterialPageRoute(
                                            builder: (context) =>
                                                XieyiPdf(i.url),
                                          ),
                                        );
                
          },
        );
      }).toList()
              ),
            )
            : Center(
                child: Text("正在加载..."),
              ));
  }
}
