import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

import '../../../../Controller/More/Staffs/Addstaff/Addstaffcontroller.dart';
import '../../../../Helper/Constants/Colorconstants.dart';
import '../../../../Helper/custombutton.dart';
import '../../../../Helper/customederaction.dart';

class AddStaffView extends StatelessWidget {
  final bool location;

   AddStaffView({super.key, required this.location});

   AddStaffController addStaffController = Get.put(AddStaffController());

  @override
  Widget build(BuildContext context) {
    return  Scaffold(
      backgroundColor: ColorConstants.lightColor,
      appBar: AppBar(
        elevation: 0.5,
        leading: InkWell(
            onTap: () {
              Navigator.pop(context);
            },
            child: Icon(
              Icons.arrow_back,
              color: ColorConstants.secondaryColor,
              size: 28,
            )),
        titleSpacing: 1,
        title: Text('Add Staff',style: TextStyle(color: ColorConstants.secondaryColor)),
        backgroundColor: ColorConstants.primaryColor,
      ),
      body: Container(
        child: ListView(
          children: [
            GetBuilder<AddStaffController>(
              initState: (_){
                addStaffController.getLocation();
              },

                builder: (controller) {
                  return Container(
                    child: Padding(
                      padding: const EdgeInsets.only(
                          left: 10.0, right: 10.0, top: 10, bottom: 70),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          SizedBox(
                            height: 20,
                          ),
                          firstName(context),
                          SizedBox(
                            height: 12,
                          ),
                          lastName(context),
                          SizedBox(
                            height: 12,
                          ),

                          Container(
                            child: contactNo(context),
                            height: 55,
                            decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.all(Radius.circular(7)),
                            ),
                          ),
                          SizedBox(
                            height: 12,
                          ),
                          email(context),
                          SizedBox(
                            height: 12,
                          ),
                          SizedBox(
                            height: 8,
                          ),
                          Container(
                            height: 110,
                            width: Get.width,
                            decoration: BoxDecoration(
                              color: Colors.white,
                              border: Border.all(
                                color: Color.fromRGBO(220, 220, 220, 0.6),
                              ),
                              borderRadius: BorderRadius.all(
                                Radius.circular(7),
                              ),
                            ),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                SizedBox(height: 10,),
                                Padding(
                                  padding: const EdgeInsets.only(left: 10),
                                  child: Text(
                                    addStaffController.isLocation.isTrue ?'Selected Location :':"Selected Location :",
                                    style: TextStyle(fontSize: 18, color: Colors.grey[500]),
                                  ),
                                ),
                                SizedBox(height: 10,),
                                addStaffController.isLocation.value == true
                                    ? Container(
                                  height: 45,
                                  margin: EdgeInsets.only(left: 10),
                                  padding: EdgeInsets.symmetric(horizontal: 10),
                                  decoration: BoxDecoration(
                                    color: ColorConstants.primaryColor,

                                    shape: BoxShape.rectangle,
                                    border: Border.all(
                                        color:  Colors.white
                                    ),
                                    borderRadius: BorderRadius.all(
                                      Radius.circular(5),
                                    ),
                                  ),
                                  // width: 150,
                                  child: Row(
                                    mainAxisSize: MainAxisSize.min,

                                    mainAxisAlignment: MainAxisAlignment.center,
                                    children: [
                                      Container(
                                        height: 45,


                                        child: Center(
                                          child: Icon(
                                            Icons.store,
                                            color: ColorConstants.secondaryColor,

                                            size: 30,
                                          ),
                                        ),
                                      ),
                                      SizedBox(
                                        width: 5,
                                      ),
                                      Container(width: 1,color: Colors.white,),
                                      SizedBox(
                                        width: 5,
                                      ),
                                      Text(controller.locationName??'',
                                          textAlign: TextAlign.center,
                                          maxLines: 2,
                                          style: TextStyle(
                                              fontSize: 15,
                                              color: Colors.white,
                                              height: 0.9,
                                              fontWeight:  FontWeight.bold
                                          )),
                                    ],
                                  ),
                                )
                                    :
                                Expanded(

                                  child: ListView.builder(
                                    padding: EdgeInsets.only(left: 0),
                                    scrollDirection: Axis.horizontal,
                                    physics: ScrollPhysics(),
                                    itemCount: addStaffController.tenantLocation?.length,
                                    itemBuilder: (BuildContext context, int index) {
                                      return Padding(
                                        padding:
                                        const EdgeInsets.only(left: 5.0, top: 10,bottom: 12,right: 10),
                                        child: GetX<AddStaffController>(

                                            builder: (controller) {
                                              return InkWell(
                                                onTap: () {
                                                  controller.locationId.value = controller.tenantLocation?[index].locationid??0;
                                                  controller.locationAddress = controller.tenantLocation?[index].locationaddress??'';
                                                  controller.locationSuburb = controller.tenantLocation?[index].locationsuburb??'';
                                                  controller.locationCity = controller.tenantLocation?[index].locationcity??'';
                                                  controller.locationState = controller.tenantLocation?[index].locationstate??'';
                                                  controller.locationPostcode = controller.tenantLocation?[index].locationpostcode??'';
                                                  FocusScope.of(context).unfocus();

                                                  print('locationAddressonTap${controller.locationAddress}');
                                                },
                                                child: Container(
                                                  height: 40,
                                                  padding: EdgeInsets.symmetric(horizontal: 5),
                                                  decoration: BoxDecoration(
                                                    color:  controller.locationId.value == controller.tenantLocation?[index].locationid
                                                        ? ColorConstants.primaryColor
                                                        : Colors.white,
                                                    shape: BoxShape.rectangle,
                                                    border: Border.all(
                                                        color: controller.locationId.value ==
                                                            controller.tenantLocation?[index].locationid
                                                            ? Colors.white
                                                            : Colors.grey[300]!),
                                                    borderRadius: BorderRadius.all(
                                                      Radius.circular(5),
                                                    ),
                                                  ),
                                                  child: Row(
                                                    mainAxisAlignment:
                                                    MainAxisAlignment.spaceEvenly,
                                                    children: [

                                                      Container(
                                                        child: Center(
                                                          child: Icon(
                                                            Icons.store,
                                                            color: controller.locationId.value ==
                                                                controller.tenantLocation?[index].locationid
                                                                ? ColorConstants.secondaryColor
                                                                : Colors.grey[400],
                                                            size: 30,
                                                          ),
                                                        ),
                                                      ),
                                                      SizedBox(
                                                        width: 5,

                                                        // height: 5,
                                                      ),
                                                      SizedBox(
                                                        width: 1,
                                                        child: Container(color:
                                                        controller.locationId.value ==
                                                            controller.tenantLocation?[index].locationid? Colors.white:Colors.grey[300],),
                                                        // height: 5,
                                                      ),
                                                      SizedBox(
                                                        width: 5,

                                                        // height: 5,
                                                      ),
                                                      Text(  controller.tenantLocation?[index].locationname??'',
                                                          textAlign: TextAlign.center,
                                                          maxLines: 2,
                                                          style: TextStyle(
                                                              fontSize: 12,
                                                              color:controller.locationId.value ==
                                                                  controller.tenantLocation?[index].locationid? Colors.white: Colors.grey[600],
                                                              height: 0.9,
                                                              fontWeight:
                                                              controller.locationId.value ==
                                                                  controller.tenantLocation?[index].locationid
                                                                  ? FontWeight.bold
                                                                  : FontWeight.w500)),
                                                    ],
                                                  ),
                                                ),
                                              );
                                            }
                                        ),
                                      );
                                    },
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  );
                }
            )
          ],
        ),
      ),
      bottomNavigationBar: SizedBox(
        height: Get.height*0.08,
        child:  Row(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Center(
                child: Padding(
                  padding: const EdgeInsets.only(bottom: 11,top: 0),
                  child: RoundedButton(
                    onPressed: () {
                      addStaffController.describeValidation();
                    },
                    color:ColorConstants.primaryColor,
                    title: 'Create',
                    controller: addStaffController.btnController,
                    textStyle: const TextStyle(
                        fontSize: 18,
                        fontFamily: "Lato",
                        color: Colors.white,
                        fontWeight: FontWeight.bold),
                    height: Get.height*0.06,
                    width: Get.width*0.8,
                  ),
                )),
          ],
        ),

      ),

    );
  }

  String? validateEmail(String? value) {
    const pattern = r"(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'"
        r'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-'
        r'\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*'
        r'[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4]'
        r'[0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9]'
        r'[0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\'
        r'x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])';
    final regex = RegExp(pattern);

    return value!.isNotEmpty && !regex.hasMatch(value)
        ? 'Enter a valid email address'
        : null;
  }


  Widget firstName(context) {
    return Container(
      decoration: BoxDecoration(
        color: ColorConstants.secondaryColor,
      ),
      child: TextFormField(
        textCapitalization: TextCapitalization.sentences,
        inputFormatters: <TextInputFormatter>[
          UpperCaseTextFormatter()
        ],
        decoration: CustomDecoration.textFieldStyle(
            labelTextStr: 'First Name',
            prefixIcon: Icon(
              Icons.person,
              color: ColorConstants.primaryColor,
            )),
        controller: addStaffController.nameController,
        // focusNode: aadharNoNode,
        onEditingComplete: () {
          FocusScope.of(context).requestFocus(FocusNode());
        },
      ),
    );
  }

  Widget lastName(context) {
    return Container(
      decoration: BoxDecoration(
        color: ColorConstants.secondaryColor,
      ),
      child: TextFormField(
        textCapitalization: TextCapitalization.sentences,
        inputFormatters: <TextInputFormatter>[
          UpperCaseTextFormatter()
        ],
        decoration: CustomDecoration.textFieldStyle(
            labelTextStr: 'Last Name',
            prefixIcon: Icon(
              Icons.person,
              color: ColorConstants.primaryColor,
            )),
        controller: addStaffController.lastNameController,
        // focusNode: aadharNoNode,
        onEditingComplete: () {
          FocusScope.of(context).requestFocus(FocusNode());
        },
      ),
    );
  }

  Widget contactNo(context) {
    return TextFormField(
      keyboardType:
      TextInputType.numberWithOptions(signed: true, decimal: true),
      maxLength: 10,
      controller: addStaffController.phoneNoController,
      buildCounter: (BuildContext context,
          {int? currentLength, int? maxLength, bool? isFocused}) =>
      null,
      onChanged: (contactNo){
        if(contactNo.length==10){
          FocusScope.of(context).unfocus();
        }
      },
      decoration: InputDecoration(
        labelText: 'Contact no',
        labelStyle: TextStyle(color: Colors.grey),
        prefixIcon:Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              width: Get.width * 0.2,
              height: Get.height * 0.04,
              child: Row(
                children: [
                  Spacer(flex: 1,),
                  Flexible(
                    child: Icon(
                      Icons.phone_android_outlined,
                      color:  ColorConstants.primaryColor,
                    ),
                  ),
                  Spacer(flex: 1,),
                  Flexible(flex: 3,child: Text("+91",style: TextStyle(color:  ColorConstants.primaryColor,fontWeight: FontWeight.bold))),
                ],
              ),
            ),
          ],
        ),

        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(7.0),
          borderSide: BorderSide(
            color: Color.fromRGBO(220, 220, 220, 0.6),
            width: 2.0,
          ),
        ),
        enabledBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(7.0),
          borderSide: BorderSide(
            color: Color.fromRGBO(220, 220, 220, 0.6),
            width: 2.0,
          ),
        ),
        focusedBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(7.0),
          borderSide: BorderSide(
            color: ColorConstants.primaryColor,
            width: 1.0,
          ),
        ),

      ),
    );
  }

  Widget email(context) {
    return Container(
      decoration: BoxDecoration(
        color: ColorConstants.secondaryColor,
      ),
      child: Form(
        autovalidateMode: AutovalidateMode.always,
        child: TextFormField(
          validator: validateEmail,
          decoration: CustomDecoration.textFieldStyle(
              labelTextStr: 'Email',
              prefixIcon: Icon(
                Icons.mail_outline,
                color: ColorConstants.primaryColor,
              )),
          controller: addStaffController.emailController,
          onEditingComplete: () {
            FocusScope.of(context).requestFocus(FocusNode());
          },
        ),
      ),
    );
  }
}


class UpperCaseTextFormatter extends TextInputFormatter {
  @override
  TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
    return TextEditingValue(
      text: capitalize(newValue.text),
      selection: newValue.selection,
    );
  }
}
String capitalize(String value) {
  if(value.trim().isEmpty) return "";
  return "${value[0].toUpperCase()}${value.substring(1)}";
}

