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

import 'package:shimmer/shimmer.dart';
import '../../../Controller/More/Locations/Locationcontroller.dart';
import '../../../Helper/Constants/AssetConstants.dart';
import '../../../Helper/Constants/Colorconstants.dart';
import '../../../Helper/utility.dart';
import '../../Home/Homeview.dart';
import 'Addlocation/Addlocationview.dart';
import 'Staffs/Staffdetailsview.dart';

class LocationView extends StatelessWidget {
   LocationView({super.key});
  LocationController  locationController = Get.put(LocationController());


  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: ()async{
        Get.to(()=>HomeView(selectedIndex: 3));
        return false;
      },
      child: Scaffold(
        backgroundColor: ColorConstants.lightColor,
        appBar: AppBar(
          elevation: 0.5,
          leading: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Container(
                height: 40,
                width: 40,
                child: CircleAvatar(
                  backgroundColor: ColorConstants. primaryColor,
                  // radius: 20,
                  backgroundImage: AssetImage(AssetConstants.NearleAppbarIcon),
                ),
              ),
            ],
          ),
          titleSpacing: 1,
          title: Text('Locations'),
          backgroundColor:  ColorConstants.primaryColor,
        ),
        body: GetBuilder<LocationController>(
            initState: (_){
              locationController.getLocation();
            },
            builder: (controller) {
              return Stack(
                children: [
                  Column(
                    children: [
                      SizedBox(
                        height: 8,
                      ),
                      Expanded(
                        flex: 9,
                        child: Padding(
                          padding: const EdgeInsets.only(left: 5, right: 5, top: 5),
                          child: controller.shimmer == true
                              ? shimmerLocation()
                              : controller.locations.isEmpty
                              ? Center(
                              child: Column(
                                mainAxisAlignment: MainAxisAlignment.center,
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: [
                                  Image(
                                    height: 160,
                                    width: 160,
                                    image: AssetImage(AssetConstants.NoRecords),
                                  ),
                                  SizedBox(
                                    height: 8,
                                  ),
                                  Text(
                                    "No Locations at this moment",
                                    style: TextStyle(
                                        color: Colors.grey[600],
                                        fontSize: 16),
                                  ),
                                ],
                              ))
                              : listLocationMobile(),
                        ),
                      )
                    ],
                  ),
                  Positioned(
                    bottom: 5,
                    left: 0,
                    right: 0,
                    child: Container(
                      margin: EdgeInsets.symmetric(horizontal: 20, vertical: 10),
                      height: 60,
                      // width: Get.width * 0.8,
                      decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(30), color: ColorConstants.primaryColor),
                      child: Center(
                        child: Row(
                          mainAxisSize: MainAxisSize.min,
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            Expanded(
                              child: InkWell(
                                onTap: () {
                                  print('hello');
                                  Navigator.of(context)
                                      .pushReplacement(MaterialPageRoute(
                                      builder: (context) => HomeView(
                                        selectedIndex: 0,
                                      )));
                                },
                                child: Column(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    Icon(
                                      Icons.home,
                                      color: Colors.white,
                                    ),
                                    SizedBox(
                                      height: 3,
                                    ),
                                    Text(
                                      'Home',
                                      style: TextStyle(color: Colors.white),
                                    )
                                  ],
                                ),
                              ),
                            ),
                            SizedBox(
                              height: 60,
                              width: 0.5,
                              child: Container(
                                color: Colors.white,
                              ),
                            ),
                            Expanded(
                              child: InkWell(
                                onTap: () {
                                 Get.to(()=>AddLocationView(editMode: false,));
                                },
                                child: Column(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    Icon(
                                      Icons.add_location_alt_sharp,
                                      color: Colors.white,
                                    ),
                                    SizedBox(
                                      height: 3,
                                    ),
                                    Text(
                                      'Add Location',
                                      style: TextStyle(color: Colors.white),
                                    )
                                  ],
                                ),
                              ),
                            ),
                            SizedBox(
                              height: 60,
                              width: 0.5,
                              child: Container(
                                color: Colors.white,
                              ),
                            ),
                            Expanded(
                              child: InkWell(
                                onTap: () {
                                  Get.to(HomeView(selectedIndex: 3));
                                },
                                child: Column(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    Icon(
                                      Icons.arrow_back,
                                      color: Colors.white,
                                    ),
                                    SizedBox(
                                      height: 3,
                                    ),
                                    Text(
                                      'Back',
                                      style: TextStyle(color: Colors.white),
                                    )
                                  ],
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                ],
              );
            }
        ),
      ),
    );
  }

   Widget listLocationMobile() {
     return GetBuilder<LocationController>(
         builder: (controller) {
           return ListView.builder(
               padding: EdgeInsets.only(bottom: 70),
               itemCount: controller.locations.length,
               itemBuilder: (BuildContext context, int index) {
                 return Card(
                   // color: Colors.grey[150],
                     elevation: 0,
                     child:  Container(
                       height: 120,
                       child: Row(
                         children: [
                           Expanded(
                             flex: 2,
                             child: Container(
                               decoration: BoxDecoration(
                                 color: UtilityPromotion.getLightColors(
                                     controller.locations[index].locationname?[0] ??
                                         'A'.toString().trimLeft())
                                     .withAlpha(200),
                                 borderRadius: BorderRadius.circular(5),
                               ),
                               child: Column(
                                 mainAxisAlignment: MainAxisAlignment.center,
                                 children: [
                                   Stack(
                                     children: [
                                       Container(
                                           padding: EdgeInsets.all(12),
                                           decoration: BoxDecoration(
                                               color: Colors.white70,
                                               shape: BoxShape.circle),
                                           child: Icon(
                                             Icons.storefront,
                                             color: ColorConstants.primaryColor,
                                             size: 50,
                                           )),
                                       Positioned(
                                         bottom: 0,
                                         right: 0,
                                         child: GestureDetector(
                                           onTap: () {
                                             Get.to(()=> AddLocationView(editMode: true,
                                               locationInfo: controller.locations[index],
                                             ),);

                                           },
                                           child: Container(
                                             decoration: BoxDecoration(
                                               color: ColorConstants.primaryColor,
                                               shape: BoxShape.circle,
                                               // borderRadius: BorderRadius.circular(12),
                                             ),
                                             child: Padding(
                                               padding: const EdgeInsets.symmetric(
                                                   vertical: 6, horizontal: 6),
                                               child: Icon(
                                                 Icons.edit,
                                                 color: Colors.white,
                                                 size: 16,
                                               ),
                                             ),
                                           ),
                                         ),
                                       )

                                     ],
                                   )
                                 ],
                               ),
                             ),
                           ),
                           SizedBox(
                             width: 10,
                           ),
                           Expanded(
                             flex: 4,
                             child: Column(
                               mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                               children: [
                                 // SizedBox(
                                 //   height: 3,
                                 // ),
                                 Row(
                                   children: [
                                     Icon(
                                       Icons.location_on,
                                       color: Colors.black45,
                                       size: 17,
                                     ),
                                     SizedBox(
                                       width: 5,
                                     ),
                                     Flexible(
                                       flex: 3,
                                       child: Text(
                                         "${ controller.locations[index].locationname}",
                                         overflow: TextOverflow.ellipsis,
                                         style: TextStyle(
                                             fontSize: 18,
                                             color: Colors.black54,
                                             fontWeight: FontWeight.w600),
                                       ),
                                     ),
                                     SizedBox(
                                       width: 3,
                                     ),
                                   ],
                                 ),

                                 Row(
                                   children: [
                                     Icon(
                                       Icons.phone_android_sharp,
                                       color: Colors.black45,
                                       size: 16,
                                     ),
                                     SizedBox(
                                       width: 5,
                                     ),
                                     Text(
                                       controller.locations[index].locationcontact == null
                                           ? "Contact No"
                                           : '${ controller.locations[index].locationcontact}',
                                       style: TextStyle(
                                           fontSize: 15,
                                           color: Colors.black87,
                                           fontWeight: FontWeight.w400),
                                     ),
                                   ],
                                 ),

                                 Row(
                                   crossAxisAlignment: CrossAxisAlignment.center,
                                   mainAxisAlignment: MainAxisAlignment.start,
                                   children: [
                                     Icon(
                                       Icons.location_on_outlined,
                                       size: 16,
                                       color: Colors.black45,
                                     ),
                                     SizedBox(
                                       width: 4,
                                     ),
                                     Flexible(
                                       child: Text(
                                         "${ controller.locations[index].locationsuburb}",
                                         overflow: TextOverflow.ellipsis,
                                         style: TextStyle(
                                             fontSize: 15, color: Colors.black87

                                           //                                  fontWeight: FontWeight.w600
                                         ),
                                       ),
                                     ),
                                   ],
                                 ),
                               ],
                             ),
                           ),
                         ],
                       ),
                     ),

                     // ExpansionTile(
                     //   maintainState: true,
                     //
                     //   backgroundColor: ColorConstants.secondaryColor,
                     //   tilePadding: EdgeInsets.only(left: 8, top: 2),
                     //   initiallyExpanded: true,
                     //
                     //   childrenPadding: EdgeInsets.only(left: 2, right: 2, bottom: 0),
                     //   trailing: Padding(
                     //     padding: const EdgeInsets.only(top: 30.0, right: 10),
                     //     child: Column(
                     //       mainAxisSize: MainAxisSize.max,
                     //       children: [
                     //         Expanded(
                     //           // flex: 3,
                     //           child: Icon(
                     //             Icons.keyboard_arrow_down_sharp,
                     //             color: Colors.black87,
                     //             size: 24,
                     //           ),
                     //         ),
                     //       ],
                     //     ),
                     //   ),
                     //   title: Container(
                     //     height: 120,
                     //     child: Row(
                     //       children: [
                     //         Expanded(
                     //           flex: 2,
                     //           child: Container(
                     //             decoration: BoxDecoration(
                     //               color: UtilityPromotion.getLightColors(
                     //                   controller.locations[index].locationname?[0] ??
                     //                       'A'.toString().trimLeft())
                     //                   .withAlpha(200),
                     //               borderRadius: BorderRadius.circular(5),
                     //             ),
                     //             child: Column(
                     //               mainAxisAlignment: MainAxisAlignment.center,
                     //               children: [
                     //                 Stack(
                     //                   children: [
                     //                     Container(
                     //                         padding: EdgeInsets.all(12),
                     //                         decoration: BoxDecoration(
                     //                             color: Colors.white70,
                     //                             shape: BoxShape.circle),
                     //                         child: Icon(
                     //                           Icons.storefront,
                     //                           color: ColorConstants.primaryColor,
                     //                           size: 50,
                     //                         )),
                     //                     Positioned(
                     //                       bottom: 0,
                     //                       right: 0,
                     //                       child: GestureDetector(
                     //                         onTap: () {
                     //                           Get.to(()=> AddLocationView(editMode: true,
                     //                             locationInfo: controller.locations[index],
                     //                           ),);
                     //
                     //                         },
                     //                         child: Container(
                     //                           decoration: BoxDecoration(
                     //                             color: ColorConstants.primaryColor,
                     //                             shape: BoxShape.circle,
                     //                             // borderRadius: BorderRadius.circular(12),
                     //                           ),
                     //                           child: Padding(
                     //                             padding: const EdgeInsets.symmetric(
                     //                                 vertical: 6, horizontal: 6),
                     //                             child: Icon(
                     //                               Icons.edit,
                     //                               color: Colors.white,
                     //                               size: 16,
                     //                             ),
                     //                           ),
                     //                         ),
                     //                       ),
                     //                     )
                     //
                     //                   ],
                     //                 )
                     //               ],
                     //             ),
                     //           ),
                     //         ),
                     //         SizedBox(
                     //           width: 10,
                     //         ),
                     //         Expanded(
                     //           flex: 4,
                     //           child: Column(
                     //             mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                     //             children: [
                     //               // SizedBox(
                     //               //   height: 3,
                     //               // ),
                     //               Row(
                     //                 children: [
                     //                   Icon(
                     //                     Icons.location_on,
                     //                     color: Colors.black45,
                     //                     size: 17,
                     //                   ),
                     //                   SizedBox(
                     //                     width: 5,
                     //                   ),
                     //                   Flexible(
                     //                     flex: 3,
                     //                     child: Text(
                     //                       "${ controller.locations[index].locationname}",
                     //                       overflow: TextOverflow.ellipsis,
                     //                       style: TextStyle(
                     //                           fontSize: 18,
                     //                           color: Colors.black54,
                     //                           fontWeight: FontWeight.w600),
                     //                     ),
                     //                   ),
                     //                   SizedBox(
                     //                     width: 3,
                     //                   ),
                     //                 ],
                     //               ),
                     //
                     //               Row(
                     //                 children: [
                     //                   Icon(
                     //                     Icons.phone_android_sharp,
                     //                     color: Colors.black45,
                     //                     size: 16,
                     //                   ),
                     //                   SizedBox(
                     //                     width: 5,
                     //                   ),
                     //                   Text(
                     //                     controller.locations[index].locationcontact == null
                     //                         ? "Contact No"
                     //                         : '${ controller.locations[index].locationcontact}',
                     //                     style: TextStyle(
                     //                         fontSize: 15,
                     //                         color: Colors.black87,
                     //                         fontWeight: FontWeight.w400),
                     //                   ),
                     //                 ],
                     //               ),
                     //
                     //               Row(
                     //                 crossAxisAlignment: CrossAxisAlignment.center,
                     //                 mainAxisAlignment: MainAxisAlignment.start,
                     //                 children: [
                     //                   Icon(
                     //                     Icons.location_on_outlined,
                     //                     size: 16,
                     //                     color: Colors.black45,
                     //                   ),
                     //                   SizedBox(
                     //                     width: 4,
                     //                   ),
                     //                   Flexible(
                     //                     child: Text(
                     //                       "${ controller.locations[index].locationsuburb}",
                     //                       overflow: TextOverflow.ellipsis,
                     //                       style: TextStyle(
                     //                           fontSize: 15, color: Colors.black87
                     //
                     //                         //                                  fontWeight: FontWeight.w600
                     //                       ),
                     //                     ),
                     //                   ),
                     //                 ],
                     //               ),
                     //             ],
                     //           ),
                     //         ),
                     //       ],
                     //     ),
                     //   ),
                     //   children: [
                     //     Divider(
                     //       thickness: 1,
                     //       color: Colors.grey[300],
                     //     ),
                     //     Container(
                     //       margin: EdgeInsets.only(top: 10, bottom: 0, right: 10),
                     //       height:  90,
                     //       child: Column(
                     //         mainAxisAlignment: MainAxisAlignment.center,
                     //         crossAxisAlignment: CrossAxisAlignment.start,
                     //         children: [
                     //           Expanded(
                     //             flex: 7,
                     //             child: Row(
                     //               mainAxisAlignment: MainAxisAlignment.spaceAround,
                     //               children: [
                     //                 Expanded(
                     //                   child: InkWell(
                     //                     onTap: () {
                     //                      Get.to(()=> AddLocationView(editMode: true,
                     //                        locationInfo: controller.locations[index],
                     //                      ),);
                     //
                     //                     },
                     //                     child: Column(children: [
                     //
                     //                       Container(
                     //                         height: 45,
                     //                         width: 45,
                     //                         decoration: BoxDecoration(
                     //                             shape: BoxShape.circle,
                     //                             border: Border.all(
                     //                                 color: ColorConstants.primaryColor)),
                     //                         child: Center(
                     //                           child: Icon(
                     //                             Icons.edit_location_alt,
                     //                             color: ColorConstants.primaryColor,
                     //                             size: 30,
                     //                           ),
                     //                         ),
                     //                       ),
                     //                       SizedBox(
                     //                         height: 5,
                     //                       ),
                     //                       Text("Location",
                     //                           textAlign: TextAlign.center,
                     //                           style: TextStyle(
                     //                               fontSize: 14,
                     //                               color: Colors.grey[800],
                     //                               height: 0.8)),
                     //                     ]),
                     //                   ),
                     //                 ),
                     //                 Expanded(
                     //                   child: InkWell(
                     //                     onTap: () {
                     //
                     //                       Get.to(()=>StaffsDetails(staffData: [], locationName:controller.locations[index].locationname??'',));
                     //
                     //                     },
                     //                     child: Column(children: [
                     //
                     //                       Container(
                     //                         height: 45,
                     //                         width: 45,
                     //                         decoration: BoxDecoration(
                     //                             shape: BoxShape.circle,
                     //                             border: Border.all(
                     //                                 color: ColorConstants.primaryColor)),
                     //                         child: Center(
                     //                           child: Icon(
                     //                             Icons.person,
                     //                             color: ColorConstants.primaryColor,
                     //                             size: 30,
                     //                           ),
                     //                         ),
                     //                       ),
                     //                       SizedBox(
                     //                         height: 5,
                     //                       ),
                     //                       Text("Staff's",
                     //                           textAlign: TextAlign.center,
                     //                           style: TextStyle(
                     //                               fontSize: 14,
                     //                               color: Colors.grey[800],
                     //                               height: 0.8)),
                     //                     ]),
                     //                   ),
                     //                 ),
                     //
                     //                 Expanded(
                     //                   child: Container(),
                     //                 ),
                     //                 Expanded(
                     //                   child: Container(),
                     //                 ),
                     //               ],
                     //             ),
                     //           ),
                     //           // controller.moduleId != 4?Divider(
                     //           //   thickness: 2,
                     //           //   color: Colors.grey[100],
                     //           // ):SizedBox(),
                     //           // controller. moduleId != 4?Expanded(
                     //           //     flex: 8,
                     //           //     child: Row(
                     //           //       mainAxisAlignment: MainAxisAlignment.spaceAround,
                     //           //       children: [
                     //           //         Expanded(
                     //           //           child: InkWell(
                     //           //             onTap: () {
                     //           //
                     //           //             },
                     //           //             child: Column(children: [
                     //           //               SizedBox(
                     //           //                 height: 15,
                     //           //               ),
                     //           //               Container(
                     //           //                 height: 45,
                     //           //                 width: 45,
                     //           //                 decoration: BoxDecoration(
                     //           //                     shape: BoxShape.circle,
                     //           //                     border: Border.all(
                     //           //                         color: primaryColor)),
                     //           //                 child: Center(
                     //           //                   child: Icon(
                     //           //                     Icons.person,
                     //           //                     color: primaryColor,
                     //           //                     size: 30,
                     //           //                   ),
                     //           //                 ),
                     //           //               ),
                     //           //               SizedBox(
                     //           //                 height: 10,
                     //           //               ),
                     //           //               Text("Staff's",
                     //           //                   textAlign: TextAlign.center,
                     //           //                   style: TextStyle(
                     //           //                       fontSize: 14,
                     //           //                       color: Colors.grey[800],
                     //           //                       height: 0.8)),
                     //           //             ]),
                     //           //           ),
                     //           //         ),
                     //           //         Expanded(
                     //           //           child: Container(),
                     //           //         ),
                     //           //         Expanded(
                     //           //           child: Container(),
                     //           //         ),
                     //           //         Expanded(
                     //           //           child: Container(),
                     //           //         )
                     //           //       ],
                     //           //     )):SizedBox(),
                     //         ],
                     //       ),
                     //     )
                     //   ],
                     // )
                 );
               });
         }
     );
   }

   shimmerLocation(){
    return Shimmer.fromColors(
      baseColor: Colors.grey[300]!,
      highlightColor: Colors.grey[100]!,

      enabled: true,
      child: ListView.builder(
        itemBuilder: (_, __) => Padding(
            padding: const EdgeInsets.only(
              bottom: 8.0,
            ),
            child: Column(
              children: [
                Container(
                  height: 120,
                  decoration: BoxDecoration(
                      border: Border.all(
                          color: ColorConstants.secondaryColor!,width: 3)),
                  child: Row(
                    children: [
                      Expanded(
                        flex: 3,
                        child: Container(

                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Container(
                                decoration: BoxDecoration(
                                    shape: BoxShape.rectangle,
                                    color: Colors.white),
                                child: Padding(
                                  padding: const EdgeInsets.all(13.0),
                                  child: Container(
                                    height: 50,
                                    width: 50,
                                    decoration: BoxDecoration(
                                        color: ColorConstants.secondaryColor),
                                  ),
                                ),
                              ),

                            ],
                          ),
                        ),
                      ),
                      Expanded(
                        flex: 7,
                        child: Container(

                          child: Column(
                            // mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              SizedBox(
                                height: 20,
                              ),
                              Row(
                                children: [
                                  SizedBox(
                                    width: 10,
                                  ),
                                  Container(
                                    height: 12,
                                    width: 150,
                                    color: ColorConstants.secondaryColor,
                                  ),
                                ],
                              ),
                              SizedBox(
                                height:17,
                              ),
                              Row(
                                children: [
                                  SizedBox(
                                    width: 10,
                                  ),
                                  Container(
                                    height: 12,
                                    width: 150,
                                    color: ColorConstants.secondaryColor,
                                  ),
                                  SizedBox(
                                    width: 6,
                                  ),

                                ],
                              ),
                              SizedBox(
                                height:17,
                              ),
                              Row(
                                children: [
                                  SizedBox(
                                    width: 10,
                                  ),
                                  Container(
                                    height: 12,
                                    width: 150,
                                    color: ColorConstants.secondaryColor,
                                  ),
                                ],
                              ),

                            ],
                          ),
                        ),
                      )
                    ],
                  ),
                ),
                SizedBox(
                  height: 10,
                )
              ],
            )
        ),
        itemCount: 6,
      ),
    );
   }

}

