import 'package:deus_app/common/style/TitleBar.dart';
import 'package:deus_app/common/style/gsy_style.dart';
import 'package:deus_app/common/utils/DioUtil.dart';
import 'package:deus_app/common/utils/ToastUtils.dart';
import 'package:deus_app/generated/json/patrol_job_select_entity_entity_helper.dart';
import 'package:deus_app/model/patrol_job_select_entity_entity.dart';
import 'package:deus_app/page/patrol/patrol_job_add.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class PatrolJobSelect extends StatefulWidget {

  var id;

  @override
  State createState() {
    return _PatrolJobSelect(id);
  }
  PatrolJobSelect({super.key, required this.id});
}

class _PatrolJobSelect extends State<PatrolJobSelect> {

  var id;

  _PatrolJobSelect(this.id);

  final RefreshController _refreshController =
      RefreshController(initialRefresh: false);
  int index = 1;

  List<PatrolJobSelectEntityDataPatrolItemModelVOS> patrolItemModelVOS = [];

  PatrolJobSelectEntityEntity jobEdit = PatrolJobSelectEntityEntity();

  @override
  void initState() {
    super.initState();
    patrolItemModel();
  }

  patrolItemModel() async {
    var result = await DioUtil().request('patrolItemModel/list',
        method: DioMethod.post, data: {'index': index, 'size': 20});
    _refreshController.loadComplete();
    _refreshController.refreshCompleted();
    if (0 == result['code']) {
      setState(() {
        jobEdit = patrolJobSelectEntityEntityFromJson(
            PatrolJobSelectEntityEntity(), result);
        patrolItemModelVOS.clear();
        patrolItemModelVOS.addAll(jobEdit.data.patrolItemModelVOS);
      });
    } else {
      if (index > 1) {
        index--;
      }
      showToast(result['msg']);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: TitleBar().backAppbar("选择巡检项目"),
        backgroundColor: const Color(0xfff2f2f2),
        body: Stack(
          children: [
            Container(
                    child: SmartRefresher(
              enablePullDown: true,
              enablePullUp: true,
              header: WaterDropHeader(),
              footer: CustomFooter(
                builder: (BuildContext context, LoadStatus? mode) {
                  Widget body;
                  if (mode == LoadStatus.idle) {
                    body = Text("上拉加载");
                  } else if (mode == LoadStatus.loading) {
                    body = CupertinoActivityIndicator();
                  } else if (mode == LoadStatus.failed) {
                    body = Text("加载失败！点击重试！");
                  } else if (mode == LoadStatus.canLoading) {
                    body = Text("松手,加载更多!");
                  } else {
                    body = Text("没有更多数据了!");
                  }
                  return Container(
                    height: 55.0,
                    child: Center(child: body),
                  );
                },
              ),
              controller: _refreshController,
              onRefresh: _onRefresh,
              onLoading: _onLoading,
              child: ListView.builder(
                itemCount: patrolItemModelVOS.length,
                itemBuilder: (context, index) {
                  return _patrol_job_list(index);
                },
              ),
            ))
          ],
        ));
  }

  void _onRefresh() async {
    if (mounted) {
      setState(() {
        index = 1;
        patrolItemModel();
      });
    }
  }

  void _onLoading() async {
    if (mounted) {
      setState(() {
        if (jobEdit.data.total > patrolItemModelVOS.length) {
          index++;
          patrolItemModel();
        } else {
          _refreshController.loadNoData();
        }
      });
    }
  }

  Widget _patrol_job_list(int index) {
    return Container(
        margin: EdgeInsets.only(top: 12, left: 10, right: 10),
        padding: EdgeInsets.only(top: 12, bottom: 10),
        color: Colors.white,
        child: ListTile(
          title: Column(
            children: [
              Container(
                // padding: const EdgeInsets.fromLTRB(30.0, 0.0, 0.0, 0.0),
                alignment: Alignment.centerLeft,
                child: Text(
                  patrolItemModelVOS[index].name,
                  style: const TextStyle(
                    fontSize: GSYConstant.smallTextSize,
                    color: Colors.black,
                  ),
                ),
              ),
              Container(
                margin: const EdgeInsets.only(top: 10),
                // padding: const EdgeInsets.fromLTRB(30.0, 0.0, 0.0, 10.0),
                // alignment: Alignment.centerLeft,
                alignment: Alignment.centerLeft,
                child: Text(
                  patrolItemModelVOS[index].params,
                  style: GSYConstant.smallTextLight,
                  textAlign: TextAlign.left,
                ),
              ),
              SizedBox(
                height: 10,
              ),
              Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
                Container(
                  child: Text(
                    '类型:',
                    style: TextStyle(
                      color: GSYColors.primaryLightValue,
                      fontSize: GSYConstant.smallTextSize,
                    ),
                  ),
                ),
                Container(
                  child: Text(
                    patrolItemModelVOS[index].type == 0
                        ? '数值'
                        : patrolItemModelVOS[index].type == 1
                            ? '长文本'
                            : patrolItemModelVOS[index].type == 2
                                ? '单选'
                                : '多选',
                    textAlign: TextAlign.right,
                    style: TextStyle(
                      color: GSYColors.primaryLightValue,
                      fontSize: GSYConstant.smallTextSize,
                    ),
                  ),
                ),
              ]),
            ],
          ),
          onTap: () {
            Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) =>
                        PatrolJobAdd(responseData: patrolItemModelVOS[index], id: id,)));
          },
        ));
  }

}
