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('配种详情',
          style: TextStyle(
            color: Colors.white,
          ),),
        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,
                        //                     if (selectReslut.value == '1')//1本交  2 受精
                        //                       {
                        //                         state.isMating = true,
                        //                         state.entity.value.frozenSemenBatch = '',
                        //                         state.frozenSemenBatch = '',
                        //                         state.entity.value.other = '',
                        //                       }
                        //                     else
                        //                       {
                        //                         state.isMating = false,
                        //                         state.entity.value.cattleresumeId = '',
                        //                       },
                        //                     logic.update(),
                        //                   }),
                        //         }),
                        getItemView(
                            '冻精信息',
                            state.frozenSemenBatch ?? '',
                                () async => {
                              DialogUtils.showSingleListDialog(await DictUtils.getDictList('zxgl_frozen_semen'),
                                  onPositive: (selectReslut) => {
                                    state.frozenSemenBatch = selectReslut.key,
                                    state.entity.value.frozenSemenBatch = selectReslut.value,
                                    if (selectReslut.value == '1')
                                      {
                                        state.isOther = true,
                                        state.entity.value.cattleresumeId = '',
                                      }
                                    else
                                      {
                                        state.isOther = false,
                                        state.entity.value.other = '',
                                        state.entity.value.cattleresumeId = selectReslut.key.substring(selectReslut.key.lastIndexOf('/')+1, selectReslut.key.length),
                                      },
                                    logic.update(),
                                  }),
                            }),
                        getItemView(
                            '冻精注册号',
                            state.entity.value.cattleresumeId ?? '',
                            () => {
                                      // DialogUtils.showInputDialog(
                                      //     hintText: '输入冻精注册号',
                                      //     onPositive: (text) => {
                                      //           state.entity.value.cattleresumeId = text ?? '',
                                      //           logic.update(),
                                      //         }),
                                }),
                      ],
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(2.0),
                    child: Row(
                      children: [
                        const SizedBox(
                          width: 10,
                        ),
                        getItemView(
                            '其它',
                            state.entity.value.other ?? '',
                            () => {
                                  if (state.isOther)
                                    {
                                      DialogUtils.showInputDialog(
                                          hintText: '输入冻精批次',
                                          onPositive: (text) => {
                                                state.entity.value.other = text ?? '',
                                                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.remark ?? '',
                                () => {
                              // DialogUtils.showInputDialog(
                              //     hintText: '输入备注',
                              //     onPositive: (text) => {
                              //           itemDateList[i].remark = text ?? '',
                              //           update(),
                              //         }),
                            }),
                      ],
                    ),
                  ),
                  // Padding(
                  //   padding: const EdgeInsets.all(2.0),
                  //   child: Row(
                  //     children: [
                  //       const SizedBox(
                  //         width: 10,
                  //       ),
                  //     ],
                  //   ),
                  // ),
                  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,
              ),
            ],
          ),
        ),
      ),
    );
  }
}
