import 'dart:async';

import 'package:ef/ef.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_svg/svg.dart';
import 'package:flutterflow_ui/flutterflow_ui.dart';
import 'package:vbvs_app/common/util/FitTool.dart';
import 'package:vbvs_app/common/util/MyUtils.dart';
import 'package:vbvs_app/component/tool/CustomCard.dart';
import 'package:vbvs_app/component/tool/TopSlideNotification.dart';
import 'package:vbvs_app/controller/main_bottom/global_controller.dart';
import 'package:vbvs_app/controller/mh_controller/people_info_controller.dart';

import 'package:vbvs_app/pages/common/selectDialog.dart';

class PeopleInfoPage extends GetView<PeopleInfoController> {
  Map data;
  PeopleInfoPage({required this.data});

  getLine() {
    return Divider(
      color: Color(0XFF929699),
      thickness: 0.5.rpx,
      height: 0,
    );
  }

  PeopleInfoController controller = Get.put(PeopleInfoController());
  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, boxConstraints) => GestureDetector(
          onTap: () {
            // 触摸收起键盘
            FocusScope.of(context).requestFocus(FocusNode());
          },
          child: Container(
            decoration: const BoxDecoration(
              image: DecorationImage(
                image: AssetImage('assets/images/new_background.png'), // 本地图片
                fit: BoxFit.fill, // 填满整个 Container
              ),
            ),
            child: Scaffold(
              backgroundColor: Colors.transparent,
              appBar: AppBar(
                elevation: 0,
                surfaceTintColor: Colors.transparent,
                backgroundColor: Colors.transparent,
                automaticallyImplyLeading: false,
                iconTheme: IconThemeData(color: Colors.white),
                titleSpacing: 0,
                title: SizedBox(
                  width: double.infinity,
                  height: 180.rpx,
                  child: Stack(
                    alignment: Alignment.center,
                    children: [
                      // 中间居中的标题
                      Text(
                        '人员资料'.tr,
                        textAlign: TextAlign.center,
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 30.rpx,
                        ),
                      ),
                      // 左侧图标
                      Positioned(
                        left: 0.rpx,
                        child: returnIconButtomNew,
                      ),
                      Positioned(
                          right: 30.rpx,
                          child: CustomCard(
                            borderRadius: 16.rpx,
                            gradientDirection: GradientDirection.vertical,
                            onTap: () async {
                              bool isValid = true;

                              // 数据验证
                              for (int i = 0;
                                  i < controller.model.peopleList.length;
                                  i++) {
                                var d = controller.model.peopleList[i];
                                String prefix = "人员信息${i == 0 ? "A" : "B"}：";

                                if (d['height'] != null &&
                                    d['height'].toString().isNotEmpty &&
                                    int.tryParse(d['height'].toString()) ==
                                        null) {
                                  TopSlideNotification.show(context,
                                      text: "请选择身高".tr,
                                      textColor: Color(0xFFFF7159));
                                  isValid = false;
                                  break;
                                }

                                if (d['weight'] != null &&
                                    d['weight'].toString().isNotEmpty &&
                                    int.tryParse(d['weight'].toString()) ==
                                        null) {
                                  TopSlideNotification.show(context,
                                      text: "请选择体重".tr,
                                      textColor: Color(0xFFFF7159));
                                  isValid = false;
                                  break;
                                }

                                if (d['contact'] != null &&
                                    d['contact'].toString().isNotEmpty &&
                                    !MyUtils.isValidPhoneNumber(
                                        d['contact'].toString())) {
                                  TopSlideNotification.show(context,
                                      text: "请输入正确的联系人电话".tr,
                                      textColor: Color(0xFFFF7159));
                                  isValid = false;
                                  break;
                                }
                              }

                              // 所有数据合法，开始保存
                              if (isValid) {
                                for (var data in controller.model.peopleList) {
                                  try {
                                    await controller.savePeoples(data);
                                    TopSlideNotification.show(context,
                                        text: "更新成功".tr,
                                        textColor: Color(0XFF00C1AA));
                                  } catch (e) {
                                    print("保存失败：$e");
                                    TopSlideNotification.show(context,
                                        text: "更新失败".tr,
                                        textColor: Color(0xFFFF7159));
                                  }
                                }
                              }
                            },
                            colors: const [
                              Color(0xFFFCFCFC),
                              Color(0xFFF8FAF9),
                              Color(0XFFECF6F3),
                              Color(0XFFD9F0E9),
                              Color(0xFFCEECE3)
                            ],
                            child: Container(
                              width: 120.rpx,
                              height: 60.rpx,
                              alignment: Alignment.center,
                              // decoration: BoxDecoration(
                              //   borderRadius: BorderRadius.circular(5),
                              //   color: stringToColor("#182B7C"),
                              // ),
                              child: Text(
                                "保存".tr,
                                style: TextStyle(
                                  fontFamily: 'Readex Pro',
                                  color: Color(0XFF011D33),
                                  letterSpacing: 0,
                                  fontSize: 30.rpx,
                                ),
                              ),
                            ),
                          ))
                    ],
                  ),
                ),
                centerTitle: false,
              ),
              body: SafeArea(
                top: true,
                child: Container(
                    padding: EdgeInsets.only(left: 30.rpx, right: 30.rpx),
                    width: MediaQuery.sizeOf(context).width,
                    height: MediaQuery.sizeOf(context).height * 1.123,
                    // decoration: BoxDecoration(
                    //   color: Colors.transparent,
                    //   image: DecorationImage(
                    //     image: AssetImage("assets/images/background.png"),
                    //     fit: BoxFit.cover,
                    //   ),
                    // ),
                    child: SingleChildScrollView(
                      child: Obx(() => Column(
                            children: [
                              if (controller.model.peopleList.isNotEmpty)
                                ...List.generate(
                                    controller.model.peopleList.length,
                                    (index) {
                                  final person =
                                      controller.model.peopleList[index];
                                  String location_ = '';
                                  // if ("${data["bindMacB"]}".length > 6 &&
                                  //     (person["direction"] == 1 ||
                                  //         person["direction"] == 2)) {
                                  //   location_ = person["direction"] == 1 ? '左侧' : '右侧';
                                  // }
                                  return Column(
                                    mainAxisSize: MainAxisSize.max,
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      Container(
                                        margin: EdgeInsets.only(
                                            left: 18.rpx,
                                            top: index == 0 ? 30.rpx : 90.rpx,
                                            bottom: 20.rpx),
                                        child: Text(
                                          "人员资料${index == 0 ? "A" : "B"}",
                                          style: TextStyle(
                                              color: Colors.white,
                                              fontSize: 30.rpx),
                                        ),
                                      ),
                                      Container(
                                        child: Column(
                                          children: [
                                            getLine(),
                                            Obx(
                                              () => Container(
                                                width: double.infinity,
                                                height: 90.rpx,
                                                margin: EdgeInsets.only(
                                                    left: 40.rpx,
                                                    right: 35.rpx),
                                                decoration: BoxDecoration(),
                                                child: Row(
                                                  mainAxisSize:
                                                      MainAxisSize.max,
                                                  mainAxisAlignment:
                                                      MainAxisAlignment
                                                          .spaceBetween,
                                                  children: [
                                                    Text(
                                                      '姓名',
                                                      style: TextStyle(
                                                        fontFamily:
                                                            'Readex Pro',
                                                        color:
                                                            Color(0xFF9EA4B7),
                                                        fontSize: 30.rpx,
                                                        letterSpacing: 0,
                                                      ),
                                                    ),
                                                    Container(
                                                      width: 300.rpx,
                                                      child: TextField(
                                                        obscureText: false,
                                                        textAlign:
                                                            TextAlign.right,
                                                        style: TextStyle(
                                                            fontSize: 30.rpx,
                                                            color:
                                                                Colors.white),
                                                        decoration:
                                                            const InputDecoration(
                                                                fillColor: Colors
                                                                    .transparent,
                                                                filled: true,
                                                                hintText:
                                                                    "请输入姓名",
                                                                hintStyle: TextStyle(
                                                                    color: Colors
                                                                        .white),
                                                                border:
                                                                    InputBorder
                                                                        .none,
                                                                contentPadding:
                                                                    EdgeInsets
                                                                        .all(
                                                                            0)),
                                                        onChanged: (value) {
                                                          controller.model
                                                                      .peopleList[
                                                                  index]
                                                              ["name"] = value;
                                                        },
                                                        controller: controller.onReDraw(
                                                            TextEditingController(
                                                                text: controller
                                                                            .model
                                                                            .peopleList[index]
                                                                        [
                                                                        "name"] ??
                                                                    ""),
                                                            (textEditingController) {
                                                          textEditingController
                                                              .text = controller
                                                                      .model
                                                                      .peopleList[
                                                                  index]["name"] ??
                                                              "";
                                                        }, "people_name_$index"),
                                                      ),
                                                    ),
                                                  ],
                                                ),
                                              ),
                                            ),
                                            getLine(),
                                            Obx(
                                              () => Container(
                                                  margin: EdgeInsets.only(
                                                      left: 40.rpx,
                                                      right: 35.rpx),
                                                  width: double.infinity,
                                                  height: 90.rpx,
                                                  decoration: BoxDecoration(),
                                                  child: InkWell(
                                                    onTap: () {
                                                      // 延迟执行的代码
                                                      FocusScope.of(context)
                                                          .requestFocus(
                                                              FocusNode());
                                                      Future.delayed(
                                                          const Duration(
                                                              milliseconds:
                                                                  250), () {
                                                        showOneSelectionDialog(
                                                            context,
                                                            arr: ["女", "男"],
                                                            checkIndex: controller
                                                                            .model
                                                                            .peopleList[index]
                                                                        [
                                                                        'gender'] ==
                                                                    "女"
                                                                ? 0
                                                                : 1,
                                                            checkChange:
                                                                (sindex) {
                                                          controller.model.peopleList[
                                                                      index]
                                                                  ['gender'] =
                                                              sindex; // 👈 保存为 0 / 1
                                                          controller
                                                              .updateAll();
                                                        });
                                                      });
                                                    },
                                                    child: Row(
                                                      mainAxisSize:
                                                          MainAxisSize.max,
                                                      mainAxisAlignment:
                                                          MainAxisAlignment
                                                              .spaceBetween,
                                                      children: [
                                                        Text(
                                                          '性别',
                                                          style: TextStyle(
                                                            fontFamily:
                                                                'Readex Pro',
                                                            color: Color(
                                                                0xFF9EA4B7),
                                                            fontSize: 30.rpx,
                                                            letterSpacing: 0,
                                                          ),
                                                        ),
                                                        Row(
                                                          mainAxisSize:
                                                              MainAxisSize.max,
                                                          children: [
                                                            Container(
                                                              width: 200.rpx,
                                                              child: Text(
                                                                '${controller.model.peopleList[index]['gender'] == 1 ? '男' : '女'}',
                                                                textAlign:
                                                                    TextAlign
                                                                        .right,
                                                                style:
                                                                    TextStyle(
                                                                  fontFamily:
                                                                      'Readex Pro',
                                                                  color: Colors
                                                                      .white,
                                                                  fontSize:
                                                                      30.rpx,
                                                                  letterSpacing:
                                                                      0,
                                                                ),
                                                              ),
                                                            ),
                                                            SizedBox(
                                                              width: 16.rpx,
                                                            ),
                                                            Container(
                                                                height: 30.rpx,
                                                                width: 30.rpx,
                                                                child:
                                                                    SvgPicture
                                                                        .asset(
                                                                  'assets/img/icon/expand_more.svg',
                                                                  color: Colors
                                                                      .white,
                                                                ))
                                                          ],
                                                        ),
                                                      ],
                                                    ),
                                                  )),
                                            ),
                                            getLine(),
                                            Container(
                                                width: double.infinity,
                                                height: 90.rpx,
                                                margin: EdgeInsets.only(
                                                    left: 40.rpx,
                                                    right: 35.rpx),
                                                decoration: BoxDecoration(),
                                                child: InkWell(
                                                  onTap: () {
                                                    // Convert string height to int for initial value
                                                    final currentHeight =
                                                        controller.model
                                                                .peopleList[
                                                            index]['height'];
                                                    final initialHeight =
                                                        currentHeight != null
                                                            ? int.tryParse(
                                                                    currentHeight
                                                                        .toString()) ??
                                                                170
                                                            : 170;

                                                    FocusScope.of(context)
                                                        .requestFocus(
                                                            FocusNode());
                                                    Future.delayed(
                                                        const Duration(
                                                            milliseconds: 250),
                                                        () {
                                                      showHeightPickerDialog(
                                                        context,
                                                        initialHeight:
                                                            initialHeight,
                                                        onConfirm: (int
                                                            selectedHeight) {
                                                          controller.model.peopleList[
                                                                      index]
                                                                  ['height'] =
                                                              selectedHeight
                                                                  .toString();
                                                          controller
                                                              .updateAll();
                                                          print(
                                                              "身高: $selectedHeight cm");
                                                        },
                                                      );
                                                    });
                                                  },
                                                  child: Row(
                                                    mainAxisSize:
                                                        MainAxisSize.max,
                                                    mainAxisAlignment:
                                                        MainAxisAlignment
                                                            .spaceBetween,
                                                    children: [
                                                      Text(
                                                        '身高(cm)',
                                                        style: TextStyle(
                                                          fontFamily:
                                                              'Readex Pro',
                                                          color:
                                                              Color(0xFF9EA4B7),
                                                          fontSize: 30.rpx,
                                                          letterSpacing: 0,
                                                        ),
                                                      ),
                                                      Row(
                                                        children: [
                                                          Text(
                                                            controller.model.peopleList[
                                                                            index]
                                                                        [
                                                                        'height'] !=
                                                                    null
                                                                ? "${controller.model.peopleList[index]['height']} cm"
                                                                : '',
                                                            style: TextStyle(
                                                              fontFamily:
                                                                  'Readex Pro',
                                                              color:
                                                                  Colors.white,
                                                              fontSize: 30.rpx,
                                                            ),
                                                          ),
                                                          SizedBox(
                                                              width: 16.rpx),
                                                          Container(
                                                              height: 30.rpx,
                                                              width: 30.rpx,
                                                              child: SvgPicture
                                                                  .asset(
                                                                'assets/img/icon/expand_more.svg',
                                                                color: Colors
                                                                    .white,
                                                              ))
                                                        ],
                                                      ),
                                                    ],
                                                  ),
                                                )),
                                            getLine(),
                                            Obx(
                                              () => Container(
                                                  width: double.infinity,
                                                  height: 90.rpx,
                                                  margin: EdgeInsets.only(
                                                      left: 40.rpx,
                                                      right: 35.rpx),
                                                  decoration: BoxDecoration(),
                                                  child: InkWell(
                                                    onTap: () {
                                                      FocusScope.of(context)
                                                          .requestFocus(
                                                              FocusNode());
                                                      Future.delayed(
                                                          const Duration(
                                                              milliseconds:
                                                                  250), () {
                                                        showWeightPickerDialog(
                                                          context,
                                                          initialWeight: controller
                                                                          .model
                                                                          .peopleList[
                                                                      index]
                                                                  ['weight'] ??
                                                              "",
                                                          onConfirm: (int
                                                              selectedWeight) {
                                                            controller.model.peopleList[
                                                                        index]
                                                                    ['weight'] =
                                                                selectedWeight
                                                                    .toString(); // ✅ 转成字符串
                                                            controller
                                                                .updateAll();
                                                            print(
                                                                "体重: $selectedWeight kg");
                                                          },
                                                        );
                                                      });
                                                    },
                                                    child: Row(
                                                      mainAxisSize:
                                                          MainAxisSize.max,
                                                      mainAxisAlignment:
                                                          MainAxisAlignment
                                                              .spaceBetween,
                                                      children: [
                                                        Text(
                                                          '体重(kg)',
                                                          style: TextStyle(
                                                            fontFamily:
                                                                'Readex Pro',
                                                            color: Color(
                                                                0xFF9EA4B7),
                                                            fontSize: 30.rpx,
                                                            letterSpacing: 0,
                                                          ),
                                                        ),
                                                        Row(
                                                          children: [
                                                            Text(
                                                              controller.model.peopleList[
                                                                              index]
                                                                          [
                                                                          'weight'] !=
                                                                      null
                                                                  ? "${controller.model.peopleList[index]['weight']} kg"
                                                                  : '',
                                                              style: TextStyle(
                                                                fontFamily:
                                                                    'Readex Pro',
                                                                color: Colors
                                                                    .white,
                                                                fontSize:
                                                                    30.rpx,
                                                              ),
                                                            ),
                                                            SizedBox(
                                                                width: 16.rpx),
                                                            Container(
                                                                height: 30.rpx,
                                                                width: 30.rpx,
                                                                child:
                                                                    SvgPicture
                                                                        .asset(
                                                                  'assets/img/icon/expand_more.svg',
                                                                  color: Colors
                                                                      .white,
                                                                ))
                                                          ],
                                                        ),
                                                      ],
                                                    ),
                                                  )),
                                            ),
                                            getLine(),
                                            Obx(
                                              () => Container(
                                                  width: double.infinity,
                                                  height: 90.rpx,
                                                  margin: EdgeInsets.only(
                                                      left: 40.rpx,
                                                      right: 35.rpx),
                                                  decoration: BoxDecoration(),
                                                  child: InkWell(
                                                    onTap: () {
                                                      // 延迟执行的代码
                                                      FocusScope.of(context)
                                                          .requestFocus(
                                                              FocusNode());
                                                      Future.delayed(
                                                          const Duration(
                                                              milliseconds:
                                                                  250), () {
                                                        showDateSelectionDialog(
                                                            context,
                                                            checkDate: controller
                                                                        .model
                                                                        .peopleList[index]['birthday']
                                                                    is DateTime
                                                                ? controller
                                                                        .model
                                                                        .peopleList[index]
                                                                    ['birthday']
                                                                : DateTime.tryParse(
                                                                        controller.model.peopleList[index]['birthday'] ??
                                                                            '') ??
                                                                    DateTime.now(),
                                                            checkChange:
                                                                (DateTime d) {
                                                          controller.model
                                                                      .peopleList[
                                                                  index]
                                                              ['birthday'] = d;
                                                          controller
                                                              .updateAll();
                                                          print("$d");
                                                        });
                                                      });
                                                    },
                                                    child: Row(
                                                      mainAxisSize:
                                                          MainAxisSize.max,
                                                      mainAxisAlignment:
                                                          MainAxisAlignment
                                                              .spaceBetween,
                                                      children: [
                                                        Text(
                                                          '生日',
                                                          style: TextStyle(
                                                            fontFamily:
                                                                'Readex Pro',
                                                            color: Color(
                                                                0xFF9EA4B7),
                                                            fontSize: 30.rpx,
                                                            letterSpacing: 0,
                                                          ),
                                                        ),
                                                        Row(
                                                          mainAxisSize:
                                                              MainAxisSize.max,
                                                          children: [
                                                            Container(
                                                              constraints:
                                                                  BoxConstraints(
                                                                      minWidth:
                                                                          200.rpx),
                                                              child: Text(
                                                                controller.model.peopleList[index]
                                                                            [
                                                                            'birthday'] !=
                                                                        null
                                                                    ? time_08_Formatter_pattern(
                                                                        controller
                                                                            .model
                                                                            .peopleList[index]['birthday'],
                                                                        "yyyy年MM月dd日")
                                                                    : '',
                                                                textAlign:
                                                                    TextAlign
                                                                        .right,
                                                                style:
                                                                    TextStyle(
                                                                  fontFamily:
                                                                      'Readex Pro',
                                                                  color: Colors
                                                                      .white,
                                                                  fontSize:
                                                                      30.rpx,
                                                                  letterSpacing:
                                                                      0,
                                                                ),
                                                              ),
                                                            ),
                                                            SizedBox(
                                                              width: 16.rpx,
                                                            ),
                                                            Container(
                                                                height: 30.rpx,
                                                                width: 30.rpx,
                                                                child:
                                                                    SvgPicture
                                                                        .asset(
                                                                  'assets/img/icon/expand_more.svg',
                                                                  color: Colors
                                                                      .white,
                                                                ))
                                                          ],
                                                        ),
                                                      ],
                                                    ),
                                                  )),
                                            ),
                                            getLine(),
                                            Container(
                                              width: double.infinity,
                                              height: 90.rpx,
                                              margin: EdgeInsets.only(
                                                  left: 40.rpx, right: 35.rpx),
                                              decoration: BoxDecoration(),
                                              child: Row(
                                                mainAxisSize: MainAxisSize.max,
                                                mainAxisAlignment:
                                                    MainAxisAlignment
                                                        .spaceBetween,
                                                children: [
                                                  Text(
                                                    '联系人',
                                                    style: TextStyle(
                                                      fontFamily: 'Readex Pro',
                                                      color: Color(0xFF9EA4B7),
                                                      fontSize: 30.rpx,
                                                      letterSpacing: 0,
                                                    ),
                                                  ),
                                                  Container(
                                                    width: 300.rpx,
                                                    child: TextField(
                                                      obscureText: false,
                                                      keyboardType:
                                                          TextInputType.number,
                                                      textInputAction:
                                                          TextInputAction.done,
                                                      textAlign:
                                                          TextAlign.right,
                                                      style: TextStyle(
                                                          fontSize: 30.rpx,
                                                          color: Colors.white),
                                                      decoration: InputDecoration(
                                                          fillColor: Colors
                                                              .transparent,
                                                          filled: true,
                                                          hintText: "请输入联系人",
                                                          hintStyle: TextStyle(
                                                              color:
                                                                  Colors.white),
                                                          border:
                                                              InputBorder.none,
                                                          contentPadding:
                                                              EdgeInsets.all(
                                                                  0)),
                                                      onChanged: (value) {
                                                        controller.model
                                                                    .peopleList[
                                                                index]
                                                            ['contact'] = value;
                                                        controller.updateAll();
                                                      },
                                                      controller: controller.onReDraw(
                                                          TextEditingController(
                                                              text: controller
                                                                          .model
                                                                          .peopleList[index]
                                                                      [
                                                                      'contact'] ??
                                                                  ""),
                                                          (textEditingController) {
                                                        textEditingController
                                                            .text = controller
                                                                    .model
                                                                    .peopleList[
                                                                index]['contact'] ??
                                                            "";
                                                      }, "contact_$index"),
                                                    ),
                                                  ),
                                                ],
                                              ),
                                            ),
                                            getLine(),
                                          ],
                                        ),
                                      )
                                    ],
                                  );
                                }),
                              SizedBox(
                                height: 100.rpx,
                              ),
                            ],
                          )),
                    )),
              ),
            ),
          )),
    );
  }

  String time_08_Formatter_pattern(dynamic date, String pattern) {
    if (date == null || date.toString().isEmpty) return "-";
    try {
      String normalized = date.toString().replaceAll("/", "-");
      DateTime dt = DateTime.parse(normalized);
      return DateFormat(pattern).format(dt);
    } catch (e) {
      return "-";
    }
  }
}
