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_easyloading/flutter_easyloading.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;

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

  void getList() {
    EasyLoading.show(status: '数据加载中...');
    getAddress().then((value) {
      final list = <Address>[];
      value['records'].forEach((item) {
        list.add(Address.fromJson(item));
      });
      addressController.addressList(list);
      EasyLoading.dismiss();
    }).catchError((error) {
      EasyLoading.dismiss();
      Fluttertoast.showToast(msg: error.error['message']);
    });
  }

  void onChange(id) {
    addressDefault(id).then((value) {
      getList();
    });
  }

  void onDelete(id) {
    modal.showDialog(
        title: '提示',
        content: Container(
          alignment: Alignment.center,
          child: const Text('确定要删除此地址吗？'),
        ),
        onCancel: () => true,
        onConfirm: () {
              addressDelete(id).then((value) {
                List<Address> list = addressController.addressList.value.toList();
                list.removeWhere((element) => element.addressId == id);
                addressController.addressList(list);
                Fluttertoast.showToast(msg: '删除成功！');
              });
            });
  }

  Widget buildList() {
    return Obx(() {
      var index = 0;
      var list = addressController.addressList.value;
      List<Widget> children = list.map((item) {
        return AddressCard(
          No: index ++,
          item: item,
          isBack: isBack,
          onChange: () {
            onChange(item.addressId);
          },
          onEdit: () {
            Get.offNamed('/addAddress', arguments: {
              'isBack': isBack,
              'id': item.addressId,
              'item': item.address.toString()
            });
          },
          onDelete: () {
            onDelete(item.addressId);
          },
          onBack: () {
            //选择地址
            addressController.address.value = item;
            Get.back();
          },
        );
      }).toList();

      return Column(children: children,);
    });
  }

  @override
  Widget builder(BuildContext context) {
    naviTitle = '地址管理';
    return ListView(
      children: [
        Container(
            padding: EdgeInsets.all(15.w),
            child: Column(
              //左对齐
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                buildList(),
                DefaultButton(
                  color: const Color(0xffffffff),
                  backColor: const Color(0xFFFF703B),
                  width: 345.w,
                  height: 49.h,
                  text: '+新增地址',
                  onPressed: () {
                    Get.offNamed('/addAddress', arguments: {'isBack': isBack});
                  },
                  margin: const EdgeInsets.all(0),
                  fontSize: 20.sp,
                  radius: 24.5.w,
                ),
              ],
            )),
      ],
    );
  }
}
