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

import '../../../utils/dialog_utils.dart';
import '../../../utils/dict_utils.dart';
import 'breeding_record_detail_logic.dart';

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

  final logic = Get.find<BreedingRecordDetailLogic>();
  final state = Get.find<BreedingRecordDetailLogic>().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<BreedingRecordDetailLogic>(builder: (logic) {
              return ListView(
                children: [
                  Padding(
                    padding: const EdgeInsets.all(2.0),
                    child: Row(
                      children: [
                        const SizedBox(
                          width: 10,
                        ),
                        getItemView('高频耳标', state.entity.value.registrationNo ?? '', () => {}),
                        const SizedBox(
                          width: 10,
                        ),
                      ],
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(2.0),
                    child: Row(
                      children: [
                        const SizedBox(
                          width: 10,
                        ),
                        getItemView('可视耳标', state.entity.value.individualNo ?? '', () => {}),
                        const SizedBox(
                          width: 10,
                        ),
                      ],
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(2.0),
                    child: Row(
                      children: [
                        const SizedBox(
                          width: 10,
                        ),
                        getItemView(
                            '配种方式',
                            state.matingStyle ?? '',
                            () async => {
                                  DialogUtils.showSingleListDialog(
                                      await DictUtils.getDictList('zxgl_cattlemating_matingStyle'),
                                      onPositive: (selectReslut) => {
                                            state.matingStyle = selectReslut.key,
                                            state.entity.value.matingStyle = selectReslut.value,
                                            logic.update(),
                                          }),
                                }),
                        getItemView(
                            '配种日期',
                            state.entity.value.matingDate ?? '',
                            () => {
                                  DialogUtils.showTimeDialog(
                                    Get.context!,
                                    isTime: false,
                                    title: '请选择配种日期',
                                    onPositive: (date) => {
                                      state.entity.value.matingDate = date,
                                      logic.calculateDueDate(date),
                                    },
                                  ),
                                }),
                      ],
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(2.0),
                    child: Row(
                      children: [
                        const SizedBox(
                          width: 10,
                        ),
                        getItemView(
                            '预产日期',
                            state.entity.value.dueDate ?? '',
                            () => {
                                  // DialogUtils.showTimeDialog(
                                  //   Get.context!,
                                  //   isTime: false,
                                  //   title: '请选择预产日期',
                                  //   onPositive: (date) => {
                                  //     itemDateList[i].dueDate = date,
                                  //     update(),
                                  //   },
                                  // ),
                                }),
                        getItemView(
                            '冻精批次',
                            state.entity.value.frozenSemenBatch ?? '',
                            () => {
                                  DialogUtils.showInputDialog(
                                      hintText: '输入冻精批次',
                                      onPositive: (text) => {
                                            state.entity.value.frozenSemenBatch = text ?? '',
                                            logic.update(),
                                          }),
                                }),
                      ],
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(2.0),
                    child: Row(
                      children: [
                        const SizedBox(
                          width: 10,
                        ),
                        getItemView(
                            '备注',
                            state.entity.value.remark ?? '',
                            () => {
                                  // DialogUtils.showInputDialog(
                                  //     hintText: '输入备注',
                                  //     onPositive: (text) => {
                                  //           itemDateList[i].remark = text ?? '',
                                  //           update(),
                                  //         }),
                                }),
                      ],
                    ),
                  ),
                  const SizedBox(
                    height: 5,
                  ),
                ],
              );
            }),
          ),
          // 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,
              ),
            ],
          ),
        ),
      ),
    );
  }
}
