import 'package:fis_jsonrpc/rpc.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:vnoteapp/components/button.dart';
import 'package:vnoteapp/components/checkbox.dart';
import 'package:vnoteapp/components/date_picker.dart';
import 'package:vnoteapp/components/form.dart';
import 'package:vnoteapp/components/input.dart';
import 'package:vnoteapp/components/panel.dart';
import 'package:vnoteapp/components/select.dart';
import 'package:vnoteapp/consts/nations.dart';
import 'package:vnoteapp/consts/rpc_enum_labels.dart';
import 'package:vnoteapp/pages/controllers/crowd_labels.dart';
import 'package:vnoteapp/pages/patient/create/controller.dart';

export './view_new.dart';

class CreatePatientPage extends GetView<CreatePatientController> {
  const CreatePatientPage({super.key});

  @override
  Widget build(BuildContext context) {
    const panelSpace = SizedBox(height: 10);
    return Container(
      padding: const EdgeInsets.all(8),
      color: Colors.grey.shade200,
      alignment: Alignment.bottomLeft,
      child: Scrollbar(
        thumbVisibility: true,
        child: ListView(
          shrinkWrap: true,
          children: [
            VPanel(child: _SignDistrict()),
            panelSpace,
            VPanel(child: _InfoFormView()),
            panelSpace,
            VPanel(
              padding: const EdgeInsets.only(top: 16, bottom: 8),
              child: _PersonTagsView(),
            ),
            const SizedBox(height: 30),
            _buildOpRow(),
          ],
        ),
      ),
    );
  }

  Widget _buildOpRow() {
    return Obx(
      () {
        if (controller.state.isCreateOnly) {
          return Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              VButton(
                label: "返  回",
                type: VButtonType.cancel,
                onTap: () {
                  Get.back(id: 1001);
                },
              ),
              VButton(
                label: "保  存",
                type: VButtonType.primary,
                onTap: controller.gotoPatientDetail,
              ),
            ],
          );
        }
        return Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            VButton(
              label: "下一步",
              type: VButtonType.primary,
              onTap: controller.gotoSignContract,
            ),
            VButton(
              label: "存为档案",
              type: VButtonType.primary,
              onTap: controller.gotoPatientDetail,
            ),
          ],
        );
      },
    );
  }
}

class _InfoFormView extends GetView<CreatePatientController> {
  static const labelWidth = 85.0;
  static const itemWidth = 200.0;
  static const cellSpace = SizedBox(width: 25);
  @override
  Widget build(BuildContext context) {
    const rowSpace = SizedBox(height: 20);

    return Container(
      alignment: Alignment.bottomLeft,
      padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _buildCardRow(),
          rowSpace,
          _buildRow(
            Row(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                _buildName(),
                cellSpace,
                _buildGender(),
                cellSpace,
                _buildNation(),
              ],
            ),
          ),
          rowSpace,
          _buildBirthdayRow(),
          rowSpace,
          _buildCensusRegisterRow(),
          rowSpace,
          _buildAddressRow(),
          rowSpace,
          _buildRow(
            Row(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                _buildPhoneNo(),
                const SizedBox(width: 5),
                _buildPersonalNo(),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildCardRow() {
    return _buildRow(
      VFormCell(
        labelWidth: labelWidth,
        label: "证件号：",
        isExpanded: false,
        child: Row(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            SizedBox(
              width: itemWidth,
              child: VInput(
                placeholder: "请输入证件号",
                onChanged: (value) {
                  controller.state.cardNo = value;
                },
              ),
            ),
            cellSpace,
            SizedBox(
              width: 140,
              child: VSelect<CardTypeEnum, CardTypeEnum>(
                source: CardTypeEnum.values,
                value: CardTypeEnum.Identity,
                labelGetter: (e) => RpcEnumLabels.cardType[e]!,
                valueGetter: (e) => e,
                onChanged: (value) {
                  controller.state.cardType = value;
                },
              ),
            ),
            cellSpace,
            SizedBox(
              width: 90,
              child: VButton(
                label: "读卡",
                onTap: () {
                  controller.onReadCardClicked();
                },
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildName() {
    return VFormCell(
      labelWidth: labelWidth,
      isExpanded: false,
      label: "姓名：",
      child: SizedBox(
        width: itemWidth,
        height: 40,
        child: VInput(
          placeholder: "请输入姓名",
          onChanged: (value) {
            controller.state.name = value;
          },
        ),
      ),
    );
  }

  Widget _buildGender() {
    return VFormCell(
      labelWidth: 60,
      isExpanded: false,
      label: "性别：",
      child: SizedBox(
        width: itemWidth,
        child: VSelect<GenderEnum, GenderEnum>(
          source: const [
            GenderEnum.Male,
            GenderEnum.Female,
            GenderEnum.Unknown,
            GenderEnum.Unspecified
          ],
          value: GenderEnum.Male,
          labelGetter: (e) => RpcEnumLabels.gender[e]!,
          valueGetter: (e) => e,
          onChanged: (value) {
            controller.state.gender = value;
          },
        ),
      ),
    );
  }

  Widget _buildNation() {
    return VFormCell(
      labelWidth: 60,
      isExpanded: false,
      label: "民族：",
      child: SizedBox(
        width: itemWidth,
        child: VSelect<String, String>(
          source: Nations.china,
          value: Nations.china.first,
          labelGetter: (e) => e,
          valueGetter: (e) => e,
          onChanged: (value) {
            controller.state.nation = value;
          },
        ),
      ),
    );
  }

  Widget _buildPhoneNo() {
    return VFormCell(
      labelWidth: labelWidth,
      isExpanded: false,
      label: "手机号码：",
      child: SizedBox(
        width: itemWidth,
        child: VInput(
          placeholder: "请输入手机号码",
          onChanged: (value) {
            controller.state.phoneNo = value;
          },
        ),
      ),
    );
  }

  Widget _buildPersonalNo() {
    return VFormCell(
      labelWidth: 85,
      isExpanded: false,
      label: "个人编号：",
      child: SizedBox(
        width: itemWidth,
        child: VInput(
          placeholder: "请输入个人编号",
          onChanged: (value) {
            controller.state.personalNo = value;
          },
        ),
      ),
    );
  }

  Widget _buildBirthdayRow() {
    return _buildRow(
      Row(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          VFormCell(
            labelWidth: labelWidth,
            isExpanded: false,
            label: "出生日期：",
            child: SizedBox(
              width: itemWidth,
              child: Obx(
                () => VDatePicker(
                  value: controller.state.birthday,
                  onChanged: (value) {
                    controller.state.birthday = value;
                  },
                ),
              ),
            ),
          ),
          cellSpace,
          VFormCell(
            labelWidth: 60,
            isExpanded: false,
            label: "年龄：",
            child: SizedBox(
              width: itemWidth,
              child: Obx(
                () => VInput(
                  initialValue: controller.state.age.toString(),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildAddressRow() {
    return _buildRow(
      Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          VFormCell(
            labelWidth: labelWidth,
            isExpanded: false,
            label: "现 住 址：",
            child: SizedBox(
              width: 400,
              child: Obx(
                () => VInput(
                  initialValue: controller.state.address,
                  placeholder: "请输入现住址",
                  onChanged: (value) {
                    controller.state.address = value;
                  },
                ),
              ),
            ),
          ),
          const SizedBox(width: 8),
          VCheckBox(
            label: "同户籍地址",
            onChanged: (value) {
              controller.onSyncAddressCheckChanged(value);
            },
          ),
        ],
      ),
    );
  }

  Widget _buildCensusRegisterRow() {
    return _buildRow(
      VFormCell(
        labelWidth: labelWidth,
        isExpanded: false,
        label: "户籍地址：",
        child: SizedBox(
          width: 400,
          child: VInput(
            initialValue: controller.state.censusRegister,
            placeholder: "请输入户籍地址",
            onChanged: (value) {
              controller.onCensusRegisterChanged(value);
            },
          ),
        ),
      ),
    );
  }

  Widget _buildRow(Widget cell) {
    return SizedBox(
      height: 40,
      child: cell,
    );
  }
}

class _PersonTagsView extends GetView<CrowdLabelsController> {
  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.bottomLeft,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
            padding: const EdgeInsets.only(left: 12, bottom: 8),
            child: Text(
              "人群分类",
              style: TextStyle(
                color: Theme.of(context).primaryColor,
                fontSize: 24,
              ),
            ),
          ),
          Divider(thickness: 1, color: Colors.grey.shade300),
          Padding(
            padding: const EdgeInsets.all(12.0),
            child: _buildBaseClassRow(),
          ),
          Padding(
            padding: const EdgeInsets.all(12.0),
            child: _buildDiseaseRow(),
          ),
        ],
      ),
    );
  }

  /// 基本人群分类
  Widget _buildBaseClassRow() {
    return Obx(
      () => VCheckBoxGroup<LabelDTO, String>(
        source: controller.state.normalOptions,
        labelGetter: (data) => data.labelName!,
        valueGetter: (data) => data.code!,
        itemSpace: 36,
        onChanged: (value) {
          controller.onNormalSelectedChanged(value);
        },
      ),
    );
  }

  /// 特殊病分类
  Widget _buildDiseaseRow() {
    return Obx(
      () => VCheckBoxGroup<LabelDTO, String>(
        source: controller.state.diseaseOptions,
        labelGetter: (data) => data.labelName!,
        valueGetter: (data) => data.code!,
        itemSpace: 36,
        onChanged: (value) {
          controller.onDiseaseSelectedChanged(value);
        },
      ),
    );
  }
}

class _SignDistrict extends GetView<CreatePatientController> {
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              Text(
                "签约区域",
                style: TextStyle(
                  color: Theme.of(context).primaryColor,
                  fontSize: 28,
                  fontWeight: FontWeight.bold,
                ),
              ),
              const SizedBox(width: 24),
              Column(
                children: [
                  Text(
                    "重庆市沙坪坝区童家桥街道",
                    style: TextStyle(
                      color: Theme.of(context).primaryColor,
                      fontSize: 16,
                    ),
                  ),
                  const SizedBox(height: 4),
                ],
              ),
            ],
          ),
          SizedBox(
            width: 140,
            height: 40,
            child: VSelect(
              source: const [
                "磁器口社区",
                "蝴蝶湾社区",
                "尖沙咀社区",
              ],
              value: "尖沙咀社区",
              labelGetter: (e) => e,
              valueGetter: (e) => e,
              onChanged: (value) {
                //TODO:
              },
            ),
          ),
        ],
      ),
    );
  }
}
