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

class PatientInfoBaseView extends GetView<PatientInfoController> {
  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Scrollbar(
          thumbVisibility: true,
          child: SingleChildScrollView(
            child: Container(
              alignment: Alignment.topCenter,
              color: const Color.fromRGBO(238, 238, 238, 1),
              padding: const EdgeInsets.only(bottom: 64),
              child: SizedBox(
                width: 800,
                child: Column(
                  children: [
                    const SizedBox(height: 16),
                    _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: 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;
            }
          },
        ),
      ),
      Obx(
        () => VListFormCell(
          label: "证件号",
          content: controller.state.cardNo,
          onTap: () async {
            final result = await VDialogInput(
              title: "证件号",
              initialValue: controller.state.cardNo,
              placeholder: "请填写证件号",
            ).show();
            if (result != null) {
              controller.state.cardNo = result;
            }
          },
        ),
      ),
      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.toString(),
        ),
      ),
      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.toString(),
        ),
      ),
      Obx(
        () => VListFormCell(
          label: "建档人",
          content: controller.state.createdDoctorName.toString(),
        ),
      ),
      Obx(
        () => VListFormCell(
          label: "建档时间",
          content: controller.state.createTime.toString(),
        ),
      ),
      Obx(
        () => VListFormCell(
          label: "责任人",
          content: controller.state.contractedDoctorName.toString(),
        ),
      ),
    ]);
  }
}
