import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:huixiang/generated/l10n.dart';
import 'package:huixiang/retrofit/data/coupon.dart';
import 'package:huixiang/retrofit/retrofit_api.dart';
import 'package:huixiang/utils/flutter_utils.dart';
import 'package:huixiang/utils/font_weight.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:huixiang/view_widget/classic_header.dart';
import 'package:huixiang/view_widget/item_title.dart';
import 'package:huixiang/view_widget/my_footer.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class DiscountZone extends StatefulWidget {
  final List<Coupon> coupon;

  DiscountZone(this.coupon);
  @override
  State<StatefulWidget> createState() {
    return _DiscountZone();
  }
}

class _DiscountZone extends State<DiscountZone> {
  ApiService apiService;

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

  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      margin: EdgeInsets.only(top: 14.h,),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          Padding(padding:EdgeInsets.symmetric(horizontal: 14.w),
            child:  Text(
              "特惠专区",
              style: TextStyle(
                color: Color(0xFF0D0D0D),
                fontSize: 15.sp,
                fontWeight: FontWeight.bold,
              ),
            )),
          if(widget.coupon.length != 0)
          Container(
            height: 120.h,
            margin: EdgeInsets.only(top: 10.h),
            child: ListView.builder(
              scrollDirection: Axis.horizontal,
              physics: BouncingScrollPhysics(),
              padding: EdgeInsets.symmetric(horizontal: 14.w),
              itemBuilder: (context, position) {
                return GestureDetector(
                  onTap: (){
                    Navigator.of(context).pushNamed('/router/welfare_page');
                  },
                  child: discountItem(widget.coupon[position]),
                );
              },
              itemCount:widget.coupon.length>5?5:widget.coupon.length,
            ),
          ),
        ],
      ),
    );
  }

  Widget discountItem(Coupon coupon) {
    return Container(
      width: 290.w,
      height: 120.h,
      decoration: BoxDecoration(
        image: DecorationImage(
          fit: BoxFit.fill,
          image: AssetImage("assets/image/discount.webp"),
        ),
      ),
      margin: EdgeInsets.only(right: 10.w),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Expanded(
            flex: 2,
              child: Container(
            margin: EdgeInsets.only(top: 12.h,bottom: 12.h,left: 20.w,right: 27.w),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  (coupon != null ? coupon.couponName ?? "" : ""),
                  maxLines: 2,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(
                    fontSize: 14.sp,
                    height: 1.5.h,
                    fontWeight: MyFontWeight.semi_bold,
                    color: Color(0xFFFFA607),
                  ),
                ),
                SizedBox(height:5.w),
                (coupon.useStartTime == null &&
                    coupon.useEndTime == null)?
                Text(
                  S.of(context).quantian,
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(
                    fontSize: 12.sp,
                    fontWeight: MyFontWeight.regular,
                    color: Color(0xFFFFA607),
                  ),
                ):Text(
                  "发放时间: ${coupon.useEndTime.substring(0, coupon.publishStartTime.indexOf(" "))}",
                  maxLines:1,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(
                    fontSize: 12.sp,
                    fontWeight: MyFontWeight.regular,
                    color: Color(0xFFFFA607),
                  ),
                ),
              ],
            ),
          )),
          Expanded(
              flex: 1,
              child:Container(
                margin: EdgeInsets.only(top: 12.h,bottom: 12.h),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Expanded(child: Container(
                      padding: EdgeInsets.only(right: 5.w),
                      child:discountWidget(coupon),
                    )),
                  ],
                ),
              )),
        ],
      )
    );
  }

  Widget discountWidget(Coupon coupon) {
    if (coupon.bizType == 1) {
      return Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.baseline,
            textBaseline: TextBaseline.alphabetic,
            children: [
              Text(
                "¥",
                style: TextStyle(
                  fontSize: 12.sp,
                  fontWeight: MyFontWeight.medium,
                  color:  coupon.status == 3 ? Color(0xFFB3B3B3):Color(0xFFFFFFFF),
                ),
              ),
              SizedBox(
                height: 4.h,
              ),
              Text(
                coupon != null
                    ? double.tryParse("${coupon.discountAmount}" ?? "0").toInt()
                    .toString()
                    : "",
                style: TextStyle(
                  fontSize: 30.sp,
                  fontWeight: FontWeight.w900,
                  color:  coupon.status == 3 ? Color(0xFFB3B3B3):Color(0xFFFFFFFF),
                ),
              ),
            ],
          ),
          Text(
            S.of(context).manyuankeyong(coupon != null
                ? ("${coupon.fullAmount}" ?? "0")
                .toString()
                : ""),
            style: TextStyle(
              fontSize: 12.sp,
              fontWeight: MyFontWeight.semi_bold,
              color:  coupon.status == 3 ? Color(0xFFB3B3B3):Color(0xFFFFFFFF),
            ),
          ),
        ],
      );
    } else if (coupon.bizType == 3) {
      return Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Container(
            padding: EdgeInsets.only(bottom:10.h,top: 7.h,left: 12.w),
            child: Text(
              S.of(context).duihuanquan,
              style: TextStyle(
                fontSize: 26.sp,
                fontWeight: MyFontWeight.semi_bold,
                color:  coupon.status == 3 ? Color(0xFFB3B3B3):Color(0xFFFFFFFF),
              ),
            ),
          ),
          Text(
            S.of(context).xianshangshiyong,
            style: TextStyle(
              fontSize: 12.sp,
              fontWeight: MyFontWeight.semi_bold,
              color:  coupon.status == 3 ? Color(0xFFB3B3B3):Color(0xFFFFFFFF),
            ),
          ),
        ],
      );
    } else if (coupon.bizType == 5) {
      return Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Container(
            padding: EdgeInsets.only(bottom:10.h,top: 7.h,left: 12.w),
            child: Text(
              S.of(context).duihuanquan,
              style: TextStyle(
                fontSize: 26.sp,
                fontWeight: MyFontWeight.semi_bold,
                color:  coupon.status == 3 ? Color(0xFFB3B3B3):Color(0xFFFFFFFF),
              ),
            ),
          ),
          Text(
            S.of(context).xianxiashiyong,
            style: TextStyle(
              fontSize: 12.sp,
              fontWeight: MyFontWeight.semi_bold,
              color:  coupon.status == 3 ? Color(0xFFB3B3B3):Color(0xFFFFFFFF),
            ),
          ),
        ],
      );
    } else if (coupon.bizType == 6) {
      return Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.baseline,
            textBaseline: TextBaseline.alphabetic,
            children: [
              Text(
                "¥",
                style: TextStyle(
                  fontSize: 12.sp,
                  fontWeight: MyFontWeight.medium,
                  color:  coupon.status == 3 ? Color(0xFFB3B3B3):Color(0xFFFFFFFF),
                ),
              ),
              SizedBox(
                height: 4.h,
              ),
              Text(
                coupon != null
                    ? "${AppUtils.calculateDouble(double.tryParse(coupon.limitAmount) ?? 0)}"
                    : "",
                style: TextStyle(
                  fontSize: 28.sp,
                  fontWeight: FontWeight.w900,
                  color:  coupon.status == 3 ? Color(0xFFB3B3B3):Color(0xFFFFFFFF),
                ),
              ),
            ],
          ),
        ],
      );
    }else{
      return Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.baseline,
            textBaseline: TextBaseline.alphabetic,
            children: [
              Text(
                coupon != null
                    ? "${(coupon.discountPercent / 10.0 >= 10) ? 10 : coupon.discountPercent / 10.0}" ??
                    "0"
                    : "",
                style: TextStyle(
                  fontSize: 30.sp,
                  fontWeight: FontWeight.w900,
                  color:  coupon.status == 3 ? Color(0xFFB3B3B3):Color(0xFFFFFFFF),
                ),
              ),
              SizedBox(
                height: 4.h,
              ),
              Text(
                "折",
                style: TextStyle(
                  fontSize: 14.sp,
                  fontWeight: MyFontWeight.semi_bold,
                  color:  coupon.status == 3 ? Color(0xFFB3B3B3):Color(0xFFFFFFFF),
                ),
              ),
            ],
          ),
        ],
      );
    }
  }

}