import 'package:farmer_client/services/news.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';

import '../../models/entities/NewsInformationInfoTextModel.dart';
import '../../models/entities/NewsInformationModel.dart';
import '../../widgets/layout/BasicPage.dart';

class ArticleInfo extends BasicPage {
  String newsId = '';
  final newsInfoContent = Rx<NewsInformationInfoTextModel>(NewsInformationInfoTextModel());
  final newsList = Rx<List<ExtendContent>>([]);

  @override
  void beforeShow() {
    // TODO: implement beforeShow
    super.beforeShow();
    if (Get.arguments['newsId'] != null) {
      newsId = Get.arguments['newsId'];

      getNewsInfo(newsId).then((value) {
        newsInfoContent.value = NewsInformationInfoTextModel.fromJson(value);
        newsList.value =  newsInfoContent.value.contentList!;

      });

    }
  }

  @override
  Widget builder(BuildContext context) {
    naviTitle = '资讯详情';
    return Container(
      padding: EdgeInsets.fromLTRB(15, 30, 15, 50),
      child: Column(
        children: [
          Container(
            alignment: Alignment.center,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Image(
                  image: AssetImage('images/icons/decorate.png'),
                  width: 17,
                ),
                Obx(()=>
                    Text(
                      newsInfoContent.value.title.toString(),
                      style: TextStyle(
                        fontSize: 17.sp,
                        fontWeight: FontWeight.bold,
                      ),
                    )
                ),
                Image(
                    image: AssetImage('images/icons/decorate.png'), width: 17),
              ],
            ),
          ),
          Obx(() => Column(
                children:  newsList.value.map((item) {
                  if (item.contentType != 'image') {
                    return Container(
                      padding: EdgeInsets.fromLTRB(0, 20, 0, 10),
                      width: 344.w,
                      child: Text(
                        item.content.toString(),
                        style: TextStyle(fontSize: 14.sp),
                      ),
                    );
                  } else {
                    return Container(
                      padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
                      child: Image(
                        image: NetworkImage(item.content.toString()),
                        width: 320.w,
                        height: 260.w,
                        fit: BoxFit.fill,
                      ),
                    );
                  }
                }).toList(),
              )),
        ],
      ),
    );
  }
}
