import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:supplier_new/common/settings.dart';
import 'package:supplier_new/login/login_signup_screen.dart';

import 'otp_screen.dart';

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

  @override
  State<SignUpMobileNumberScreen> createState() => _SignUpMobileNumberScreenState();
}

class _SignUpMobileNumberScreenState extends State<SignUpMobileNumberScreen> {
  TextEditingController mobileNumberController = TextEditingController();
  final _formKey = GlobalKey<FormState>();
  @override
  Widget build(BuildContext context) {

    return Scaffold(
      backgroundColor: Colors.white,
        body: SingleChildScrollView(
          child: Padding(
            padding: EdgeInsets.all(24),
            child: Column(
              children: [
                SizedBox(height: MediaQuery.of(context).size.height * .2),
                Center(
                  child: Text("AQUICK SUPPLIER",
                      style: fontTextStyle(20, Color(0XFF515253), FontWeight.w800)),
                ),
                SizedBox(height: MediaQuery.of(context).size.height * .05),
                CircleAvatar(radius: 80, backgroundColor: Color(0XFFF3F1FB)),
                SizedBox(height: MediaQuery.of(context).size.height * .05),
                Center(
                  child: Text(
                    "Welcome to Aquick Supplier",
                    style: fontTextStyle(20, Color(0XFF343637), FontWeight.w700),
                  ),
                ),
                SizedBox(height: MediaQuery.of(context).size.height * .004),
                Center(
                  child: Text(
                    "Sign up to be listed as a supplier, start deliveries and track orders",
                    style: fontTextStyle(12, Color(0XFF7E7F80), FontWeight.w400),
                    textAlign: TextAlign.center, // Keeps text centered in multiple lines
                  ),
                ),
                SizedBox(height:MediaQuery.of(context).size.height * .016,),

                Form(
                  key: _formKey,
                  child: Column(
                    children: [

                      TextFormField(
                        controller: mobileNumberController,
                        keyboardType: TextInputType.number,
                        maxLength: 10,
                        autovalidateMode: AutovalidateMode.onUserInteraction,

                        decoration: textFormFieldDecoration(Icons.phone,'Mobile Number'),

                        style: fontTextStyle(14, Color(0XFF2A2A2A), FontWeight.w400),
                        cursorColor: Color(0XFF8270DB),

                        inputFormatters: [

                          FilteringTextInputFormatter.digitsOnly,
                          TextInputFormatter.withFunction(
                                (oldValue, newValue) {

                              if (newValue.text.isEmpty) {
                                return newValue;
                              }

                              // First digit must be 6-9
                              if (!RegExp(r'^[6-9]').hasMatch(newValue.text)) {
                                return oldValue;
                              }

                              return newValue;
                            },
                          ),
                        ],

                        validator: (value) {

                          if (value == null || value.isEmpty) {
                            return "Please enter mobile number";
                          }

                          if (!RegExp(r'^[6-9]').hasMatch(value)) {
                            return "Please enter digits 6,7,8,9";
                          }

                          if (value.length != 10) {
                            return "Enter valid 10 digit number";
                          }

                          return null;
                        },
                      ),

                    ],
                  ),
                ),

                SizedBox(height: MediaQuery.of(context).size.height * .04),
                Container(
                    width: double.infinity,
                    height: MediaQuery.of(context).size.height * .06,
                    child: ElevatedButton(
                      style: ElevatedButton.styleFrom(
                        foregroundColor: Colors.white,
                        backgroundColor: primaryColor,
                        shape: RoundedRectangleBorder(
                          borderRadius:
                          BorderRadius.circular(24.0), // Customize the radius
                        ),
                      ),
                      onPressed: () async {

                        if (!_formKey.currentState!.validate()) {
                          return;
                        }

                        AppSettings.preLoaderDialog(context);

                        bool isOnline =
                        await AppSettings.internetConnectivity();

                        if(isOnline){

                          var payload = {
                            "mobileNumbers":
                            mobileNumberController.text.trim()
                          };

                          bool forgotPwd =
                          await AppSettings.getOtp(payload);

                          Navigator.of(context,
                              rootNavigator: true).pop();

                          if(forgotPwd){

                            Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (__)=>Otpscreen(
                                      mobileNumber:
                                      mobileNumberController.text.trim(),
                                    )));

                          }
                          else{

                            AppSettings.longFailedToast(
                                'Please enter valid registered mobile number');

                          }

                        }
                        else{

                          Navigator.of(context,
                              rootNavigator: true).pop();

                          AppSettings.longFailedToast(
                              "Please Check internet");

                        }

                      },
                      child: Text(
                        'Continue',
                        style: fontTextStyle(14, Color(0XFFFFFFFF), FontWeight.w600),
                      ),
                    )),
                SizedBox(height: MediaQuery.of(context).size.height * .012),
                Container(
                  width: double.infinity,
                  height: MediaQuery.of(context).size.height * .06,
                  child: ElevatedButton(
                    style: ElevatedButton.styleFrom(
                      foregroundColor: Color(0XFF757575),
                      backgroundColor: Colors.white,
                      shape: RoundedRectangleBorder(
                        borderRadius:
                        BorderRadius.circular(24.0), // Customize the radius
                        side: BorderSide(
                          color: Color(0XFF757575), // Border color
                          width: 1, // Border width
                        ),
                      ),
                    ),
                    onPressed: () async {
                    Navigator.pop(context);
                    },
                    child: Text(
                      'Back',
                      style: fontTextStyle(14, Color(0XFF646566), FontWeight.w500),
                    ),
                  ),
                )
              ],
            ),
          ),
        )
        );
  }
}
