import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:vitalapp/components/button.dart';
import 'package:vitalapp/components/dialog_select.dart';
import 'package:vitalapp/components/side_nav/defines.dart';
import 'package:vitalapp/components/side_nav/side_nav.dart';
import 'package:vitalapp/pages/contract/package_list/widgets/capture_portrait.dart';
import 'package:vitalapp/pages/contract/package_list/widgets/family_doctor_service_package.dart';
import 'package:vitalapp/pages/contract/package_list/widgets/personal_information.dart';
import 'package:vitalapp/pages/contract/package_list/widgets/service_information.dart';
import 'package:vitalapp/pages/contract/package_list/widgets/signing_status.dart';
import 'controller.dart';

class ServicePackageContractPage
    extends GetView<ServicePackageContractController> {
  const ServicePackageContractPage({super.key});
  static const double labelSize = 20;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: _buildContent(context),
    );
  }

  static const TextStyle _textStyle = TextStyle(fontSize: 20);
  Widget _buildContent(BuildContext context) {
    TextEditingController? textEditingController =
        TextEditingController(text: controller.state.reasonRefusal);
    return Column(
      children: [
        Padding(
          padding: const EdgeInsets.only(left: 24.0),
          child: Obx(
            () => Row(
              children: [
                const Text(
                  "签约选项:",
                  style: _textStyle,
                ),
                const SizedBox(
                  width: 8,
                ),
                SizedBox(
                  height: 50,
                  child: Row(
                    children: [
                      _OptionWidget(
                        text: "签约",
                        checked: !controller.state.isDenySbaVisa,
                        onTap: () {
                          if (controller.state.isDenySbaVisa) {
                            controller.state.isDenySbaVisa = false;
                          }
                        },
                      ),
                      _OptionWidget(
                        text: "拒签",
                        checked: controller.state.isDenySbaVisa,
                        onTap: () async {
                          if (!controller.state.isDenySbaVisa) {
                            controller.state.isDenySbaVisa = true;
                          }
                          // final result = await VDialogSelect<String, String>(
                          //   title: "拒签原因",
                          //   source: controller.state.reasonRefusals,
                          //   valueGetter: (data) => data,
                          //   labelGetter: (data) => data,
                          //   initialValue: controller.state.reasonRefusal,
                          // ).show();
                          // if (result != null) {
                          //   controller.state.reasonRefusal = result;
                          // }
                        },
                      ),
                    ],
                  ),
                ),
                const SizedBox(
                  width: 20,
                ),
                if (controller.state.isDenySbaVisa)
                  ElevatedButton(
                    onPressed: () {
                      controller.submitContract();
                    },
                    style: ButtonStyle(
                      foregroundColor:
                          const MaterialStatePropertyAll(Colors.white),
                      backgroundColor: MaterialStatePropertyAll(
                          Theme.of(context).primaryColor),
                      elevation: const MaterialStatePropertyAll(0),
                      padding: const MaterialStatePropertyAll(
                        EdgeInsets.symmetric(horizontal: 1, vertical: 1),
                      ),
                    ),
                    child: const Text(
                      "提交",
                      style: _textStyle,
                    ),
                  ),
              ],
            ),
          ),
        ),
        Obx(() {
          if (!controller.state.isDenySbaVisa) {
            return Container();
          }
          return Padding(
            padding: const EdgeInsets.only(left: 24.0),
            child: Row(
              children: [
                const Text(
                  "拒签原因:",
                  style: _textStyle,
                ),
                Radio<int>(
                  value: 0,
                  groupValue: controller.state.selectedReasonIndex,
                  onChanged: (value) {
                    controller.state.selectedReasonIndex = value!;
                  },
                ),
                const Text(
                  '不自愿',
                  style: _textStyle,
                ),
                Radio<int>(
                  value: 1,
                  groupValue: controller.state.selectedReasonIndex,
                  onChanged: (value) {
                    controller.state.selectedReasonIndex = value!;
                  },
                ),
                const Text(
                  '举家外出',
                  style: _textStyle,
                ),
                Radio<int>(
                  value: 2,
                  groupValue: controller.state.selectedReasonIndex,
                  onChanged: (value) {
                    controller.state.selectedReasonIndex = value!;
                  },
                ),
                const Text(
                  '外出上学',
                  style: _textStyle,
                ),
                Radio<int>(
                  value: 3,
                  groupValue: controller.state.selectedReasonIndex,
                  onChanged: (value) {
                    controller.state.selectedReasonIndex = value!;
                  },
                ),
                const Text(
                  '婚出',
                  style: _textStyle,
                ),
                Radio<int>(
                  value: 4,
                  groupValue: controller.state.selectedReasonIndex,
                  onChanged: (value) {
                    controller.state.selectedReasonIndex = value!;
                  },
                ),
                const Text(
                  '服役',
                  style: _textStyle,
                ),
                Radio<int>(
                  value: 5,
                  groupValue: controller.state.selectedReasonIndex,
                  onChanged: (value) {
                    controller.state.selectedReasonIndex = value!;
                  },
                ),
                const Text(
                  '服刑',
                  style: _textStyle,
                ),
                Radio<int>(
                  value: 6,
                  groupValue: controller.state.selectedReasonIndex,
                  onChanged: (value) {
                    controller.state.selectedReasonIndex = value!;
                  },
                ),
                const Text(
                  '其他',
                  style: _textStyle,
                ),
                const SizedBox(
                  width: 8,
                ),
                if (controller.state.selectedReasonIndex == 6)
                  Expanded(
                    child: TextField(
                      keyboardType: TextInputType.text,
                      controller: textEditingController,
                      onChanged: (value) {
                        controller.state.reasonRefusal = value;
                      },
                    ),
                  ),
                const SizedBox(
                  width: 30,
                ),
              ],
            ),
          );
        }),
        const SizedBox(
          height: 8,
        ),
        const Divider(height: 1),
        Expanded(
          child: VSideNavView(
            // navId: NavIds.CONTRACT,
            items: _buildItems(),
          ),
        ),
        const Divider(height: 1),
        const SizedBox(
          height: 8,
        ),
        Obx(() {
          if (controller.state.isDenySbaVisa) {
            return Container();
          }
          return VButton(
            label: "签约协议",
            onTap: () {
              if (!controller.verifyData()) {
                return;
              }
              Get.toNamed(
                "/contract/contract_template",
                parameters: {
                  "templateCode": controller.templateCode,
                  "patientInfo": json.encode(controller.patient.toJson()),
                  "servicePackageCodes":
                      controller.state.selectedServicePackageCode,
                  "servicePackageNames":
                      controller.state.selectedServicePackageName,
                  "serviceTime": controller.state.serviceTime.toString(),
                  "serviceStartDate":
                      controller.state.serviceStartDate.toString(),
                  "base64Image": controller.state.userImage ?? '',
                  "notes": controller.state.notes,
                },
              );
            },
          );
        }),
        const SizedBox(
          height: 8,
        ),
      ],
    );
  }

  List<VSideNavMenuItem> _buildItems() {
    final items = <VSideNavMenuItem>[];
    // items.add(_buildCapturePortrait());
    items.add(_buildPersonalInformation());
    items.add(_buildServiceInformation());
    items.add(_buildFamilyDoctorServicePackage());
    // items.add(_buildSigningStatusItem());
    return items;
  }

  // VSideNavMenuItem _buildCapturePortrait() {
  //   return VSideNavMenuItem(
  //     title: "人像采集",
  //     icon: Icon(Icons.edit_document, color: Colors.grey.shade700),
  //     pageBuilder: (context) {
  //       return const CapturePortraitPage();
  //     },
  //   );
  // }

  VSideNavMenuItem _buildServiceInformation() {
    return VSideNavMenuItem(
      title: "服务信息",
      icon: Icon(Icons.edit_document, color: Colors.grey.shade700),
      pageBuilder: (context) {
        return const ServiceInformationPage();
      },
    );
  }

  VSideNavMenuItem _buildFamilyDoctorServicePackage() {
    return VSideNavMenuItem(
      title: "服务包",
      icon: Icon(Icons.edit_document, color: Colors.grey.shade700),
      pageBuilder: (context) {
        return const FamilyDoctorServicePackagePage();
      },
    );
  }

  VSideNavMenuItem _buildPersonalInformation() {
    return VSideNavMenuItem(
      title: "个人信息",
      icon: Icon(Icons.edit_document, color: Colors.grey.shade700),
      pageBuilder: (context) {
        return const PersonalInformationPage();
      },
    );
  }

  // VSideNavMenuItem _buildSigningStatusItem() {
  //   return VSideNavMenuItem(
  //     title: "签约状态",
  //     isRequired: true,
  //     icon: Icon(Icons.edit_document, color: Colors.grey.shade700),
  //     pageBuilder: (context) {
  //       return const SigningStatusPage();
  //     },
  //   );
  // }
}

class _OptionWidget extends StatelessWidget {
  final String text;
  final bool checked;
  final VoidCallback onTap;

  const _OptionWidget({
    super.key,
    required this.text,
    required this.checked,
    required this.onTap,
  });
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        height: 32,
        width: 80,
        alignment: Alignment.center,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(4),
          color: checked ? Theme.of(context).primaryColor : null,
        ),
        child: Text(
          text,
          style: TextStyle(color: checked ? Colors.white : null, fontSize: 20),
        ),
      ),
    );
  }
}
