import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:peasmartcare/blocs/select_electricity_location_list/select_electricity_location_list_bloc.dart';
import 'package:peasmartcare/blocs/select_electricity_location_save/select_electricity_location_save_bloc.dart';
import 'package:peasmartcare/constraints/app_data.dart';
import 'package:peasmartcare/constraints/image_asset.dart';
import 'package:peasmartcare/pages/auth/logout.dart';
import 'package:peasmartcare/widgets/loading.dart';
import 'package:peasmartcare/widgets/sweetalert/sweetalert.dart';

class SelectElectricityLocationForm extends StatefulWidget {
  @override
  _SelectElectricityLocationFormState createState() => _SelectElectricityLocationFormState();
}

class _SelectElectricityLocationFormState extends State<SelectElectricityLocationForm> {
  final _formKey = GlobalKey<FormState>();
  TextEditingController _caNumber = TextEditingController();
  TextEditingController _peaNumber = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        _form(),
      ],
    );
  }

  Widget _form() {
    Size deviceSize = MediaQuery.of(context).size;
    TextStyle textStyle = TextStyle(color: Color(AppData.primaryColor), fontSize: 18);
    TextStyle errortyle = TextStyle(color: Colors.orange[900], fontSize: 18, height: 1);
    return Form(
      key: _formKey,
      child: Center(
        child: Container(
          // height: deviceSize.height - 170,
          constraints: BoxConstraints(maxWidth: 500),
          child: Column(
            children: <Widget>[
              SizedBox(
                height: 40,
              ),
              Container(
                height: 85,
                padding: EdgeInsets.only(left: 30, right: 30),
                child: TextFormField(
                  validator: (value) {
                    if (value.isEmpty) {
                      return "กรุณากรอกหมายเลขผู้ใช้ไฟฟ้า";
                    }
                    return null;
                  },
                  controller: _caNumber,
                  maxLines: 1,
                  style: textStyle,
                  decoration: InputDecoration(
                    hintText: "กรุณากรอกหมายเลขผู้ใช้ไฟฟ้า",
                    hintStyle: textStyle,
                    labelText: "หมายเลขผู้ใช้ไฟฟ้า",
                    labelStyle: textStyle,
                    errorStyle: errortyle,
                    floatingLabelBehavior: FloatingLabelBehavior.auto,
                    filled: true,
                    fillColor: Color(AppData.whiteColor),
                    contentPadding: EdgeInsets.symmetric(vertical: 10, horizontal: 15),
                    border: OutlineInputBorder(
                      borderRadius: const BorderRadius.all(
                        const Radius.circular(30),
                      ),
                    ),
                  ),
                ),
              ),
              Container(
                height: 85,
                padding: EdgeInsets.only(left: 30, right: 30, bottom: 15),
                child: TextFormField(
                  validator: (value) {
                    if (value.isEmpty) {
                      return "กรุณากรอกรหัสเครื่องวัดไฟฟ้า";
                    }
                    return null;
                  },
                  controller: _peaNumber,
                  maxLines: 1,
                  style: textStyle,
                  decoration: InputDecoration(
                    hintText: "กรุณากรอกรหัสเครื่องวัดไฟฟ้า",
                    hintStyle: textStyle,
                    labelText: "รหัสเครื่องวัดไฟฟ้า",
                    labelStyle: textStyle,
                    errorStyle: errortyle,
                    floatingLabelBehavior: FloatingLabelBehavior.auto,
                    filled: true,
                    fillColor: Color(AppData.whiteColor),
                    contentPadding: EdgeInsets.symmetric(vertical: 10, horizontal: 15),
                    border: OutlineInputBorder(
                      borderRadius: const BorderRadius.all(
                        const Radius.circular(30),
                      ),
                    ),
                  ),
                ),
              ),
              Container(
                child: Image.asset(
                  ImageAsset.sampleBillImg,
                  width: 350,
                  fit: BoxFit.fill,
                ),
              ),
              BlocListener<SelectElectricityLocationSaveBloc, SelectElectricityLocationSaveState>(
                listener: (context, state) {
                  if (state is SelectElectricityLocationSaveLoaded) {
                    if (state.isSuccess) {
                      SweetAlert.show(
                        context,
                        subtitle: "บันทึกสถานที่ใช้ไฟฟ้าสำเร็จ",
                        style: SweetAlertStyle.success,
                        showCancelButton: false,
                        onPress: (bool isConfirm) {
                          context.read<SelectElectricityLocationListBloc>().add(SelectElectricityLocationListGetList());
                          Navigator.pop(context);
                          Navigator.pop(context);
                          return false;
                        },
                      );
                    } else {
                      SweetAlert.show(
                        context,
                        subtitle: state.errorMessage,
                        style: SweetAlertStyle.error,
                      );
                    }
                  } else if (state is SelectElectricityLocationSaveErrorUnauthorized) {
                    Navigator.of(context).popUntil((route) => route.isFirst);
                    Navigator.pushReplacement(
                      context,
                      MaterialPageRoute(builder: (context) => LogoutPage()),
                    );
                  }
                },
                child: BlocBuilder<SelectElectricityLocationSaveBloc, SelectElectricityLocationSaveState>(builder: (context, state) {
                  if (state is SelectElectricityLocationSaveLoading) {
                    return Loading(height: 50);
                  }
                  return Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Container(
                        padding: EdgeInsets.fromLTRB(0, 30, 0, 15),
                        child: RaisedButton(
                          padding: EdgeInsets.only(top: 12, left: 30, right: 30, bottom: 12),
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(7),
                          ),
                          child: Text(
                            "บันทึก",
                            style: TextStyle(
                              fontWeight: FontWeight.w800,
                              color: Colors.white,
                              fontSize: 18,
                            ),
                          ),
                          color: Color(AppData.yellowColor),
                          onPressed: () {
                            if (_formKey.currentState.validate()) {
                              context.read<SelectElectricityLocationSaveBloc>().add(
                                    SelectElectricityLocationSaveOnSubmit(
                                      caNumber: _caNumber.text,
                                      peaNumber: _peaNumber.text,
                                    ),
                                  );
                            }
                          },
                        ),
                      ),
                      SizedBox(
                        width: 20,
                      ),
                      Container(
                        padding: EdgeInsets.fromLTRB(0, 30, 0, 15),
                        child: RaisedButton(
                          padding: EdgeInsets.only(top: 12, left: 30, right: 30, bottom: 12),
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(7),
                          ),
                          child: Text(
                            "ยกเลิก",
                            style: TextStyle(
                              fontWeight: FontWeight.w800,
                              color: Colors.white,
                              fontSize: 18,
                            ),
                          ),
                          color: Color(AppData.greyColor),
                          onPressed: () {
                            Navigator.pop(context);
                          },
                        ),
                      ),
                    ],
                  );
                }),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
