import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:vitalapp/components/button.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,
      // appBar: VAppBar(
      //   titleWidget: const Text(
      //     "签约",
      //     style: TextStyle(fontSize: 24),
      //   ),
      // ),
      body: Stack(
        children: [
          VSideNavView(
            // navId: NavIds.CONTRACT,
            items: _buildItems(),
          ),
          Positioned(
            bottom: 8,
            left: 200,
            right: 200,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                Obx(
                  () => controller.state.signingStatus == '签约'
                      ? VButton(
                          label: "下一步",
                          onTap: () {
                            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,
                              },
                            );
                          },
                        )
                      : VButton(
                          label: "提交",
                          onTap: () {
                            controller.submitContract();
                          },
                        ),
                ),
              ],
            ),
          ),
          // const FloatingWindow(),
        ],
      ),
    );
  }

  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();
      },
    );
  }
}
