import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:worker_client/models/entities/Machinery.dart';
import 'package:worker_client/services/machinery.dart';
import 'package:worker_client/widgets/Jianbian/index.dart';
import 'package:worker_client/widgets/layout/BasicPage.dart';

class MachineryDetail extends BasicPage {
  MachineryDetail({Key? key}) : super(key: key) {
    naviTitle = '查看详情';
  }
  final machinery = Rx<Machinery>(Machinery());
  @override
  void beforeShow() {
    // TODO: implement beforeShow
    super.beforeShow();
    if (Get.arguments != null) {
      getMachineryDetail(Get.arguments['id'], {
        'attached': true,
        'location': '118.84002,31.95266'
      }).then((res) => {machinery(Machinery.fromJson(res))});
    }
  }

  @override
  Widget builder(BuildContext context) {
    return SizedBox(
      width: 375.w,
      child: ListView(
        children: [
          Obx(() => Stack(
                children: [
                  Positioned(
                      top: 0,
                      left: 0,
                      child: SizedBox(
                        width: 375.w,
                        child: CarouselSlider(
                          items: machinery.value.imagesList!
                              .map((item) => Image.network(item.url.toString(),
                                  fit: BoxFit.cover, width: 375.w))
                              .toList(),
                          options: CarouselOptions(
                            autoPlay: true,
                            enlargeCenterPage: false,
                            //图片中心放大
                            viewportFraction: 1,
                            height: 187.5.w,
                            initialPage: 1, //初始页
                          ),
                        ),
                      )),
                  Container(
                      margin: EdgeInsets.only(top: 157.5.w),
                      decoration: BoxDecoration(
                          color: const Color(0xffffffff),
                          borderRadius: BorderRadius.only(
                              topLeft: Radius.circular(20.w),
                              topRight: Radius.circular(20.w))),
                      child: Column(
                        children: [
                          Container(
                            height: 46.w,
                            clipBehavior: Clip.hardEdge,
                            decoration: BoxDecoration(
                                borderRadius: BorderRadius.only(
                                    topLeft: Radius.circular(20.w))),
                            child: Stack(
                              children: [
                                Jianbian(
                                  status: machinery.value.status,
                                  type: 'machinery',
                                  value: machinery.value.jobStatus,
                                )
                              ],
                            ),
                          ),
                          Container(
                            margin: EdgeInsets.only(
                                top: 6.h, left: 15.w, right: 15.w),
                            alignment: Alignment.centerLeft,
                            child: Text(
                              machinery.value.name.toString(),
                              style: TextStyle(
                                  fontSize: 18.sp,
                                  fontWeight: FontWeight.bold,
                                  color: const Color(0xff222222)),
                            ),
                          ),
                          Container(
                            margin: EdgeInsets.fromLTRB(15.w, 43.h, 15.w, 25.h),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Image.asset(
                                  'images/machinery/micon2.png',
                                  width: 17.w,
                                ),
                                Container(
                                  padding:
                                      EdgeInsets.only(left: 15.w, right: 15.w),
                                  child: Text(
                                    '农机详情',
                                    style: TextStyle(
                                        color: const Color(0xff222222),
                                        fontWeight: FontWeight.bold,
                                        fontSize: 15.sp,
                                        height: 1.2),
                                  ),
                                ),
                                Image.asset(
                                  'images/machinery/micon2.png',
                                  width: 17.w,
                                ),
                              ],
                            ),
                          ),
                          Container(
                            margin: EdgeInsets.only(left: 15.w, right: 15.w),
                            child: Column(
                              children: machinery.value.contentList != null
                                  ? machinery.value.contentList!.map((e) {
                                      if (e.contentType == 'image') {
                                        return Image.network(
                                            e.content.toString(),
                                            fit: BoxFit.cover,
                                            width: 345.w);
                                      } else {
                                        return Container(
                                          alignment: Alignment.centerLeft,
                                          margin: EdgeInsets.only(
                                              top: 15.h, bottom: 15.h),
                                          child: Text(
                                            e.content.toString(),
                                            style: TextStyle(
                                              fontSize: 15.sp,
                                              color: const Color(0xff222222),
                                            ),
                                          ),
                                        );
                                      }
                                    }).toList()
                                  : [],
                            ),
                          )
                        ],
                      )),
                ],
              ))
        ],
      ),
    );
  }
}
