import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import '../../../constants/color_constants.dart';
import '../../../constants/font_constants.dart';
import '../../../domain/provider/profile/create_request.dart';
import '../../../modules/profile/customer_request.dart';
import '../../../widgets/text_widget.dart';

class CustomerRequestPage extends StatefulWidget {
  const CustomerRequestPage({super.key});

  @override
  State<CustomerRequestPage> createState() => _CustomerRequestPageState();
}

class _CustomerRequestPageState extends State<CustomerRequestPage> {
  final _formKey = GlobalKey<FormState>();
  final TextEditingController subjectController = TextEditingController();
  final TextEditingController remarkController = TextEditingController();
  final CustomerRequestProvider provider = CustomerRequestProvider();

  Future<void> _submitRequest() async {
    if (_formKey.currentState!.validate()) {
      final model = CustomerRequestModel(
        referencedate: DateTime.now().toUtc().toIso8601String(),
        referencetype: "",
        customerid: 6164,
        tenantid: 0,
        locationid: 0,
        subject: subjectController.text.trim(),
        remarks: remarkController.text.trim(),
        status: 0,
        apptypeid: 98,
      );

      final success = await provider.sendRequest(
        subjectController.text.trim(),
        remarkController.text.trim(),
      );

      if (success) {
        Fluttertoast.showToast(
          msg: "Request submitted successfully!",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.TOP,
          backgroundColor: Colors.green,
          textColor: Colors.white,
          fontSize: 14,
        );

        Navigator.pop(context, true);
      } else {
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(content: Text("Failed to submit request!")),
        );
      }
    }
  }

  @override
  void dispose() {
    subjectController.dispose();
    remarkController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      top: false,
      child: Scaffold(
        backgroundColor: Colors.grey[100],
        appBar: AppBar(
          elevation: 0,
          leadingWidth: 200,
          leading: Row(
            children: [
              IconButton(
                onPressed: () => Navigator.of(context).pop(),
                icon: const Icon(Icons.arrow_back, color: Colors.black),
              ),
              const Expanded(
                child: ReusableTextWidget(
                  text: "Help & Support",
                  color: Colors.black,
                  fontFamily: FontConstants.fontFamily,
                  fontSize: 16,
                  fontWeight: FontWeight.bold,
                  textAlign: TextAlign.start,
                  overflow: TextOverflow.ellipsis,
                  maxLines: 1,
                ),
              ),
            ],
          ),
          backgroundColor: Colors.white,
        ),

        body: Padding(
          padding: const EdgeInsets.all(16),
          child: Form(
            key: _formKey,
            child: SingleChildScrollView(
              child: Container(
                padding: const EdgeInsets.all(18),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(16),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.black.withOpacity(0.05),
                      blurRadius: 10,
                      spreadRadius: 2,
                      offset: const Offset(0, 4),
                    )
                  ],
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [

                    /// Your Requested Text Widget Usage
                    ReusableTextWidget(
                      text: "Customer Support",
                      color: Colors.black.withOpacity(0.7),
                      fontFamily: FontConstants.fontFamily,
                      fontSize:  10,
                      fontWeight: FontWeight.bold,
                      textAlign: TextAlign.center,
                      overflow: TextOverflow.ellipsis,
                      maxLines: 1,
                    ),

                    const SizedBox(height: 20),

                    /// SUBJECT
                    const ReusableTextWidget(
                      text: "Subject",
                      color: Colors.black,
                      fontFamily: FontConstants.fontFamily,
                      fontSize: 14,
                      fontWeight: FontWeight.w600,
                    ),

                    const SizedBox(height: 8),

                    TextFormField(
                      controller: subjectController,
                      decoration: InputDecoration(
                        hintText: "Enter subject",
                        filled: true,
                        fillColor: Colors.grey[100],
                        contentPadding: const EdgeInsets.symmetric(
                            horizontal: 14, vertical: 14),
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(12),
                          borderSide: BorderSide.none,
                        ),
                      ),
                      validator: (value) =>
                      value!.isEmpty ? "Please enter subject" : null,
                    ),

                    const SizedBox(height: 20),

                    /// REMARK
                    const ReusableTextWidget(
                      text: "Remark",
                      color: Colors.black,
                      fontFamily: FontConstants.fontFamily,
                      fontSize: 14,
                      fontWeight: FontWeight.w600,
                    ),

                    const SizedBox(height: 8),

                    TextFormField(
                      controller: remarkController,
                      maxLines: 4,
                      decoration: InputDecoration(
                        hintText: "Enter your remark",
                        filled: true,
                        fillColor: Colors.grey[100],
                        contentPadding: const EdgeInsets.symmetric(
                            horizontal: 14, vertical: 14),
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(12),
                          borderSide: BorderSide.none,
                        ),
                      ),
                      validator: (value) =>
                      value!.isEmpty ? "Please enter remark" : null,
                    ),

                    const SizedBox(height: 30),

                    /// SUBMIT BUTTON
                    SizedBox(
                      width: double.infinity,
                      child: ElevatedButton(
                        onPressed: _submitRequest,
                        style: ElevatedButton.styleFrom(
                          elevation: 3,
                          backgroundColor: ColorConstants.primaryColor,
                          padding: const EdgeInsets.symmetric(vertical: 15),
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(14),
                          ),
                        ),
                        child: const Text(
                          "Submit Request",
                          style: TextStyle(
                            fontSize: 15,
                            fontWeight: FontWeight.w600,
                            color: Colors.white,
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}