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

import '../../congifs.dart';
import '../../routes/routes.dart';
import '../../utils/dialog_utils.dart';
import '../../utils/dict_utils.dart';
import '../../utils/empty_utils.dart';
import 'performance_logic.dart';

class PerformancePage extends StatelessWidget {
  PerformancePage({Key? key}) : super(key: key);
  final logic = Get.find<PerformanceLogic>();
  final state = Get.find<PerformanceLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(
          '性能测定',
          style: TextStyle(
            color: Colors.white,
          ),
        ),
        backgroundColor: Colors.blue,
        centerTitle: true,
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          Expanded(
            child: GetBuilder<PerformanceLogic>(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.age ?? '',
                            () async => {
                                  if (EmptyUtils.isStrNotEmpty(state.entity.value.cattleresumeId))
                                    {
                                      DialogUtils.showSingleListDialog(
                                          await DictUtils.getDictList('zxgl_performance_age'),
                                          onPositive: (selectReslut) => {
                                                logic.checkAge(selectReslut.key, selectReslut.value),
                                              }),
                                    }
                                  else
                                    {
                                      DialogUtils.showWarningDialog('请扫描高频耳标或者输入可视耳标'),
                                    }
                                }),
                        getItemView(
                            '测量日期',
                            state.entity.value.measureDate ?? '',
                            () => {
                                  DialogUtils.showTimeDialog(
                                    context,
                                    isTime: false,
                                    title: '请选择测量日期',
                                    onPositive: (date) => {
                                      state.entity.value.measureDate = date,
                                      logic.update(),
                                    },
                                  ),
                                }),
                        const SizedBox(
                          width: 10,
                        ),
                      ],
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(2.0),
                    child: Row(
                      children: [
                        const SizedBox(
                          width: 10,
                        ),
                        getItemView(
                            '体高(cm)',
                            (state.entity.value.height ?? '').toString(),
                            () => {
                                  DialogUtils.showInputDialog(
                                      hintText: '输入牛只体高',
                                      isNum: true,
                                      onPositive: (text) => {
                                            state.entity.value.height = double.tryParse(text ?? '0'),
                                            logic.update(),
                                          }),
                                }),
                        getItemView(
                            '体长(cm)',
                            (state.entity.value.length ?? '').toString(),
                            () => {
                                  DialogUtils.showInputDialog(
                                      hintText: '输入牛只体长',
                                      isNum: true,
                                      onPositive: (text) => {
                                            state.entity.value.length = double.tryParse(text ?? '0'),
                                            logic.update(),
                                          }),
                                }),
                        const SizedBox(
                          width: 10,
                        ),
                      ],
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(2.0),
                    child: Row(
                      children: [
                        const SizedBox(
                          width: 10,
                        ),
                        getItemView(
                            '胸围(cm)',
                            (state.entity.value.bust ?? '').toString(),
                            () => {
                                  DialogUtils.showInputDialog(
                                      hintText: '输入牛只胸围',
                                      isNum: true,
                                      onPositive: (text) => {
                                            state.entity.value.bust = double.tryParse(text ?? '0'),
                                            logic.update(),
                                          }),
                                }),
                        getItemView(
                            '体重',
                            (state.entity.value.weight ?? '').toString(),
                            () => {
                                  DialogUtils.showInputDialog(
                                      hintText: '输入牛只体重',
                                      isNum: true,
                                      onPositive: (text) => {
                                            state.entity.value.weight = double.tryParse(text ?? '0'),
                                            logic.update(),
                                          }),
                                }),
                        const SizedBox(
                          width: 10,
                        ),
                      ],
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(2.0),
                    child: Row(
                      children: [
                        const SizedBox(
                          width: 10,
                        ),
                        getItemView(
                            '胸宽(cm)',
                            (state.entity.value.chestWidth ?? '').toString(),
                            () => {
                                  DialogUtils.showInputDialog(
                                      hintText: '输入牛只胸宽',
                                      isNum: true,
                                      onPositive: (text) => {
                                            state.entity.value.chestWidth = double.tryParse(text ?? '0'),
                                            logic.update(),
                                          }),
                                }),
                        getItemView(
                            '胸深(cm)',
                            (state.entity.value.chestDeep ?? '').toString(),
                            () => {
                                  DialogUtils.showInputDialog(
                                      hintText: '输入牛只胸深',
                                      isNum: true,
                                      onPositive: (text) => {
                                            state.entity.value.chestDeep = double.tryParse(text ?? '0'),
                                            logic.update(),
                                          }),
                                }),
                        const SizedBox(
                          width: 10,
                        ),
                      ],
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(2.0),
                    child: Row(
                      children: [
                        const SizedBox(
                          width: 10,
                        ),
                        getItemView(
                            '十字部高(cm)',
                            (state.entity.value.crossHeight ?? '').toString(),
                            () => {
                                  DialogUtils.showInputDialog(
                                      hintText: '输入牛只十字部高度',
                                      isNum: true,
                                      onPositive: (text) => {
                                            state.entity.value.crossHeight = double.tryParse(text ?? '0'),
                                            logic.update(),
                                          }),
                                }),
                        getItemView(
                            '十字部宽(cm)',
                            (state.entity.value.crossWidth ?? '').toString(),
                            () => {
                                  DialogUtils.showInputDialog(
                                      hintText: '输入牛只十字部宽',
                                      isNum: true,
                                      onPositive: (text) => {
                                            state.entity.value.crossWidth = double.tryParse(text ?? '0'),
                                            logic.update(),
                                          }),
                                }),
                        const SizedBox(
                          width: 10,
                        ),
                      ],
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(2.0),
                    child: Row(
                      children: [
                        const SizedBox(
                          width: 10,
                        ),
                        getItemView(
                            '管维(cm)',
                            (state.entity.value.guanWei ?? '').toString(),
                            () => {
                                  DialogUtils.showInputDialog(
                                      hintText: '输入牛只管维',
                                      isNum: true,
                                      onPositive: (text) => {
                                            state.entity.value.guanWei = double.tryParse(text ?? '0'),
                                            logic.update(),
                                          }),
                                }),
                        getItemView(
                            '眼肌面积',
                            (state.entity.value.eyeMuscleArea ?? '').toString(),
                            () => {
                                  DialogUtils.showInputDialog(
                                      hintText: '输入牛只眼肌面积',
                                      isNum: true,
                                      onPositive: (text) => {
                                            state.entity.value.eyeMuscleArea = double.tryParse(text ?? '0'),
                                            logic.update(),
                                          }),
                                }),
                        const SizedBox(
                          width: 10,
                        ),
                      ],
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(2.0),
                    child: Row(
                      children: [
                        const SizedBox(
                          width: 10,
                        ),
                        getItemView(
                            '等级',
                            state.grade ?? '',
                            () async => {
                                  DialogUtils.showSingleListDialog(
                                      await DictUtils.getDictList('zxgl_cattleresume_grade'),
                                      onPositive: (selectReslut) => {
                                            state.grade = selectReslut.key,
                                            state.entity.value.grade = selectReslut.value,
                                            logic.update(),
                                          }),
                                }),
                        getItemView(
                            '毛长度(cm)',
                            (state.entity.value.hairLength ?? '').toString(),
                            () => {
                                  DialogUtils.showInputDialog(
                                      hintText: '输入牛只毛长度',
                                      isNum: true,
                                      onPositive: (text) => {
                                            state.entity.value.hairLength = double.tryParse(text ?? '0'),
                                            logic.update(),
                                          }),
                                }),
                        const SizedBox(
                          width: 10,
                        ),
                      ],
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(2.0),
                    child: Row(
                      children: [
                        const SizedBox(
                          width: 10,
                        ),
                        getItemView(
                            '毛细度(微米)',
                            (state.entity.value.hairFineness ?? '').toString(),
                            () => {
                                  DialogUtils.showInputDialog(
                                      hintText: '输入牛只毛细度',
                                      isNum: true,
                                      onPositive: (text) => {
                                            state.entity.value.hairFineness = double.tryParse(text ?? '0'),
                                            logic.update(),
                                          }),
                                }),
                        getItemView(
                            '剪毛量(kg)',
                            (state.entity.value.shearing ?? '').toString(),
                            () => {
                                  DialogUtils.showInputDialog(
                                      hintText: '输入牛只剪毛量',
                                      isNum: true,
                                      onPositive: (text) => {
                                            state.entity.value.shearing = double.tryParse(text ?? '0'),
                                            logic.update(),
                                          }),
                                }),
                        const SizedBox(
                          width: 10,
                        ),
                      ],
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(2.0),
                    child: Row(
                      children: [
                        const SizedBox(
                          width: 10,
                        ),
                        getItemView(
                            '背腰宽',
                            (state.entity.value.backWidth ?? '').toString(),
                            () => {
                                  DialogUtils.showInputDialog(
                                      hintText: '输入牛只背腰宽',
                                      isNum: true,
                                      onPositive: (text) => {
                                            state.entity.value.backWidth = double.tryParse(text ?? '0'),
                                            logic.update(),
                                          }),
                                }),
                        getItemView(
                            '腰角宽(cm)',
                            (state.entity.value.waistWidth ?? '').toString(),
                            () => {
                                  DialogUtils.showInputDialog(
                                      hintText: '输入牛只腰角宽',
                                      isNum: true,
                                      onPositive: (text) => {
                                            state.entity.value.waistWidth = double.tryParse(text ?? '0'),
                                            logic.update(),
                                          }),
                                }),
                        const SizedBox(
                          width: 10,
                        ),
                      ],
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(2.0),
                    child: Row(
                      children: [
                        const SizedBox(
                          width: 10,
                        ),
                        getItemView(
                            '健康状况',
                            state.entity.value.health ?? '',
                            () => {
                                  DialogUtils.showInputDialog(
                                      hintText: '输入牛只健康状况',
                                      onPositive: (text) => {
                                            state.entity.value.health = text ?? '',
                                            logic.update(),
                                          }),
                                }),
                        getItemView(
                            '免疫情况',
                            state.entity.value.immune ?? '',
                            () => {
                                  DialogUtils.showInputDialog(
                                      hintText: '输入牛只免疫情况',
                                      onPositive: (text) => {
                                            state.entity.value.immune = 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.breedRes ?? '',
                            () => {
                                  DialogUtils.showInputDialog(
                                      hintText: '输入牛只繁殖成绩',
                                      onPositive: (text) => {
                                            state.entity.value.breedRes = text ?? '',
                                            logic.update(),
                                          }),
                                }),
                        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,
              ),
            ],
          ),
        ),
      ),
    );
  }
}
