import 'package:flutter/material.dart';
import 'package:huixiang/retrofit/data/settleOrderInfo.dart';
import 'package:huixiang/retrofit/data/store_info.dart';
import 'package:huixiang/settlement/settlement_view/coupon.dart';
import 'package:huixiang/utils/font_weight.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class SettlementCoupon extends StatefulWidget {
  final StoreInfo storeInfo;
  final SettleOrderInfo settleOrderInfo;
  final CouponListBean couponBean;

  SettlementCoupon(
    this.settleOrderInfo,
    this.storeInfo, {
    this.couponBean,
  });

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

class _SettlementCoupon extends State<SettlementCoupon> {
  List<CouponListBean> couponCan = [];
  List<CouponListBean> couponNo = [];

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

    couponCan.clear();
    couponNo.clear();

    if (widget.settleOrderInfo != null &&
        widget.settleOrderInfo.couponList != null &&
        widget.settleOrderInfo.couponList.length > 0) {
      widget.settleOrderInfo.couponList.forEach((element) {
        if (element.usable) {
          couponCan.add(element);
        } else {
          couponNo.add(element);
        }
      });
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return StatefulBuilder(
      builder: (context1, state) {
        return Container(
          alignment: Alignment.topCenter,
          width: double.infinity,
          height: MediaQuery.of(context).size.height / 3 * 2,
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.only(
              topLeft: Radius.circular(8),
              topRight: Radius.circular(8),
            ),
          ),
          child: SingleChildScrollView(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  padding: EdgeInsets.only(
                    top: 16,
                    left: 16,
                    right: 16,
                    bottom: 8,
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Expanded(
                        child: Text(
                          "可用优惠券(${couponCan.length})",
                          style: TextStyle(
                            color: Color(0xFF000000),
                            fontSize: 14.sp,
                            fontWeight: MyFontWeight.medium,
                          ),
                        ),
                      ),
                      InkWell(
                        onTap: () {
                          Navigator.of(context).pop();
                        },
                        child: Image.asset(
                          "assets/image/cancel.webp",
                          width: 24,
                          height: 24,
                        ),
                      ),
                    ],
                  ),
                ),
                Container(
                  padding: EdgeInsets.only(
                    top: 8,
                    left: 16,
                    right: 16,
                  ),
                  child: ListView.builder(
                    itemCount: couponCan == null ? 0 : couponCan.length,
                    physics: NeverScrollableScrollPhysics(),
                    shrinkWrap: true,
                    itemBuilder: (context, position) {
                      return CouponWidget(
                        couponCan[position],
                        widget.storeInfo,
                        selected: widget?.couponBean?.id == couponCan[position]?.id,
                      );
                    },
                  ),
                ),
                if (couponNo != null || couponNo.length > 0)
                  Container(
                    padding: EdgeInsets.only(
                      top: 16,
                      left: 16,
                      right: 16,
                      bottom: 8,
                    ),
                    child: Text(
                      "不可用优惠券",
                      style: TextStyle(
                        color: Color(0xFF000000),
                        fontSize: 14.sp,
                        fontWeight: MyFontWeight.medium,
                      ),
                    ),
                  ),
                if (couponNo != null || couponNo.length > 0)
                  Container(
                    padding: EdgeInsets.only(
                      top: 8,
                      left: 16,
                      right: 16,
                    ),
                    child: ListView.builder(
                      itemCount: couponNo == null ? 0 : couponNo.length,
                      physics: NeverScrollableScrollPhysics(),
                      shrinkWrap: true,
                      itemBuilder: (context, position) {
                        return CouponWidget(
                          couponNo[position],
                          widget.storeInfo,
                        );
                      },
                    ),
                  ),
              ],
            ),
          ),
        );
      },
    );
  }
}