import 'package:deus_app/common/event/RefreshPatrolEdit.dart';
import 'package:deus_app/common/style/TitleBar.dart';
import 'package:deus_app/common/style/gsy_style.dart';
import 'package:deus_app/common/utils/ConstantString.dart';
import 'package:deus_app/common/utils/DioUtil.dart';
import 'package:deus_app/common/utils/ToastUtils.dart';
import 'package:deus_app/main.dart';
import 'package:deus_app/model/patrol_job_detail_response_entity.dart';
import 'package:deus_app/page/patrol/patrol_job_edit.dart';
import 'package:flutter/material.dart';

class PatrolJobList extends StatefulWidget {
  final PatrolJobDetailResponseData responseData;

  const PatrolJobList({super.key, required this.responseData});

  @override
  State createState() {
    return _PatrolJobList(responseData);
  }
}

class _PatrolJobList extends State<PatrolJobList> {
  PatrolJobDetailResponseData responseData;

  _PatrolJobList(this.responseData);

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        child: Scaffold(
            appBar: TitleBar().backAppbar("巡检设备详情"),
            backgroundColor: const Color(0xfff2f2f2),
            body: Stack(
              children: [
                Column(
                  children: [
                    SizedBox(
                      height: 10,
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        SizedBox(
                          width: 15,
                        ),
                        Text(ConstantString.patrolJobTitle,
                            style: GSYConstant.smallActionLightText),
                      ],
                    ),
                    Expanded(
                      child: Container(
                          margin: EdgeInsets.only(bottom: 50),
                          child: ListView.builder(
                            itemCount: responseData.equipmentVOS.length,
                            itemBuilder: (context, 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: Row(
                                              mainAxisAlignment:
                                                  MainAxisAlignment
                                                      .spaceBetween,
                                              children: [
                                                Text(
                                                    responseData
                                                        .equipmentVOS[index]
                                                        .showName,
                                                    style: GSYConstant
                                                        .smallTextBold),
                                                Text(
                                                  responseData
                                                              .equipmentVOS[
                                                                  index]
                                                              .isComplete ==
                                                          0
                                                      ? '未完成'
                                                      : '已完成',
                                                  style: TextStyle(
                                                      fontSize: GSYConstant
                                                          .smallTextSize,
                                                      color: responseData
                                                                  .equipmentVOS[
                                                                      index]
                                                                  .isComplete ==
                                                              0
                                                          ? Colors.red
                                                          : Colors.blue),
                                                ),
                                              ]),
                                        ),
                                        Container(
                                          margin:
                                              const EdgeInsets.only(top: 10),
                                          // padding: const EdgeInsets.fromLTRB(30.0, 0.0, 0.0, 10.0),
                                          // alignment: Alignment.centerLeft,
                                          child: Row(
                                              mainAxisAlignment:
                                                  MainAxisAlignment
                                                      .spaceBetween,
                                              children: [
                                                new Text(
                                                  '完成时间：',
                                                  style: GSYConstant
                                                      .smallTextLight,
                                                ),
                                                Container(
                                                  child: Text(
                                                    responseData
                                                        .equipmentVOS[index]
                                                        .itemCompleteTime,
                                                    style: GSYConstant
                                                        .smallTextLight,
                                                    textAlign: TextAlign.right,
                                                  ),
                                                ),
                                              ]),
                                        ),
                                        SizedBox(
                                          height: 5,
                                        ),
                                      ],
                                    ),
                                    onTap: () {
                                      completeEquipment(index);
                                    },
                                  ));
                            },
                          )),
                    )
                  ],
                ),
                Positioned(
                  left: 0,
                  right: 0,
                  bottom: 0,
                  // flex: 7,
                  child: SizedBox(
                    height: 50,
                    width: double.infinity,
                    child: TextButton(
                      onPressed: () {
                        if (next()) {
                          Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) =>
                                      PatrolJobEdit(id: responseData.id)));
                        }
                      },
                      style: ButtonStyle(
                        backgroundColor:
                            MaterialStateProperty.all<Color>(Color(0xFF4875EC)),
                        shape: MaterialStateProperty.all(BeveledRectangleBorder(
                            borderRadius: BorderRadius.circular(0))),
                        foregroundColor:
                            MaterialStateProperty.all<Color>(Colors.white),
                        // padding: MaterialStateProperty.all(EdgeInsets.zero)
                      ),
                      child: const Text(ConstantString.next),
                    ),
                  ),
                )
              ],
            )),
        onWillPop: () async {
          eventBus.fire(RefreshPatrolEdit());
          return true;
        });
  }

  completeEquipment(int index) async {
    if (responseData.equipmentVOS[index].isComplete == 0) {
      var result = await DioUtil().request("patrolJob/completeEquipment",
          method: DioMethod.post,
          data: {'id': responseData.equipmentVOS[index].id});
      if (result['code'] == 0) {
        setState(() {
          responseData.equipmentVOS[index].isComplete = 1;
          responseData.equipmentVOS[index].itemCompleteTime = result['data'];
        });
      } else {
        showToast(result['msg']);
      }
    }
  }

  bool next() {
    bool isNext = true;
    for (var element in responseData.equipmentVOS) {
      if (element.isComplete == 0) {
        isNext = false;
        break;
      }
    }
    return isNext;
  }
}
