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

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() {
    if (serviceLevel == 0 || serviceTaidu == 0 || speed == 0) {
      showToast('请打分');
      return;
    }

    if (_controller.text.length == 0) {
      showToast('请填写评价');
      return;
    }

//  double serviceLevel = 0;
//   double serviceTaidu = 0;
//   double speed = 0;

    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 = 0;
  double serviceTaidu = 0;
  double speed = 0;
  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
                          ? _orderUserDetail()
                          : Center(
                              child: null,
                            ),
                      Container(
                        color: Color(0xffFAF7FA),
                        height: ScreenUtil().setWidth(5),
                      ),
                      SizedBox(
                        height: 20,
                      ),
                      Row(
                        children: [
                          Container(
                            height: 13,
                            width: 2,
                            color: Colours.blue_app_main,
                          ),
                          SizedBox(
                            width: 13,
                          ),
                          Text(
                            '打分评价',
                            style: TextStyle(color: Colours.text, fontSize: 15),
                          )
                        ],
                      ),
                      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(
                              color: Color(0xffFAF7FA),
                              height: ScreenUtil().setWidth(5),
                            ),
                            SizedBox(
                              height: 20,
                            ),
                            Row(
                              children: [
                                Container(
                                  height: 13,
                                  width: 2,
                                  color: Colours.blue_app_main,
                                ),
                                SizedBox(
                                  width: 13,
                                ),
                                Text(
                                  '评价描述',
                                  style: TextStyle(
                                      color: Colours.text, fontSize: 15),
                                )
                              ],
                            ),

                            // 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,
                            //       ),
                            //     ),
                            //   ),
                            // ),

                            Container(
                              height: ScreenUtil().setWidth(120),
                              padding: EdgeInsets.only(
                                  left: 15, right: 15, bottom: 20, top: 10),
                              child: TextFormField(
                                // autofocus: true,
                                maxLength: 100,
                                cursorColor: Color(0xffcccccc),
                                controller: _controller,
                                maxLines: 5,
                                decoration: InputDecoration(
                                  contentPadding: EdgeInsets.all(0),
                                  hintText: '请描述您对专家本次服务的评价',
                                  hintStyle:
                                      TextStyle(color: Color(0xffcccccc)),
                                  focusedBorder: InputBorder.none,
                                  border: InputBorder.none,
                                  // filled: true, // 背景色
                                  // fillColor: Colors.cyan.withAlpha(35),
                                  // icon: Icon(Icons.person)
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          Container(
                            height: 44,
                            decoration: BoxDecoration(
                              color: Colours.blue_app_main,
                              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();
                              },
                            ),
                          ),
                        ],
                      ),
                    ],
                  ),
                ],
              );
            },
          ),
        ),
      ),
    );
  }

  _orderUserDetail() {
    return 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,
                    "${BbsRouter.recommendAsk}?id=${detailObj.id}&type=3");
              },
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  SizedBox(
                    width: 10,
                  ),
                  ClipRRect(
                    borderRadius: BorderRadius.circular(20),
                    child: LoadNetworkImage(
                      detailObj.avatarUrl,
                      width: ScreenUtil().setWidth(43),
                      height: ScreenUtil().setWidth(43),
                      // alignment: Alignment.centerLeft,
                    ),
                  ),
                  SizedBox(
                    width: 10,
                  ),
                  Expanded(
                    child: Container(
                      padding:
                          EdgeInsets.only(right: ScreenUtil().setWidth(15)),
                      child: Column(
                        children: <Widget>[
                          Container(
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.start,
                              children: <Widget>[
                                Row(
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: <Widget>[
                                    Text(
                                      detailObj.name ?? '',
                                      style: TextStyle(
                                        fontSize: ScreenUtil().setSp(17),
                                      ),
                                      textAlign: TextAlign.start,
                                    ),
                                    Container(
                                      decoration: BoxDecoration(
                                        //背景
                                        color: detailObj.dataTable == 1
                                            ? Colours.blue_app_main
                                            : Color(0xffFDAF2C),
                                        //设置四周圆角 角度
                                        borderRadius: BorderRadius.all(
                                            Radius.circular(4.0)),
                                      ),
                                      margin: EdgeInsets.only(left: 5),
                                      padding: EdgeInsets.only(
                                          left: 5, right: 5, top: 2, bottom: 2),
                                      child: Text(
                                        detailObj.dataTable == 1
                                            ? '问诊服务'
                                            : '出诊服务',
                                        style: TextStyle(
                                          color: Colors.white,
                                          fontSize: ScreenUtil().setSp(14),
                                        ),
                                        textAlign: TextAlign.start,
                                      ),
                                    ),
                                  ],
                                ),
                              ],
                            ),
                          ),
                          Container(
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: <Widget>[
                                Row(
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: <Widget>[
                                    Text(
                                      detailObj.createTime != null
                                          ? DateUtils.instance.getFormartData(
                                              timeSamp: detailObj.createTime,
                                              format: "MM月dd日")
                                          : '',
                                      style: TextStyle(
                                        color: Color(0xff999999),
                                        fontSize: ScreenUtil().setSp(13),
                                      ),
                                      textAlign: TextAlign.start,
                                    ),
                                    Container(
                                      padding: EdgeInsets.only(left: 5),
                                      child: Text(
                                        detailObj.brandName ?? '',
                                        style: TextStyle(
                                          color: Colours.blue_app_main,
                                          fontSize: ScreenUtil().setSp(13),
                                        ),
                                        textAlign: TextAlign.start,
                                      ),
                                    )
                                  ],
                                ),
                              ],
                            ),
                          ),
                          detailObj.dataTable != 1
                              ? Container(
                                  child: Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: <Widget>[
                                      Row(
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        children: <Widget>[
                                          Container(
                                            // padding: EdgeInsets.only(left: 5),
                                            child: Text(
                                              '出诊时间：',
                                              style: TextStyle(
                                                color: Color(0xff999999),
                                                fontSize:
                                                    ScreenUtil().setSp(13),
                                              ),
                                              textAlign: TextAlign.start,
                                            ),
                                          ),
                                          Text(
                                            detailObj.createTime != null
                                                ? DateUtils.instance
                                                    .getFormartData(
                                                        timeSamp: detailObj
                                                            .createTime,
                                                        format: "MM月dd日 HH:mm")
                                                : '',
                                            style: TextStyle(
                                              color: Color(0xff666666),
                                              fontSize: ScreenUtil().setSp(13),
                                            ),
                                            textAlign: TextAlign.start,
                                          ),
                                        ],
                                      ),
                                    ],
                                  ),
                                )
                              : Container(),
                          detailObj.dataTable != 1
                              ? Container(
                                  // margin: EdgeInsets.only(top: 6),
                                  child: Row(
                                    children: <Widget>[
                                      Container(
                                        // padding: EdgeInsets.only(left: 5),
                                        child: Text(
                                          '出诊地点：',
                                          style: TextStyle(
                                            color: Color(0xff999999),
                                            fontSize: ScreenUtil().setSp(13),
                                          ),
                                          textAlign: TextAlign.start,
                                        ),
                                      ),
                                      Container(
                                        width: ScreenUtil().setWidth(230),
                                        child: Text(
                                          '${detailObj.address ?? ''}',
                                          style: TextStyle(
                                            color: Color(0xff666666),
                                            fontSize: ScreenUtil().setSp(13),
                                            // color: Color(0xff666666),
                                            // fontSize: ScreenUtil().setSp(15),
                                          ),
                                          textAlign: TextAlign.start,
                                          maxLines: 1,
                                          overflow: TextOverflow.ellipsis,
                                        ),
                                      ),
                                    ],
                                  ),
                                )
                              : Container(),
                          Container(
                            // margin: EdgeInsets.only(top: 6),
                            child: Row(
                              children: <Widget>[
                                Container(
                                  // padding: EdgeInsets.only(left: 5),
                                  child: Text(
                                    '问题描述：',
                                    style: TextStyle(
                                      color: Color(0xff999999),
                                      fontSize: ScreenUtil().setSp(13),
                                    ),
                                    textAlign: TextAlign.start,
                                  ),
                                ),
                                Container(
                                  width: ScreenUtil().setWidth(230),
                                  child: Text(
                                    '${detailObj.expression ?? ''}',
                                    style: TextStyle(
                                      color: Color(0xff666666),
                                      fontSize: ScreenUtil().setSp(13),
                                      // color: Color(0xff666666),
                                      // fontSize: ScreenUtil().setSp(15),
                                    ),
                                    textAlign: TextAlign.start,
                                    maxLines: 1,
                                    overflow: TextOverflow.ellipsis,
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  )
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
