import 'package:flutter/material.dart';
import 'package:huixiang/generated/l10n.dart';
import 'package:huixiang/retrofit/data/goods.dart';
import 'package:huixiang/utils/flutter_utils.dart';
import 'package:huixiang/utils/font_weight.dart';
import 'package:huixiang/view_widget/border_text.dart';
import 'package:huixiang/view_widget/custom_image.dart';
import 'package:huixiang/view_widget/item_title.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class HomeIntegralStore extends StatefulWidget {

  final GestureTapCallback callback;
  final List<Goods> gooods;

  HomeIntegralStore(this.gooods, this.callback);

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

}

class _HomeIntegralStore extends State<HomeIntegralStore> {
  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(
          left: 16.5.w, right: 16.5.w, bottom: 40.h, top: 10.h),
      padding: EdgeInsets.only(bottom: 10.h, top: 16.h),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(4),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withAlpha(12),
            offset: Offset(0, 3),
            blurRadius: 14,
            spreadRadius: 0,
          )
        ],
        color: Colors.white,
      ),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Expanded(
                flex: 1,
                child: ItemTitle(
                  text: S.of(context).jifenshangcheng,
                  imgPath: "assets/image/icon_points_mall.png",
                ),
              ),
              GestureDetector(
                onTap: widget.callback,
                child: Container(
                  padding: EdgeInsets.symmetric(
                    vertical: 3.h, horizontal: 8.w,),
                  margin: EdgeInsets.only(right: 16.w),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(11.5),
                    color: Color(0xFF32A060),
                  ),
                  child: Row(
                    children: [
                      Text(
                        "GO",
                        style: TextStyle(
                          fontSize: 14.sp,
                          fontWeight: FontWeight.bold,
                          color: Colors.white,
                        ),
                      ),
                      Icon(
                        Icons.chevron_right,
                        color: Colors.white,
                        size: 18,
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
          SizedBox(
            height: 14.h,
          ),
          Divider(
            // indent: 0.0,
            thickness: 1,
            color: Color(0xffF2F2F2),
          ),
          integralStore(),
        ],
      ),
    );
  }

  Widget integralStore() {
    return GridView.builder(
      itemCount: (widget.gooods != null && widget.gooods.length > 0) ? (widget.gooods.length > 2 ? 2 : widget.gooods.length) : 0,
      padding: EdgeInsets.all(13.w),
      shrinkWrap: true,
      physics: NeverScrollableScrollPhysics(),
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        //一行的Widget数量
        crossAxisCount: 2,
        //水平子Widget之间间距
        crossAxisSpacing: 13,
        //垂直子Widget之间间距
        mainAxisSpacing: 16,
        //子Widget宽高比例
        childAspectRatio: AppUtils.textScale(context) > 1
            ? (1 - ((AppUtils.textScale(context) - 1)))
            : 0.85,
      ),
      itemBuilder: (context, index) {
        return GestureDetector(
          onTap: () {
            Navigator.of(context).pushNamed('/router/integral_store_page',
                arguments: {"goodsId": widget.gooods[index].id});
          },
          child: buildItem(widget.gooods[index]),
        );
      },
    );
  }


  Widget buildItem(Goods goods) {
    return Container(
      alignment: Alignment.center,
      child: Column(
        children: [
          MImage(
            goods.mainImgPath,
            aspectRatio: 5 / 3,
            radius: BorderRadius.circular(4),
            fit: BoxFit.cover,
            errorSrc: "assets/image/default_1.png",
            fadeSrc: "assets/image/default_1.png",
          ),
          SizedBox(
            height: 4.h,
          ),
          Container(
            margin: EdgeInsets.only(left: 8.w, right: 8.w),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                SizedBox(
                  height: 14.h,
                ),
                Row(
                  children: [
                    Expanded(
                      flex: 1,
                      child: Text(
                        goods.name,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(
                          color: Color(0xff353535),
                          fontWeight: MyFontWeight.regular,
                          fontSize: 14.sp,
                        ),
                      ),
                    ),
                    BorderText(
                      padding: EdgeInsets.all(2.h),
                      text: S.of(context).haowu,
                      fontSize: 12.sp,
                      fontWeight: FontWeight.bold,
                      textColor: Color(0xFF32A060),
                      borderColor: Color(0xFF32A060),
                      borderWidth: 1.w,
                    ),
                  ],
                ),
                SizedBox(
                  height: 5.h,
                ),
                Text(
                  goods.description,
                  overflow: TextOverflow.ellipsis,
                  maxLines: 2,
                  style: TextStyle(
                    color: Color(0xFF727272),
                    fontSize: 12.sp,
                    fontWeight: MyFontWeight.regular,
                  ),
                ),
              ],
            ),
          )
        ],
      ),
    );
  }

}