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/info/controller.dart';

class PatientInfoBaseView extends GetView<PatientInfoController> {
  const PatientInfoBaseView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scrollbar(
      thumbVisibility: true,
      child: SingleChildScrollView(
        child: Container(
          alignment: Alignment.topCenter,
          child: Column(
            children: [
              Padding(
                padding: const EdgeInsets.only(left: 16, right: 16, bottom: 50),
                child: _HealthInfoPanel(),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class _HealthInfoPanel extends GetView<PatientInfoController> {
  @override
  Widget build(BuildContext context) {
    final state = controller.state;
    return VListFormCellGroup(children: [
      Obx(
        () => VListFormCell(
          label: "姓名",
          content: controller.state.name,
          onTap: () async {
            final result = await VDialogInput(
              title: "姓名",
              initialValue: controller.state.name,
              placeholder: "请填写姓名",
            ).show();
            if (result != null) {
              controller.state.name = result;
            }
          },
        ),
      ),
      Obx(
        () => 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;
            }
          },
        ),
      ),
      Obx(
        () => 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;
            }
          },
        ),
      ),
      Obx(
        () => 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;
            }
          },
        ),
      ),
      Obx(
        () => VListFormCell(
          label: "证件号",
          content: controller.state.cardNo,
        ),
      ),
      Obx(
        () => 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;
            }
          },
        ),
      ),
      Obx(
        () => VListFormCell(
          label: "年龄",
          content: controller.state.age,
        ),
      ),
      Obx(
        () => 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;
            }
          },
        ),
      ),
      Obx(
        () => VListFormCell(
          label: "户籍地址",
          content: controller.state.permanentResidenceAddress,
          onTap: () async {
            final result = await VDialogInput(
              title: "户籍地址",
              initialValue: controller.state.permanentResidenceAddress,
              placeholder: "请填写户籍地址",
            ).show();
            if (result != null) {
              controller.state.permanentResidenceAddress = result;
            }
          },
        ),
      ),
      Obx(
        () => VListFormCell(
          label: "建档单位",
          content: controller.state.createdOrgName,
        ),
      ),
      Obx(
        () => VListFormCell(
          label: "建档人",
          content: controller.state.createdDoctorName,
        ),
      ),
      Obx(
        () => VListFormCell(
          label: "建档时间",
          content: controller.state.createTime,
        ),
      ),
      // Obx(
      //   () => VListFormCell(
      //     label: "责任人",
      //     content: controller.state.contractedDoctorName.isEmpty
      //         ? controller.state.createdDoctorName
      //         : controller.state.contractedDoctorName,
      //   ),
      // ),
    ]);
  }
}
