import 'package:flutter/material.dart';
import 'package:initial_folder/models/zero_price_model.dart';
import 'package:initial_folder/providers/cart_provider.dart';
import 'package:initial_folder/providers/carts_provider.dart';
import 'package:initial_folder/providers/my_course_provider.dart';
import 'package:initial_folder/providers/order_provider.dart';
import 'package:initial_folder/providers/payments_provider.dart';
import 'package:initial_folder/providers/user_info_provider.dart' as userInfo;
import 'package:initial_folder/screens/checkout/batas_bayar.dart';
import 'package:initial_folder/screens/checkout/success_paid_course.dart';
import 'package:initial_folder/size_config.dart';
import 'package:initial_folder/theme.dart';
import 'package:initial_folder/widgets/login_regist/default_button.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';

class DetailZeroPayment extends StatelessWidget {
  final String? discountPrice;
  final String? price;
  final List<String>? idCart;
  const DetailZeroPayment(
      {Key? key, this.idCart, this.discountPrice, this.price})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    Future<void> deleteCourse() async {
      List<String> idCarts = idCart!;

      idCarts.forEach((element) async {
        await Provider.of<CartProvider>(context, listen: false)
            .deleteCart(element);
        await Provider.of<CartsProvider>(context, listen: false).getCarts();
      });
    }

    var invoice = Provider.of<OrderProvider>(context, listen: false).invoice;
    var totalPrice =
        Provider.of<OrderProvider>(context, listen: false).totalPrice!;
    var orders = Provider.of<OrderProvider>(context, listen: false).orders;
    print(orders);
    var zero = Provider.of<PaymentsProvider>(context).zeroPrice;
    Widget bottomNav() {
      return Container(
        margin: EdgeInsets.symmetric(
            horizontal: getProportionateScreenWidth(90),
            vertical: getProportionateScreenHeight(10)),
        child: DefaultButton(
          text: 'Lanjutkan',
          press: () {
            Provider.of<PaymentsProvider>(context, listen: false)
                .zeroPayment(invoice, totalPrice.toString());
            deleteCourse();
            Navigator.of(context).push(
                MaterialPageRoute(builder: (context) => SuccessPaidCourse()));
          },
        ),
      );
    }

    Widget listCourse({String? title, String? instructor, String? price}) {
      return Container(
        padding: EdgeInsets.symmetric(vertical: 8),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Flexible(
              flex: 7,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    title!,
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                    style: primaryTextStyle.copyWith(
                      letterSpacing: 0.5,
                      fontWeight: reguler,
                      fontSize: getProportionateScreenWidth(11),
                      color: tenthColor,
                    ),
                  ),
                  SizedBox(height: 4),
                  Text(
                    'Oleh $instructor',
                    style: primaryTextStyle.copyWith(
                      letterSpacing: 0.5,
                      fontWeight: reguler,
                      fontSize: getProportionateScreenWidth(9.5),
                      color: secondaryColor,
                    ),
                  ),
                ],
              ),
            ),
            Flexible(
              flex: 3,
              child: Text(
                'Rp. ${NumberFormat.currency(locale: 'id', symbol: '', decimalDigits: 0).format(double.parse(price!))}',
                style: primaryTextStyle.copyWith(
                  letterSpacing: 0.5,
                  fontWeight: reguler,
                  fontSize: getProportionateScreenWidth(12),
                  color: tenthColor,
                ),
              ),
            ),
          ],
        ),
      );
    }

    var user = Provider.of<userInfo.UserInfoProvider>(context).result;
    // var zero = Provider.of<PaymentsProvider>(context).zeroPrice;
    return Scaffold(
      //backgroundColor: Colors.black,
      appBar: AppBar(
        title: Text(
          'Checkout',
          style: secondaryTextStyle.copyWith(
              letterSpacing: 1,
              fontWeight: semiBold,
              fontSize: getProportionateScreenWidth(14)),
        ),
      ),
      body: SingleChildScrollView(
        child: Container(
          margin:
              EdgeInsets.symmetric(horizontal: getProportionateScreenWidth(16)),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              SizedBox(height: 10),
              Container(
                decoration: BoxDecoration(
                  color: Color(0xFF212121),
                  borderRadius: BorderRadius.circular(8),
                ),
                margin: EdgeInsets.only(
                    left: getProportionateScreenWidth(2),
                    right: getProportionateScreenWidth(2)),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Container(
                            padding: EdgeInsets.symmetric(
                                vertical: 15,
                                horizontal: getProportionateScreenWidth(15)),
                            child: Text(
                              'Informasi Pembeli',
                              style: secondaryTextStyle.copyWith(
                                letterSpacing: 1,
                                fontWeight: semiBold,
                                fontSize: getProportionateScreenWidth(14),
                                color: tenthColor,
                              ),
                            ),
                          ),
                          Divider(
                              color: Color(0xff2D2D2D),
                              thickness: 0.5,
                              height: 1),
                          Container(
                            padding: EdgeInsets.symmetric(
                                vertical: 10,
                                horizontal: getProportionateScreenWidth(15)),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                // Text(
                                //   'Order ID',
                                //   style: primaryTextStyle.copyWith(
                                //     letterSpacing: 0.5,
                                //     fontWeight: reguler,
                                //     fontSize: getProportionateScreenWidth(10),
                                //     color: secondaryColor,
                                //   ),
                                // ),
                                // Text(
                                //   // state.detailOrder[0].idOrder,
                                //   // zero!.data![0].orderId as String,
                                //   '123',
                                //   style: primaryTextStyle.copyWith(
                                //     letterSpacing: 0.5,
                                //     fontWeight: reguler,
                                //     fontSize: getProportionateScreenWidth(12),
                                //     color: tenthColor,
                                //   ),
                                // ),
                                // SizedBox(height: 16),
                                Text(
                                  'Nama Lengkap',
                                  style: primaryTextStyle.copyWith(
                                    letterSpacing: 0.5,
                                    fontWeight: reguler,
                                    fontSize: getProportionateScreenWidth(10),
                                    color: secondaryColor,
                                  ),
                                ),
                                Text(
                                  user!.data[0].fullname as String,
                                  style: primaryTextStyle.copyWith(
                                    letterSpacing: 0.5,
                                    fontWeight: reguler,
                                    fontSize: getProportionateScreenWidth(12),
                                    color: tenthColor,
                                  ),
                                ),
                                SizedBox(height: 16),
                                Text(
                                  'Email',
                                  style: primaryTextStyle.copyWith(
                                    letterSpacing: 0.5,
                                    fontWeight: reguler,
                                    fontSize: getProportionateScreenWidth(10),
                                    color: secondaryColor,
                                  ),
                                ),
                                Text(
                                  user.data[0].email as String,
                                  style: primaryTextStyle.copyWith(
                                    letterSpacing: 0.5,
                                    fontWeight: reguler,
                                    fontSize: getProportionateScreenWidth(12),
                                    color: tenthColor,
                                  ),
                                ),
                                SizedBox(height: 12),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
              SizedBox(
                height: getProportionateScreenHeight(20),
              ),
              Container(
                decoration: BoxDecoration(
                  color: Color(0xFF212121),
                  borderRadius: BorderRadius.circular(8),
                ),
                margin: EdgeInsets.only(
                    left: getProportionateScreenWidth(2),
                    right: getProportionateScreenWidth(2)),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Container(
                            padding: EdgeInsets.symmetric(
                                vertical: 15,
                                horizontal: getProportionateScreenWidth(15)),
                            child: Text(
                              'Informasi Pembeli',
                              style: secondaryTextStyle.copyWith(
                                letterSpacing: 1,
                                fontWeight: semiBold,
                                fontSize: getProportionateScreenWidth(14),
                                color: tenthColor,
                              ),
                            ),
                          ),
                          Divider(
                              color: Color(0xff2D2D2D),
                              thickness: 0.5,
                              height: 1),
                          Container(
                            padding: EdgeInsets.symmetric(
                                vertical: 15,
                                horizontal: getProportionateScreenWidth(15)),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                SizedBox(height: 20),
                                Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                  children: [
                                    Text(
                                      'Total Harga',
                                      style: primaryTextStyle.copyWith(
                                        letterSpacing: 0.5,
                                        fontWeight: reguler,
                                        fontSize:
                                            getProportionateScreenWidth(10),
                                        color: secondaryColor,
                                      ),
                                    ),
                                    discountPrice != "0"
                                        ? Text(
                                            "Rp. ${NumberFormat.currency(locale: 'id', symbol: '', decimalDigits: 0).format(double.parse(discountPrice!))}",
                                            style: primaryTextStyle.copyWith(
                                              letterSpacing: 0.5,
                                              fontWeight: reguler,
                                              fontSize:
                                                  getProportionateScreenWidth(
                                                      12),
                                              color: tenthColor,
                                            ),
                                          )
                                        : Text(
                                            "Rp. ${NumberFormat.currency(locale: 'id', symbol: '', decimalDigits: 0).format(double.parse(price!))}",
                                            style: primaryTextStyle.copyWith(
                                              letterSpacing: 0.5,
                                              fontWeight: reguler,
                                              fontSize:
                                                  getProportionateScreenWidth(
                                                      12),
                                              color: tenthColor,
                                            ),
                                          ),
                                  ],
                                ),
                                SizedBox(height: 16),
                                Divider(
                                    color: Color(0xff2D2D2D),
                                    thickness: 0.5,
                                    height: 1),
                                SizedBox(height: 16),
                                Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                  children: [
                                    Text(
                                      'Total Bayar',
                                      style: primaryTextStyle.copyWith(
                                        letterSpacing: 0.5,
                                        fontWeight: reguler,
                                        fontSize:
                                            getProportionateScreenWidth(12),
                                        color: secondaryColor,
                                      ),
                                    ),
                                    Text(
                                      'Rp. 0',
                                      style: secondaryTextStyle.copyWith(
                                        letterSpacing: 1,
                                        fontWeight: semiBold,
                                        fontSize:
                                            getProportionateScreenWidth(14),
                                        color: tenthColor,
                                      ),
                                    ),
                                  ],
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
              SizedBox(
                height: getProportionateScreenHeight(20),
              ),
              Container(
                decoration: BoxDecoration(
                  color: Color(0xFF212121),
                  borderRadius: BorderRadius.circular(8),
                ),
                margin: EdgeInsets.only(
                    left: getProportionateScreenWidth(2),
                    right: getProportionateScreenWidth(2)),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Container(
                            padding: EdgeInsets.symmetric(
                                vertical: 15,
                                horizontal: getProportionateScreenWidth(15)),
                            child: Text(
                              'Kursus yang dibeli',
                              style: secondaryTextStyle.copyWith(
                                letterSpacing: 1,
                                fontWeight: semiBold,
                                fontSize: getProportionateScreenWidth(14),
                                color: tenthColor,
                              ),
                            ),
                          ),
                          Divider(
                              color: Color(0xff2D2D2D),
                              thickness: 0.5,
                              height: 1),
                          Container(
                            padding: EdgeInsets.symmetric(
                                vertical: 15,
                                horizontal: getProportionateScreenWidth(15)),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: orders.map((e) {
                                return listCourse(
                                    instructor: e.instructor,
                                    title: e.title,
                                    price: e.discountPrice);
                              }).toList(),
                            ),
                          )
                        ],
                      ),
                    ),
                  ],
                ),
              ),
              SizedBox(
                height: getProportionateScreenHeight(15),
              ),
              bottomNav(),
              SizedBox(
                height: getProportionateScreenHeight(15),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
