import 'package:farmer_client/pages/main/widgets/main/index.dart';
import 'package:farmer_client/widgets/layout/BasicPage.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';

import '../../models/entities/Address.dart';
import '../../services/address.dart';

class Main extends BasicPage {
  Main({Key? key}) : super(key: key) {
    tabIndex = 3;
    naviTitle = '我的';
  }
  final addressListInfo = Rx<List<Address>>([]);
  final addressInfoContent = Rx<Address>(Address());

  @override
  void beforeShow() {
    // TODO: implement beforeShow
    super.beforeShow();
    EasyLoading.show(status: '数据加载中...');

    getAddress().then((value) {
      EasyLoading.dismiss();

      List<Address> list = [];
      value['records'].forEach((item) {
        list.add(Address.fromJson(item));
      });
      addressListInfo(list);
      for (var i = 0; i < addressListInfo.value.length; i++) {
        if (addressListInfo.value[i].isDefault == true) {
          addressInfoContent(addressListInfo.value[i]);
        }
      }
    }).catchError((error) {
      EasyLoading.dismiss();
      Fluttertoast.showToast(msg: error.error['message']);
    });
  }

  @override
  Widget builder(BuildContext context) {
    return Obx(() => MainPage(
          item: addressInfoContent.value,
        ));
  }
}
