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/LoadingDialogHelper.dart';
import 'package:deus_app/common/utils/ToastUtils.dart';
import 'package:deus_app/generated/json/equipment_info_entity_helper.dart';
import 'package:deus_app/main.dart';
import 'package:deus_app/model/equipment_info_entity.dart';
import 'package:deus_app/page/device/device_manage_update_page.dart';
import 'package:flutter/material.dart';

class DeviceManageDetailPage extends StatefulWidget {
  final EquipmentInfoData data;

  const DeviceManageDetailPage({super.key, required this.data});

  @override
  State createState() {
    return _DeviceManageDetailPage(data);
  }
}

class _DeviceManageDetailPage extends State<DeviceManageDetailPage> {
  EquipmentInfoData data;

  _DeviceManageDetailPage(this.data);

   var _event;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
        appBar: TitleBar().backAppbar("设备详情"),
        backgroundColor: const Color(0xfff2f2f2),
        body: ListView(children: _Ws()));
  }

  List<Widget> _Ws() {
    List<Widget> ws = [];
    ws.add(_deviceDetail());
    ws.add(_deviceList());
    return ws;
  }

  Widget _deviceDetail() {
    return Column(children: [
      Container(
        decoration: BoxDecoration(color: Colors.white),
        padding: EdgeInsets.only(bottom: 15, top: 15, left: 12, right: 12),
        margin: EdgeInsets.only(bottom: 15),
        child: Column(
          children: [
            Row(
              children: [
                Container(
                  // padding: EdgeInsets.fromLTRB(12, 0, 0, 0),
                  alignment: Alignment.bottomLeft,
                  child: Text(
                    data.showName,
                    style: TextStyle(
                        fontSize: GSYConstant.middleTextWhiteSize,
                        fontWeight: FontWeight.bold),
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(left: 10),
                  padding: EdgeInsets.fromLTRB(3, 2, 3, 2),
                  alignment: Alignment.centerLeft,
                  child: Text(
                    data.status == 0
                        ? '在线'
                        : data.status == 1
                            ? '离线'
                            : data.status == 2
                                ? '未激活'
                                : '未知',
                    style: TextStyle(
                        fontSize: GSYConstant.minTextSize,
                        color: data.status == 2
                            ? Colors.red
                            : data.status == 1
                                ? Colors.orange
                                : data.status == 0
                                    ? Colors.blue
                                    : Colors.black),
                  ),
                  decoration: BoxDecoration(
                    border: new Border.all(
                      color: data.status == 2
                          ? Colors.red
                          : data.status == 1
                              ? Colors.orange
                              : data.status == 0
                                  ? Colors.blue
                                  : Colors.black, //边框颜色
                      width: 1.0, //边框粗细
                    ),
                    borderRadius: const BorderRadius.all(
                        const Radius.circular(3.0)), //边框的弧度
                  ),
                )
              ],
            ),
            Container(
                padding: EdgeInsets.only(top: 15),
                child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      new Text(
                        '产品名称：',
                        style: GSYConstant.smallTextLight,
                      ),
                      Container(
                        child: Text(
                          data.productName,
                          style: GSYConstant.smallTextLight,
                          textAlign: TextAlign.right,
                        ),
                      ),
                    ])),
            Container(
                padding: EdgeInsets.only(top: 15),
                child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      new Text(
                        '激活时间：',
                        style: GSYConstant.smallTextLight,
                      ),
                      Container(
                        child: Text(
                          data.activationOn,
                          style: GSYConstant.smallTextLight,
                          textAlign: TextAlign.right,
                        ),
                      ),
                    ])),
            Container(
                padding: EdgeInsets.only(top: 15),
                child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      new Text(
                        '最后上线时间：',
                        style: GSYConstant.smallTextLight,
                      ),
                      Container(
                        child: Text(
                          data.lastOnlineOn,
                          style: GSYConstant.smallTextLight,
                          textAlign: TextAlign.right,
                        ),
                      ),
                    ])),
          ],
        ),
      ),
    ]);
  }

  Widget _deviceList() {
    return Column(
      children: [
        Container(
            decoration: BoxDecoration(color: Colors.white),
            padding: const EdgeInsets.fromLTRB(12, 15, 12, 15),
            child: Column(
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    const Text("设备参数", style: TextStyle(
                        fontSize: GSYConstant.middleTextWhiteSize,
                        fontWeight: FontWeight.bold)),
                    GestureDetector(
                      child: const Text("修改参数",
                          style: TextStyle(
                              fontSize: GSYConstant.middleTextWhiteSize,
                              color: Colors.blue),
                          textAlign: TextAlign.right),
                      onTap: () {
                        Navigator.push(
                            context,
                            MaterialPageRoute(
                                builder: (context) => DeviceManageUpdatePage(data:data)));
                      },
                    )
                  ],
                ),
                SizedBox(
                  height: 15,
                ),
                Container(
                  alignment: Alignment.centerLeft,
                  child: Text('当前设备已设置的参数如下：', style: GSYConstant.smallTextBold),
                ),
                SizedBox(
                  height: 15,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    new Text('A侧速度：', style: GSYConstant.smallTextLight),
                    new Text(data.speedA==''?'未设置':data.speedA, style: GSYConstant.smallTextLight,textAlign: TextAlign.right),
                  ],
                ),
                SizedBox(
                  height: 15,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    new Text('B侧速度：', style: GSYConstant.smallTextLight),
                    Text(data.speedB==''?'未设置':data.speedB, style: GSYConstant.smallTextLight,textAlign: TextAlign.right),
                  ],
                ),
                SizedBox(
                  height: 15,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    new Text('A侧纤度：', style: GSYConstant.smallTextLight),
                    Text(data.finenessA==''?'未设置':data.finenessA, style: GSYConstant.smallTextLight,textAlign: TextAlign.right),
                  ],
                ),
                SizedBox(
                  height: 15,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    const Text('B侧纤度：', style: GSYConstant.smallTextLight),
                    Text(data.finenessB==''?'未设置':data.finenessB, style: GSYConstant.smallTextLight,textAlign: TextAlign.right),
                  ],
                ),
                SizedBox(
                  height: 15,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    new Text('A侧孔数：', style: GSYConstant.smallTextLight),
                    new Text(data.holeCountA==''?'未设置':data.holeCountA, style: GSYConstant.smallTextLight,textAlign: TextAlign.right),
                  ],
                ),
                SizedBox(
                  height: 15,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    new Text('B侧孔数：', style: GSYConstant.smallTextLight),
                    new Text(data.holeCountB==''?'未设置':data.holeCountB, style: GSYConstant.smallTextLight,textAlign: TextAlign.right),
                  ],
                )
              ],
            ))
      ],
    );
  }
  @override
  void initState() {
    super.initState();
    _event = eventBus.on<Object>().listen((event) {
      setState(() {
        equipmentInfo(data.id);
      });
    });
  }

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

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _event.cancel();
  }
}
