import 'package:flutter/material.dart'; import 'package:initial_folder/providers/total_price_provider.dart'; import 'package:initial_folder/screens/checkout/components/course_list.dart'; import 'package:initial_folder/screens/checkout/detail_zero_payment.dart'; import 'package:initial_folder/size_config.dart'; import 'package:initial_folder/theme.dart'; import 'package:intl/intl.dart'; import 'package:provider/provider.dart'; import 'package:initial_folder/providers/order_provider.dart' as provOrder; import '../../providers/payments_provider.dart'; import 'snap_payment_page.dart'; class CheckoutCartCouponPage extends StatefulWidget { CheckoutCartCouponPage({ Key? key, required this.idCart, this.potonganKupon, this.discountHarga, this.isKupon, }) : super(key: key); final List idCart; final int? potonganKupon; final int? discountHarga; final bool? isKupon; @override State createState() => _CheckoutCartCouponPageState(); } class _CheckoutCartCouponPageState extends State { final controller = TextEditingController(); @override Widget build(BuildContext context) { final priceCoupon = Provider.of(context).priceCoupon; final totalPrice2 = Provider.of(context).totalPrice; final totalPrice3 = Provider.of(context).totalPrices; final typeCoupon = Provider.of(context).typeCoupon; final finalPriceCoupon = Provider.of(context).finalPriceCoupon; final potonganKupon2 = Provider.of(context).potonganKupon; Widget bottomNav(String totalPrice) { String totalHarga = "Rp. ${NumberFormat.currency(locale: 'id', symbol: '', decimalDigits: 0).format(totalPrice == '0' ? widget.discountHarga : double.parse(totalPrice))}"; return Container( width: double.infinity, height: getProportionateScreenHeight(64), decoration: BoxDecoration( color: Theme.of(context).colorScheme.background, boxShadow: [ BoxShadow( color: Theme.of(context).colorScheme.onPrimary.withOpacity(0.1), spreadRadius: 4, blurRadius: 15, offset: Offset(0, 4), ), ], ), child: Row( children: [ SizedBox(width: 13), Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ SizedBox(height: getProportionateScreenHeight(6)), Text( "Total", style: thirdTextStyle.copyWith( fontSize: getProportionateScreenHeight(12), fontWeight: semiBold, ), ), SizedBox(height: getProportionateScreenHeight(2)), if (widget.isKupon == null) Text( 'Rp. ${NumberFormat.currency(locale: 'id', symbol: '', decimalDigits: 0).format(finalPriceCoupon! < 50000 ? finalPriceCoupon + 5000 : finalPriceCoupon)}', style: thirdTextStyle.copyWith( fontSize: SizeConfig.blockVertical! * 2.5, letterSpacing: 0.23, ), ), if (widget.isKupon == true) Text( 'Rp. ${NumberFormat.currency(locale: 'id', symbol: '', decimalDigits: 0).format(widget.discountHarga! < 50000 ? widget.discountHarga! + 5000 : widget.discountHarga)}', style: thirdTextStyle.copyWith( fontSize: SizeConfig.blockVertical! * 2.5, letterSpacing: 0.23, ), ), SizedBox(height: getProportionateScreenHeight(2)), ], ), Spacer(), GestureDetector( onTap: () async { print("Ini id cart? ${widget.idCart}"); if (totalHarga == 'Rp. 0') { // Jika harga total adalah 0, arahkan ke halaman zero-payment Navigator.of(context).push( MaterialPageRoute( builder: (context) => DetailZeroPayment(idCart: widget.idCart), ), ); } else { // harga dan id transaksi untuk Snap Midtrans String orderId = 'order-${DateTime.now().millisecondsSinceEpoch}'; int grossAmount = int.parse(totalHarga.replaceAll('Rp. ', '').replaceAll(',', '')); // Persiapkan data_invoice berdasarkan pesanan List> dataInvoice = widget.idCart.map((id) { return { 'id_kursus': id, 'title_kursus': 'Nama Kursus', 'harga': grossAmount.toString(), 'qty': '1', }; }).toList(); // Panggil Snap melalui provider final paymentProvider = Provider.of(context, listen: false); Map? paymentResponse = await paymentProvider.startSnapPayment( orderId: orderId, grossAmount: grossAmount, dataInvoice: dataInvoice, ); if (paymentResponse != null) { // Jika Snap berhasil, tampilkan halaman WebView Snap Navigator.of(context).push( MaterialPageRoute( builder: (context) => SnapPaymentPage( transactionToken: paymentResponse['transactionToken'] ?? '', orderId: orderId, grossAmount: grossAmount, courseTitle: '', courseThumbnail: '', courseInstructor: '', courseId: '', ), ), ); } else { print("Pembayaran gagal"); } } }, child: Container( margin: EdgeInsets.only(right: getProportionateScreenWidth(15)), width: getProportionateScreenWidth(135), height: getProportionateScreenHeight(35), decoration: BoxDecoration( color: primaryColor, borderRadius: BorderRadius.circular(10), ), child: Center( child: Text( 'Pilih Pembayaran', style: thirdTextStyle.copyWith( fontSize: SizeConfig.blockVertical! * 1.7, fontWeight: semiBold, letterSpacing: 0.5, color: baruTextutih, ), ), ), ), ) ], ), ); } return Scaffold( appBar: AppBar( backgroundColor: Theme.of(context).colorScheme.background, centerTitle: true, title: Text( 'Checkout', style: thirdTextStyle.copyWith( letterSpacing: 1, fontWeight: semiBold, fontSize: getProportionateScreenWidth(14)), ), ), body: SingleChildScrollView( child: Container( margin: EdgeInsets.symmetric(horizontal: getProportionateScreenWidth(16)), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( 'Detail Pesanan', style: thirdTextStyle.copyWith( letterSpacing: 1, fontWeight: semiBold, fontSize: getProportionateScreenWidth(14), ), ), SizedBox(height: getProportionateScreenHeight(20)), Consumer( builder: (context, state, _) { print(state.orders.map((e) => e.idCourse)); return Column( children: [ ...state.orders.map( (order) => CourseList( idCourse: order.idCourse, title: order.title, price: widget.isKupon == null ? finalPriceCoupon.toString() : (potonganKupon2 == 0 ? priceCoupon.toString() : (potonganKupon2! > totalPrice2! ? finalPriceCoupon.toString() : totalPrice3.toString())), discountPrice: order.discountPrice, imageUrl: order.imageUrl, ), ), ], ); }, ), SizedBox(height: getProportionateScreenHeight(5)), Text( 'Rincian Harga', style: thirdTextStyle.copyWith( letterSpacing: 1, fontWeight: semiBold, fontSize: getProportionateScreenWidth(14), ), ), SizedBox(height: getProportionateScreenHeight(10)), Row( children: [ Text( 'Subtotal Harga Kursus', style: thirdTextStyle.copyWith( fontWeight: reguler, fontSize: getProportionateScreenWidth(10), ), ), Spacer(), if (widget.isKupon == null) Text( 'Rp. ${NumberFormat.currency(locale: 'id', symbol: '', decimalDigits: 0).format(priceCoupon!)}', style: thirdTextStyle.copyWith( letterSpacing: 0.5, fontWeight: reguler, fontSize: getProportionateScreenWidth(12), ), ), if (widget.isKupon == true) Text( 'Rp. ${NumberFormat.currency(locale: 'id', symbol: '', decimalDigits: 0).format(finalPriceCoupon == 0 ? priceCoupon : (potonganKupon2! > totalPrice2! ? finalPriceCoupon : totalPrice3))}', style: thirdTextStyle.copyWith( letterSpacing: 0.5, fontWeight: reguler, fontSize: getProportionateScreenWidth(12), ), ), ], ), SizedBox(height: getProportionateScreenHeight(5)), Row( children: [ Text( 'Biaya Layanan', style: thirdTextStyle.copyWith( fontWeight: reguler, fontSize: getProportionateScreenWidth(10), ), ), Spacer(), Text( 'Rp ${NumberFormat.currency(locale: 'id', symbol: '', decimalDigits: 0).format(double.parse(finalPriceCoupon! < 5000 ? "5000" : "0"))}', style: thirdTextStyle.copyWith( fontWeight: reguler, fontSize: getProportionateScreenWidth(12), ), ), ], ), SizedBox(height: getProportionateScreenHeight(5)), Row( children: [ Text( 'Potongan Kupon', style: primaryTextStyle.copyWith( fontWeight: reguler, fontSize: getProportionateScreenWidth(10), ), ), Spacer(), Text( 'Rp ${NumberFormat.currency(locale: 'id', symbol: '', decimalDigits: 0).format(double.parse(typeCoupon != "3" ? widget.potonganKupon.toString() : (priceCoupon! - widget.potonganKupon!).toString()))}', style: primaryTextStyle.copyWith( fontWeight: reguler, fontSize: getProportionateScreenWidth(12), ), ), ], ), SizedBox(height: getProportionateScreenHeight(8)), Row( children: [ Text( 'Total Bayar', style: thirdTextStyle.copyWith( fontWeight: reguler, fontSize: getProportionateScreenWidth(14), ), ), Spacer(), if (widget.isKupon == null) Text( 'Rp. ${NumberFormat.currency(locale: 'id', symbol: '', decimalDigits: 0).format(finalPriceCoupon! < 50000 ? finalPriceCoupon + 5000 : finalPriceCoupon)}', style: thirdTextStyle.copyWith( fontWeight: semiBold, fontSize: getProportionateScreenWidth(14), ), ), if (widget.isKupon == true) Text( 'Rp. ${NumberFormat.currency(locale: 'id', symbol: '', decimalDigits: 0).format(widget.discountHarga! < 50000 ? widget.discountHarga! + 5000 : widget.discountHarga)}', style: thirdTextStyle.copyWith( fontWeight: semiBold, fontSize: getProportionateScreenWidth(14), ), ), ], ), Row( children: [ Text(''), Spacer(), if (widget.isKupon == null) Text( (int.parse(Provider.of(context) .totalPrice!) != 0 && int.parse( Provider.of(context).totalPrice!) < 50000) || finalPriceCoupon! < 50000 ? "+ admin Rp 5000" : "", style: secondaryTextStyle.copyWith( letterSpacing: 1, fontWeight: reguler, fontSize: getProportionateScreenWidth(10), ), ), if (widget.isKupon != null) Text( (widget.discountHarga != 0 && widget.discountHarga! < 50000) ? "+ admin Rp 5000" : "", style: secondaryTextStyle.copyWith( letterSpacing: 1, fontWeight: reguler, fontSize: getProportionateScreenWidth(10), ), ), ], ), ], ), ), ), bottomNavigationBar: bottomNav( (int.parse(Provider.of(context).totalPrice as String)).toString(), ), ); } @override void dispose() { controller.dispose(); super.dispose(); } }