import 'package:deus_app/common/dialog/CloseTaskDialog.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/equipment_info_entity.dart';
import 'package:deus_app/widget/gsy_flex_button.dart';
import 'package:flutter/material.dart';

class DeviceManageUpdatePage extends StatefulWidget {

  final EquipmentInfoData data;

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


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



class _DeviceManageUpdatePage extends State<DeviceManageUpdatePage>{

  EquipmentInfoData data;

  _DeviceManageUpdatePage(this.data);

  TextEditingController _finenessAController= TextEditingController();
  TextEditingController _finenessBController= TextEditingController();
  TextEditingController _holeCountAController= TextEditingController();
  TextEditingController _holeCountBController= TextEditingController();
  TextEditingController _speedAController= TextEditingController();
  TextEditingController _speedBController= TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: TitleBar().backAppbar("修改参数"),
      backgroundColor: const Color(0xfff2f2f2),
      body: SingleChildScrollView (
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children:<Widget> [
            Padding(
              padding: const EdgeInsets.only(top: 50.0, left: 10, right: 10),
              child: Row(children: [
                Expanded(
                  flex: 1,
                  child: Text(
                    'A测速度：',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: GSYConstant.smallTextSize,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
                Expanded(
                  child: Container(
                    height: 40,
                    child: TextField(
                      decoration: InputDecoration(
                        border: OutlineInputBorder(),
                      ),
                      controller: _speedAController,
                      style: TextStyle(fontSize: 14),

                      onChanged: (value) {
                        setState(() {
                          data.speedA=value;
                        });
                      },
                    ),
                  ),
                  flex: 3,
                ),
              ]),
            ),
            Padding(
              padding: const EdgeInsets.only(top: 20.0, left: 10, right: 10),
              child: Row(children: [
                Expanded(
                  flex: 1,
                  child: Text(
                    'B侧速度：',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: GSYConstant.smallTextSize,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
                Expanded(
                  child: Container(
                    height: 40,
                    child: TextField(
                      controller: _speedBController,
                      decoration: InputDecoration(border: OutlineInputBorder()),
                      style: TextStyle(fontSize: 14),
                      onChanged: (value) {
                        setState(() {
                          data.speedB=value;
                        });
                      },
                    ),
                  ),
                  flex: 3,
                ),
              ]),
            ),
            Padding(
              padding: const EdgeInsets.only(top: 20.0, left: 10, right: 10),
              child: Row(children: [
                Expanded(
                  flex: 1,
                  child: Text(
                    'A侧纤度：',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: GSYConstant.smallTextSize,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
                Expanded(
                  child: Container(
                    height: 40,
                    child: TextField(
                      controller: _finenessAController,
                      decoration: InputDecoration(border: OutlineInputBorder()),
                      style: TextStyle(fontSize: 14),
                      onChanged: (value) {
                        setState(() {
                          data.finenessA=value;
                        });
                      },
                    ),
                  ),
                  flex: 3,
                ),
              ]),
            ),
            Padding(
              padding: const EdgeInsets.only(top: 20.0, left: 10, right: 10),
              child: Row(children: [
                Expanded(
                  flex: 1,
                  child: Text(
                    'B侧纤度：',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: GSYConstant.smallTextSize,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
                Expanded(
                  flex: 3,
                  child: Container(
                    height: 40,
                    child: TextField(
                      controller: _finenessBController,
                      decoration: InputDecoration(border: OutlineInputBorder()),
                      style: TextStyle(fontSize: 14),
                      onChanged: (value) {
                        setState(() {
                          data.finenessB=value;
                        });
                      },
                    ),
                  ),
                ),
              ]),
            ),
            Padding(
              padding: const EdgeInsets.only(top: 20.0, left: 10, right: 10),
              child: Row(children: [
                Expanded(
                  flex: 1,
                  child: Text(
                    'A侧孔数：',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: GSYConstant.smallTextSize,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
                Expanded(
                  flex: 3,
                  child: Container(
                    height: 40,
                    child: TextField(
                      controller: _holeCountAController,
                      decoration: InputDecoration(border: OutlineInputBorder()),
                      style: TextStyle(fontSize: 14),
                      onChanged: (value) {
                        setState(() {
                          data.holeCountA=value;
                        });
                      },
                    ),
                  ),
                ),
              ]),
            ),
            Padding(
              padding: const EdgeInsets.only(top: 20.0, left: 10, right: 10),
              child: Row(children: [
                Expanded(
                  flex: 1,
                  child: Text(
                    'B侧孔数：',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: GSYConstant.smallTextSize,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
                Expanded(
                  flex: 3,
                  child: Container(
                    height: 40,
                    child: TextField(
                      controller: _holeCountBController,
                      decoration: InputDecoration(border: OutlineInputBorder()),
                      style: TextStyle(fontSize: 14),
                      onChanged: (value) {
                        setState(() {
                          data.holeCountB=value;
                        });
                      },
                    ),
                  ),
                ),
              ]),
            ),
            SizedBox(
              height: 30,
            ),
            _getButton()
          ],
        ),
      ),
    );
  }

  parameter() async {
    var result = await DioUtil().request("equipment/parameter",
        method: DioMethod.post, data: {'id':data.id,'finenessA':data.finenessA,'finenessB':data.finenessB
    ,'holeCountA':data.holeCountA,'holeCountB':data.holeCountB,'speedA':data.speedA,'speedB':data.speedB});
    if (result['code']== 0) {
      setState(() {
        //发出事件
        eventBus.fire(Object());
        Navigator.pop(context);
      });
    } else {
      showToast(result['msg']);
    }
  }

  @override
  void initState() {
    super.initState();
    _finenessAController=TextEditingController(text: data.finenessA);
    _finenessBController=TextEditingController(text: data.finenessB);
    _holeCountAController=TextEditingController(text: data.holeCountA);
    _holeCountBController=TextEditingController(text: data.holeCountB);
    _speedAController=TextEditingController(text: data.speedA);
    _speedBController=TextEditingController(text: data.speedB);
  }

  Widget _getButton() {
    return Container(
      height: 50,
      width: double.infinity,
      margin: EdgeInsets.all(10),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(4.0),
      ),
      child: GSYFlexButton(
        text: ConstantString.updateText,
        color: Color(0xFF4875EC),
        textColor: GSYColors.textWhite,
        fontSize: 16,
        onPress: (){
          CloseTaskDialog.showAlertDialog(context, () {
            parameter();
          }, '您确认修改参数？');
        },
      ),
    );
  }

}