import 'package:farmer_client/models/entities/Address.dart';
import 'package:farmer_client/services/address.dart';
import 'package:farmer_client/widgets/DefaultButton.dart';
import 'package:farmer_client/widgets/layout/BasicPage.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';

class AddAddress extends BasicPage {

  final address =Rx<Address>(Address());
  late String text = '';
  late String id = '';

  @override
  void beforeShow() {
    super.beforeShow();
    if (Get.arguments != null) {
      print( Get.arguments['item']);
      // address(Get.arguments['item']);
      text = Get.arguments['item'];
      id = Get.arguments['id'];
    }
  }

  @override
  Widget builder(BuildContext context) {
    naviTitle='添加地址';
    return Container(
      padding: EdgeInsets.all(15.w),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            '详细地址：',
            style: TextStyle(
                fontWeight: FontWeight.bold,
                fontSize: 17.sp,
                color: const Color(0xFF333333)),
          ),
          Container(
            margin: EdgeInsets.fromLTRB(0, 0, 0, 50.h),
            padding: EdgeInsets.symmetric(vertical: 5.w, horizontal: 18.5.w),
            decoration: BoxDecoration(
                color: const Color(0xFFfefefe),
                borderRadius: BorderRadius.all(Radius.circular(10.w)),
                border: Border.all(
                    color: const Color(0xcc000000),
                    width: 1.h,
                    style: BorderStyle.solid)),
            child: TextFormField(
              initialValue: text,
              minLines: 6,
              maxLines: 6,
              style: TextStyle(fontSize: 17.sp, height: 1.5),
              decoration: const InputDecoration(
                isCollapsed: true,
                hintText: '请输入地址信息',
                border: InputBorder.none,
                counterText: '', //去掉计数
                floatingLabelBehavior: FloatingLabelBehavior.never,
              ),
              onChanged: (e) {
                  address().address=e;
              },
            ),
          ),
          DefaultButton(
            color: const Color(0xffffffff),
            backColor: const Color(0xFFFF703B),
            width: 345.w,
            height: 49.h,
            text: '保存',
            onPressed: () {
              if(address.value.address==null){
                Fluttertoast.showToast(
                    msg: '请输入地址！');
              } else{
                if(text==''){
                  addressSave(address().address.toString()).then((value){
                    Fluttertoast.showToast(
                        msg: '添加地址成功！');
                    Get.back();
                  });

                }else{
                  addressUpdate(id,address().address.toString()).then((value) {
                    Fluttertoast.showToast(
                        msg: '修改地址成功！');
                    Get.back();
                  });
                }
              }
            },
            margin: const EdgeInsets.all(0),
            fontSize: 20.sp,
            radius: 24.5.w,
          ),
        ],
      ),
    );
  }
}
