import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'my_page/revise_password_page.dart';
import 'package:flutter_picker/flutter_picker.dart';
import '../showDialog.dart';
import '../login_page.dart';
import '../util/api.dart';
import '../util/models.dart';
import '../util/session.dart';
import '../util/util.dart';

class MyPage extends StatefulWidget {
  final Widget child;

  MyPage({
    Key key,
    this.child
  }): super(key: key);

  _MyPageState createState() => _MyPageState();
}

class _MyPageState extends State < MyPage > with AutomaticKeepAliveClientMixin {

  bool check = false;
  String _siteName = "请选择站点";
  List < String > stationList = [];
  Station stationObj;
  String name = '物流';
  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    super.initState();
    getStationList().then((resp) {
      if (isNotError(context, resp)) {
        stationObj = Station.fromJson(resp.data);
        List < String > tempList = [];
        for (int i = 0; i < stationObj.data.length; i++) {
          tempList.add(stationObj.data[i].name.toString());
        }
        getKey('nickName').then((value) {
          setState(() {
            stationList = tempList;
            name = value;
            _siteName = tempList[0];
          });
        });
        print(stationObj.data[0].id.toString());
        setKey('stationId', stationObj.data[0].id.toString());
      } 
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Scaffold(
        appBar: AppBar(
          elevation: 0,
          title: Text('我的',
            style: TextStyle(
              fontSize: ScreenUtil.getInstance().setSp(36)
            ), ),
          centerTitle: true,
          backgroundColor: Color.fromRGBO(64, 98, 254, 1),
        ),
        body: Container(
          color: Color.fromRGBO(246, 246, 254, 1),
          child: Column(
            children: < Widget > [
              _avatarWidget(),
              _funWrapWidget(check, context),
            ],
          ),
        ),
      )
    );
  }

  Widget _avatarWidget() {
    return Container(
      color: Color.fromRGBO(64, 98, 254, 1),
      padding: EdgeInsets.fromLTRB(0, ScreenUtil.getInstance().setHeight(59), 0, ScreenUtil.getInstance().setHeight(67), ),
      child: Column(
        children: < Widget > [
          Center(
            child: Padding(
              padding: EdgeInsets.only(bottom: ScreenUtil.getInstance().setWidth(24), ),
              child: ClipOval(
                child: Image.asset('lib/images/icon_logo.png',
                  fit: BoxFit.cover,
                  width: ScreenUtil.getInstance().setWidth(120),
                  height: ScreenUtil.getInstance().setWidth(120), ),
              )

            )
          ),
          Center(child: Text(name, style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(255, 255, 255, 1)), ), )
        ],
      ),
    );
  }

  Widget _funWrapWidget(bool check, BuildContext context) {
    return Container(
      padding: EdgeInsets.only(top: ScreenUtil.getInstance().setHeight(20)),
      child: Column(
        children: < Widget > [
          _switchSite(context),
          _passWordWidget(),
          _linkPrint(check),
          _quitWidget(context)
        ],
      ),
    );
  }

  Widget _switchSite(BuildContext context) {
    return InkWell(
      child: Container(
        height: ScreenUtil.getInstance().setHeight(90),
        color: Colors.white,
        padding: EdgeInsets.fromLTRB(ScreenUtil.getInstance().setHeight(30), 0, 0, 0),
        child: Column(
          children: < Widget > [
            Expanded(
              child: Center(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: < Widget > [
                    Expanded(
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: < Widget > [
                          Padding(
                            padding: EdgeInsets.only(right: ScreenUtil.getInstance().setWidth(22)),
                            child: Image.asset('lib/images/icon_zhandianss.png',
                              width: ScreenUtil.getInstance().setWidth(30),
                              height: ScreenUtil.getInstance().setHeight(34),
                            )
                          ),
                          Text('站点切换', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(74, 74, 74, 1)), ),
                        ],
                      ),
                    ),
                    Expanded(
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.end,
                        children: < Widget > [
                          Text(_siteName, style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(153, 153, 153, 1)), ),
                          Icon(Icons.keyboard_arrow_right, color: Color.fromRGBO(199, 199, 204, 1))
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
            Expanded(
              flex: 0,
              child: _underLine(),
            )
          ],
        )
      ),
      onTap: () {
        // showDialog < Null > (
        //   context: context, //BuildContext对象
        //   barrierDismissible: false,
        //   builder: (BuildContext context) {
        //     return SiteDialog( //调用对话框

        //     );
        //   });
        showPickerModal(context);
      },
    );
  }
  showPickerModal(BuildContext context) {
    new Picker(
      adapter: PickerDataAdapter < String > (pickerdata: stationList),
      height: ScreenUtil.getInstance().setHeight(450),
      confirmText: '确定',
      confirmTextStyle: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(64, 98, 254, 1)),
      cancelText: '取消',
      cancelTextStyle: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(153, 153, 153, 1)),
      headercolor: Color.fromRGBO(245, 245, 245, 1),
      itemExtent: ScreenUtil.getInstance().setHeight(80),
      textStyle: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(42, 42, 42, 1)),
      changeToFirst: true,
      hideHeader: false,
      onConfirm: (Picker picker, List value) {
        setState(() {
          _siteName = stationObj.data[value[0]].name;
        });
        setKey('stationId', stationObj.data[value[0]].id.toString());
      }
    ).showModal(this.context); //_scaffoldKey.currentState);
  }
  Widget _underLine() {
    return Container(
      height: ScreenUtil.getInstance().setHeight(1),
      color: Color.fromRGBO(223, 223, 223, 1),
    );
  }

  Widget _passWordWidget() {
    return
    GestureDetector(
      child: Container(
        height: ScreenUtil.getInstance().setHeight(90),
        color: Colors.white,
        padding: EdgeInsets.fromLTRB(ScreenUtil.getInstance().setHeight(30), 0, 0, 0),
        child: Column(
          children: < Widget > [
            Expanded(
              child: Center(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: < Widget > [
                    Expanded(
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: < Widget > [
                          Padding(
                            padding: EdgeInsets.only(right: ScreenUtil.getInstance().setWidth(22)),
                            child: Image.asset('lib/images/icon_mimas.png',
                              width: ScreenUtil.getInstance().setWidth(30),
                              height: ScreenUtil.getInstance().setHeight(34),
                            )
                          ),
                          Text('修改密码', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(74, 74, 74, 1)), ),
                        ],
                      ),
                    ),
                    Icon(Icons.keyboard_arrow_right, color: Color.fromRGBO(199, 199, 204, 1))
                  ],
                ),
              ),
            )
          ],
        )
      ),
      onTap: () {
        Navigator.push(context, MaterialPageRoute(builder: (context) {
          return RevisePasswordPage();
        }));
      },
    );
  }

  Widget _linkPrint(bool check) {
    return Container(
      height: ScreenUtil.getInstance().setHeight(90),
      color: Colors.white,
      padding: EdgeInsets.fromLTRB(ScreenUtil.getInstance().setHeight(30), 0, 0, 0),
      margin: EdgeInsets.only(top: ScreenUtil.getInstance().setHeight(20)),
      child: Column(
        children: < Widget > [
          Expanded(
            child: Center(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: < Widget > [
                  Expanded(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: < Widget > [
                        Padding(
                          padding: EdgeInsets.only(right: ScreenUtil.getInstance().setWidth(22)),
                          child: Image.asset('lib/images/icon_dayinji.png',
                            width: ScreenUtil.getInstance().setWidth(30),
                            height: ScreenUtil.getInstance().setHeight(34),
                          )
                        ),
                        Text('打印机连接', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(74, 74, 74, 1)), ),
                      ],
                    ),
                  ),
                  Switch(
                    value: this.check,
                    activeColor: Colors.blue,
                    onChanged: (bool val) {
                      print("val:${val}");
                      this.setState(() {
                        this.check = val;
                      });
                      print('check:${this.check}');
                    },
                  )
                ],
              ),
            ),
          )
        ],
      )
    );
  }

  Widget _quitWidget(BuildContext context) {
    return InkWell(
      child: Container(
        height: ScreenUtil.getInstance().setHeight(90),
        color: Colors.white,
        padding: EdgeInsets.fromLTRB(ScreenUtil.getInstance().setHeight(30), 0, 0, 0),
        margin: EdgeInsets.only(top: ScreenUtil.getInstance().setHeight(20)),
        child: Column(
          children: < Widget > [
            Expanded(
              child: Center(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: < Widget > [
                    Text('退出', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(74, 74, 74, 1)), ),
                    Icon(Icons.keyboard_arrow_right, color: Color.fromRGBO(199, 199, 204, 1))
                  ],
                ),
              ),
            )
          ],
        )
      ),
      onTap: () {
        showDialog < Null > (
          context: context, //BuildContext对象
          barrierDismissible: false,
          builder: (BuildContext context) {
            return new LoadingDialog( //调用对话框
              text: '确认退出？', childCallback: (val) {
                if (val) {
                  Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) {
                    return LoginPage();
                  }));
                } else {
                  return;
                }
              },
            );
          });
      },
    );

  }
}



// //打印机弹出层
// class PrintDialog extends Dialog {
//   PrintDialog({
//     Key key,
//   }): super(key: key);
// }