import 'package:flutter/material.dart';
import 'package:liftmanager/res/gaps.dart';
import 'package:liftmanager/net/api_service.dart';
import 'package:liftmanager/utils/toast.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:liftmanager/internal/search/search_router.dart';
import 'package:liftmanager/widgets/app_search_bar.dart';
import 'package:liftmanager/res/resources.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/widgets/load_image.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:liftmanager/internal/friends/friends_router.dart';
import 'package:flustars/flustars.dart' as FlutterStars;
import 'package:liftmanager/internal/friends/model/near_model.dart';
import 'package:liftmanager/utils/log_util.dart';
import 'dart:convert';
import 'package:liftmanager/internal/bbs/bbs_router.dart';
import 'package:liftmanager/utils/theme_utils.dart';

class NearDetail extends StatefulWidget {
  // QuestionList(this.index);

  // final String index;
  

  @override
  State<StatefulWidget> createState() {
    return NearDetailState();
  }
}

class NearDetailState extends State<NearDetail> {
  // NewsDetailItem item = NewsDetailItem();
  
  ScrollController _scrollController = new ScrollController();
  dynamic obj;
  void initState() {
    super.initState();
    
    obj = FlutterStars.SpUtil.getObj(
          "nearUserDetail", (v) => NearModel.fromJson(v));
    LogUtil.d(JsonEncoder().convert(obj));
    print(123456);
  }

  
  Future createPrivateChatRoom(id) async {
    
    await NewApiService().createPrivateChatRoom(id.toString(), onSuccess: (res) {
      // LogUtil.d(JsonEncoder().convert(res.records));
      // LogUtil.d("5555664");
      print(res["room"]);
      String roomId = res["room"];
      NavigatorUtils.push(context, "${BbsRouter.chatRoom}?id=$roomId&type=nearToOne&toUserId=$id");
      // if(res["type"]==1){
      //   NavigatorUtils.push(context, "${BbsRouter.chatRoom}?id=$roomId&type=someToOne");
      // }else {
      //   NavigatorUtils.push(context, "${BbsRouter.chatRoom}?id=$roomId&type=oneToOne");
      // }
      print(41414111);
      
      setState(() {});
    }, onError: (code, msg) {
      toasts(msg);
    });
  }

  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {

    

    double width = MediaQuery.of(context).size.width;

    return Scaffold(
      appBar: MyAppBar(
        centerTitle: "",
      ),
      body:Container(
              child:ListView(
                children: <Widget>[
                  Container(
      // height: 20,
      padding:EdgeInsets.only(bottom:ScreenUtil().setWidth(10),top:ScreenUtil().setWidth(10)),
      
      child:  Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Expanded(
              child: GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: (){
                },
                child:Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children:<Widget>[
                  Container(
                    margin: EdgeInsets.only(left:ScreenUtil().setWidth(10),right:ScreenUtil().setWidth(10),bottom:ScreenUtil().setWidth(15)),
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(ScreenUtil().setWidth(22)),
                    ),
                    child: ClipRRect(
                      borderRadius:  BorderRadius.all(Radius.circular(22)),
                      child: LoadNetworkImage(
                      // image: AssetImage(i['img']),
                      obj.avatarUrl,
                      width: ScreenUtil().setWidth(43),
                      height:ScreenUtil().setWidth(43),
                      // alignment: Alignment.centerLeft,
                    ),
                    )
                  ),
                  Expanded(
                    child: Container(
                      padding: EdgeInsets.only(right:ScreenUtil().setWidth(15),bottom:ScreenUtil().setWidth(15)),
                      decoration: BoxDecoration(
                        border: Border(
                                  bottom: BorderSide(width: 0.5, color: Colours.line),
                                ),
                      ),
                      child:Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Container(
                          child:Text(
                                    obj.name??"",
                                    style: TextStyle(
                                      fontSize:ScreenUtil().setSp(17)
                                    ),
                                    textAlign:TextAlign.start,
                                  ),
                        ),
                        Container(
                          child:Text(
                                    (obj.distance.toString()??"")+"米以内",
                                    style: TextStyle(
                                      color:Color(0xff999999),
                                      fontSize:ScreenUtil().setSp(13)
                                    ),
                                    textAlign:TextAlign.start,
                                  ),
                        ),
                      ],
                    )
                    )
                  )
                ]
              ),
              )
            ),
            
          ],
        ),
    ),
    Container(
                width: width,
                padding: EdgeInsets.only(top:ScreenUtil().setWidth(100),bottom:ScreenUtil().setWidth(15),left:ScreenUtil().setWidth(25),right:ScreenUtil().setWidth(25)),
                color:ThemeUtils.getTabsBg(context),
                child: Container(
                      height:ScreenUtil().setWidth(44),
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(22.0),
                        gradient: const LinearGradient(
                                        colors: [Color(0xFF00D9FF), Color(0xFF0287FF)]),
                      ),
                      child: FlatButton(
                        // padding: EdgeInsets.all(15.0),
                        child: Text("打招呼"),
                        textColor: Colors.white,
                        // textColor: Colors.white,
                        onPressed: () {
                          // NavigatorUtils.push(context, FriendsRouter.chatRoom);
                          createPrivateChatRoom(obj.userId);
                        },
                      ),
                    ),
              )
                ]
              ),
            ),
    );
  }
}

