import 'package:farmer_client/models/addressController.dart';
import 'package:farmer_client/models/entities/Address.dart';
import 'package:farmer_client/pages/addressList/widget/AddressCard.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 AddressList extends BasicPage {
  bool isBack = false;
  AddressController addressController=AddressController.t;

  @override
  void beforeShow() {
    super.beforeShow();
    if (Get.arguments != null) {
      isBack = Get.arguments['isBack'];
    }
  }

  final addressList = Rx<List<Address>>([
    Address.fromJson({'address': '777', 'isDefault': false}),
    Address.fromJson({'address': '999', 'isDefault': false}),
    Address.fromJson({'address': '这是一个正经的地址', 'isDefault': false}),
  ]);

  void onChange(index) {
    for (var element in addressList.value) {
      if (element.isDefault == true) {
        element.isDefault = false;
      }
    }
    addressList.value[index].isDefault = true;
  }

  void onDelete(index) {
    Fluttertoast.showToast(msg: '删除成功！');
  }

  @override
  Widget builder(BuildContext context) {
    naviTitle = '地址管理';
    return Column(
      children: [
        Container(
          padding: EdgeInsets.all(15.w),
          child: Column(
            //左对齐
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Column(
                  children: addressList.value
                      .asMap()
                      .keys
                      .map(
                        (e) => AddressCard(
                          No: e + 1,
                          item: addressList.value[e],
                          isBack: isBack,
                          onChange: () {
                            onChange(e);
                          },
                          onEdit: () {
                            Get.toNamed('/addAddress',
                                arguments: {'item': addressList.value[e]});
                          },
                          onDelete: () {
                            onDelete(e);
                          },
                          onBack: () {
                            addressController.address.value=addressList.value[e];
                            Get.back();
                          },
                        ),
                      )
                      .toList()),
              DefaultButton(
                color: const Color(0xffffffff),
                backColor: const Color(0xFFFF703B),
                width: 345.w,
                height: 49.h,
                text: '+新增收货地址',
                onPressed: () {
                  Get.toNamed('/addAddress');
                },
                margin: const EdgeInsets.all(0),
                fontSize: 20.sp,
                radius: 24.5.w,
              ),
            ],
          ),
        ),
      ],
    );
  }
}
