import 'dart:io';

import 'package:anchor_collect_flutter/utils/dialog_utils.dart';
import 'package:anchor_collect_flutter/utils/dict_utils.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../congifs.dart';
import '../../routes/routes.dart';
import '../../utils/empty_utils.dart';
import '../../widget/NullDataView.dart';
import 'cattle_resume_logic.dart';
import 'package:flutter_speed_dial/flutter_speed_dial.dart';

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

  @override
  Widget build(BuildContext context) {
    state.isCamera = false;
    return Scaffold(
      appBar: AppBar(
        title: const Text('基础信息'),
        backgroundColor: Colors.blue,
        centerTitle: true,
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          Expanded(
            child: GetBuilder<CattleResumeLogic>(builder: (logic) {
              return Obx(() {
                return ListView(
                  children: [
                    ///图片
                    InkWell(
                      child: AspectRatio(
                        aspectRatio: 1.0 / 1.0,
                        child: EmptyUtils.isStrNotEmpty(state.photoPath)
                            ? Image.file(
                                File(state.photoPath ?? ''),
                                fit: BoxFit.contain,
                              )
                            : const NullDataView(),
                      ),
                      onTap: () async {
                        if (Config.controller != null) {
                          var photoPath = await Get.toNamed(AppRoute.CAMERA, arguments: state.unid);
                          if (EmptyUtils.isStrNotEmpty(photoPath)) {
                            state.isCamera = true;
                            state.photoPath = photoPath;
                            logic.update();
                          }
                        }else{
                          DialogUtils.showToast('初始化相机失败');
                        }
                      },
                    ),
                    Padding(
                      padding: const EdgeInsets.all(2.0),
                      child: Row(
                        children: [
                          const SizedBox(
                            width: 10,
                          ),
                          getItemView(
                              '高频耳标',
                              state.entity.value.registrationNo ?? '',
                              () => {
                                    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 ?? '',
                              () => {
                                    DialogUtils.showInputDialog(
                                        hintText: '请输入可视耳标',
                                        onPositive: (text) => {
                                              state.entity.value.individualNo = 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.birthHeavy ?? '').toString(),
                              () => {
                                    DialogUtils.showInputDialog(
                                        hintText: '输入出生重量(kg)',
                                        isNum: true,
                                        onPositive: (text) => {
                                              state.entity.value.birthHeavy = double.parse(text),
                                              logic.update(),
                                            }),
                                  }),
                          getItemView(
                              '出生日期',
                              state.entity.value.birthdate ?? '',
                              () => {
                                    DialogUtils.showTimeDialog(
                                      context,
                                      isTime: false,
                                      title: '请选择出生日期',
                                      onPositive: (date) => {
                                        state.entity.value.birthdate = date,
                                        logic.update(),
                                      },
                                    ),
                                  }),
                          const SizedBox(
                            width: 10,
                          ),
                        ],
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.all(2.0),
                      child: Row(
                        children: [
                          const SizedBox(
                            width: 10,
                          ),
                          getItemView(
                              '饲养类型',
                              state.raiseType ?? '',
                              () async => {
                                    DialogUtils.showSingleListDialog(await DictUtils.getDictList('zxgl_raiseType'),
                                        onPositive: (selectReslut) => {
                                              state.raiseType = selectReslut.key,
                                              state.entity.value.raiseType = selectReslut.value,
                                              logic.update(),
                                            }),
                                  }),
                          getItemView(
                              '饲养品种',
                              state.raiseVarieties ?? '',
                              () async => {
                                    DialogUtils.showSingleListDialog(
                                        await DictUtils.getDictList('zxgl_cattle_raiseVarieties'),
                                        onPositive: (selectReslut) => {
                                              state.raiseVarieties = selectReslut.key,
                                              state.entity.value.raiseVarieties = selectReslut.value,
                                              logic.update(),
                                            }),
                                  }),
                          const SizedBox(
                            width: 10,
                          ),
                        ],
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.all(2.0),
                      child: Row(
                        children: [
                          const SizedBox(
                            width: 10,
                          ),
                          getItemView(
                              '性别',
                              state.sex ?? '',
                              () async => {
                                    DialogUtils.showSingleListDialog(
                                        await DictUtils.getDictList('zxgl_cattleresume_sex'),
                                        onPositive: (selectReslut) => {
                                              state.sex = selectReslut.key,
                                              state.entity.value.sex = selectReslut.value,
                                              logic.update(),
                                            }),
                                  }),
                          getItemView(
                              '出生难易度',
                              state.birthDifficulty ?? '',
                              () async => {
                                    DialogUtils.showSingleListDialog(
                                        await DictUtils.getDictList('zxgl_cattleresume_birthDifficulty'),
                                        onPositive: (selectReslut) => {
                                              state.birthDifficulty = selectReslut.key,
                                              state.entity.value.birthDifficulty = selectReslut.value,
                                              logic.update(),
                                            }),
                                  }),
                          const SizedBox(
                            width: 10,
                          ),
                        ],
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.all(2.0),
                      child: Row(
                        children: [
                          const SizedBox(
                            width: 10,
                          ),
                          getItemView(
                              '犊牛毛色',
                              state.entity.value.colour ?? '',
                              () => {
                                    DialogUtils.showInputDialog(
                                        hintText: '输入犊牛毛色',
                                        onPositive: (text) => {
                                              state.entity.value.colour = text,
                                              logic.update(),
                                            }),
                                  }),
                          getItemView(
                              '是否双胞胎',
                              state.isTwins ?? '',
                              () async => {
                                    DialogUtils.showSingleListDialog(await DictUtils.getDictList('sys_yes_no'),
                                        onPositive: (selectReslut) => {
                                              state.isTwins = selectReslut.key,
                                              state.entity.value.isTwins = selectReslut.value,
                                              logic.update(),
                                            }),
                                  }),
                          const SizedBox(
                            width: 10,
                          ),
                        ],
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.all(2.0),
                      child: Row(
                        children: [
                          const SizedBox(
                            width: 10,
                          ),
                          getItemView(
                              '母本胎次',
                              state.entity.value.childTime ?? '',
                              () => {
                                    DialogUtils.showInputDialog(
                                        hintText: '输入母本胎次',
                                        onPositive: (text) => {
                                              state.entity.value.childTime = text,
                                              logic.update(),
                                            }),
                                  }),
                          getItemView(
                              '父登记号',
                              state.entity.value.fatherReg ?? '',
                              () => {
                                    DialogUtils.showInputDialog(
                                        hintText: '输入父登记号',
                                        onPositive: (text) => {
                                              state.entity.value.fatherReg = 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.motherReg ?? '',
                              () => {
                                    DialogUtils.showInputDialog(
                                        hintText: '输入母登记号',
                                        onPositive: (text) => {
                                              state.entity.value.motherReg = text,
                                              logic.update(),
                                            }),
                                  }),
                          getItemView(
                              '母牛年龄',
                              (state.entity.value.age ?? '0').toString(),
                              () => {
                                    DialogUtils.showInputDialog(
                                        hintText: '输入母牛年龄',
                                        isNum: true,
                                        onPositive: (text) => {
                                              state.entity.value.age = int.parse(text),
                                              logic.update(),
                                            }),
                                  }),
                          const SizedBox(
                            width: 10,
                          ),
                        ],
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.all(2.0),
                      child: Row(
                        children: [
                          const SizedBox(
                            width: 10,
                          ),
                          // getItemView(
                          //     '圈舍号',
                          //     state.circleNo ?? '',
                          //     () async => {
                          //           DialogUtils.showSingleListDialog(await DictUtils.getDictList('zxgl_circle_list'),
                          //               onPositive: (selectReslut) => {
                          //                     state.circleNo = selectReslut.key,
                          //                     state.entity.value.circleNo = selectReslut.value,
                          //                     logic.update(),
                          //                   }),
                          //         }),
                          getItemView(
                              '健康状态',
                              state.health ?? '',
                              () async => {
                                    DialogUtils.showSingleListDialog(
                                        await DictUtils.getDictList('zxgl_cattleresume_health'),
                                        onPositive: (selectReslut) => {
                                              state.health = selectReslut.key,
                                              state.entity.value.health = selectReslut.value,
                                              logic.update(),
                                            }),
                                  }),
                          getItemView(
                              '状态',
                              state.status ?? '',
                                  () async => {
                                DialogUtils.showSingleListDialog(
                                    await DictUtils.getDictList('zxgl_cattleresume_status'),
                                    onPositive: (selectReslut) => {
                                      state.status = selectReslut.key,
                                      state.entity.value.status = selectReslut.value,
                                      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("保存"),
          ),
          //   ),
          // ]),
        ],
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
      floatingActionButton: Padding(
        padding: const EdgeInsets.only(right: 20.0, bottom: 80.0),
        child: SpeedDial(
          icon: Icons.add,
          activeIcon: Icons.close,
          spacing: 3,
          // openCloseDial: ValueNotifier.value,
          childPadding: const EdgeInsets.all(5),
          buttonSize: const Size(40, 40),
          elevation: 8.0,
          children: [
            // SpeedDialChild(
            //   child: const Icon(IconFont.cattle_resume),
            //   backgroundColor: Colors.black,
            //   foregroundColor: Colors.white,
            //   label: '基础信息',
            //   onTap: () {
            //     // DialogUtils.showToast('toast...');
            //   },
            //   onLongPress: () => debugPrint('FIRST CHILD LONG PRESS'),
            // ),
            SpeedDialChild(
              child: const Icon(IconFont.performance),
              backgroundColor: const Color.fromARGB(255, 104, 152, 218),
              foregroundColor: Colors.white,
              label: '性能测定',
              onTap: () => {
                logic.forward('性能测定'),
              },
            ),
            SpeedDialChild(
              child: const Icon(IconFont.medical_record),
              backgroundColor: const Color.fromARGB(255, 220, 128, 70),
              foregroundColor: Colors.white,
              label: '诊疗记录',
              visible: true,
              onTap: () => {  logic.forward('诊疗记录'),},
              // onLongPress: () => debugPrint('THIRD CHILD LONG PRESS'),
            ),
            SpeedDialChild(
              child: const Icon(IconFont.breeding_record),
              backgroundColor: const Color.fromARGB(255, 210, 76, 154),
              foregroundColor: Colors.white,
              label: '配种记录',
              onTap: () => {logic.forward('配种记录'),},
              // onLongPress: () => debugPrint('FIRST CHILD LONG PRESS'),
            ),
            SpeedDialChild(
              child: const Icon(IconFont.immune),
              backgroundColor: const Color.fromARGB(255, 116, 154, 110),
              foregroundColor: Colors.white,
              label: '免疫记录',
              onTap: () => {logic.forward('免疫记录'),},
            ),
            SpeedDialChild(
              child: const Icon(IconFont.weight),
              backgroundColor: const Color.fromARGB(255, 150, 114, 89),
              foregroundColor: Colors.white,
              label: '称重记录',
              visible: true,
              onTap: () => {logic.forward('称重记录'),},
              // onLongPress: () => ,
            ),
          ],
        ),
      ),
    );
  }

  ///信息条目
  getItemView(String title, String context, Function() onClick) {
    return Expanded(
      child: Container(
          decoration: const BoxDecoration(
            color: Colors.white,
            boxShadow: [
              BoxShadow(
                color: Colors.black12,
                blurRadius: 2,
              ),
            ],
          ),
          padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
          child: InkWell(
            onTap: onClick,
            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,
                ),
              ],
            ),
          ),
        ),
    );
  }
}
