import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:liftmanager/common/common.dart';
import 'package:liftmanager/internal/account/account_router.dart';
import 'package:liftmanager/internal/account/model/user_info_entity.dart';
import 'package:liftmanager/internal/account/provider/user_provider.dart';
import 'package:liftmanager/internal/wode/wode_router.dart';
import 'package:liftmanager/net/api_service.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/utils/image_utils.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:liftmanager/widgets/click_item.dart';
import 'package:liftmanager/widgets/load_image.dart';
import 'package:liftmanager/widgets/my_button.dart';
import 'package:provider/provider.dart';
import 'package:liftmanager/widgets/bbs_content.dart';
import 'package:liftmanager/internal/bbs/bbs_router.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:liftmanager/widgets/star_item.dart';
import 'package:liftmanager/res/resources.dart';
import 'package:liftmanager/internal/bbs/model/liftcase_model.dart';
import 'package:liftmanager/utils/toast.dart';
import 'package:liftmanager/utils/time_format.dart';
import 'package:liftmanager/utils/url.dart';
import 'package:liftmanager/common/common.dart';
import 'package:flustars/flustars.dart' as flustars;

class Comment extends StatefulWidget {
  Comment(this.id);

  final String id;
  @override
  State<StatefulWidget> createState() {
    return CommentState();
  }
}

class CommentState extends State<Comment> with AutomaticKeepAliveClientMixin {
  UserProvider provider = UserProvider();

  bool _hasData = false;
  LiftCasesDetailModel detailObj;

  void getBrandDetail() {
    NewApiService().getLiftcaseDetail(widget.id, onSuccess: (res) {
      detailObj = res;
      _hasData = true;
      setState(() {});
    }, onError: (code, msg) {
      toasts(msg);
    });
  }

  void submitComment() {
    showLoading(context);
    NewApiService().submitComments({
      "attitude": serviceTaidu,
      "comment": _controller.text,
      "lifeCaseId": widget.id,
      "responseSpeed": speed,
      "tech": serviceLevel,
      "expertId": detailObj.chargerId,
      "userId":int.parse(flustars.SpUtil.getString(Constant.userId)),
    }, onSuccess: (res) {
      dismissLoading(context);
      toasts("评论成功");
      NavigatorUtils.push(context, "${WodeRouter.orderPage}?checkType=0");
    }, onError: (code, msg) {
      dismissLoading(context);
      toasts(msg);
    });
  }

  double serviceLevel = 5;
  double serviceTaidu = 5;
  double speed = 5;
  TextEditingController _controller = TextEditingController();
  @override
  void initState() {
    getBrandDetail();
    super.initState();
  }

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

  FocusNode blankNode = FocusNode();

  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;
    return
        // Center(child:new Text("个人中心"));
        ChangeNotifierProvider<UserProvider>(
      create: (_) => provider,
      child: Scaffold(
        appBar: MyAppBar(
          centerTitle: "评价本次服务",
        ),
        body: GestureDetector(
          onTap: () {
            // 点击空白页面关闭键盘
            FocusScope.of(context).requestFocus(blankNode);
          },
          child: Consumer<UserProvider>(
            builder: (_, provider, __) {
              return Stack(
                children: <Widget>[
                  ListView(
                    padding: EdgeInsets.all(0.0),
                    children: <Widget>[
                      _hasData
                          ? Container(
                              // height: 20,
                              padding: EdgeInsets.only(
                                  bottom: ScreenUtil().setWidth(20),
                                  top: ScreenUtil().setWidth(10)),
                              child: Row(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: <Widget>[
                                  Expanded(
                                    child: GestureDetector(
                                      onTap: () {
                                        NavigatorUtils.push(
                                            context, WodeRouter.orderDetail);
                                      },
                                      child: Row(
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        children: <Widget>[
                                          Container(
                                            margin: EdgeInsets.only(
                                              left: ScreenUtil().setWidth(15),
                                              right: ScreenUtil().setWidth(10),
                                              top: ScreenUtil().setWidth(5),
                                            ),
                                            decoration: BoxDecoration(
                                              borderRadius:
                                                  BorderRadius.circular(
                                                      ScreenUtil()
                                                          .setWidth(25)),
                                            ),
                                            child: LoadNetworkImage(
                                              detailObj.avatarUrl,
                                              width: ScreenUtil().setWidth(43),
                                              height: ScreenUtil().setWidth(43),
                                              // alignment: Alignment.centerLeft,
                                            ),
                                          ),
                                          Expanded(
                                            child: Container(
                                              padding: EdgeInsets.only(
                                                  right: ScreenUtil()
                                                      .setWidth(15)),
                                              child: Column(
                                                crossAxisAlignment: CrossAxisAlignment.start,
                                                children: <Widget>[
                                                  Container(
                                                    child: Row(
                                                      mainAxisAlignment:
                                                          MainAxisAlignment
                                                              .spaceBetween,
                                                      children: <Widget>[
                                                        Row(
                                                          crossAxisAlignment:
                                                              CrossAxisAlignment
                                                                  .center,
                                                          children: <Widget>[
                                                            Text(
                                                              detailObj.name,
                                                              style: TextStyle(
                                                                  color: Color(
                                                                      0xff333333),
                                                                  fontSize:
                                                                      ScreenUtil()
                                                                          .setSp(
                                                                              18)),
                                                              textAlign:
                                                                  TextAlign
                                                                      .start,
                                                            ),
                                                            Container(
                                                              padding: EdgeInsets
                                                                  .only(
                                                                      left: 5),
                                                              child: Text(
                                                                detailObj
                                                                        .brandName ??
                                                                    '',
                                                                style: TextStyle(
                                                                    color: Color(
                                                                        0xff666666),
                                                                    fontSize: ScreenUtil()
                                                                        .setSp(
                                                                            14)),
                                                                textAlign:
                                                                    TextAlign
                                                                        .start,
                                                              ),
                                                            )
                                                          ],
                                                        ),
                                                      ],
                                                    ),
                                                  ),
                                                  Container(
                                                    child: Text(
                                                      detailObj.expression ??
                                                          '',
                                                      style: TextStyle(
                                                        color:
                                                            Color(0xff999999),
                                                        fontSize: ScreenUtil()
                                                            .setSp(14),
                                                      ),
                                                      textAlign:
                                                          TextAlign.start,
                                                    ),
                                                  ),
                                                ],
                                              ),
                                            ),
                                          )
                                        ],
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                            )
                          : Center(
                              child: null,
                            ),
                      Container(
                        color: Color(0xffFAF7FA),
                        height: ScreenUtil().setWidth(5),
                      ),
                      Container(
                        padding: EdgeInsets.only(
                            left: ScreenUtil().setWidth(15),
                            top: ScreenUtil().setWidth(15),
                            bottom: ScreenUtil().setWidth(15)),
                        child: Text(
                          '您对专家的服务满意吗?',
                          style: TextStyle(
                              color: Color(0xff333333),
                              fontSize: ScreenUtil().setSp(16)),
                          textAlign: TextAlign.start,
                        ),
                      ),
                      Container(
                        child: Column(
                          children: <Widget>[
                            StarItem(
                              title: "技术水平:",
                              starRating: serviceLevel,
                              onRatingChanged: (res) {
                                serviceLevel = res;
                                setState(() {});
                              },
                            ),
                            StarItem(
                              title: "服务态度:",
                              starRating: serviceTaidu,
                              onRatingChanged: (res) {
                                serviceTaidu = res;
                                setState(() {});
                              },
                            ),
                            StarItem(
                              title: "响应速度:",
                              starRating: speed,
                              onRatingChanged: (res) {
                                speed = res;
                                setState(() {});
                              },
                            ),
                            SizedBox(
                              height: 8,
                            ),
                            Container(
                              decoration: BoxDecoration(
                                border: Border.all(
                                    width: 0.5, color: Color(0xffcccccc)),
                                color: Colors.white,
                              ),
                              margin: EdgeInsets.only(
                                  left: ScreenUtil().setWidth(15),
                                  right: ScreenUtil().setWidth(15)),
                              child: Padding(
                                padding: const EdgeInsets.only(
                                    top: 5,
                                    left: 15.0,
                                    right: 15.0,
                                    bottom: 8.0),
                                child: TextField(
                                  maxLength: 200,
                                  maxLines: 5,
                                  autofocus: false,
                                  controller: _controller,
                                  decoration: InputDecoration(
                                    hintText: "您的满意是对专家最大的支持和鼓励",
                                    border: InputBorder.none,
                                    hintStyle: TextStyles.textGray14,
                                  ),
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        Container(
                          height: ScreenUtil().setWidth(44),
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(
                                ScreenUtil().setWidth(22)),
                            gradient: const LinearGradient(
                                colors: [Color(0xFF00D9FF), Color(0xFF0287FF)]),
                          ),
                          margin: EdgeInsets.only(top:ScreenUtil().setWidth(20),bottom:ScreenUtil().setWidth(20)),
                          width: width * 0.9,
                          child: FlatButton(
                            child: Text("提交"),
                            textColor: Colors.white,
                            onPressed: () {
                              // NavigatorUtils.push(context, WodeRouter.orderPage);
                              submitComment();
                            },
                          ),
                        ),
                      ],
                    ),
                    ],
                  ),
                  
                ],
              );
            },
          ),
        ),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
