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';
import 'package:farmer_client/widgets/Modal.dart' as modal;

import '../../services/address.dart';

class AddressList extends BasicPage {
  bool isBack = false;
  AddressController addressController = AddressController.t;
  final addressList = Rx<List<Address>>([]);

  @override
  void beforeShow() {
    super.beforeShow();
    getAddress().then((value) {
      final list = <Address>[];
      value['records'].forEach((item) {
        list.add(Address.fromJson(item));
      });
      addressList(list);
    });
    if (Get.arguments != null) {
      isBack = Get.arguments['isBack'];
    }
  }

  void onChange(id) {
    print(id);
    addressDefault(id.toString()).then((value) {
      final defaultlist = [...addressList()];
      for (var element in addressList()) {
        if (element.isDefault == true) {
          element.isDefault = false;
        }
      }
      addressList(defaultlist);
    });
  }

  void onDelete(id) {
    print(id);
    modal.showDialog(
        title: '提示',
        content: Container(
          alignment: Alignment.center,
          child: Text('确定要删除此地址吗？'),
        ),
        onCancel: () => true,
        onConfirm: () => {
              addressDelete(id).then((value) {
                final newslist = [...addressList()];
                for (var item in addressList()) {
                  if (item.addressId == id) {
                    newslist.remove(item);
                  }
                }
                addressList(newslist);
                Fluttertoast.showToast(msg: '删除成功！');
              }),
            });
  }

  @override
  Widget builder(BuildContext context) {
    naviTitle = '地址管理';
    return Column(
      children: [
        Container(
          padding: EdgeInsets.all(15.w),
          child: Column(
            //左对齐
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Obx(
                () => Column(
                    children: addressList()
                        .asMap()
                        .keys
                        .map(
                          (e) => AddressCard(
                            No: e + 1,
                            item: addressList.value[e],
                            isBack: isBack,
                            onChange: () {
                              onChange(addressList()[e].addressId);
                            },
                            onEdit: () {
                              Get.toNamed('/addAddress', arguments: {
                                'id': addressList.value[e].addressId,
                                'item': addressList.value[e].address.toString()
                              });
                            },
                            onDelete: () {
                              onDelete(addressList.value[e].addressId);
                            },
                            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,
              ),
            ],
          ),
        ),
      ],
    );
  }
}
