import 'package:farmer_client/pages/orderInfo/widgets/star.dart';
import 'package:farmer_client/widgets/DefaultButton.dart';
import 'package:ff_stars/ff_stars.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:farmer_client/widgets/Modal.dart' as modal;

import '../../models/entities/OrderInfoModel.dart';
import '../../services/orderAPI.dart';
import '../../widgets/OrderInfoCard.dart';
import '../../widgets/layout/BasicPage.dart';

// class OrderContentInfo {
//   String title;
//   Color styleColor;
//   StyleObj ({ required this.title, required this.styleColor});
// }

class OrderPageInfo extends BasicPage {
  final id = Rx<String>('');
  final orderStateText = Rx<String>('待支付');
  final orderStateColor = Rx<Color>(Colors.black);
  final orderInfoContent = Rx<OrderInfoModel>(OrderInfoModel());
  final evalText=Rx<String>('');
  final evalStars=Rx<num>(-1);


  // orderStates

  @override
  void beforeShow() {
    // TODO: implement beforeShow
    super.beforeShow();
    if (Get.arguments['id'] != null) {
      id(Get.arguments['id']);

      orderStateText(Get.arguments['title']);
      orderStateColor(Get.arguments['styleColor']);

      orderInfo(id()).then((value) {
        orderInfoContent(OrderInfoModel.fromJson(value));
      });
    }
  }

  @override
  Widget builder(BuildContext context) {
    naviTitle = '订单详情';

    return Column(
      children: [
        Obx(
          () => OrderInfoCard(
              item: orderInfoContent(),
              orderStateText: orderStateText(),
              orderStateColor: orderStateColor()),
        ),
        // Obx(() => _BottomWidget(id: id())),

        Obx(
          () => Container(
              padding: EdgeInsets.fromLTRB(15, 50.w, 15, 0),
              child: _bottomJudgment(id.value)),
        ),
      ],
    );
  }

  Widget _bottomJudgment(String id) {
    return orderStateText.value == '已完成'
        ? Text('')
        : orderStateText.value == '进行中'
            ? Text('')
            : orderStateText.value == '待评价'
                ? DefaultButton(
                    color: const Color(0xffffffff),
                    backColor: const Color(0xFFFF703B),
                    width: 300.w,
                    height: 49.h,
                    text: '评价',
                    onPressed: () {

                      modal.showDialog(
                          title: '评价此订单',
                          content: Container(
                            alignment: Alignment.center,
                            child:Container(
                              margin: const EdgeInsets.fromLTRB(15, 20, 15, 20),
                              child: Column(
                                children: [
                                  Container(
                                    padding: const EdgeInsets.fromLTRB(0, 0, 0, 10),
                                    decoration: const BoxDecoration(
                                        border: Border(
                                            bottom: BorderSide(
                                                width: 0.8, color: Color(0x20000000)
                                              // 0x17000000
                                            ))),
                                    child: Row(
                                      children: [
                                        Text('满意度：',style: TextStyle(fontSize: 17.sp,fontWeight: FontWeight.bold),),
                                        FFStars(
                                          //https://pub.dev/packages/ff_stars
                                          normalStar:Image.asset('images/icons/starOff.png'),
                                          selectedStar:Image.asset('images/icons/starOn.png'),
                                          starsChanged: (realStars, selectedStars) {
                                            evalStars(realStars);
                                            print("real: $selectedStars, final: $realStars");
                                          },
                                          step: 1,
                                          defaultStars: 0,
                                        ),
                                      ],
                                    ),
                                  ),
                                  Container(
                                    margin: const EdgeInsets.fromLTRB(0, 20, 0, 0),
                                    width: 300.w,
                                    height: 100.w,
                                    decoration: BoxDecoration(
                                      border: Border.all(
                                        color: Color(0x20000000),
                                        width: 0.8,
                                      ),
                                    ),
                                    child:  TextField(
                                      minLines: 6,
                                      maxLines: 6,
                                      style: TextStyle(fontSize: 17.sp, height: 1.5),
                                      decoration: const InputDecoration(
                                        isCollapsed: true,
                                        border: InputBorder.none,
                                        counterText: '',
                                        hintText: '请输入评价内容',
                                        floatingLabelBehavior: FloatingLabelBehavior.never,
                                      ),
                                      onChanged: (e) {
                                        evalText(e);
                                      },
                                    ),
                                  )

                                ],
                              )
                              ,
                            )
                          ),
                          onCancel: () => true,
                          onConfirm:() {
                                if(evalStars.value!=-1&&evalText.value!=''){
                                    orderEvaluation(id,evalStars.value,evalText.value).then((value) {
                                      Fluttertoast.showToast(msg: '评价成功！');
                                       Get.offNamed('/order');
                                    });
                                }else{
                                  Fluttertoast.showToast(msg: '请完整选择评分或输入评价内容');
                                  return false;
                                }
                          }
                          );
                    },
                    margin: const EdgeInsets.all(0),
                    fontSize: 20.sp,
                    radius: 24.5.w,
                  )
                : orderStateText.value == '待作业'
                    ? DefaultButton(
                        color: const Color(0xffffffff),
                        backColor: const Color(0xFFFF703B),
                        width: 300.w,
                        height: 49.h,
                        text: '退单',
                        onPressed: () {
                          modal.showDialog(
                              title: '提示',
                              content: Container(
                                alignment: Alignment.center,
                                child: Text('确定要申请退单吗？'),
                              ),
                              onCancel: () => true,
                              onConfirm: () => {
                                    orderRefund(id).then((value) {
                                      Fluttertoast.showToast(msg: '退单成功！');
                                      Get.offNamed('/order');
                                    }),
                                  });
                        },
                        margin: const EdgeInsets.all(0),
                        fontSize: 20.sp,
                        radius: 24.5.w,
                      )
                    : orderStateText.value == '已付款'
                        ? DefaultButton(
                            color: const Color(0xffffffff),
                            backColor: const Color(0xFFFF703B),
                            width: 300.w,
                            height: 49.h,
                            text: '退单',
                            onPressed: () {
                              modal.showDialog(
                                  title: '提示',
                                  content: Container(
                                    alignment: Alignment.center,
                                    child: Text('确定要申请退单吗？'),
                                  ),
                                  onCancel: () => true,
                                  onConfirm: () => {
                                        orderRefund(id).then((value) {
                                          Fluttertoast.showToast(msg: '退单成功！');
                                          Get.offNamed('/order');
                                        }),
                                      });
                            },
                            margin: const EdgeInsets.all(0),
                            fontSize: 20.sp,
                            radius: 24.5.w,
                          )
                        : orderStateText.value == '待付款'
                            ? _BottomWidget(
                                id: id,
                              )
                            : orderStateText.value == '已退单'
                                ? Text('')
                                : orderStateText.value == '退单申请中'
                                    ? Text(orderStateText.value)
                                    : Text('');
  }
}

class _BottomWidget extends StatelessWidget {
  final id;
  final resultMessage = Rx<String>('---');

  _BottomWidget({Key? key, required this.id}) : super(key: key);

  void _orderDeletels() {
    modal.showDialog(
        title: '提示',
        content: Container(
          alignment: Alignment.center,
          child: Text('确定要取消此订单吗？'),
        ),
        onCancel: () => true,
        onConfirm: () => {
              orderDelete(id).then((value) {
                Fluttertoast.showToast(msg: '订单取消成功！');
                Get.offNamed('/order');
              })
            });
  }

  //显示底部弹框的功能
  void showBottomSheet(context) {
    //用于在底部打开弹框的效果
    showModalBottomSheet(
        context: context,
        isScrollControlled: false,
        backgroundColor: Colors.white,
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.all(Radius.circular(10))),
        builder: (BuildContext context) {
          return Container(
            height: 200.w, //对话框高度就是此高度
            child: Center(
                child: Column(
              children: [
                Padding(
                  padding: EdgeInsets.fromLTRB(0, 25.w, 0, 34.w),
                  child: Text(
                    "支付方式",
                    style:
                        TextStyle(fontSize: 17.sp, fontWeight: FontWeight.bold),
                  ),
                ),
                Padding(
                  padding: EdgeInsets.fromLTRB(15.w, 0, 15.w, 0),
                  child: ListTile(
                    contentPadding: EdgeInsets.symmetric(horizontal: 0.0),
                    // 这边使用了contentPadding
                    leading: Image(
                      image: AssetImage('images/alipay.png'),
                      width: 30.w,
                      height: 30.w,
                    ),
                    title: Transform(
                      transform: Matrix4.translationValues(-15, 0.0, 0.0),
                      child: Text("支付宝",
                          style: TextStyle(
                              fontSize: 18.sp, color: Color(0xff333333))),
                    ),
                    trailing: Image(
                      image: AssetImage('images/userRight.png'),
                      width: 10.w,
                      height: 18.w,
                    ),
                  ),
                ),
                Padding(
                  padding: EdgeInsets.fromLTRB(15.w, 0, 15.w, 0),
                  child: ListTile(
                    onTap: (){

                    },
                    contentPadding: EdgeInsets.symmetric(horizontal: 0.0),
                    // 这边使用了contentPadding
                    leading: Image(
                      image: AssetImage('images/weChatPay.png'),
                      width: 30.w,
                      height: 30.w,
                    ),
                    title: Transform(
                      transform: Matrix4.translationValues(-15, 0.0, 0.0),
                      child: Text("微信",
                          style: TextStyle(
                              fontSize: 18.sp, color: Color(0xff333333))),
                    ),
                    trailing: Image(
                      image: AssetImage('images/userRight.png'),
                      width: 10.w,
                      height: 18.w,
                    ),
                  ),
                ),
              ],
            )),
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.bottomCenter,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          SizedBox(
            width: 150.w,
            height: 49.h,
            child: ElevatedButton(
              onPressed: () {
                _orderDeletels();
              },
              child: const Text(
                "取消",
                style: TextStyle(
                    fontSize: 18,
                    color: Color(0xFFFF703B),
                    fontWeight: FontWeight.bold),
              ),
              style: ButtonStyle(
                side: MaterialStateProperty.all(
                    BorderSide(width: 1, color: Color(0xFFFF703B))), //边框
                elevation: MaterialStateProperty.all(0),
                backgroundColor: MaterialStateProperty.all(Colors.white),
                shape: MaterialStateProperty.all(
                  RoundedRectangleBorder(
                      borderRadius: BorderRadius.all(Radius.circular(24.4))),
                ),
              ),
            ),
          ),
          SizedBox(
            child: DefaultButton(
              color: const Color(0xffffffff),
              backColor: const Color(0xFFFF703B),
              width: 150.w,
              height: 49.h,
              text: '支付',
              onPressed: () {
                showBottomSheet(context);
                // Fluttertoast.showToast(msg: '支付成功！');
                // Get.offAllNamed('/');
              },
              margin: const EdgeInsets.all(0),
              fontSize: 20.sp,
              radius: 24.5.w,
            ),
          ),
        ],
      ),
    );
  }
}
