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

import '../../congifs.dart';
import '../../models/breeding_record_entity.dart';
import '../../utils/dialog_utils.dart';
import 'breeding_record_logic.dart';

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

  final logic = Get.find<BreedingRecordLogic>();
  final state = Get.find<BreedingRecordLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      appBar: AppBar(
        title: const Text('配种记录',
          style: TextStyle(
            color: Colors.white,
          ),),
        backgroundColor: Colors.blue,
        centerTitle: true,
        actions: [
          Config.isAdmin?Container():Padding(
            padding: const EdgeInsets.all(5.0),
            child: InkWell(
              child: const Center(
                child: Text('新增'),
              ),
              onTap: () => {
                if (EmptyUtils.isStrNotEmpty(state.entity.value.unid))
                  {
                    logic.itemOnClickListener(),
                  }
                else
                  {
                    DialogUtils.showWarningDialog('请扫描高频耳标和可视耳标'),
                  }
              },
            ),
          ),
        ],
      ),
      body: GetBuilder<BreedingRecordLogic>(builder: (logic) {
        return Center(
          child: Column(
            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) => {
                                            state.entity.value.registrationNo = '',
                                            state.entity.value.individualNo = text,
                                            state.entity.value = BreedingRecordEntity(),
                                            logic.queryBreedingRecord('', text),
                                          }),
                                }
                            }),
                    const SizedBox(
                      width: 10,
                    ),
                  ],
                ),
              ),
              Expanded(
                child: ListView.separated(
                  itemCount: state.items.length,
                  shrinkWrap: true,
                  itemBuilder: (context, index) {
                    return state.items[index];
                  },
                  separatorBuilder: (context, index) {
                    return Container(
                      height: 3,
                      color: Colors.black12,
                    );
                  },
                ),
              ),
            ],
          ),
        );
      }),
    );
  }

  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,
              ),
            ],
          ),
        ),
      ),
    );
  }
}
