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

import '../../../congifs.dart';
import '../../../utils/dialog_utils.dart';
import '../../../utils/dict_utils.dart';
import 'immune_batch_list_logic.dart';

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

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

  late EasyRefreshController _controller;

  @override
  Widget build(BuildContext context) {
    _controller = EasyRefreshController();
    logic.queryFirstPage();
    return Scaffold(
      resizeToAvoidBottomInset: false,
      appBar: AppBar(
        title: const Text('免疫记录'),
        backgroundColor: Colors.blue,
        centerTitle: true,
        actions: [
          Padding(
            padding: const EdgeInsets.all(5.0),
            child: InkWell(
              child: const Icon(Icons.search),
              onTap: () {
                logic.clean();
                logic.isShow = !logic.isShow;
                logic.update();
              },
            ),
          ),
          Config.isAdmin?Container():Padding(
            padding: const EdgeInsets.all(5.0),
            child: InkWell(
              child: const Center(
                child: Text('新增'),
              ),
              onTap: () => {
                logic.itemOnClickListener(),
              },
            ),
          ),
        ],
      ),
      body: GetBuilder<ImmuneBatchListLogic>(builder: (logic) {
        return Center(
          child: Stack(children: [
            EasyRefresh(
              controller: _controller,
              onLoad: () => logic.onLoad(),
              onRefresh: () => logic.onRefresh(),
              child: ListView.separated(
                itemCount: state.items.length,
                itemBuilder: (context, index) {
                  return state.items[index];
                },
                separatorBuilder: (context, index) {
                  return Container(
                    height: 3,
                    color: Colors.black12,
                  );
                },
              ),
            ),
            logic.isShow
                ? Row(
                    children: [
                      Expanded(
                        child: InkWell(
                          child: Container(
                            height: double.infinity,
                            color: Colors.black26,
                          ),
                          onTap: () => {
                            logic.isShow = false,
                            logic.update(),
                          },
                        ),
                      ),
                      GetBuilder<ImmuneBatchListLogic>(builder: (logic) {
                        return Container(
                          width: 280,
                          color: Colors.white,
                          padding: const EdgeInsets.all(20),
                          child: Column(
                            mainAxisSize: MainAxisSize.max,
                            children: [
                              TextField(
                                readOnly: true,
                                controller: logic.highController,
                                onChanged: (text) {
                                  state.filterCircleName = text;
                                },
                                decoration: const InputDecoration(
                                  prefixIcon: Icon(Icons.search),
                                  border: OutlineInputBorder(),
                                  hintText: '圈舍',
                                  contentPadding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 8.0),
                                ),
                                onTap: () async => {
                                  DialogUtils.showSingleListDialog(await DictUtils.getDictList('zxgl_circle_list'),
                                      onPositive: (selectReslut) => {
                                            state.filterCircleNo = selectReslut.key,
                                            state.filterCircleName = selectReslut.value,
                                            logic.update(),
                                          }),
                                },
                              ),
                              const SizedBox(
                                height: 10,
                              ),
                              TextField(
                                onChanged: (text) {
                                  state.filterVaccineName = text;
                                },
                                decoration: const InputDecoration(
                                  prefixIcon: Icon(Icons.search),
                                  border: OutlineInputBorder(),
                                  hintText: '疫苗名称',
                                  contentPadding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 8.0),
                                ),
                              ),
                              const SizedBox(
                                height: 10,
                              ),
                              SizedBox(
                                width: Get.width,
                                child: TextButton(
                                  style: ButtonStyle(
                                    backgroundColor: MaterialStateProperty.all(Colors.blue),
                                    foregroundColor: MaterialStateProperty.all(Colors.white),
                                    padding: MaterialStateProperty.all(const EdgeInsets.all(10)),
                                  ),
                                  onPressed: () => {
                                    logic.queryByFilter(),
                                  },
                                  child: const Text("查询"),
                                ),
                              ),
                              const SizedBox(
                                height: 10,
                              ),
                              SizedBox(
                                width: Get.width,
                                child: TextButton(
                                  style: ButtonStyle(
                                    backgroundColor: MaterialStateProperty.all(Colors.white),
                                    foregroundColor: MaterialStateProperty.all(Colors.blue),
                                    padding: MaterialStateProperty.all(const EdgeInsets.all(10)),
                                    shape: MaterialStateProperty.all<RoundedRectangleBorder>(
                                      RoundedRectangleBorder(
                                        borderRadius: BorderRadius.circular(10),
                                      ),
                                    ),
                                    side: MaterialStateProperty.all<BorderSide>(const BorderSide(
                                      color: Colors.black54, // 边框颜色
                                      width: 1, // 边框宽度
                                    )),
                                  ),
                                  onPressed: () => logic.clean(),
                                  child: const Text("重置"),
                                ),
                              ),
                            ],
                          ),
                        );
                      }),
                    ],
                  )
                : Container(),
          ]),
        );
      }),
    );
  }
}
