import 'package:flutter/material.dart';

import '../common/settings.dart';

class CancelOrderScreen extends StatefulWidget {
  var order;
  var status;
  CancelOrderScreen({this.order, this.status});

  @override
  State<CancelOrderScreen> createState() => _CancelOrderScreenState();
}

class _CancelOrderScreenState extends State<CancelOrderScreen> {
  String? selectedReason;
  final TextEditingController reasonController = TextEditingController();

  final List<String> reasons = [
    "Tanker not available",
    "Vehicle got damaged",
    "Buyer asked me to cancel",
    "Other",
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: Colors.white,
        elevation: 0,
        scrolledUnderElevation: 0,
        title: Text(
          'Cancel Order',
          style: fontTextStyle(14, Color(0XFF2A2A2A), FontWeight.w500),
        ),
        iconTheme: IconThemeData(color: Color(0XFF2A2A2A)),
        leading: GestureDetector(
          onTap: () {
            Navigator.pop(context);
          },
          child: Padding(
            padding:
            const EdgeInsets.fromLTRB(8, 8, 8, 8), // Add padding if needed
            child: Image.asset(
              'images/backbutton_appbar.png', // Replace with your image path
              fit: BoxFit.contain,
              color: Color(0XFF2A2A2A),
              height: 24,
              width: 24,
            ),
          ),
        ),
      ),
      body: Padding(
        padding:EdgeInsets.all(24.0),
        child: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                "Why are you cancelling?",
                style: fontTextStyle(14, Color(0XFF2A2A2A), FontWeight.w600),
              ),
              SizedBox(height:MediaQuery.of(context).size.height * .008,),

              // Radio buttons
              ...reasons.map((reason) {
                return RadioListTile<String>(
                  value: reason,
                  groupValue: selectedReason,
                  activeColor: primaryColor,
                  contentPadding: EdgeInsets.zero,
                  title: Text(
                    reason,
                    style:
                    fontTextStyle(12, Color(0XFF2D2E30), FontWeight.w400),
                  ),
                  onChanged: (value) {
                    setState(() {
                      selectedReason = value;
                    });
                  },
                );
              }).toList(),

              if (selectedReason == "Other")
                Padding(
                  padding: const EdgeInsets.symmetric(vertical: 10),
                  child: TextFormField(
                    controller: reasonController,
                    maxLines: 5,
                    style: fontTextStyle(14, Color(0XFF101214), FontWeight.w400),
                    cursorColor: Color(0XFF1D7AFC),
                    textCapitalization: TextCapitalization.sentences,
                    decoration: textFormFieldDecorationHintText(
                      Icons.phone,
                      'Describe your reason..',
                    ),
                  ),
                ),

              SizedBox(height:MediaQuery.of(context).size.height * .008,),
              Text(
                "Cancellation Policy",
                style:fontTextStyle(14, Color(0XFF2A2A2A), FontWeight.w600),
              ),
              SizedBox(height:MediaQuery.of(context).size.height * .016,),
              Text(
                'Cancel anytime before delivery starts. If it’s already on the way, a ₹100 cancellation fee may apply.  Please review our full terms and conditions for more details.',
                style:fontTextStyle(12, Color(0XFF2A2A2A), FontWeight.w400),
              ),
              SizedBox(height:MediaQuery.of(context).size.height * .016,),
              GestureDetector(
                onTap: () {
                  // open terms page
                },
                child: Text(
                  "View Terms & Conditions",
                  style: fontTextStylewithUnderline(10, Color(0XFF4692FD), FontWeight.w400,Color(0XFF4692FD)),
                ),
              ),

            ],
          ),
        ),
      ),
      bottomNavigationBar: SafeArea(
        child: Padding(
          padding: const EdgeInsets.all(24.0),
          child: Row(
            children: [
              // Secondary
              Expanded(
                child: OutlinedButton(
                  onPressed: () => Navigator.pop(context),
                  style: OutlinedButton.styleFrom(
                    side: const BorderSide(color: Color(0xFF2A2A2A)),
                    padding: const EdgeInsets.symmetric(vertical: 14),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(24),
                    ),
                  ),
                  child: Text(
                    "Don’t cancel",
                    style: fontTextStyle(14, const Color(0xFF2A2A2A), FontWeight.w600),
                  ),
                ),
              ),
              const SizedBox(width: 12),
              // Primary
              Expanded(
                child: ElevatedButton(
                  onPressed: () async {
                    if (selectedReason == null) {
                      ScaffoldMessenger.of(context).showSnackBar(
                        const SnackBar(content: Text("Please select a reason")),
                      );
                      return;
                    }

                    String finalReason =
                    selectedReason == "Other"
                        ? reasonController.text.trim()
                        : selectedReason!;

                    if (finalReason.isEmpty) {
                      ScaffoldMessenger.of(context).showSnackBar(
                        const SnackBar(content: Text("Please enter cancellation reason")),
                      );
                      return;
                    }

                    bool success = await AppSettings.cancelTankerBooking(
                      widget.order.dbId,
                      "cancel",
                      finalReason,
                    );

                    if (success) {
                      ScaffoldMessenger.of(context).showSnackBar(
                        const SnackBar(content: Text("Order cancelled successfully")),
                      );
                      Navigator.pop(context, true);
                    } else {
                      ScaffoldMessenger.of(context).showSnackBar(
                        const SnackBar(content: Text("Failed to cancel order")),
                      );
                    }
                  },
                  style: ElevatedButton.styleFrom(
                    backgroundColor: const Color(0XFFE2483D),
                    padding: const EdgeInsets.symmetric(vertical: 14),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(24),
                    ),
                  ),
                  child: Text(
                    "Cancel Order",
                    style: fontTextStyle(14, const Color(0XFFFFFFFF), FontWeight.w600),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),

    );
  }
}

