import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.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/res/colors.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/utils/theme_utils.dart';
import 'package:liftmanager/utils/toast.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:liftmanager/widgets/bbs_content.dart';
import 'package:liftmanager/widgets/text_field_item.dart';
import 'package:provider/provider.dart';

class BaoPrice extends StatefulWidget {
  BaoPrice(this.id,this.travelCost,this.serviceCost);

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

class BaoPriceState extends State<BaoPrice> with AutomaticKeepAliveClientMixin {
  UserProvider provider = UserProvider();

  TextEditingController _techService = TextEditingController();
  TextEditingController _travelExpenses = TextEditingController();

  double totalPrice = 0.0;

  int indexNow = 0;
  @override
  void initState() {
    super.initState();
    _techService.text = widget.serviceCost;
    _travelExpenses.text = widget.travelCost;
    changeTotalPrice();
  }

  changeTotalPrice() {
    print(_techService.text);
    print(_travelExpenses.text);
    double techPrice = _techService.text == '' ? 0 : double.parse(_techService.text);
    double travelPrice =
        _travelExpenses.text == '' ? 0 : double.parse(_travelExpenses.text);
    totalPrice = techPrice + travelPrice;
    setState(() {});
  }

  void confirmQuote() {
    if (_techService.text == '') {
      toasts("请输入技术服务费");
      return;
    }
    if (_travelExpenses.text == '') {
      toasts("请输入差旅费");
      return;
    }

    NewApiService().quote({
      "id": widget.id,
      "travelCost": double.parse(_travelExpenses.text),
      "serviceCost": double.parse(_techService.text)
    }, onSuccess: (res) {
      toasts("报价成功");
      
      NavigatorUtils.push(context, "${WodeRouter.orderPageMaster}?checkType=0");
      setState(() {});
    }, onError: (code, msg) {
      toasts(msg);
    });
  }

  @override
  void dispose() {
    // provider.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(
        resizeToAvoidBottomPadding: false,
        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>[
                    TextFieldItem(
                      title: "技术服务费",
                      content: "",
                      controller: _techService,
                      keyboardType: TextInputType.numberWithOptions(decimal: true),
                      hintText: "请输入金额",
                      onChanged: (res) {
                        changeTotalPrice();
                      },
                    ),
                    TextFieldItem(
                      title: "差旅费",
                      content: "",
                      keyboardType: TextInputType.numberWithOptions(decimal: true),
                      controller: _travelExpenses,
                      hintText: "请输入金额",
                      onChanged: (res) {
                        changeTotalPrice();
                      },
                    ),
                    Heng(
                      left: "报价合计",
                      right: "￥" + totalPrice.toStringAsFixed(2).toString(),
                      leftColor: 0xff666666,
                      rightColor: 0xffff0000,
                    ),
                    SizedBox(height: ScreenUtil().setWidth(80))
                  ]),
                  Positioned(
                    bottom: 0,
                    left: 0,
                    child: Container(
                      width: width,
                      padding: EdgeInsets.only(
                        top: ScreenUtil().setWidth(15),
                        bottom: ScreenUtil().setWidth(15),
                        left: ScreenUtil().setWidth(25),
                        right: ScreenUtil().setWidth(25),
                      ),
                      color: ThemeUtils.getDialogTextFieldColor(context),
                      child: Container(
                        height: ScreenUtil().setWidth(44),
                        decoration: BoxDecoration(
                          color:Colours.blue_app_main,
                          borderRadius: BorderRadius.circular(
                            ScreenUtil().setWidth(22),
                          ),
                          // gradient: const LinearGradient(
                          //     colors: [Color(0xFF00D9FF), Color(0xFF0287FF)]),
                        ),
                        child: FlatButton(
                          // padding: EdgeInsets.all(15.0),
                          child: Text("确认报价"),
                          textColor: Colors.white,
                          // textColor: Colors.white,
                          onPressed: () {
                            confirmQuote();
                          },
                        ),
                      ),
                    ),
                  )
                ],
              );
            },
          ),
        ),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
