// ignore_for_file: must_be_immutable

import 'package:fis_jsonrpc/rpc.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:vitalapp/architecture/utils/datetime.dart';
import 'package:vitalapp/components/cell.dart';
import 'package:vitalapp/components/dialog_date.dart';
import 'package:vitalapp/components/dialog_input.dart';
import 'package:vitalapp/components/dialog_select.dart';
import 'package:vitalapp/consts/nations.dart';
import 'package:vitalapp/consts/rpc_enum_labels.dart';
import 'package:vitalapp/pages/patient/create/controller.dart';

class PatientInfo extends GetView<CreatePatientController> {
  PatientInfo({super.key, this.isNotScanCode = true});
  bool? isNotScanCode;

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.only(bottom: 80),
      child: _buildContent(),
    );
  }

  Widget _buildContent() {
    return SingleChildScrollView(
      child: Column(
        children: [_buildVListFormCellGroup()],
      ),
    );
  }

  Widget _buildVListFormCellGroup() {
    final state = controller.state;

    return Obx(
      () => VListFormCellGroup(
        children: [
          if (isNotScanCode!)
            VListFormCell(
              label: "证件类型",
              content: RpcEnumLabels.cardType[state.cardType],
              onTap: () async {
                final result = await VDialogSelect<CardTypeEnum, CardTypeEnum>(
                  title: "选择证件类型",
                  source: CardTypeEnum.values,
                  valueGetter: (data) => data,
                  labelGetter: (data) => RpcEnumLabels.cardType[data] ?? "",
                  initialValue: state.cardType,
                ).show();
                if (result != null) {
                  controller.state.cardType = result;
                }
              },
            ),
          VListFormCell(
            labelWidget: RichText(
              text: const TextSpan(
                text: '* ',
                style: TextStyle(color: Colors.red, fontSize: 20),
                children: [
                  TextSpan(
                    text: '证件号',
                    style: TextStyle(color: Colors.black, fontSize: 20),
                  ),
                ],
              ),
            ),
            content: controller.state.cardNo,
            onTap: () async {
              final result = await VDialogInput(
                title: "证件号",
                initialValue: controller.state.cardNo,
                placeholder: "请填写证件号",
              ).show();
              if (result != null) {
                controller.state.cardNo = result;
                if (!result.isEmpty && result.length > 17) {
                  var sexNum = result.substring(16, 17);
                  if (!sexNum.isEmpty && RegExp(r'^\d+$').hasMatch(sexNum)) {
                    if (int.parse(sexNum) % 2 == 1) {
                      controller.state.gender = GenderEnum.Male;
                    } else {
                      controller.state.gender = GenderEnum.Female;
                    }
                  } else {
                    controller.state.gender = GenderEnum.Unknown;
                  }
                }
              }
            },
          ),
          VListFormCell(
            labelWidget: RichText(
              text: const TextSpan(
                text: '* ',
                style: TextStyle(color: Colors.red, fontSize: 20),
                children: [
                  TextSpan(
                    text: '姓名',
                    style: TextStyle(color: Colors.black, fontSize: 20),
                  ),
                ],
              ),
            ),
            content: controller.state.name,
            onTap: () async {
              final result = await VDialogInput(
                title: "姓名",
                initialValue: controller.state.name,
                placeholder: "请填写姓名",
              ).show();
              if (result != null) {
                controller.state.name = result.toString().trim();
              }
            },
          ),
          VListFormCell(
            label: "性别",
            content: RpcEnumLabels.gender[state.gender],
            onTap: () async {
              final result = await VDialogSelect<GenderEnum, GenderEnum>(
                title: "选择性别",
                source: const [
                  GenderEnum.Male,
                  GenderEnum.Female,
                  GenderEnum.Unknown,
                  GenderEnum.Unspecified
                ],
                valueGetter: (data) => data,
                labelGetter: (data) => RpcEnumLabels.gender[data] ?? "",
                initialValue: state.gender,
              ).show();
              if (result != null) {
                controller.state.gender = result;
              }
            },
          ),
          VListFormCell(
            label: "民族",
            content: state.nation,
            onTap: () async {
              final result = await VDialogSelect<String, String>(
                title: "选择民族",
                source: Nations.china,
                valueGetter: (data) => data,
                labelGetter: (data) => data,
                initialValue: state.nation,
              ).show();
              if (result != null) {
                controller.state.nation = result;
              }
            },
          ),
          VListFormCell(
            label: "出生日期",
            content: controller.state.birthday != null
                ? DataTimeUtils.formatDateString(controller.state.birthday!)
                : "",
            onTap: () async {
              final result = await VDialogDate(
                title: "设置出生日期",
                initialValue: controller.state.birthday,
                maxValue: DateTime.now(),
              ).show();
              if (result != null) {
                controller.state.birthday = result;
              }
            },
          ),
          VListFormCell(
            label: "年龄",
            content: controller.state.age.toString(),
          ),
          VListFormCell(
            label: "手机号码",
            content: controller.state.phoneNo,
            onTap: () async {
              final result = await VDialogInput(
                title: "手机号码",
                initialValue: controller.state.phoneNo,
                placeholder: "请填写手机号码",
              ).show();
              if (result != null) {
                controller.state.phoneNo = result;
              }
            },
          ),
          VListFormCell(
            label: "同步户籍地址到现住地址",
            labelWidth: 250,
            contentWidget: Container(
              child: Switch(
                onChanged: (value) {
                  controller.onSyncAddressCheckChanged(value);
                },
                value: controller.state.isSyncAddresses,
              ),
            ),
          ),
          VListFormCell(
            label: "户籍地址",
            content: controller.state.censusRegister,
            onTap: () async {
              final result = await VDialogInput(
                title: "户籍地址",
                initialValue: controller.state.censusRegister,
                placeholder: "请填写户籍地址",
              ).show();
              if (result != null) {
                controller.onCensusRegisterChanged(result);
              }
            },
          ),
          VListFormCell(
            label: "现住地址",
            content: controller.state.address,
            onTap: () async {
              final result = await VDialogInput(
                title: "现住地址",
                initialValue: controller.state.address,
                placeholder: "请填写现住地址",
              ).show();
              if (result != null) {
                controller.state.address = result;
              }
            },
          ),
        ],
      ),
    );
  }
}
