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

import '../../utils/dialog_utils.dart';
import '../../utils/dict_utils.dart';
import '../../utils/empty_utils.dart';
import 'weight_man_logic.dart';

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

  final logic = Get.find<WeightManLogic>();
  final state = Get.find<WeightManLogic>().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<WeightManLogic>(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.weight??'').toString(),
                            () async => {
                                  DialogUtils.showInputDialog(
                                      hintText: '输入牛只重量',
                                      isNum: true,
                                      onPositive: (text) => {
                                            state.entity.value.weight = double.parse(text ?? '0'),
                                            logic.update(),
                                          }),
                                }),
                        getItemView(
                            '称重日期',
                            state.entity.value.wTime ?? '',
                            () => {
                                  if (EmptyUtils.isStrNotEmpty(state.entity.value.cattleresumeId))
                                    {
                                      DialogUtils.showTimeDialog(
                                        context,
                                        isTime: false,
                                        title: '请选择测量日期',
                                        onPositive: (date) => {
                                          logic.checkWeightDate(date),
                                        },
                                      ),
                                    }
                                  else
                                    {
                                      DialogUtils.showWarningDialog('请扫描高频耳标或者输入可视耳标'),
                                    }
                                }),
                        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,
              ),
            ],
          ),
        ),
      ),
    );
  }
}
