import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:liftmanager/internal/bbs/model/news_detail_item.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 'dart:convert';
import 'package:flustars/flustars.dart' as FlutterStars;
import 'package:liftmanager/common/common.dart';
import 'package:liftmanager/internal/account/account_router.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';

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

  final String id;

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

class NewsDetailPageState extends State<NewsDetailPage> {
  NewsDetailItem item = NewsDetailItem();

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

  getNewsDetail() {
    ApiService().newsDetail(widget.id, onSuccess: (res) {
      print(JsonEncoder().convert(res));
      item = res;
      setState(() {});
    }, onError: (code, msg) {
      toasts(msg);
    });
  }
  zanClick(){
      ApiService(context: context).newsZan(widget.id,onSuccess: (res){
        item.isLike =true;
        item.likeNum ="${int.parse(item.likeNum)+1}";
        setState(() {

        });
      },onError: (code,msg){
        toasts(msg);
      });
    }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        centerTitle: "新闻详情",
      ),
      body: SafeArea(
          child: item.id.length == 0
              ? Container()
              : ListView(
                  padding: EdgeInsets.fromLTRB(15, 18, 15, 18),
//              mainAxisAlignment: MainAxisAlignment.start,
//              crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text(
                      "${item.title}",
                      style: TextStyle(
                          fontSize: 15,
                          // color: 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,
                    ),
                    Row(
                      children: <Widget>[
                        Text(
                          "阅读${item.lookNum}次·点赞${item.likeNum}",
                          style:
                              TextStyle(fontSize: 11, color: Colours.text_gray),
                        ),
                      ],
                    ),
                    Gaps.vGap15,

                    Center(
                      child:GestureDetector(
                        onTap: (){
                          if(FlutterStars.SpUtil.getString(Constant.userId) == "-1"){
                                          toasts("请登录");
                                          NavigatorUtils.push(context, AccountRouter.loginPage, clearStack: true);
                                        }else {
                          zanClick();}
                        },
                        child: Column(
                          children: <Widget>[
                            LoadAssetImage(
                              "news/${item.isLike?'zan_yes':'zan_no'}",
                              width: 45.0,
                              height: 45,
                            ),
                            Text("${item.isLike?'已赞':'点赞'}")
                          ],
                        ),
                      ) ,
                    )

                  ],
                )),
    );
  }
}
