import 'package:flutter/material.dart';
import 'package:flyinsono/lab/color/lab_colors.dart';
import 'package:flyinsono/lab/pages/lab_record_info/widgets/index.dart';
import 'package:flyinsono/lab/pages/lab_record_info/widgets/medication_usage.dart';
import 'package:flyinsono/lab/style/lab_box_decoration.dart';
import 'package:get/get.dart';

import 'index.dart';

class LabRecordInfoPage extends GetView<LabRecordInfoController> {
  const LabRecordInfoPage({Key? key, this.initArguments}) : super(key: key);

  final dynamic initArguments;

  @override
  String? get tag => tabHashCode;

  get tabHashCode => this.hashCode.toString();

  // 主视图
  Widget _buildView() {
    return Obx(() {
      print(controller.state.isPerson);
      return Row(
        children: [
          SizedBox(width: 10),
          Flexible(
            flex: 3,
            fit: FlexFit.tight,
            child: _buildMainContent(),
          ),
          Flexible(
            flex: 2,
            fit: FlexFit.tight,
            child: Container(
              constraints: BoxConstraints(
                maxWidth: 200,
              ),
              child: _buildRightPart(),
            ),
          ),
        ],
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<LabRecordInfoController>(
      init: LabRecordInfoController(
        tabHashCode: this.hashCode,
        initArguments: this.initArguments,
      ),
      tag: tabHashCode, // 如果一个页面要想实现多开，必须加 tag，且 tag 要传入每一个子组件
      id: LabRecordInfoController.labRecordInfoId,
      builder: (_) {
        return _buildView();
      },
    );
  }

  Widget _buildMainContent() {
    return Container(
      height: double.infinity,
      padding: EdgeInsets.symmetric(vertical: 10),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Flexible(
            child: Container(
              decoration: LabBoxDecoration.base,
              child: BaseInfoFields(
                tabHashCode: tabHashCode,
                isPerson: controller.state.isPerson,
              ),
            ),
          ),
          SizedBox(height: 10),
          Flexible(
            child: Container(
              decoration: LabBoxDecoration.base,
              clipBehavior: Clip.antiAlias,
              child: RecordInfoFields(
                tabHashCode: tabHashCode,
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildRightPart() {
    return Container(
      padding: EdgeInsets.all(10),
      child: Column(
        children: [
          Expanded(
            child: Container(
              decoration: LabBoxDecoration.base,
              child: GetBuilder<LabRecordInfoController>(
                id: LabRecordInfoController.baseInfoMedicationUsage,
                tag: tabHashCode,
                builder: (context) {
                  return MedicationUsage(
                    medicationUsageResultList:
                        controller.medicationUsageResultList,
                  );
                },
              ),
            ),
          ),
          SizedBox(
            height: 16,
          ),
          Container(
            width: double.infinity,
            height: 50,
            child: ElevatedButton.icon(
              icon: const Icon(Icons.save_outlined),
              style: ElevatedButton.styleFrom(
                elevation: 0.0,
                backgroundColor: LabColors.buttonColor,
              ),
              label: const Text("保存编辑"),
              onPressed: () {
                controller.saveLabRecordInfo();
                print(controller.fieldsResult.toString());
              },
            ),
          ),
        ],
      ),
    );
  }
}
