import 'dart:math';

import 'package:flutter/material.dart';
import 'package:huixiang/generated/l10n.dart';
import 'package:huixiang/retrofit/data/coupon.dart';
import 'package:huixiang/view_widget/custom_image.dart';
import 'package:huixiang/view_widget/round_button.dart';
import 'package:huixiang/view_widget/separator.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class CouponWidget extends StatelessWidget {
  final GestureTapCallback callback;
  final Coupon coupon;

  CouponWidget(this.coupon, this.callback);

//
//   @override
//   State<StatefulWidget> createState() {
//     return _CouponWidget();
//   }
// }
//
// class _CouponWidget extends State<CouponWidget> {
  @override
  Widget build(BuildContext context) {
    return buildCoupon(context);
  }

  Widget buildCoupon(BuildContext context) {
    return Container(
      margin: EdgeInsets.fromLTRB(14, 6, 14, 6),
      child: AspectRatio(
        aspectRatio: 2.485,
        child: Stack(
          children: [
            Image.asset(
              coupon.status != 3
                  ? "assets/image/ic_coupon_bg.png"
                  : "assets/image/ic_coupon_invalid_bg.png",
              fit: BoxFit.cover,
              width: double.infinity,
              height: double.infinity,
            ),
            if (coupon.status == 3)
              Positioned(
                top: 10.h,
                right: 0,
                child: Transform.rotate(
                  angle: pi / 5.0,
                  child: Text(
                    S.of(context).yishijiao, //已失效
                    style: TextStyle(
                      fontSize: 8.sp,
                      color: Color(0xFF353535),
                    ),
                  ),
                ),
              ),
            Opacity(
              opacity: coupon.status != 3 ? 1 : 0.54,
              child: Container(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Expanded(
                      flex: 7,
                      child: Container(
                        padding: EdgeInsets.only(top: 18, bottom: 20),
                        child: Row(
                          children: [
                            Container(
                                margin: EdgeInsets.only(left: 20),
                                child: MImage(
                                  (coupon == null ||
                                          coupon.couponImg == null ||
                                          coupon.couponImg == "")
                                      ? ""
                                      : coupon.couponImg,
                                )
                                // Image.network(
                                //   width: 60,
                                //   height: 60,
                                //   fit: BoxFit.cover,
                                //   errorBuilder: (context, error, e) {
                                //     return Image.asset(
                                //         "assets/image/default_1.png");
                                //   },
                                // ),
                                ),
                            Expanded(
                              flex: 1,
                              child: Container(
                                height: 60.h,
                                margin: EdgeInsets.only(left: 12),
                                child: Column(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceAround,
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Container(
                                      margin: EdgeInsets.only(right: 15),
                                      child: Row(
                                        children: [
                                          Expanded(
                                            flex: 1,
                                            child: Text(
                                              coupon.couponName ?? "",
                                              style: TextStyle(
                                                fontWeight: FontWeight.bold,
                                                fontSize: 14.sp,
                                                color: Color(0xFF353535),
                                              ),
                                            ),
                                          ),
                                          (coupon.status == 0)
                                              ? Row(
                                                  children: [
                                                    RoundButton(
                                                      width: 42.w,
                                                      height: 21.h,
                                                      text:
                                                          S.of(context).lingqu,
                                                      textColor: Colors.white,
                                                      fontSize: 12.sp,
                                                      padding: EdgeInsets.only(
                                                          top: 2,
                                                          bottom: 2,
                                                          left: 8,
                                                          right: 8),
                                                      backgroup:
                                                          Color(0xff32A060),
                                                      callback: () {
                                                        showAlertDialog(
                                                            context);
                                                      },
                                                    ),
                                                  ],
                                                )
                                              : (coupon.status == 1)
                                                  ? Row(
                                                      children: [
                                                        Text(
                                                          S
                                                              .of(context)
                                                              .qushiyong,
                                                          style: TextStyle(
                                                            color: Color(
                                                                0xFF32A060),
                                                            fontSize: 12.sp,
                                                          ),
                                                        ),
                                                        Icon(
                                                          Icons
                                                              .keyboard_arrow_right,
                                                          size: 16,
                                                          color:
                                                              Color(0xFF32A060),
                                                        )
                                                      ],
                                                    )
                                                  : Container(),
                                        ],
                                      ),
                                    ),
                                    Expanded(
                                      flex: 1,
                                      child: Container(
                                        margin: EdgeInsets.only(right: 37),
                                        alignment: Alignment.centerLeft,
                                        child: Text(
                                          coupon.couponDescription ?? "",
                                          style: TextStyle(
                                            fontSize: 10.sp,
                                            color: Color(0xFF4C4C4C),
                                          ),
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.only(left: 23, right: 23),
                      child: MySeparator(
                        width: 5,
                        height: 1,
                        color: Color(0xFF353535),
                      ),
                    ),
                    Expanded(
                      flex: 3,
                      child: Container(
                        padding: EdgeInsets.only(left: 20),
                        alignment: Alignment.centerLeft,
                        child: Text(
                          coupon.status == 0
                              ? S
                                  .of(context)
                                  .faxingshijian(coupon.publishStartTime)
                              : coupon.status == 1
                                  ? S
                                      .of(context)
                                      .lingqushijian(coupon.receiveTime)
                                  : S.of(context).shiyongriqi,
                          style: TextStyle(
                            color: Color(0xFF727272),
                            fontSize: 10.sp,
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  showAlertDialog(BuildContext context) {
    //显示对话框
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return SimpleDialog(
          titlePadding: EdgeInsets.all(10),
          backgroundColor: Colors.transparent,
          elevation: 0,
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.all(Radius.circular(6))),
          children: <Widget>[
            Stack(
              alignment: Alignment.topCenter,
              children: [
                Container(
                  margin: EdgeInsets.only(top: 40),
                  decoration: new BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.all(Radius.circular(8))),
                  width: 218,
                  height: 210,
                  child: Column(
                    children: [
                      Padding(
                        padding: EdgeInsets.only(top: 50, bottom: 19),
                        child: Text(
                          S.of(context).lingquchenggong,
                          style: TextStyle(
                              color: Color(0xff353535),
                              fontSize: 16.sp,
                              fontWeight: FontWeight.bold),
                        ),
                      ),
                      Text(
                        S.of(context).ninyichenggonglingquyouhuiquan,
                        style: TextStyle(
                            color: Color(0xff353535), fontSize: 14.sp),
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          GestureDetector(
                            child: Text(
                              S.of(context).kabaokeshiyong,
                              style: TextStyle(
                                  color: Color(0xffFF7A1A),
                                  fontSize: 14.sp,
                                  fontWeight: FontWeight.bold),
                            ),
                            onTap: callback,
                          ),
                          Text(
                            S.of(context).zhongchakan,
                            style: TextStyle(
                                color: Color(0xff353535), fontSize: 14.sp),
                          ),
                        ],
                      ),
                      Padding(
                        padding: EdgeInsets.only(top: 30.h),
                        child: RoundButton(
                          width: 130.w,
                          height: 34.h,
                          text: S.of(context).queren,
                          textColor: Colors.white,
                          fontSize: 12.sp,
                          callback: () {
                            Navigator.of(context).pop();
                          },
                          padding: EdgeInsets.only(
                              top: 2, bottom: 2, left: 8, right: 8),
                          backgroup: Color(0xff32A060),
                        ),
                      )
                    ],
                  ),
                ),
                Image.asset(
                  "assets/image/icon_order_success.png",
                  fit: BoxFit.cover,
                  width: 80,
                  height: 80,
                ),
              ],
            )
          ],
        );
      },
    );
  }
}