import 'package:dotted_border/dotted_border.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:worker_client/models/bankCardList.dart';
import 'package:worker_client/models/entities/Bank.dart';
import 'package:worker_client/models/entities/BankCard.dart';
import 'package:worker_client/pages/bankList/widget/BankCard.dart';
import 'package:worker_client/services/bank.dart';
import 'package:worker_client/widgets/NullCard.dart';
import 'package:worker_client/widgets/layout/BasicPage.dart';

class BankListPages extends BasicPage {
  BankListPages({Key? key}) : super(key: key) {
    naviTitle = '我的银行卡';
  }
  BankCardListController bankCardListStore = BankCardListController.t;
  @override
  void beforeShow() {
    // TODO: implement beforeShow
    super.beforeShow();
    getList();
  }

  void goAddBank() {
    Get.toNamed('/addBankCard');
  }

  void getList() {
    if (bankCardListStore.bankCardList().isEmpty) {
      getBankCardList({'pageSize': 500}).then((res) {
        getBankList({'pageSize': 500}).then((res2) {
          var list1 = <BankCardModel>[];
          var bankcardlist = <BankCardModel>[];
          var banklist = <Bank>[];
          var current;
          res['records'].forEach((item) {
            bankcardlist.add(BankCardModel.fromJson(item));
          });
          res2['records'].forEach((item) {
            banklist.add(Bank.fromJson(item));
          });
          for (var item in bankcardlist) {
            for (var v in banklist) {
              if (item.ownerBank == v.name) {
                current = v;
              }
            }
            item.color1 = current.color1;
            item.color2 = current.color2;
            item.bankCode = current.bankCode;
            list1.add(item);
          }
          bankCardListStore.bankCardList(list1);
        });
      });
    }
  }

  void unBind(id) {
    // 弹窗
    unbindBankCard(id).then((value) {
      Fluttertoast.showToast(msg: '解绑成功');
      getList();
    });
  }

  @override
  Widget builder(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(15.w),
      child: Column(
        children: [
          Expanded(
            child: Obx(
              () => bankCardListStore.bankCardList.value.isNotEmpty
                  ? ListView(
                      children:
                          bankCardListStore.bankCardList.value.map((item) {
                      return BankCard(
                        item: item,
                        unBind: () {
                          unBind(item.cardId);
                        },
                      );
                    }).toList())
                  : const NullCard(text: '暂未绑定银行卡'),
            ),
          ),
          Container(
            margin: EdgeInsets.only(bottom: 25.h),
            child: GestureDetector(
              onTap: () {
                goAddBank();
              },
              child: DottedBorder(
                borderType: BorderType.RRect,
                color: const Color(0xffBFBFBF),
                radius: Radius.circular(20.w),
                dashPattern: const [3, 3, 3, 3],
                child: ClipRRect(
                  child: SizedBox(
                    height: 45.w,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Padding(
                          padding: EdgeInsets.fromLTRB(0, 0, 10.w, 0),
                          child: Image.asset(
                            'images/bank/addBank.png',
                            width: 18.w,
                            fit: BoxFit.cover,
                          ),
                        ),
                        Text(
                          '添加银行卡',
                          style: TextStyle(
                              fontWeight: FontWeight.bold,
                              fontSize: 16.sp,
                              color: const Color(0xff323232)),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ),
          )
        ],
      ),
    );
  }
}
