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/generated/json/equipment_info_entity_helper.dart';
import 'package:deus_app/generated/json/equipment_list_entity_helper.dart';
import 'package:deus_app/model/equipment_info_entity.dart';
import 'package:deus_app/model/equipment_list_entity.dart';
import 'package:deus_app/page/device/device_manage_detail_page.dart';
import 'package:deus_app/widget/MyDrawer.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class DeviceManagePage extends StatefulWidget {
  static var routeName = '/DeviceManagePage';

  const DeviceManagePage({super.key});

  @override
  State createState() {
    return _DeviceManage();
  }
}

class _DeviceManage extends State<DeviceManagePage> {
  String deviceNum = "0";
  String _device = '', _username = '';
  dynamic _satus, _isEnabled;

  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

  List<EquipmentListDataEquipmentVOS> equipmentVOS =
      <EquipmentListDataEquipmentVOS>[];

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

  @override
  Widget build(BuildContext contexts) {
    return Scaffold(
      key: _scaffoldKey,
      appBar: TitleBar().drawAppBar(ConstantString.deviceManageText, () {
        _scaffoldKey.currentState?.openEndDrawer();
      }),
      backgroundColor: const Color(0xfff2f2f2),
      endDrawer: MyDrawer(
        callback:
            (String device, String name, dynamic stase, dynamic isEnabled) {
          _device = device;
          _username = name;
          _satus = stase;
          _isEnabled = isEnabled;
          equipmentList();
        },
      ),
      //抽屉
      body: equipmentVOS.isNotEmpty
          ? Column(
              children: [
                SizedBox(
                  height: 10,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    SizedBox(
                      width: 15,
                    ),
                    Text(ConstantString.deviceNum,
                        style: GSYConstant.smallActionLightText),
                    Text(deviceNum, style: GSYConstant.normalTextBigWhiteBold),
                  ],
                ),
                Expanded(
                    child: 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: 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: [
                                Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: [
                                      Container(
                                        child: Text(
                                          equipmentVOS[index].showName,
                                          style: const TextStyle(
                                            color: Colors.black,
                                            fontSize:
                                                GSYConstant.middleTextWhiteSize,
                                            fontWeight: FontWeight.bold,
                                          ),
                                        ),
                                      ),
                                      Container(
                                        padding: EdgeInsets.only(
                                            top: 3,
                                            bottom: 3,
                                            left: 5,
                                            right: 5),
                                        child: Text(
                                          equipmentVOS[index].status == 0
                                              ? '在线'
                                              : equipmentVOS[index].status == 1
                                                  ? '离线'
                                                  : equipmentVOS[index]
                                                              .status ==
                                                          2
                                                      ? '未激活'
                                                      : '未知',
                                          textAlign: TextAlign.right,
                                          style: TextStyle(
                                            color: equipmentVOS[index].status ==
                                                    0
                                                ? Colors.blue
                                                : equipmentVOS[index].status ==
                                                        1
                                                    ? Colors.orange
                                                    : equipmentVOS[index]
                                                                .status ==
                                                            2
                                                        ? Colors.red
                                                        : Colors.black,
                                            fontSize: GSYConstant.minTextSize,
                                          ),
                                        ),
                                        decoration: BoxDecoration(
                                          border: new Border.all(
                                            color: equipmentVOS[index].status ==
                                                    0
                                                ? Colors.blue
                                                : equipmentVOS[index].status ==
                                                        1
                                                    ? Colors.orange
                                                    : equipmentVOS[index]
                                                                .status ==
                                                            2
                                                        ? Colors.red
                                                        : Colors.black, //边框颜色
                                            width: 1.0, //边框粗细
                                          ),
                                          borderRadius: const BorderRadius.all(
                                              const Radius.circular(
                                                  3.0)), //边框的弧度
                                        ),
                                      )
                                    ]),
                                // SizedBox(
                                //   height: 12,
                                // ),
                                // Row(
                                //     mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                //     children: [
                                //       Container(
                                //         child: Text(
                                //           'devicekey:',
                                //           style: TextStyle(
                                //             color: GSYColors.primaryLightValue,
                                //             fontSize: GSYConstant.middleTextWhiteSize,
                                //           ),
                                //         ),
                                //       ),
                                //       Container(
                                //         child: Text(
                                //           'devicekey',
                                //           textAlign: TextAlign.right,
                                //           style: TextStyle(
                                //             color: GSYColors.primaryLightValue,
                                //             fontSize: GSYConstant.middleTextWhiteSize,
                                //           ),
                                //         ),
                                //       ),
                                //     ]),
                                // SizedBox(
                                //   height: 10,
                                // ),
                                // Row(
                                //     mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                //     children: [
                                //       Container(
                                //         child: Text(
                                //           'IMEI:',
                                //           style: TextStyle(
                                //             color: GSYColors.primaryLightValue,
                                //             fontSize: GSYConstant.middleTextWhiteSize,
                                //           ),
                                //         ),
                                //       ),
                                //       Container(
                                //         child: Text(
                                //           'IMEI',
                                //           textAlign: TextAlign.right,
                                //           style: TextStyle(
                                //             color: GSYColors.primaryLightValue,
                                //             fontSize: GSYConstant.middleTextWhiteSize,
                                //           ),
                                //         ),
                                //       ),
                                //     ]),
                                SizedBox(
                                  height: 5,
                                ),
                              ],
                            ),
                            onTap: () {
                              equipmentInfo(equipmentVOS[index].id);
                            },
                          ));
                    },
                  ),
                ))
              ],
            )
          : Container(
              alignment: Alignment.center,
              child: const Text(
                '暂无数据',
                textAlign: TextAlign.center,
                style: TextStyle(
                  color: GSYColors.primaryLightValue,
                  fontSize: GSYConstant.middleTextWhiteSize,
                ),
              )),
    );
  }

  equipmentList() async {
    // LoadingDialogHelper.showLoading(context);
    var result = await DioUtil()
        .request("equipment/equipmentList", method: DioMethod.post, data: {
      'index': 1,
      'size': 50,
      'query': {
        'status': _satus,
        'name': _device,
        'isEnable': _isEnabled,
        'showName': _username
      }
    });
    EquipmentListEntity entity = equipmentListEntityFromJson(
      EquipmentListEntity(),
      result,
    );
    // LoadingDialogHelper.dismissLoading(context);
    if (entity.code == 0) {
      setState(() {
        deviceNum = entity.data.total.toString();
        equipmentVOS.clear();
        equipmentVOS.addAll(entity.data.equipmentVOS);
      });
    } else {
      showToast(entity.msg);
    }
  }

  equipmentInfo(int id) async {
    var result = await DioUtil().request("equipment/equipmentInfo",
        method: DioMethod.get, params: {'id': id});
    EquipmentInfoEntity entity =
        equipmentInfoEntityFromJson(EquipmentInfoEntity(), result);
    if (entity.code == 0) {
      setState(() {
        Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) =>
                    DeviceManageDetailPage(data: entity.data)));
      });
    } else {
      showToast(entity.msg);
    }
  }
}
