import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/style.dart';
import 'package:liftmanager/net/api_service.dart';
import 'package:liftmanager/res/colors.dart';
import 'package:liftmanager/res/gaps.dart';
import 'package:liftmanager/utils/theme_utils.dart';
import 'package:liftmanager/utils/toast.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:liftmanager/widgets/load_image.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class NewsDetailPage extends StatefulWidget {
  NewsDetailPage(this.id);

  final String id;

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

class NewsDetailPageState extends State<NewsDetailPage> {
  dynamic item;

  @override
  void initState() {
    super.initState();
    getNewsDetail();
  }

  getNewsDetail() {
    print(666);
    NewApiService().newsDetail(widget.id, onSuccess: (res) {
      if (res != null) {
        _hasData = true;
        item = res;
      }
      
      
      setState(() {});
    }, onError: (code, msg) {
      toasts(msg);
    });
  }

  zanClick(){
    ApiService(context: context).newsZan(widget.id,onSuccess: (res){
      item.isLike =item.isLike?false:true;
      item.likeNum ="${int.parse(item.likeNum)+(item.isLike?1:-1)}";
      setState(() {

      });
    },onError: (code,msg){
      toasts(msg);
    });
  }
  bool _hasData = false;
  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;
    bool isDark = ThemeUtils.isDark(context);
    return Scaffold(
      backgroundColor: ThemeUtils.getTabsBg(context),
      appBar: MyAppBar(
        centerTitle: "新闻详情",
      ),
      body: SafeArea(
          child: _hasData? ListView(
                  padding: EdgeInsets.fromLTRB(15, 18, 15, 18),
//              mainAxisAlignment: MainAxisAlignment.start,
//              crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text(
                      "${item.title}",
                      style: TextStyle(
                          fontSize: 15,
                          color: isDark ? Colours.dark_text : Colours.text,
                          fontWeight: FontWeight.w500),
                    ),
                    Container(
                      padding: EdgeInsets.only(top: 10, bottom: 10),
                      child: Row(
                        children: <Widget>[
                          Text(
                            "${item.releaseDate}",
                            style: TextStyle(
                                fontSize: 11, color: Color(0xFF999999)),
                          ),
                          Gaps.hGap10,
                          Text(
                            "@${item.releaseUser}",
                            style: TextStyle(
                                fontSize: 11, color: Color(0xFF999999)),
                          ),
                        ],
                      ),
                    ),
                    Html(
                      data: item.content,
                      style: {
                        "html": Style(
                          width: width
                        ),
                        "p":Style(
                          fontSize: FontSize(ScreenUtil().setSp(15))
                        )
                      },
                    ),
                    Row(
                      children: <Widget>[
                        Text(
                          "阅读${item.lookNum}次·点赞${item.likeNum}",
                          style:
                              TextStyle(fontSize: 11, color: Colours.text_gray),
                        ),
                      ],
                    ),
                    Gaps.vGap15,

                    Center(
                      child:GestureDetector(
                        onTap: (){
                          zanClick();
                        },
                        child: Column(
                          children: <Widget>[
                            LoadAssetImage(
                              "news/${item.isLike?'zan_yes':'zan_no'}",
                              width: 45.0,
                              height: 45,
                            ),
                            Text("${item.isLike?'已赞':'点赞'}")
                          ],
                        ),
                      ) ,
                    )

                  ],
                ): Center(
              child: Text("正在加载..."),
            ),),
    );
  }
}
