import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../utils/dialog_utils.dart';
import '../../utils/empty_utils.dart';
import 'medical_record_logic.dart';

class MedicalRecordPage extends StatelessWidget {
  MedicalRecordPage({Key? key}) : super(key: key);

  final logic = Get.find<MedicalRecordLogic>();
  final state = Get
      .find<MedicalRecordLogic>()
      .state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('诊疗记录'),
        backgroundColor: Colors.blue,
        centerTitle: true,
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          Expanded(
            child: GetBuilder<MedicalRecordLogic>(builder: (logic) {
              return ListView(
                children: [
                  Padding(
                    padding: const EdgeInsets.all(2.0),
                    child: Row(
                      children: [
                        const SizedBox(
                          width: 10,
                        ),
                        getItemView(
                            '高频耳标',
                            state.entity.value.registrationNo ?? '',
                                () =>
                            {
                              if (!state.isModify)
                                {
                                  logic.scanRfid(),
                                }
                            }),
                        const SizedBox(
                          width: 10,
                        ),
                      ],
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(2.0),
                    child: Row(
                      children: [
                        const SizedBox(
                          width: 10,
                        ),
                        getItemView(
                            '可视耳标',
                            state.entity.value.individualNo ?? '',
                                () =>
                            {
                              if (!state.isModify)
                                {
                                  DialogUtils.showInputDialog(
                                      hintText: '请输入可视耳标',
                                      onPositive: (text) =>
                                      {
                                        logic.queryCattleResume(1, text),
                                      }),
                                }
                            }),
                        const SizedBox(
                          width: 10,
                        ),
                      ],
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(2.0),
                    child: Row(
                      children: [
                        const SizedBox(
                          width: 10,
                        ),
                        getItemView(
                            '诊疗人员',
                            state.entity.value.treatPersonnel ?? '',
                                () async =>
                            {
                              DialogUtils.showInputDialog(
                                  hintText: '输入诊疗人员',
                                  onPositive: (text) =>
                                  {
                                    state.entity.value.treatPersonnel = text,
                                    logic.update(),
                                  }),
                            }),
                        getItemView(
                          '诊疗日期',
                          state.entity.value.treatDate ?? '',
                              () =>
                          {
                            DialogUtils.showTimeDialog(
                              context,
                              isTime: false,
                              title: '请选择诊疗日期',
                              onPositive: (date) =>
                              {
                                state.entity.value.treatDate = date,
                                logic.update(),
                              },
                            ),
                          },
                        ),
                        const SizedBox(
                          width: 10,
                        ),
                      ],
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(2.0),
                    child: Row(
                      children: [
                        const SizedBox(
                          width: 10,
                        ),
                        getItemView(
                            '病因',
                            state.entity.value.causeOf ?? '',
                                () async =>
                            {
                              DialogUtils.showInputDialog(
                                  hintText: '输入病因',
                                  onPositive: (text) =>
                                  {
                                    state.entity.value.causeOf = text,
                                    logic.update(),
                                  }),
                            }),
                        getItemView(
                            '用药名称',
                            state.entity.value.drugName ?? '',
                                () =>
                            {
                              DialogUtils.showInputDialog(
                                  hintText: '输入用药名称',
                                  onPositive: (text) =>
                                  {
                                    state.entity.value.drugName = text,
                                    logic.update(),
                                  }),
                            }),
                        const SizedBox(
                          width: 10,
                        ),
                      ],
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(2.0),
                    child: Row(
                      children: [
                        const SizedBox(
                          width: 10,
                        ),
                        getItemView(
                            '规格',
                            state.entity.value.specifications ?? '',
                                () async =>
                            {
                              DialogUtils.showInputDialog(
                                  hintText: '输入规格',
                                  onPositive: (text) =>
                                  {
                                    state.entity.value.specifications = text,
                                    logic.update(),
                                  }),
                            }),
                        getItemView(
                            '用药方法',
                            state.entity.value.drugMethods ?? '',
                                () =>
                            {
                              DialogUtils.showInputDialog(
                                  hintText: '输入用药方法',
                                  onPositive: (text) =>
                                  {
                                    state.entity.value.drugMethods = text,
                                    logic.update(),
                                  }),
                            }),
                        const SizedBox(
                          width: 10,
                        ),
                      ],
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(2.0),
                    child: Row(
                      children: [
                        const SizedBox(
                          width: 10,
                        ),
                        getItemView(
                            '用药数量',
                            (state.entity.value.drugNumber ?? '').toString(),
                                () async =>
                            {
                              DialogUtils.showInputDialog(
                                  hintText: '输入用药数量',
                                  isNum: true,
                                  onPositive: (text) =>
                                  {
                                    state.entity.value.drugNumber = double.tryParse(text),
                                    logic.update(),
                                  }),
                            }),
                        getItemView(
                            '诊疗结果',
                            state.entity.value.results ?? '',
                                () =>
                            {
                              DialogUtils.showInputDialog(
                                  hintText: '输入诊疗结果',
                                  onPositive: (text) =>
                                  {
                                    state.entity.value.results = text,
                                    logic.update(),
                                  }),
                            }),
                        const SizedBox(
                          width: 10,
                        ),
                      ],
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(2.0),
                    child: Row(
                      children: [
                        const SizedBox(
                          width: 10,
                        ),
                        getItemView(
                            '备注',
                            state.entity.value.remark ?? '',
                                () =>
                            {
                              DialogUtils.showInputDialog(
                                  hintText: '输入备注',
                                  onPositive: (text) =>
                                  {
                                    state.entity.value.remark = text,
                                    logic.update(),
                                  }),
                            }),
                        const SizedBox(
                          width: 10,
                        ),
                      ],
                    ),
                  ),
                ],
              );
            }),
          ),
          // Row(children: [
          //   Expanded(
          //     child:
          TextButton(
            style: ButtonStyle(
              backgroundColor: MaterialStateProperty.all(Colors.blue),
              foregroundColor: MaterialStateProperty.all(Colors.white),
              padding: MaterialStateProperty.all(const EdgeInsets.all(10)),
            ),
            onPressed: () =>
            {
              logic.save(),
            },
            child: const Text("保存"),
          ),
          //   ),
          // ]),
        ],
      ),
    );
  }

  getItemView(String title, String context, Function() onClick) {
    return Expanded(
      child: InkWell(
        onTap: onClick,
        child: Container(
          decoration: const BoxDecoration(
            color: Colors.white,
            boxShadow: [
              BoxShadow(
                color: Colors.black12,
                blurRadius: 2,
              ),
            ],
          ),
          padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              const SizedBox(
                height: 10,
              ),
              Text(
                title,
                style: const TextStyle(
                  color: Colors.blue,
                  fontSize: 14,
                ),
              ),
              const SizedBox(
                height: 10,
              ),
              SizedBox(
                height: 1,
                child: Container(
                  color: Colors.black12,
                  height: 1,
                ),
              ),
              const SizedBox(
                height: 10,
              ),
              Text(
                context,
                style: const TextStyle(
                  height: 1.5,
                  color: Colors.black,
                  fontSize: 14,
                ),
              ),
              const SizedBox(
                height: 10,
              ),
            ],
          ),
        ),
      ),
    );
  }
}
