import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_tzh/base/YSBase.dart';
import 'package:flutter_tzh/mine/YSMineChangePassword.dart';
import 'package:flutter_tzh/mine/YSMineGatherPlan.dart';
import 'package:flutter_tzh/tool/YSAlertView.dart';
import 'package:flutter_tzh/tool/YSFileView.dart';
import 'package:flutter_tzh/tool/YSNetWork.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../login/YSLogin.dart';
import '../tool/YSTools.dart';

class YSMine extends StatefulWidget {
  const YSMine({Key? key}) : super(key: key);

  @override
  YSMineState createState() => YSMineState();
}

class YSMineState extends State<YSMine> {
  final List _menuArray = [
    {'image':'mine_menu1','title':'采集计划'},
    {'image':'mine_menu2','title':'修改密码'},
    {'image':'mine_menu3','title':'退出登陆'}
  ];
  Map _data = {};
  @override
  void initState() {
    networkDelay((){
      _getUserCenter();
    });
    super.initState();
  }
  _getUserCenter() async{
    YSNetWork.ysRequestHttp(context, type: RequestType.get, api: '/user/center', parameter: {}, successSetter: (dict){
      _data = dict['data']??{};
      setState(() {});
    });
  }
  @override
  Widget build(BuildContext context) {
    return YSBase(
      ysTitle: '个人中心',
      isTab: true,
      ysChild: Column(
        children: [
          Container(
            decoration: BoxDecoration(
              color: const Color(0xFF2E3138),
              borderRadius: const BorderRadius.all(Radius.circular(24)),
              border: Border.all(color: const Color(0xFF3E434E),width: hsp(1))
            ),
            height: hsp(333),
            width: ysWidth(context)-hsp(40),
            margin: EdgeInsets.only(top: hsp(20)),
            child: Column(
              children: [
                Container(
                  height: hsp(161),
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    border: Border.all(color: const Color(0xFF3E434E),width: hsp(1))
                  ),
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Container(
                        width: ysWidth(context)-hsp(140),
                        height: hsp(100),
                        decoration: const BoxDecoration(
                          image: DecorationImage(image: AssetImage('images/mine_bg.png'))
                        ),
                        alignment: Alignment.center,
                        child: GestureDetector(
                          onLongPress: () async{
                            bool isAgree = await permissionHandler('file');
                            if(isAgree&&mounted){
                              ysShowCenterAlertView(context, YSTipsAlertView(valueSetter: (value) {
                                if(value){
                                  uploadFile(context, (value) async{
                                    // LogUtil.d(value['url']);
                                    YSNetWork.ysRequestHttp(context, type: RequestType.post, api: '/user/modify', parameter: {'avatar':value['url']}, successSetter: (dict){
                                      bool data = dict['data']??false;
                                      if(data){
                                        _getUserCenter();
                                      }
                                    });
                                  });
                                }
                              },tipsStr: '是否修改头像？',));
                            }

                          },
                          child: Container(
                            height: hsp(60),
                            width: hsp(60),
                            decoration: _data.isEmpty?const BoxDecoration(
                                borderRadius: BorderRadius.all(Radius.circular(7)),
                                color: Colors.white,
                            ):BoxDecoration(
                              borderRadius: const BorderRadius.all(Radius.circular(7)),
                              color: Colors.white,
                              image: DecorationImage(image: NetworkImage('$fileUrl${_data['avatar']}'),fit: BoxFit.fill)
                            ),
                          ),
                        ),
                      ),
                      Container(
                        padding: EdgeInsets.only(left: hsp(13),right: hsp(13),top: hsp(5),bottom: hsp(5)),
                        decoration: const BoxDecoration(
                          color: Color(0xFF23262B),
                          borderRadius: BorderRadius.all(Radius.circular(2)),
                        ),
                        margin: EdgeInsets.only(bottom: hsp(3)),
                        child: Text(_data['name']??'',style: TextStyle(fontSize: zsp(11),color: const Color(0xFF8A93A0),backgroundColor: const Color(0xFF23262B))),
                      ),
                      Container(
                        height: hsp(20),
                        padding: EdgeInsets.only(left: hsp(5),right: hsp(5)),
                        child: Text(_data['phone']??'',style: TextStyle(fontSize: zsp(12),color: const Color(0xFFACB5C5)),),
                      )

                    ],
                  ),
                ),
                SizedBox(
                  height: hsp(170),
                  child: ListView.separated(
                    itemBuilder: (context, index) {
                      Map item = _menuArray[index];
                      return GestureDetector(
                        onTap: (){
                          if(item['title']=='采集计划'){
                            Navigator.of(context).push(
                              CupertinoPageRoute(builder: (context){
                                return const YSMineGatherPlan();
                              })
                            );
                          }else if(item['title']=='修改密码'){
                            Navigator.of(context).push(
                                CupertinoPageRoute(builder: (context){
                                  return const YSMineChangePassword();
                                })
                            );
                          }else if(item['title']=='退出登陆'){
                            ysShowCenterAlertView(context, YSTipsAlertView(
                              valueSetter: (value) async{
                                if(value){
                                  YSNetWork.ysRequestHttp(context, type: RequestType.get, api: '/auth/logout', parameter: {}, successSetter: (dict) async{
                                    SharedPreferences prefs = await SharedPreferences.getInstance();
                                    prefs.remove('token');
                                    if(!mounted)return;
                                    Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (context){
                                      return const YSLogin();
                                    }), (route) => false);
                                  });
                                }
                              },
                              tipsStr: '是否退出登录?',
                            ));
                          }
                        },
                        behavior: HitTestBehavior.opaque,
                        child: SizedBox(
                          height: hsp(40),
                          child: Row(
                            children: [
                              Image.asset('images/${item['image']}.png',height: hsp(7),width: hsp(7),),
                              Container(
                                width: ysWidth(context)-hsp(80),
                                padding: EdgeInsets.only(left: hsp(10)),
                                child: Text(item['title'],style: TextStyle(fontSize: zsp(15),color: const Color(0xFFDBE1EA)),),
                              )
                            ],
                          ),
                        ),
                      );
                    },
                    separatorBuilder: (context,index){
                      return Container(height: hsp(12),);
                    },
                    itemCount: _menuArray.length,
                    shrinkWrap: true,
                    padding: EdgeInsets.only(top: hsp(12),left: hsp(15),right: hsp(15)),
                    physics: const NeverScrollableScrollPhysics(),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
